blob: 0240576564dc39e592aaf320a8708ae4f911a031 [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);
NeilBrown9fd01322012-09-19 12:52:30 +1000383 if (rdev && test_bit(Faulty, &rdev->flags))
384 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000385 if (!rdev || test_bit(Faulty, &rdev->flags))
386 degraded++;
387 else if (test_bit(In_sync, &rdev->flags))
388 ;
389 else
390 /* not in-sync or faulty.
391 * If the reshape increases the number of devices,
392 * this is being recovered by the reshape, so
393 * this 'previous' section is not in_sync.
394 * If the number of devices is being reduced however,
395 * the device can only be part of the array if
396 * we are reverting a reshape, so this section will
397 * be in-sync.
398 */
399 if (conf->raid_disks >= conf->previous_raid_disks)
400 degraded++;
401 }
402 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100403 if (conf->raid_disks == conf->previous_raid_disks)
404 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000405 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100406 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000407 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100408 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown9fd01322012-09-19 12:52:30 +1000409 if (rdev && test_bit(Faulty, &rdev->flags))
410 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000411 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100412 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000413 else if (test_bit(In_sync, &rdev->flags))
414 ;
415 else
416 /* not in-sync or faulty.
417 * If reshape increases the number of devices, this
418 * section has already been recovered, else it
419 * almost certainly hasn't.
420 */
421 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100422 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000423 }
424 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 if (degraded2 > degraded)
426 return degraded2;
427 return degraded;
428}
429
430static int has_failed(struct r5conf *conf)
431{
432 int degraded;
433
434 if (conf->mddev->reshape_position == MaxSector)
435 return conf->mddev->degraded > conf->max_degraded;
436
437 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000438 if (degraded > conf->max_degraded)
439 return 1;
440 return 0;
441}
442
NeilBrownb5663ba2009-03-31 14:39:38 +1100443static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100444get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000445 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct stripe_head *sh;
448
Dan Williams45b42332007-07-09 11:56:43 -0700449 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 spin_lock_irq(&conf->device_lock);
452
453 do {
NeilBrown72626682005-09-09 16:23:54 -0700454 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000455 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700456 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100457 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!sh) {
459 if (!conf->inactive_blocked)
460 sh = get_free_stripe(conf);
461 if (noblock && sh == NULL)
462 break;
463 if (!sh) {
464 conf->inactive_blocked = 1;
465 wait_event_lock_irq(conf->wait_for_stripe,
466 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800467 (atomic_read(&conf->active_stripes)
468 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 || !conf->inactive_blocked),
470 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000471 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 conf->inactive_blocked = 0;
473 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100474 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100477 BUG_ON(!list_empty(&sh->lru)
478 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else {
480 if (!test_bit(STRIPE_HANDLE, &sh->state))
481 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700482 if (list_empty(&sh->lru) &&
483 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700484 BUG();
485 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
488 } while (sh == NULL);
489
490 if (sh)
491 atomic_inc(&sh->count);
492
493 spin_unlock_irq(&conf->device_lock);
494 return sh;
495}
496
NeilBrown6712ecf2007-09-27 12:47:43 +0200497static void
498raid5_end_read_request(struct bio *bi, int error);
499static void
500raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700501
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000502static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700503{
NeilBrownd1688a62011-10-11 16:49:52 +1100504 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700505 int i, disks = sh->disks;
506
507 might_sleep();
508
509 for (i = disks; i--; ) {
510 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100511 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100512 struct bio *bi, *rbi;
513 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200514 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
515 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
516 rw = WRITE_FUA;
517 else
518 rw = WRITE;
519 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700520 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100521 else if (test_and_clear_bit(R5_WantReplace,
522 &sh->dev[i].flags)) {
523 rw = WRITE;
524 replace_only = 1;
525 } else
Dan Williams91c00922007-01-02 13:52:30 -0700526 continue;
527
528 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100529 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700530
531 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100532 rbi->bi_rw = rw;
533 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700534 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100535 rbi->bi_end_io = raid5_end_write_request;
536 } else
Dan Williams91c00922007-01-02 13:52:30 -0700537 bi->bi_end_io = raid5_end_read_request;
538
539 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100540 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100541 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
542 rdev = rcu_dereference(conf->disks[i].rdev);
543 if (!rdev) {
544 rdev = rrdev;
545 rrdev = NULL;
546 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100547 if (rw & WRITE) {
548 if (replace_only)
549 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100550 if (rdev == rrdev)
551 /* We raced and saw duplicates */
552 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100553 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100554 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100555 rdev = rrdev;
556 rrdev = NULL;
557 }
NeilBrown977df362011-12-23 10:17:53 +1100558
Dan Williams91c00922007-01-02 13:52:30 -0700559 if (rdev && test_bit(Faulty, &rdev->flags))
560 rdev = NULL;
561 if (rdev)
562 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100563 if (rrdev && test_bit(Faulty, &rrdev->flags))
564 rrdev = NULL;
565 if (rrdev)
566 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700567 rcu_read_unlock();
568
NeilBrown73e92e52011-07-28 11:39:22 +1000569 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100570 * need to check for writes. We never accept write errors
571 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000572 */
573 while ((rw & WRITE) && rdev &&
574 test_bit(WriteErrorSeen, &rdev->flags)) {
575 sector_t first_bad;
576 int bad_sectors;
577 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
578 &first_bad, &bad_sectors);
579 if (!bad)
580 break;
581
582 if (bad < 0) {
583 set_bit(BlockedBadBlocks, &rdev->flags);
584 if (!conf->mddev->external &&
585 conf->mddev->flags) {
586 /* It is very unlikely, but we might
587 * still need to write out the
588 * bad block log - better give it
589 * a chance*/
590 md_check_recovery(conf->mddev);
591 }
majianpeng8d936982012-07-03 12:11:54 +1000592 /*
593 * Because md_wait_for_blocked_rdev
594 * will dec nr_pending, we must
595 * increment it first.
596 */
597 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000598 md_wait_for_blocked_rdev(rdev, conf->mddev);
599 } else {
600 /* Acknowledged bad block - skip the write */
601 rdev_dec_pending(rdev, conf->mddev);
602 rdev = NULL;
603 }
604 }
605
Dan Williams91c00922007-01-02 13:52:30 -0700606 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100607 if (s->syncing || s->expanding || s->expanded
608 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700609 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
610
Dan Williams2b7497f2008-06-28 08:31:52 +1000611 set_bit(STRIPE_IO_STARTED, &sh->state);
612
Dan Williams91c00922007-01-02 13:52:30 -0700613 bi->bi_bdev = rdev->bdev;
614 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700615 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700616 bi->bi_rw, i);
617 atomic_inc(&sh->count);
618 bi->bi_sector = sh->sector + rdev->data_offset;
619 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700620 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700621 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
622 bi->bi_io_vec[0].bv_offset = 0;
623 bi->bi_size = STRIPE_SIZE;
624 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100625 if (rrdev)
626 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700627 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100628 }
629 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100630 if (s->syncing || s->expanding || s->expanded
631 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100632 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
633
634 set_bit(STRIPE_IO_STARTED, &sh->state);
635
636 rbi->bi_bdev = rrdev->bdev;
637 pr_debug("%s: for %llu schedule op %ld on "
638 "replacement disc %d\n",
639 __func__, (unsigned long long)sh->sector,
640 rbi->bi_rw, i);
641 atomic_inc(&sh->count);
642 rbi->bi_sector = sh->sector + rrdev->data_offset;
643 rbi->bi_flags = 1 << BIO_UPTODATE;
644 rbi->bi_idx = 0;
645 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
646 rbi->bi_io_vec[0].bv_offset = 0;
647 rbi->bi_size = STRIPE_SIZE;
648 rbi->bi_next = NULL;
649 generic_make_request(rbi);
650 }
651 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000652 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700653 set_bit(STRIPE_DEGRADED, &sh->state);
654 pr_debug("skip op %ld on disc %d for sector %llu\n",
655 bi->bi_rw, i, (unsigned long long)sh->sector);
656 clear_bit(R5_LOCKED, &sh->dev[i].flags);
657 set_bit(STRIPE_HANDLE, &sh->state);
658 }
659 }
660}
661
662static struct dma_async_tx_descriptor *
663async_copy_data(int frombio, struct bio *bio, struct page *page,
664 sector_t sector, struct dma_async_tx_descriptor *tx)
665{
666 struct bio_vec *bvl;
667 struct page *bio_page;
668 int i;
669 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700670 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700671 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700672
673 if (bio->bi_sector >= sector)
674 page_offset = (signed)(bio->bi_sector - sector) * 512;
675 else
676 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700677
Dan Williams0403e382009-09-08 17:42:50 -0700678 if (frombio)
679 flags |= ASYNC_TX_FENCE;
680 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
681
Dan Williams91c00922007-01-02 13:52:30 -0700682 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000683 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700684 int clen;
685 int b_offset = 0;
686
687 if (page_offset < 0) {
688 b_offset = -page_offset;
689 page_offset += b_offset;
690 len -= b_offset;
691 }
692
693 if (len > 0 && page_offset + len > STRIPE_SIZE)
694 clen = STRIPE_SIZE - page_offset;
695 else
696 clen = len;
697
698 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000699 b_offset += bvl->bv_offset;
700 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700701 if (frombio)
702 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700703 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700704 else
705 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700706 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700707 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700708 /* chain the operations */
709 submit.depend_tx = tx;
710
Dan Williams91c00922007-01-02 13:52:30 -0700711 if (clen < len) /* hit end of page */
712 break;
713 page_offset += len;
714 }
715
716 return tx;
717}
718
719static void ops_complete_biofill(void *stripe_head_ref)
720{
721 struct stripe_head *sh = stripe_head_ref;
722 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100723 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700724 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700725
Harvey Harrisone46b2722008-04-28 02:15:50 -0700726 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700727 (unsigned long long)sh->sector);
728
729 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000730 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700731 for (i = sh->disks; i--; ) {
732 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700733
734 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700735 /* and check if we need to reply to a read request,
736 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000737 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700738 */
739 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700740 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700741
Dan Williams91c00922007-01-02 13:52:30 -0700742 BUG_ON(!dev->read);
743 rbi = dev->read;
744 dev->read = NULL;
745 while (rbi && rbi->bi_sector <
746 dev->sector + STRIPE_SECTORS) {
747 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200748 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700749 rbi->bi_next = return_bi;
750 return_bi = rbi;
751 }
Dan Williams91c00922007-01-02 13:52:30 -0700752 rbi = rbi2;
753 }
754 }
755 }
Dan Williams83de75c2008-06-28 08:31:58 +1000756 spin_unlock_irq(&conf->device_lock);
757 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700758
759 return_io(return_bi);
760
Dan Williamse4d84902007-09-24 10:06:13 -0700761 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700762 release_stripe(sh);
763}
764
765static void ops_run_biofill(struct stripe_head *sh)
766{
767 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100768 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700769 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700770 int i;
771
Harvey Harrisone46b2722008-04-28 02:15:50 -0700772 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700773 (unsigned long long)sh->sector);
774
775 for (i = sh->disks; i--; ) {
776 struct r5dev *dev = &sh->dev[i];
777 if (test_bit(R5_Wantfill, &dev->flags)) {
778 struct bio *rbi;
779 spin_lock_irq(&conf->device_lock);
780 dev->read = rbi = dev->toread;
781 dev->toread = NULL;
782 spin_unlock_irq(&conf->device_lock);
783 while (rbi && rbi->bi_sector <
784 dev->sector + STRIPE_SECTORS) {
785 tx = async_copy_data(0, rbi, dev->page,
786 dev->sector, tx);
787 rbi = r5_next_bio(rbi, dev->sector);
788 }
789 }
790 }
791
792 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700793 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
794 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700795}
796
Dan Williams4e7d2c02009-08-29 19:13:11 -0700797static void mark_target_uptodate(struct stripe_head *sh, int target)
798{
799 struct r5dev *tgt;
800
801 if (target < 0)
802 return;
803
804 tgt = &sh->dev[target];
805 set_bit(R5_UPTODATE, &tgt->flags);
806 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
807 clear_bit(R5_Wantcompute, &tgt->flags);
808}
809
Dan Williamsac6b53b2009-07-14 13:40:19 -0700810static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700811{
812 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700813
Harvey Harrisone46b2722008-04-28 02:15:50 -0700814 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700815 (unsigned long long)sh->sector);
816
Dan Williamsac6b53b2009-07-14 13:40:19 -0700817 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700818 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700819 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700820
Dan Williamsecc65c92008-06-28 08:31:57 +1000821 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
822 if (sh->check_state == check_state_compute_run)
823 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700824 set_bit(STRIPE_HANDLE, &sh->state);
825 release_stripe(sh);
826}
827
Dan Williamsd6f38f32009-07-14 11:50:52 -0700828/* return a pointer to the address conversion region of the scribble buffer */
829static addr_conv_t *to_addr_conv(struct stripe_head *sh,
830 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700831{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700832 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
833}
834
835static struct dma_async_tx_descriptor *
836ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
837{
Dan Williams91c00922007-01-02 13:52:30 -0700838 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700839 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700840 int target = sh->ops.target;
841 struct r5dev *tgt = &sh->dev[target];
842 struct page *xor_dest = tgt->page;
843 int count = 0;
844 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700845 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700846 int i;
847
848 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700849 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700850 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
851
852 for (i = disks; i--; )
853 if (i != target)
854 xor_srcs[count++] = sh->dev[i].page;
855
856 atomic_inc(&sh->count);
857
Dan Williams0403e382009-09-08 17:42:50 -0700858 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700859 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700860 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700861 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700862 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700863 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700864
Dan Williams91c00922007-01-02 13:52:30 -0700865 return tx;
866}
867
Dan Williamsac6b53b2009-07-14 13:40:19 -0700868/* set_syndrome_sources - populate source buffers for gen_syndrome
869 * @srcs - (struct page *) array of size sh->disks
870 * @sh - stripe_head to parse
871 *
872 * Populates srcs in proper layout order for the stripe and returns the
873 * 'count' of sources to be used in a call to async_gen_syndrome. The P
874 * destination buffer is recorded in srcs[count] and the Q destination
875 * is recorded in srcs[count+1]].
876 */
877static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
878{
879 int disks = sh->disks;
880 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
881 int d0_idx = raid6_d0(sh);
882 int count;
883 int i;
884
885 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100886 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700887
888 count = 0;
889 i = d0_idx;
890 do {
891 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
892
893 srcs[slot] = sh->dev[i].page;
894 i = raid6_next_disk(i, disks);
895 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700896
NeilBrowne4424fe2009-10-16 16:27:34 +1100897 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700898}
899
900static struct dma_async_tx_descriptor *
901ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
902{
903 int disks = sh->disks;
904 struct page **blocks = percpu->scribble;
905 int target;
906 int qd_idx = sh->qd_idx;
907 struct dma_async_tx_descriptor *tx;
908 struct async_submit_ctl submit;
909 struct r5dev *tgt;
910 struct page *dest;
911 int i;
912 int count;
913
914 if (sh->ops.target < 0)
915 target = sh->ops.target2;
916 else if (sh->ops.target2 < 0)
917 target = sh->ops.target;
918 else
919 /* we should only have one valid target */
920 BUG();
921 BUG_ON(target < 0);
922 pr_debug("%s: stripe %llu block: %d\n",
923 __func__, (unsigned long long)sh->sector, target);
924
925 tgt = &sh->dev[target];
926 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
927 dest = tgt->page;
928
929 atomic_inc(&sh->count);
930
931 if (target == qd_idx) {
932 count = set_syndrome_sources(blocks, sh);
933 blocks[count] = NULL; /* regenerating p is not necessary */
934 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700935 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
936 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700937 to_addr_conv(sh, percpu));
938 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
939 } else {
940 /* Compute any data- or p-drive using XOR */
941 count = 0;
942 for (i = disks; i-- ; ) {
943 if (i == target || i == qd_idx)
944 continue;
945 blocks[count++] = sh->dev[i].page;
946 }
947
Dan Williams0403e382009-09-08 17:42:50 -0700948 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
949 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700950 to_addr_conv(sh, percpu));
951 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
952 }
953
954 return tx;
955}
956
957static struct dma_async_tx_descriptor *
958ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
959{
960 int i, count, disks = sh->disks;
961 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
962 int d0_idx = raid6_d0(sh);
963 int faila = -1, failb = -1;
964 int target = sh->ops.target;
965 int target2 = sh->ops.target2;
966 struct r5dev *tgt = &sh->dev[target];
967 struct r5dev *tgt2 = &sh->dev[target2];
968 struct dma_async_tx_descriptor *tx;
969 struct page **blocks = percpu->scribble;
970 struct async_submit_ctl submit;
971
972 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
973 __func__, (unsigned long long)sh->sector, target, target2);
974 BUG_ON(target < 0 || target2 < 0);
975 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
976 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
977
Dan Williams6c910a72009-09-16 12:24:54 -0700978 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 * slot number conversion for 'faila' and 'failb'
980 */
981 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100982 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700983 count = 0;
984 i = d0_idx;
985 do {
986 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
987
988 blocks[slot] = sh->dev[i].page;
989
990 if (i == target)
991 faila = slot;
992 if (i == target2)
993 failb = slot;
994 i = raid6_next_disk(i, disks);
995 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700996
997 BUG_ON(faila == failb);
998 if (failb < faila)
999 swap(faila, failb);
1000 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1001 __func__, (unsigned long long)sh->sector, faila, failb);
1002
1003 atomic_inc(&sh->count);
1004
1005 if (failb == syndrome_disks+1) {
1006 /* Q disk is one of the missing disks */
1007 if (faila == syndrome_disks) {
1008 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001009 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1010 ops_complete_compute, sh,
1011 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001012 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001013 STRIPE_SIZE, &submit);
1014 } else {
1015 struct page *dest;
1016 int data_target;
1017 int qd_idx = sh->qd_idx;
1018
1019 /* Missing D+Q: recompute D from P, then recompute Q */
1020 if (target == qd_idx)
1021 data_target = target2;
1022 else
1023 data_target = target;
1024
1025 count = 0;
1026 for (i = disks; i-- ; ) {
1027 if (i == data_target || i == qd_idx)
1028 continue;
1029 blocks[count++] = sh->dev[i].page;
1030 }
1031 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001032 init_async_submit(&submit,
1033 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1034 NULL, NULL, NULL,
1035 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001036 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1037 &submit);
1038
1039 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001040 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1041 ops_complete_compute, sh,
1042 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001043 return async_gen_syndrome(blocks, 0, count+2,
1044 STRIPE_SIZE, &submit);
1045 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001046 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001047 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1048 ops_complete_compute, sh,
1049 to_addr_conv(sh, percpu));
1050 if (failb == syndrome_disks) {
1051 /* We're missing D+P. */
1052 return async_raid6_datap_recov(syndrome_disks+2,
1053 STRIPE_SIZE, faila,
1054 blocks, &submit);
1055 } else {
1056 /* We're missing D+D. */
1057 return async_raid6_2data_recov(syndrome_disks+2,
1058 STRIPE_SIZE, faila, failb,
1059 blocks, &submit);
1060 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001061 }
1062}
1063
1064
Dan Williams91c00922007-01-02 13:52:30 -07001065static void ops_complete_prexor(void *stripe_head_ref)
1066{
1067 struct stripe_head *sh = stripe_head_ref;
1068
Harvey Harrisone46b2722008-04-28 02:15:50 -07001069 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001070 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001071}
1072
1073static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001074ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1075 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001076{
Dan Williams91c00922007-01-02 13:52:30 -07001077 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001078 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001079 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001080 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001081
1082 /* existing parity data subtracted */
1083 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1084
Harvey Harrisone46b2722008-04-28 02:15:50 -07001085 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001086 (unsigned long long)sh->sector);
1087
1088 for (i = disks; i--; ) {
1089 struct r5dev *dev = &sh->dev[i];
1090 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001091 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001092 xor_srcs[count++] = dev->page;
1093 }
1094
Dan Williams0403e382009-09-08 17:42:50 -07001095 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001096 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001097 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001098
1099 return tx;
1100}
1101
1102static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001103ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001104{
1105 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001106 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001107
Harvey Harrisone46b2722008-04-28 02:15:50 -07001108 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001109 (unsigned long long)sh->sector);
1110
1111 for (i = disks; i--; ) {
1112 struct r5dev *dev = &sh->dev[i];
1113 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001114
Dan Williamsd8ee0722008-06-28 08:32:06 +10001115 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001116 struct bio *wbi;
1117
NeilBrowncbe47ec2011-07-26 11:20:35 +10001118 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001119 chosen = dev->towrite;
1120 dev->towrite = NULL;
1121 BUG_ON(dev->written);
1122 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001123 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001124
1125 while (wbi && wbi->bi_sector <
1126 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001127 if (wbi->bi_rw & REQ_FUA)
1128 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001129 tx = async_copy_data(1, wbi, dev->page,
1130 dev->sector, tx);
1131 wbi = r5_next_bio(wbi, dev->sector);
1132 }
1133 }
1134 }
1135
1136 return tx;
1137}
1138
Dan Williamsac6b53b2009-07-14 13:40:19 -07001139static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001140{
1141 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001142 int disks = sh->disks;
1143 int pd_idx = sh->pd_idx;
1144 int qd_idx = sh->qd_idx;
1145 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001146 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001147
Harvey Harrisone46b2722008-04-28 02:15:50 -07001148 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001149 (unsigned long long)sh->sector);
1150
Tejun Heoe9c74692010-09-03 11:56:18 +02001151 for (i = disks; i--; )
1152 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1153
Dan Williams91c00922007-01-02 13:52:30 -07001154 for (i = disks; i--; ) {
1155 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001156
Tejun Heoe9c74692010-09-03 11:56:18 +02001157 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001158 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001159 if (fua)
1160 set_bit(R5_WantFUA, &dev->flags);
1161 }
Dan Williams91c00922007-01-02 13:52:30 -07001162 }
1163
Dan Williamsd8ee0722008-06-28 08:32:06 +10001164 if (sh->reconstruct_state == reconstruct_state_drain_run)
1165 sh->reconstruct_state = reconstruct_state_drain_result;
1166 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1167 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1168 else {
1169 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1170 sh->reconstruct_state = reconstruct_state_result;
1171 }
Dan Williams91c00922007-01-02 13:52:30 -07001172
1173 set_bit(STRIPE_HANDLE, &sh->state);
1174 release_stripe(sh);
1175}
1176
1177static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001178ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1179 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001180{
Dan Williams91c00922007-01-02 13:52:30 -07001181 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001182 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001183 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001184 int count = 0, pd_idx = sh->pd_idx, i;
1185 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001186 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001187 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001188
Harvey Harrisone46b2722008-04-28 02:15:50 -07001189 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001190 (unsigned long long)sh->sector);
1191
1192 /* check if prexor is active which means only process blocks
1193 * that are part of a read-modify-write (written)
1194 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001195 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1196 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001197 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1198 for (i = disks; i--; ) {
1199 struct r5dev *dev = &sh->dev[i];
1200 if (dev->written)
1201 xor_srcs[count++] = dev->page;
1202 }
1203 } else {
1204 xor_dest = sh->dev[pd_idx].page;
1205 for (i = disks; i--; ) {
1206 struct r5dev *dev = &sh->dev[i];
1207 if (i != pd_idx)
1208 xor_srcs[count++] = dev->page;
1209 }
1210 }
1211
Dan Williams91c00922007-01-02 13:52:30 -07001212 /* 1/ if we prexor'd then the dest is reused as a source
1213 * 2/ if we did not prexor then we are redoing the parity
1214 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1215 * for the synchronous xor case
1216 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001217 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001218 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1219
1220 atomic_inc(&sh->count);
1221
Dan Williamsac6b53b2009-07-14 13:40:19 -07001222 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001223 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001224 if (unlikely(count == 1))
1225 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1226 else
1227 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001228}
1229
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230static void
1231ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1232 struct dma_async_tx_descriptor *tx)
1233{
1234 struct async_submit_ctl submit;
1235 struct page **blocks = percpu->scribble;
1236 int count;
1237
1238 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1239
1240 count = set_syndrome_sources(blocks, sh);
1241
1242 atomic_inc(&sh->count);
1243
1244 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1245 sh, to_addr_conv(sh, percpu));
1246 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001247}
1248
1249static void ops_complete_check(void *stripe_head_ref)
1250{
1251 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001252
Harvey Harrisone46b2722008-04-28 02:15:50 -07001253 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001254 (unsigned long long)sh->sector);
1255
Dan Williamsecc65c92008-06-28 08:31:57 +10001256 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001257 set_bit(STRIPE_HANDLE, &sh->state);
1258 release_stripe(sh);
1259}
1260
Dan Williamsac6b53b2009-07-14 13:40:19 -07001261static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001262{
Dan Williams91c00922007-01-02 13:52:30 -07001263 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 int pd_idx = sh->pd_idx;
1265 int qd_idx = sh->qd_idx;
1266 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001267 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001268 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001269 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001270 int count;
1271 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001272
Harvey Harrisone46b2722008-04-28 02:15:50 -07001273 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001274 (unsigned long long)sh->sector);
1275
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276 count = 0;
1277 xor_dest = sh->dev[pd_idx].page;
1278 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001279 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001280 if (i == pd_idx || i == qd_idx)
1281 continue;
1282 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001283 }
1284
Dan Williamsd6f38f32009-07-14 11:50:52 -07001285 init_async_submit(&submit, 0, NULL, NULL, NULL,
1286 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001287 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001288 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001289
Dan Williams91c00922007-01-02 13:52:30 -07001290 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001291 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1292 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001293}
1294
Dan Williamsac6b53b2009-07-14 13:40:19 -07001295static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1296{
1297 struct page **srcs = percpu->scribble;
1298 struct async_submit_ctl submit;
1299 int count;
1300
1301 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1302 (unsigned long long)sh->sector, checkp);
1303
1304 count = set_syndrome_sources(srcs, sh);
1305 if (!checkp)
1306 srcs[count] = NULL;
1307
1308 atomic_inc(&sh->count);
1309 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1310 sh, to_addr_conv(sh, percpu));
1311 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1312 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1313}
1314
Dan Williams417b8d42009-10-16 16:25:22 +11001315static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001316{
1317 int overlap_clear = 0, i, disks = sh->disks;
1318 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001319 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001321 struct raid5_percpu *percpu;
1322 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001323
Dan Williamsd6f38f32009-07-14 11:50:52 -07001324 cpu = get_cpu();
1325 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001326 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001327 ops_run_biofill(sh);
1328 overlap_clear++;
1329 }
1330
Dan Williams7b3a8712008-06-28 08:32:09 +10001331 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 if (level < 6)
1333 tx = ops_run_compute5(sh, percpu);
1334 else {
1335 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1336 tx = ops_run_compute6_1(sh, percpu);
1337 else
1338 tx = ops_run_compute6_2(sh, percpu);
1339 }
1340 /* terminate the chain if reconstruct is not set to be run */
1341 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001342 async_tx_ack(tx);
1343 }
Dan Williams91c00922007-01-02 13:52:30 -07001344
Dan Williams600aa102008-06-28 08:32:05 +10001345 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001346 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001347
Dan Williams600aa102008-06-28 08:32:05 +10001348 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001349 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001350 overlap_clear++;
1351 }
1352
Dan Williamsac6b53b2009-07-14 13:40:19 -07001353 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1354 if (level < 6)
1355 ops_run_reconstruct5(sh, percpu, tx);
1356 else
1357 ops_run_reconstruct6(sh, percpu, tx);
1358 }
Dan Williams91c00922007-01-02 13:52:30 -07001359
Dan Williamsac6b53b2009-07-14 13:40:19 -07001360 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1361 if (sh->check_state == check_state_run)
1362 ops_run_check_p(sh, percpu);
1363 else if (sh->check_state == check_state_run_q)
1364 ops_run_check_pq(sh, percpu, 0);
1365 else if (sh->check_state == check_state_run_pq)
1366 ops_run_check_pq(sh, percpu, 1);
1367 else
1368 BUG();
1369 }
Dan Williams91c00922007-01-02 13:52:30 -07001370
Dan Williams91c00922007-01-02 13:52:30 -07001371 if (overlap_clear)
1372 for (i = disks; i--; ) {
1373 struct r5dev *dev = &sh->dev[i];
1374 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1375 wake_up(&sh->raid_conf->wait_for_overlap);
1376 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001377 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001378}
1379
Dan Williams417b8d42009-10-16 16:25:22 +11001380#ifdef CONFIG_MULTICORE_RAID456
1381static void async_run_ops(void *param, async_cookie_t cookie)
1382{
1383 struct stripe_head *sh = param;
1384 unsigned long ops_request = sh->ops.request;
1385
1386 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1387 wake_up(&sh->ops.wait_for_ops);
1388
1389 __raid_run_ops(sh, ops_request);
1390 release_stripe(sh);
1391}
1392
1393static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1394{
1395 /* since handle_stripe can be called outside of raid5d context
1396 * we need to ensure sh->ops.request is de-staged before another
1397 * request arrives
1398 */
1399 wait_event(sh->ops.wait_for_ops,
1400 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1401 sh->ops.request = ops_request;
1402
1403 atomic_inc(&sh->count);
1404 async_schedule(async_run_ops, sh);
1405}
1406#else
1407#define raid_run_ops __raid_run_ops
1408#endif
1409
NeilBrownd1688a62011-10-11 16:49:52 +11001410static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001413 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001414 if (!sh)
1415 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001416
NeilBrown3f294f42005-11-08 21:39:25 -08001417 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001418 #ifdef CONFIG_MULTICORE_RAID456
1419 init_waitqueue_head(&sh->ops.wait_for_ops);
1420 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001421
NeilBrowne4e11e32010-06-16 16:45:16 +10001422 if (grow_buffers(sh)) {
1423 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001424 kmem_cache_free(conf->slab_cache, sh);
1425 return 0;
1426 }
1427 /* we just created an active stripe so... */
1428 atomic_set(&sh->count, 1);
1429 atomic_inc(&conf->active_stripes);
1430 INIT_LIST_HEAD(&sh->lru);
1431 release_stripe(sh);
1432 return 1;
1433}
1434
NeilBrownd1688a62011-10-11 16:49:52 +11001435static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001436{
Christoph Lametere18b8902006-12-06 20:33:20 -08001437 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001438 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
NeilBrownf4be6b42010-06-01 19:37:25 +10001440 if (conf->mddev->gendisk)
1441 sprintf(conf->cache_name[0],
1442 "raid%d-%s", conf->level, mdname(conf->mddev));
1443 else
1444 sprintf(conf->cache_name[0],
1445 "raid%d-%p", conf->level, conf->mddev);
1446 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1447
NeilBrownad01c9e2006-03-27 01:18:07 -08001448 conf->active_name = 0;
1449 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001451 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!sc)
1453 return 1;
1454 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001455 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001456 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001457 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return 0;
1460}
NeilBrown29269552006-03-27 01:18:10 -08001461
Dan Williamsd6f38f32009-07-14 11:50:52 -07001462/**
1463 * scribble_len - return the required size of the scribble region
1464 * @num - total number of disks in the array
1465 *
1466 * The size must be enough to contain:
1467 * 1/ a struct page pointer for each device in the array +2
1468 * 2/ room to convert each entry in (1) to its corresponding dma
1469 * (dma_map_page()) or page (page_address()) address.
1470 *
1471 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1472 * calculate over all devices (not just the data blocks), using zeros in place
1473 * of the P and Q blocks.
1474 */
1475static size_t scribble_len(int num)
1476{
1477 size_t len;
1478
1479 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1480
1481 return len;
1482}
1483
NeilBrownd1688a62011-10-11 16:49:52 +11001484static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001485{
1486 /* Make all the stripes able to hold 'newsize' devices.
1487 * New slots in each stripe get 'page' set to a new page.
1488 *
1489 * This happens in stages:
1490 * 1/ create a new kmem_cache and allocate the required number of
1491 * stripe_heads.
1492 * 2/ gather all the old stripe_heads and tranfer the pages across
1493 * to the new stripe_heads. This will have the side effect of
1494 * freezing the array as once all stripe_heads have been collected,
1495 * no IO will be possible. Old stripe heads are freed once their
1496 * pages have been transferred over, and the old kmem_cache is
1497 * freed when all stripes are done.
1498 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1499 * we simple return a failre status - no need to clean anything up.
1500 * 4/ allocate new pages for the new slots in the new stripe_heads.
1501 * If this fails, we don't bother trying the shrink the
1502 * stripe_heads down again, we just leave them as they are.
1503 * As each stripe_head is processed the new one is released into
1504 * active service.
1505 *
1506 * Once step2 is started, we cannot afford to wait for a write,
1507 * so we use GFP_NOIO allocations.
1508 */
1509 struct stripe_head *osh, *nsh;
1510 LIST_HEAD(newstripes);
1511 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001512 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001513 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001514 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001515 int i;
1516
1517 if (newsize <= conf->pool_size)
1518 return 0; /* never bother to shrink */
1519
Dan Williamsb5470dc2008-06-27 21:44:04 -07001520 err = md_allow_write(conf->mddev);
1521 if (err)
1522 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001523
NeilBrownad01c9e2006-03-27 01:18:07 -08001524 /* Step 1 */
1525 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1526 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001527 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001528 if (!sc)
1529 return -ENOMEM;
1530
1531 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001532 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001533 if (!nsh)
1534 break;
1535
NeilBrownad01c9e2006-03-27 01:18:07 -08001536 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001537 #ifdef CONFIG_MULTICORE_RAID456
1538 init_waitqueue_head(&nsh->ops.wait_for_ops);
1539 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001540
1541 list_add(&nsh->lru, &newstripes);
1542 }
1543 if (i) {
1544 /* didn't get enough, give up */
1545 while (!list_empty(&newstripes)) {
1546 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1547 list_del(&nsh->lru);
1548 kmem_cache_free(sc, nsh);
1549 }
1550 kmem_cache_destroy(sc);
1551 return -ENOMEM;
1552 }
1553 /* Step 2 - Must use GFP_NOIO now.
1554 * OK, we have enough stripes, start collecting inactive
1555 * stripes and copying them over
1556 */
1557 list_for_each_entry(nsh, &newstripes, lru) {
1558 spin_lock_irq(&conf->device_lock);
1559 wait_event_lock_irq(conf->wait_for_stripe,
1560 !list_empty(&conf->inactive_list),
1561 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001562 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001563 osh = get_free_stripe(conf);
1564 spin_unlock_irq(&conf->device_lock);
1565 atomic_set(&nsh->count, 1);
1566 for(i=0; i<conf->pool_size; i++)
1567 nsh->dev[i].page = osh->dev[i].page;
1568 for( ; i<newsize; i++)
1569 nsh->dev[i].page = NULL;
1570 kmem_cache_free(conf->slab_cache, osh);
1571 }
1572 kmem_cache_destroy(conf->slab_cache);
1573
1574 /* Step 3.
1575 * At this point, we are holding all the stripes so the array
1576 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001577 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 */
1579 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1580 if (ndisks) {
1581 for (i=0; i<conf->raid_disks; i++)
1582 ndisks[i] = conf->disks[i];
1583 kfree(conf->disks);
1584 conf->disks = ndisks;
1585 } else
1586 err = -ENOMEM;
1587
Dan Williamsd6f38f32009-07-14 11:50:52 -07001588 get_online_cpus();
1589 conf->scribble_len = scribble_len(newsize);
1590 for_each_present_cpu(cpu) {
1591 struct raid5_percpu *percpu;
1592 void *scribble;
1593
1594 percpu = per_cpu_ptr(conf->percpu, cpu);
1595 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1596
1597 if (scribble) {
1598 kfree(percpu->scribble);
1599 percpu->scribble = scribble;
1600 } else {
1601 err = -ENOMEM;
1602 break;
1603 }
1604 }
1605 put_online_cpus();
1606
NeilBrownad01c9e2006-03-27 01:18:07 -08001607 /* Step 4, return new stripes to service */
1608 while(!list_empty(&newstripes)) {
1609 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1610 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001611
NeilBrownad01c9e2006-03-27 01:18:07 -08001612 for (i=conf->raid_disks; i < newsize; i++)
1613 if (nsh->dev[i].page == NULL) {
1614 struct page *p = alloc_page(GFP_NOIO);
1615 nsh->dev[i].page = p;
1616 if (!p)
1617 err = -ENOMEM;
1618 }
1619 release_stripe(nsh);
1620 }
1621 /* critical section pass, GFP_NOIO no longer needed */
1622
1623 conf->slab_cache = sc;
1624 conf->active_name = 1-conf->active_name;
1625 conf->pool_size = newsize;
1626 return err;
1627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
NeilBrownd1688a62011-10-11 16:49:52 +11001629static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct stripe_head *sh;
1632
NeilBrown3f294f42005-11-08 21:39:25 -08001633 spin_lock_irq(&conf->device_lock);
1634 sh = get_free_stripe(conf);
1635 spin_unlock_irq(&conf->device_lock);
1636 if (!sh)
1637 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001638 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001639 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001640 kmem_cache_free(conf->slab_cache, sh);
1641 atomic_dec(&conf->active_stripes);
1642 return 1;
1643}
1644
NeilBrownd1688a62011-10-11 16:49:52 +11001645static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001646{
1647 while (drop_one_stripe(conf))
1648 ;
1649
NeilBrown29fc7e32006-02-03 03:03:41 -08001650 if (conf->slab_cache)
1651 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 conf->slab_cache = NULL;
1653}
1654
NeilBrown6712ecf2007-09-27 12:47:43 +02001655static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
NeilBrown99c0fb52009-03-31 14:39:38 +11001657 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001658 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001659 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001661 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001662 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 for (i=0 ; i<disks; i++)
1666 if (bi == &sh->dev[i].req)
1667 break;
1668
Dan Williams45b42332007-07-09 11:56:43 -07001669 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1670 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 uptodate);
1672 if (i == disks) {
1673 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
NeilBrown14a75d32011-12-23 10:17:52 +11001676 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001677 /* If replacement finished while this request was outstanding,
1678 * 'replacement' might be NULL already.
1679 * In that case it moved down to 'rdev'.
1680 * rdev is not removed until all requests are finished.
1681 */
NeilBrown14a75d32011-12-23 10:17:52 +11001682 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001683 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001684 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001688 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001689 /* Note that this cannot happen on a
1690 * replacement device. We just fail those on
1691 * any error
1692 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001693 printk_ratelimited(
1694 KERN_INFO
1695 "md/raid:%s: read error corrected"
1696 " (%lu sectors at %llu on %s)\n",
1697 mdname(conf->mddev), STRIPE_SECTORS,
1698 (unsigned long long)(sh->sector
1699 + rdev->data_offset),
1700 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001701 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001702 clear_bit(R5_ReadError, &sh->dev[i].flags);
1703 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1704 }
NeilBrown14a75d32011-12-23 10:17:52 +11001705 if (atomic_read(&rdev->read_errors))
1706 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001708 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001709 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001712 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001713 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1714 printk_ratelimited(
1715 KERN_WARNING
1716 "md/raid:%s: read error on replacement device "
1717 "(sector %llu on %s).\n",
1718 mdname(conf->mddev),
1719 (unsigned long long)(sh->sector
1720 + rdev->data_offset),
1721 bdn);
1722 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001723 printk_ratelimited(
1724 KERN_WARNING
1725 "md/raid:%s: read error not correctable "
1726 "(sector %llu on %s).\n",
1727 mdname(conf->mddev),
1728 (unsigned long long)(sh->sector
1729 + rdev->data_offset),
1730 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001731 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001732 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001733 printk_ratelimited(
1734 KERN_WARNING
1735 "md/raid:%s: read error NOT corrected!! "
1736 "(sector %llu on %s).\n",
1737 mdname(conf->mddev),
1738 (unsigned long long)(sh->sector
1739 + rdev->data_offset),
1740 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001741 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001742 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001743 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001744 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001745 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001746 else
1747 retry = 1;
1748 if (retry)
1749 set_bit(R5_ReadError, &sh->dev[i].flags);
1750 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001751 clear_bit(R5_ReadError, &sh->dev[i].flags);
1752 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001753 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
NeilBrown14a75d32011-12-23 10:17:52 +11001756 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1758 set_bit(STRIPE_HANDLE, &sh->state);
1759 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
NeilBrownd710e132008-10-13 11:55:12 +11001762static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
NeilBrown99c0fb52009-03-31 14:39:38 +11001764 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001765 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001766 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001767 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001769 sector_t first_bad;
1770 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001771 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
NeilBrown977df362011-12-23 10:17:53 +11001773 for (i = 0 ; i < disks; i++) {
1774 if (bi == &sh->dev[i].req) {
1775 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
NeilBrown977df362011-12-23 10:17:53 +11001777 }
1778 if (bi == &sh->dev[i].rreq) {
1779 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001780 if (rdev)
1781 replacement = 1;
1782 else
1783 /* rdev was removed and 'replacement'
1784 * replaced it. rdev is not removed
1785 * until all requests are finished.
1786 */
1787 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001788 break;
1789 }
1790 }
Dan Williams45b42332007-07-09 11:56:43 -07001791 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1793 uptodate);
1794 if (i == disks) {
1795 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001796 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
NeilBrown977df362011-12-23 10:17:53 +11001799 if (replacement) {
1800 if (!uptodate)
1801 md_error(conf->mddev, rdev);
1802 else if (is_badblock(rdev, sh->sector,
1803 STRIPE_SECTORS,
1804 &first_bad, &bad_sectors))
1805 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1806 } else {
1807 if (!uptodate) {
1808 set_bit(WriteErrorSeen, &rdev->flags);
1809 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001810 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1811 set_bit(MD_RECOVERY_NEEDED,
1812 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001813 } else if (is_badblock(rdev, sh->sector,
1814 STRIPE_SECTORS,
1815 &first_bad, &bad_sectors))
1816 set_bit(R5_MadeGood, &sh->dev[i].flags);
1817 }
1818 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
NeilBrown977df362011-12-23 10:17:53 +11001820 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1821 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001823 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
NeilBrown784052e2009-03-31 15:19:07 +11001826static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
NeilBrown784052e2009-03-31 15:19:07 +11001828static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 struct r5dev *dev = &sh->dev[i];
1831
1832 bio_init(&dev->req);
1833 dev->req.bi_io_vec = &dev->vec;
1834 dev->req.bi_vcnt++;
1835 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001837 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
NeilBrown977df362011-12-23 10:17:53 +11001839 bio_init(&dev->rreq);
1840 dev->rreq.bi_io_vec = &dev->rvec;
1841 dev->rreq.bi_vcnt++;
1842 dev->rreq.bi_max_vecs++;
1843 dev->rreq.bi_private = sh;
1844 dev->rvec.bv_page = dev->page;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001847 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
NeilBrownfd01b882011-10-11 16:47:53 +11001850static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001853 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001854 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001855 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
NeilBrown908f4fb2011-12-23 10:17:50 +11001857 spin_lock_irqsave(&conf->device_lock, flags);
1858 clear_bit(In_sync, &rdev->flags);
1859 mddev->degraded = calc_degraded(conf);
1860 spin_unlock_irqrestore(&conf->device_lock, flags);
1861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1862
NeilBrownde393cd2011-07-28 11:31:48 +10001863 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001864 set_bit(Faulty, &rdev->flags);
1865 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1866 printk(KERN_ALERT
1867 "md/raid:%s: Disk failure on %s, disabling device.\n"
1868 "md/raid:%s: Operation continuing on %d devices.\n",
1869 mdname(mddev),
1870 bdevname(rdev->bdev, b),
1871 mdname(mddev),
1872 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875/*
1876 * Input: a 'big' sector number,
1877 * Output: index of the data and parity disk, and the sector # in them.
1878 */
NeilBrownd1688a62011-10-11 16:49:52 +11001879static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001880 int previous, int *dd_idx,
1881 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001883 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001884 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001886 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001887 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001889 int algorithm = previous ? conf->prev_algo
1890 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001891 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1892 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001893 int raid_disks = previous ? conf->previous_raid_disks
1894 : conf->raid_disks;
1895 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* First compute the information on this sector */
1898
1899 /*
1900 * Compute the chunk number and the sector offset inside the chunk
1901 */
1902 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1903 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 /*
1906 * Compute the stripe number
1907 */
NeilBrown35f2a592010-04-20 14:13:34 +10001908 stripe = chunk_number;
1909 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001910 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /*
1912 * Select the parity disk based on the user selected algorithm.
1913 */
NeilBrown84789552011-07-27 11:00:36 +10001914 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001915 switch(conf->level) {
1916 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001917 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001918 break;
1919 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001920 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001922 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001923 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 (*dd_idx)++;
1925 break;
1926 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001927 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001928 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 (*dd_idx)++;
1930 break;
1931 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001932 pd_idx = data_disks - 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;
1935 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001936 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001937 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001939 case ALGORITHM_PARITY_0:
1940 pd_idx = 0;
1941 (*dd_idx)++;
1942 break;
1943 case ALGORITHM_PARITY_N:
1944 pd_idx = data_disks;
1945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001947 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001948 }
1949 break;
1950 case 6:
1951
NeilBrowne183eae2009-03-31 15:20:22 +11001952 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001953 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001954 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001955 qd_idx = pd_idx + 1;
1956 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001957 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001958 qd_idx = 0;
1959 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001960 (*dd_idx) += 2; /* D D P Q D */
1961 break;
1962 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001963 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001964 qd_idx = pd_idx + 1;
1965 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001966 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001967 qd_idx = 0;
1968 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001969 (*dd_idx) += 2; /* D D P Q D */
1970 break;
1971 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001972 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001973 qd_idx = (pd_idx + 1) % raid_disks;
1974 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001975 break;
1976 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001977 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001978 qd_idx = (pd_idx + 1) % raid_disks;
1979 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001980 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001981
1982 case ALGORITHM_PARITY_0:
1983 pd_idx = 0;
1984 qd_idx = 1;
1985 (*dd_idx) += 2;
1986 break;
1987 case ALGORITHM_PARITY_N:
1988 pd_idx = data_disks;
1989 qd_idx = data_disks + 1;
1990 break;
1991
1992 case ALGORITHM_ROTATING_ZERO_RESTART:
1993 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1994 * of blocks for computing Q is different.
1995 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001996 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001997 qd_idx = pd_idx + 1;
1998 if (pd_idx == raid_disks-1) {
1999 (*dd_idx)++; /* Q D D D P */
2000 qd_idx = 0;
2001 } else if (*dd_idx >= pd_idx)
2002 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002003 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002004 break;
2005
2006 case ALGORITHM_ROTATING_N_RESTART:
2007 /* Same a left_asymmetric, by first stripe is
2008 * D D D P Q rather than
2009 * Q D D D P
2010 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002011 stripe2 += 1;
2012 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002013 qd_idx = pd_idx + 1;
2014 if (pd_idx == raid_disks-1) {
2015 (*dd_idx)++; /* Q D D D P */
2016 qd_idx = 0;
2017 } else if (*dd_idx >= pd_idx)
2018 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002019 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002020 break;
2021
2022 case ALGORITHM_ROTATING_N_CONTINUE:
2023 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002024 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002025 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2026 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002027 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 break;
2029
2030 case ALGORITHM_LEFT_ASYMMETRIC_6:
2031 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002032 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002033 if (*dd_idx >= pd_idx)
2034 (*dd_idx)++;
2035 qd_idx = raid_disks - 1;
2036 break;
2037
2038 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002040 if (*dd_idx >= pd_idx)
2041 (*dd_idx)++;
2042 qd_idx = raid_disks - 1;
2043 break;
2044
2045 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002046 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2048 qd_idx = raid_disks - 1;
2049 break;
2050
2051 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002052 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2054 qd_idx = raid_disks - 1;
2055 break;
2056
2057 case ALGORITHM_PARITY_0_6:
2058 pd_idx = 0;
2059 (*dd_idx)++;
2060 qd_idx = raid_disks - 1;
2061 break;
2062
NeilBrown16a53ec2006-06-26 00:27:38 -07002063 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002064 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002065 }
2066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 if (sh) {
2070 sh->pd_idx = pd_idx;
2071 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002072 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * Finally, compute the new sector number
2076 */
2077 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2078 return new_sector;
2079}
2080
2081
NeilBrown784052e2009-03-31 15:19:07 +11002082static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
NeilBrownd1688a62011-10-11 16:49:52 +11002084 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002085 int raid_disks = sh->disks;
2086 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002088 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2089 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002090 int algorithm = previous ? conf->prev_algo
2091 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 sector_t stripe;
2093 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002094 sector_t chunk_number;
2095 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002097 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
NeilBrown16a53ec2006-06-26 00:27:38 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2101 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
NeilBrown16a53ec2006-06-26 00:27:38 -07002103 if (i == sh->pd_idx)
2104 return 0;
2105 switch(conf->level) {
2106 case 4: break;
2107 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002108 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 case ALGORITHM_LEFT_ASYMMETRIC:
2110 case ALGORITHM_RIGHT_ASYMMETRIC:
2111 if (i > sh->pd_idx)
2112 i--;
2113 break;
2114 case ALGORITHM_LEFT_SYMMETRIC:
2115 case ALGORITHM_RIGHT_SYMMETRIC:
2116 if (i < sh->pd_idx)
2117 i += raid_disks;
2118 i -= (sh->pd_idx + 1);
2119 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 case ALGORITHM_PARITY_0:
2121 i -= 1;
2122 break;
2123 case ALGORITHM_PARITY_N:
2124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002126 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 }
2128 break;
2129 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002130 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002131 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002132 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002133 case ALGORITHM_LEFT_ASYMMETRIC:
2134 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002135 case ALGORITHM_ROTATING_ZERO_RESTART:
2136 case ALGORITHM_ROTATING_N_RESTART:
2137 if (sh->pd_idx == raid_disks-1)
2138 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002139 else if (i > sh->pd_idx)
2140 i -= 2; /* D D P Q D */
2141 break;
2142 case ALGORITHM_LEFT_SYMMETRIC:
2143 case ALGORITHM_RIGHT_SYMMETRIC:
2144 if (sh->pd_idx == raid_disks-1)
2145 i--; /* Q D D D P */
2146 else {
2147 /* D D P Q D */
2148 if (i < sh->pd_idx)
2149 i += raid_disks;
2150 i -= (sh->pd_idx + 2);
2151 }
2152 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002153 case ALGORITHM_PARITY_0:
2154 i -= 2;
2155 break;
2156 case ALGORITHM_PARITY_N:
2157 break;
2158 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002159 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002160 if (sh->pd_idx == 0)
2161 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002162 else {
2163 /* D D Q P D */
2164 if (i < sh->pd_idx)
2165 i += raid_disks;
2166 i -= (sh->pd_idx + 1);
2167 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002168 break;
2169 case ALGORITHM_LEFT_ASYMMETRIC_6:
2170 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2171 if (i > sh->pd_idx)
2172 i--;
2173 break;
2174 case ALGORITHM_LEFT_SYMMETRIC_6:
2175 case ALGORITHM_RIGHT_SYMMETRIC_6:
2176 if (i < sh->pd_idx)
2177 i += data_disks + 1;
2178 i -= (sh->pd_idx + 1);
2179 break;
2180 case ALGORITHM_PARITY_0_6:
2181 i -= 1;
2182 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002183 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002184 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002185 }
2186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188
2189 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002190 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
NeilBrown112bf892009-03-31 14:39:38 +11002192 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002193 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002194 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2195 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002196 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2197 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199 }
2200 return r_sector;
2201}
2202
2203
Dan Williams600aa102008-06-28 08:32:05 +10002204static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002205schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002206 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002207{
2208 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002209 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002210 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002211
Dan Williamse33129d2007-01-02 13:52:30 -07002212 if (rcw) {
2213 /* if we are not expanding this is a proper write request, and
2214 * there will be bios with new data to be drained into the
2215 * stripe cache
2216 */
2217 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002218 sh->reconstruct_state = reconstruct_state_drain_run;
2219 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2220 } else
2221 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002222
Dan Williamsac6b53b2009-07-14 13:40:19 -07002223 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002224
2225 for (i = disks; i--; ) {
2226 struct r5dev *dev = &sh->dev[i];
2227
2228 if (dev->towrite) {
2229 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002230 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002231 if (!expand)
2232 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002233 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002234 }
2235 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002236 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002237 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002238 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002239 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002240 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002241 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2242 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2243
Dan Williamsd8ee0722008-06-28 08:32:06 +10002244 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002245 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2246 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002247 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002248
2249 for (i = disks; i--; ) {
2250 struct r5dev *dev = &sh->dev[i];
2251 if (i == pd_idx)
2252 continue;
2253
Dan Williamse33129d2007-01-02 13:52:30 -07002254 if (dev->towrite &&
2255 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002256 test_bit(R5_Wantcompute, &dev->flags))) {
2257 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002258 set_bit(R5_LOCKED, &dev->flags);
2259 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002260 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002261 }
2262 }
2263 }
2264
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002265 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002266 * are in flight
2267 */
2268 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2269 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002270 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002271
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002272 if (level == 6) {
2273 int qd_idx = sh->qd_idx;
2274 struct r5dev *dev = &sh->dev[qd_idx];
2275
2276 set_bit(R5_LOCKED, &dev->flags);
2277 clear_bit(R5_UPTODATE, &dev->flags);
2278 s->locked++;
2279 }
2280
Dan Williams600aa102008-06-28 08:32:05 +10002281 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002282 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002283 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002284}
NeilBrown16a53ec2006-06-26 00:27:38 -07002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286/*
2287 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002288 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 * The bi_next chain must be in order.
2290 */
2291static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2292{
2293 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002294 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002295 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
NeilBrowncbe47ec2011-07-26 11:20:35 +10002297 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 (unsigned long long)bi->bi_sector,
2299 (unsigned long long)sh->sector);
2300
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002303 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002305 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2306 firstwrite = 1;
2307 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 bip = &sh->dev[dd_idx].toread;
2309 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2310 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2311 goto overlap;
2312 bip = & (*bip)->bi_next;
2313 }
2314 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2315 goto overlap;
2316
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002317 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (*bip)
2319 bi->bi_next = *bip;
2320 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002321 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (forwrite) {
2324 /* check if page is covered */
2325 sector_t sector = sh->dev[dd_idx].sector;
2326 for (bi=sh->dev[dd_idx].towrite;
2327 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2328 bi && bi->bi_sector <= sector;
2329 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2330 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2331 sector = bi->bi_sector + (bi->bi_size>>9);
2332 }
2333 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2334 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2335 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002336 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002337
2338 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2339 (unsigned long long)(*bip)->bi_sector,
2340 (unsigned long long)sh->sector, dd_idx);
2341
2342 if (conf->mddev->bitmap && firstwrite) {
2343 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2344 STRIPE_SECTORS, 0);
2345 sh->bm_seq = conf->seq_flush+1;
2346 set_bit(STRIPE_BIT_DELAY, &sh->state);
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 1;
2349
2350 overlap:
2351 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2352 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return 0;
2354}
2355
NeilBrownd1688a62011-10-11 16:49:52 +11002356static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002357
NeilBrownd1688a62011-10-11 16:49:52 +11002358static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002359 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002360{
NeilBrown784052e2009-03-31 15:19:07 +11002361 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002362 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002363 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002364 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002365 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002366
NeilBrown112bf892009-03-31 14:39:38 +11002367 raid5_compute_sector(conf,
2368 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002369 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002370 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002371 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002372}
2373
Dan Williamsa4456852007-07-09 11:56:43 -07002374static void
NeilBrownd1688a62011-10-11 16:49:52 +11002375handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002376 struct stripe_head_state *s, int disks,
2377 struct bio **return_bi)
2378{
2379 int i;
2380 for (i = disks; i--; ) {
2381 struct bio *bi;
2382 int bitmap_end = 0;
2383
2384 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002385 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002386 rcu_read_lock();
2387 rdev = rcu_dereference(conf->disks[i].rdev);
2388 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002389 atomic_inc(&rdev->nr_pending);
2390 else
2391 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002392 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002393 if (rdev) {
2394 if (!rdev_set_badblocks(
2395 rdev,
2396 sh->sector,
2397 STRIPE_SECTORS, 0))
2398 md_error(conf->mddev, rdev);
2399 rdev_dec_pending(rdev, conf->mddev);
2400 }
Dan Williamsa4456852007-07-09 11:56:43 -07002401 }
2402 spin_lock_irq(&conf->device_lock);
2403 /* fail all writes first */
2404 bi = sh->dev[i].towrite;
2405 sh->dev[i].towrite = NULL;
2406 if (bi) {
2407 s->to_write--;
2408 bitmap_end = 1;
2409 }
2410
2411 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2412 wake_up(&conf->wait_for_overlap);
2413
2414 while (bi && bi->bi_sector <
2415 sh->dev[i].sector + STRIPE_SECTORS) {
2416 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2417 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002418 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002419 md_write_end(conf->mddev);
2420 bi->bi_next = *return_bi;
2421 *return_bi = bi;
2422 }
2423 bi = nextbi;
2424 }
2425 /* and fail all 'written' */
2426 bi = sh->dev[i].written;
2427 sh->dev[i].written = NULL;
2428 if (bi) bitmap_end = 1;
2429 while (bi && bi->bi_sector <
2430 sh->dev[i].sector + STRIPE_SECTORS) {
2431 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2432 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002433 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002434 md_write_end(conf->mddev);
2435 bi->bi_next = *return_bi;
2436 *return_bi = bi;
2437 }
2438 bi = bi2;
2439 }
2440
Dan Williamsb5e98d62007-01-02 13:52:31 -07002441 /* fail any reads if this device is non-operational and
2442 * the data has not reached the cache yet.
2443 */
2444 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2445 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2446 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002447 bi = sh->dev[i].toread;
2448 sh->dev[i].toread = NULL;
2449 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2450 wake_up(&conf->wait_for_overlap);
2451 if (bi) s->to_read--;
2452 while (bi && bi->bi_sector <
2453 sh->dev[i].sector + STRIPE_SECTORS) {
2454 struct bio *nextbi =
2455 r5_next_bio(bi, sh->dev[i].sector);
2456 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002457 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002458 bi->bi_next = *return_bi;
2459 *return_bi = bi;
2460 }
2461 bi = nextbi;
2462 }
2463 }
2464 spin_unlock_irq(&conf->device_lock);
2465 if (bitmap_end)
2466 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2467 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002468 /* If we were in the middle of a write the parity block might
2469 * still be locked - so just clear all R5_LOCKED flags
2470 */
2471 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002472 }
2473
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002474 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2475 if (atomic_dec_and_test(&conf->pending_full_writes))
2476 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002477}
2478
NeilBrown7f0da592011-07-28 11:39:22 +10002479static void
NeilBrownd1688a62011-10-11 16:49:52 +11002480handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002481 struct stripe_head_state *s)
2482{
2483 int abort = 0;
2484 int i;
2485
NeilBrown7f0da592011-07-28 11:39:22 +10002486 clear_bit(STRIPE_SYNCING, &sh->state);
2487 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002488 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002489 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002490 * Don't even need to abort as that is handled elsewhere
2491 * if needed, and not always wanted e.g. if there is a known
2492 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002493 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002494 * non-sync devices, or abort the recovery
2495 */
NeilBrown18b98372012-04-01 23:48:38 +10002496 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2497 /* During recovery devices cannot be removed, so
2498 * locking and refcounting of rdevs is not needed
2499 */
2500 for (i = 0; i < conf->raid_disks; i++) {
2501 struct md_rdev *rdev = conf->disks[i].rdev;
2502 if (rdev
2503 && !test_bit(Faulty, &rdev->flags)
2504 && !test_bit(In_sync, &rdev->flags)
2505 && !rdev_set_badblocks(rdev, sh->sector,
2506 STRIPE_SECTORS, 0))
2507 abort = 1;
2508 rdev = conf->disks[i].replacement;
2509 if (rdev
2510 && !test_bit(Faulty, &rdev->flags)
2511 && !test_bit(In_sync, &rdev->flags)
2512 && !rdev_set_badblocks(rdev, sh->sector,
2513 STRIPE_SECTORS, 0))
2514 abort = 1;
2515 }
2516 if (abort)
2517 conf->recovery_disabled =
2518 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002519 }
NeilBrown18b98372012-04-01 23:48:38 +10002520 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002521}
2522
NeilBrown9a3e1102011-12-23 10:17:53 +11002523static int want_replace(struct stripe_head *sh, int disk_idx)
2524{
2525 struct md_rdev *rdev;
2526 int rv = 0;
2527 /* Doing recovery so rcu locking not required */
2528 rdev = sh->raid_conf->disks[disk_idx].replacement;
2529 if (rdev
2530 && !test_bit(Faulty, &rdev->flags)
2531 && !test_bit(In_sync, &rdev->flags)
2532 && (rdev->recovery_offset <= sh->sector
2533 || rdev->mddev->recovery_cp <= sh->sector))
2534 rv = 1;
2535
2536 return rv;
2537}
2538
NeilBrown93b3dbc2011-07-27 11:00:36 +10002539/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002540 * to be read or computed to satisfy a request.
2541 *
2542 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002543 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002544 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002545static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2546 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002547{
2548 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002549 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2550 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002551
Dan Williamsf38e1212007-01-02 13:52:30 -07002552 /* is the data in this block needed, and can we get it? */
2553 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002554 !test_bit(R5_UPTODATE, &dev->flags) &&
2555 (dev->toread ||
2556 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2557 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002558 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002559 (s->failed >= 1 && fdev[0]->toread) ||
2560 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002561 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2562 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2563 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002564 /* we would like to get this block, possibly by computing it,
2565 * otherwise read it if the backing disk is insync
2566 */
2567 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2568 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2569 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002570 (s->failed && (disk_idx == s->failed_num[0] ||
2571 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002572 /* have disk failed, and we're requested to fetch it;
2573 * do compute it
2574 */
2575 pr_debug("Computing stripe %llu block %d\n",
2576 (unsigned long long)sh->sector, disk_idx);
2577 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2578 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2579 set_bit(R5_Wantcompute, &dev->flags);
2580 sh->ops.target = disk_idx;
2581 sh->ops.target2 = -1; /* no 2nd target */
2582 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002583 /* Careful: from this point on 'uptodate' is in the eye
2584 * of raid_run_ops which services 'compute' operations
2585 * before writes. R5_Wantcompute flags a block that will
2586 * be R5_UPTODATE by the time it is needed for a
2587 * subsequent operation.
2588 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002589 s->uptodate++;
2590 return 1;
2591 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2592 /* Computing 2-failure is *very* expensive; only
2593 * do it if failed >= 2
2594 */
2595 int other;
2596 for (other = disks; other--; ) {
2597 if (other == disk_idx)
2598 continue;
2599 if (!test_bit(R5_UPTODATE,
2600 &sh->dev[other].flags))
2601 break;
2602 }
2603 BUG_ON(other < 0);
2604 pr_debug("Computing stripe %llu blocks %d,%d\n",
2605 (unsigned long long)sh->sector,
2606 disk_idx, other);
2607 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2608 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2609 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2610 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2611 sh->ops.target = disk_idx;
2612 sh->ops.target2 = other;
2613 s->uptodate += 2;
2614 s->req_compute = 1;
2615 return 1;
2616 } else if (test_bit(R5_Insync, &dev->flags)) {
2617 set_bit(R5_LOCKED, &dev->flags);
2618 set_bit(R5_Wantread, &dev->flags);
2619 s->locked++;
2620 pr_debug("Reading block %d (sync=%d)\n",
2621 disk_idx, s->syncing);
2622 }
2623 }
2624
2625 return 0;
2626}
2627
2628/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002629 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002630 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002631static void handle_stripe_fill(struct stripe_head *sh,
2632 struct stripe_head_state *s,
2633 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002634{
2635 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002636
2637 /* look for blocks to read/compute, skip this if a compute
2638 * is already in flight, or if the stripe contents are in the
2639 * midst of changing due to a write
2640 */
2641 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2642 !sh->reconstruct_state)
2643 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002644 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002645 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002646 set_bit(STRIPE_HANDLE, &sh->state);
2647}
2648
2649
Dan Williams1fe797e2008-06-28 09:16:30 +10002650/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002651 * any written block on an uptodate or failed drive can be returned.
2652 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2653 * never LOCKED, so we don't need to test 'failed' directly.
2654 */
NeilBrownd1688a62011-10-11 16:49:52 +11002655static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002656 struct stripe_head *sh, int disks, struct bio **return_bi)
2657{
2658 int i;
2659 struct r5dev *dev;
2660
2661 for (i = disks; i--; )
2662 if (sh->dev[i].written) {
2663 dev = &sh->dev[i];
2664 if (!test_bit(R5_LOCKED, &dev->flags) &&
2665 test_bit(R5_UPTODATE, &dev->flags)) {
2666 /* We can return any write requests */
2667 struct bio *wbi, *wbi2;
2668 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002669 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002670 spin_lock_irq(&conf->device_lock);
2671 wbi = dev->written;
2672 dev->written = NULL;
2673 while (wbi && wbi->bi_sector <
2674 dev->sector + STRIPE_SECTORS) {
2675 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002676 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002677 md_write_end(conf->mddev);
2678 wbi->bi_next = *return_bi;
2679 *return_bi = wbi;
2680 }
2681 wbi = wbi2;
2682 }
2683 if (dev->towrite == NULL)
2684 bitmap_end = 1;
2685 spin_unlock_irq(&conf->device_lock);
2686 if (bitmap_end)
2687 bitmap_endwrite(conf->mddev->bitmap,
2688 sh->sector,
2689 STRIPE_SECTORS,
2690 !test_bit(STRIPE_DEGRADED, &sh->state),
2691 0);
2692 }
2693 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002694
2695 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2696 if (atomic_dec_and_test(&conf->pending_full_writes))
2697 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002698}
2699
NeilBrownd1688a62011-10-11 16:49:52 +11002700static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002701 struct stripe_head *sh,
2702 struct stripe_head_state *s,
2703 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002704{
2705 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002706 if (conf->max_degraded == 2) {
2707 /* RAID6 requires 'rcw' in current implementation
2708 * Calculate the real rcw later - for now fake it
2709 * look like rcw is cheaper
2710 */
2711 rcw = 1; rmw = 2;
2712 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002713 /* would I have to read this buffer for read_modify_write */
2714 struct r5dev *dev = &sh->dev[i];
2715 if ((dev->towrite || i == sh->pd_idx) &&
2716 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002717 !(test_bit(R5_UPTODATE, &dev->flags) ||
2718 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002719 if (test_bit(R5_Insync, &dev->flags))
2720 rmw++;
2721 else
2722 rmw += 2*disks; /* cannot read it */
2723 }
2724 /* Would I have to read this buffer for reconstruct_write */
2725 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2726 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002727 !(test_bit(R5_UPTODATE, &dev->flags) ||
2728 test_bit(R5_Wantcompute, &dev->flags))) {
2729 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002730 else
2731 rcw += 2*disks;
2732 }
2733 }
Dan Williams45b42332007-07-09 11:56:43 -07002734 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002735 (unsigned long long)sh->sector, rmw, rcw);
2736 set_bit(STRIPE_HANDLE, &sh->state);
2737 if (rmw < rcw && rmw > 0)
2738 /* prefer read-modify-write, but need to get some data */
2739 for (i = disks; i--; ) {
2740 struct r5dev *dev = &sh->dev[i];
2741 if ((dev->towrite || i == sh->pd_idx) &&
2742 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002743 !(test_bit(R5_UPTODATE, &dev->flags) ||
2744 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002745 test_bit(R5_Insync, &dev->flags)) {
2746 if (
2747 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002748 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002749 "%d for r-m-w\n", i);
2750 set_bit(R5_LOCKED, &dev->flags);
2751 set_bit(R5_Wantread, &dev->flags);
2752 s->locked++;
2753 } else {
2754 set_bit(STRIPE_DELAYED, &sh->state);
2755 set_bit(STRIPE_HANDLE, &sh->state);
2756 }
2757 }
2758 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002759 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002760 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002761 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002762 for (i = disks; i--; ) {
2763 struct r5dev *dev = &sh->dev[i];
2764 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002765 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002766 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002767 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002768 test_bit(R5_Wantcompute, &dev->flags))) {
2769 rcw++;
2770 if (!test_bit(R5_Insync, &dev->flags))
2771 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002772 if (
2773 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002774 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002775 "%d for Reconstruct\n", i);
2776 set_bit(R5_LOCKED, &dev->flags);
2777 set_bit(R5_Wantread, &dev->flags);
2778 s->locked++;
2779 } else {
2780 set_bit(STRIPE_DELAYED, &sh->state);
2781 set_bit(STRIPE_HANDLE, &sh->state);
2782 }
2783 }
2784 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002785 }
Dan Williamsa4456852007-07-09 11:56:43 -07002786 /* now if nothing is locked, and if we have enough data,
2787 * we can start a write request
2788 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002789 /* since handle_stripe can be called at any time we need to handle the
2790 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002791 * subsequent call wants to start a write request. raid_run_ops only
2792 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002793 * simultaneously. If this is not the case then new writes need to be
2794 * held off until the compute completes.
2795 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002796 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2797 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2798 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002799 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002800}
2801
NeilBrownd1688a62011-10-11 16:49:52 +11002802static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002803 struct stripe_head_state *s, int disks)
2804{
Dan Williamsecc65c92008-06-28 08:31:57 +10002805 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002806
Dan Williamsbd2ab672008-04-10 21:29:27 -07002807 set_bit(STRIPE_HANDLE, &sh->state);
2808
Dan Williamsecc65c92008-06-28 08:31:57 +10002809 switch (sh->check_state) {
2810 case check_state_idle:
2811 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002812 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002813 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002814 sh->check_state = check_state_run;
2815 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002816 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002817 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002818 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002819 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002820 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002821 /* fall through */
2822 case check_state_compute_result:
2823 sh->check_state = check_state_idle;
2824 if (!dev)
2825 dev = &sh->dev[sh->pd_idx];
2826
2827 /* check that a write has not made the stripe insync */
2828 if (test_bit(STRIPE_INSYNC, &sh->state))
2829 break;
2830
2831 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002832 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2833 BUG_ON(s->uptodate != disks);
2834
2835 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002836 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002837 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002838
Dan Williamsa4456852007-07-09 11:56:43 -07002839 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002840 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002841 break;
2842 case check_state_run:
2843 break; /* we will be called again upon completion */
2844 case check_state_check_result:
2845 sh->check_state = check_state_idle;
2846
2847 /* if a failure occurred during the check operation, leave
2848 * STRIPE_INSYNC not set and let the stripe be handled again
2849 */
2850 if (s->failed)
2851 break;
2852
2853 /* handle a successful check operation, if parity is correct
2854 * we are done. Otherwise update the mismatch count and repair
2855 * parity if !MD_RECOVERY_CHECK
2856 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002857 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002858 /* parity is correct (on disc,
2859 * not in buffer any more)
2860 */
2861 set_bit(STRIPE_INSYNC, &sh->state);
2862 else {
2863 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2864 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2865 /* don't try to repair!! */
2866 set_bit(STRIPE_INSYNC, &sh->state);
2867 else {
2868 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002869 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002870 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2871 set_bit(R5_Wantcompute,
2872 &sh->dev[sh->pd_idx].flags);
2873 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002874 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002875 s->uptodate++;
2876 }
2877 }
2878 break;
2879 case check_state_compute_run:
2880 break;
2881 default:
2882 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2883 __func__, sh->check_state,
2884 (unsigned long long) sh->sector);
2885 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002886 }
2887}
2888
2889
NeilBrownd1688a62011-10-11 16:49:52 +11002890static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002891 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002892 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002893{
Dan Williamsa4456852007-07-09 11:56:43 -07002894 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002895 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002896 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002897
2898 set_bit(STRIPE_HANDLE, &sh->state);
2899
2900 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002901
Dan Williamsa4456852007-07-09 11:56:43 -07002902 /* Want to check and possibly repair P and Q.
2903 * However there could be one 'failed' device, in which
2904 * case we can only check one of them, possibly using the
2905 * other to generate missing data
2906 */
2907
Dan Williamsd82dfee2009-07-14 13:40:57 -07002908 switch (sh->check_state) {
2909 case check_state_idle:
2910 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002911 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002912 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002913 * makes sense to check P (If anything else were failed,
2914 * we would have used P to recreate it).
2915 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002916 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002917 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002918 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002919 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002920 * anything, so it makes sense to check it
2921 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002922 if (sh->check_state == check_state_run)
2923 sh->check_state = check_state_run_pq;
2924 else
2925 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002926 }
Dan Williams36d1c642009-07-14 11:48:22 -07002927
Dan Williamsd82dfee2009-07-14 13:40:57 -07002928 /* discard potentially stale zero_sum_result */
2929 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002930
Dan Williamsd82dfee2009-07-14 13:40:57 -07002931 if (sh->check_state == check_state_run) {
2932 /* async_xor_zero_sum destroys the contents of P */
2933 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2934 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002935 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002936 if (sh->check_state >= check_state_run &&
2937 sh->check_state <= check_state_run_pq) {
2938 /* async_syndrome_zero_sum preserves P and Q, so
2939 * no need to mark them !uptodate here
2940 */
2941 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2942 break;
2943 }
Dan Williams36d1c642009-07-14 11:48:22 -07002944
Dan Williamsd82dfee2009-07-14 13:40:57 -07002945 /* we have 2-disk failure */
2946 BUG_ON(s->failed != 2);
2947 /* fall through */
2948 case check_state_compute_result:
2949 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002950
Dan Williamsd82dfee2009-07-14 13:40:57 -07002951 /* check that a write has not made the stripe insync */
2952 if (test_bit(STRIPE_INSYNC, &sh->state))
2953 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002954
2955 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002956 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002957 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002958 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002959 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002960 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002961 s->locked++;
2962 set_bit(R5_LOCKED, &dev->flags);
2963 set_bit(R5_Wantwrite, &dev->flags);
2964 }
2965 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002966 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002967 s->locked++;
2968 set_bit(R5_LOCKED, &dev->flags);
2969 set_bit(R5_Wantwrite, &dev->flags);
2970 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002971 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002972 dev = &sh->dev[pd_idx];
2973 s->locked++;
2974 set_bit(R5_LOCKED, &dev->flags);
2975 set_bit(R5_Wantwrite, &dev->flags);
2976 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002977 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002978 dev = &sh->dev[qd_idx];
2979 s->locked++;
2980 set_bit(R5_LOCKED, &dev->flags);
2981 set_bit(R5_Wantwrite, &dev->flags);
2982 }
2983 clear_bit(STRIPE_DEGRADED, &sh->state);
2984
2985 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002986 break;
2987 case check_state_run:
2988 case check_state_run_q:
2989 case check_state_run_pq:
2990 break; /* we will be called again upon completion */
2991 case check_state_check_result:
2992 sh->check_state = check_state_idle;
2993
2994 /* handle a successful check operation, if parity is correct
2995 * we are done. Otherwise update the mismatch count and repair
2996 * parity if !MD_RECOVERY_CHECK
2997 */
2998 if (sh->ops.zero_sum_result == 0) {
2999 /* both parities are correct */
3000 if (!s->failed)
3001 set_bit(STRIPE_INSYNC, &sh->state);
3002 else {
3003 /* in contrast to the raid5 case we can validate
3004 * parity, but still have a failure to write
3005 * back
3006 */
3007 sh->check_state = check_state_compute_result;
3008 /* Returning at this point means that we may go
3009 * off and bring p and/or q uptodate again so
3010 * we make sure to check zero_sum_result again
3011 * to verify if p or q need writeback
3012 */
3013 }
3014 } else {
3015 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3016 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3017 /* don't try to repair!! */
3018 set_bit(STRIPE_INSYNC, &sh->state);
3019 else {
3020 int *target = &sh->ops.target;
3021
3022 sh->ops.target = -1;
3023 sh->ops.target2 = -1;
3024 sh->check_state = check_state_compute_run;
3025 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3026 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3027 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3028 set_bit(R5_Wantcompute,
3029 &sh->dev[pd_idx].flags);
3030 *target = pd_idx;
3031 target = &sh->ops.target2;
3032 s->uptodate++;
3033 }
3034 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3035 set_bit(R5_Wantcompute,
3036 &sh->dev[qd_idx].flags);
3037 *target = qd_idx;
3038 s->uptodate++;
3039 }
3040 }
3041 }
3042 break;
3043 case check_state_compute_run:
3044 break;
3045 default:
3046 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3047 __func__, sh->check_state,
3048 (unsigned long long) sh->sector);
3049 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003050 }
3051}
3052
NeilBrownd1688a62011-10-11 16:49:52 +11003053static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003054{
3055 int i;
3056
3057 /* We have read all the blocks in this stripe and now we need to
3058 * copy some of them into a target stripe for expand.
3059 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003060 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003061 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3062 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003063 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003064 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003065 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003066 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003067
NeilBrown784052e2009-03-31 15:19:07 +11003068 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003069 sector_t s = raid5_compute_sector(conf, bn, 0,
3070 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003071 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003072 if (sh2 == NULL)
3073 /* so far only the early blocks of this stripe
3074 * have been requested. When later blocks
3075 * get requested, we will try again
3076 */
3077 continue;
3078 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3079 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3080 /* must have already done this block */
3081 release_stripe(sh2);
3082 continue;
3083 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003084
3085 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003086 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003087 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003088 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003089 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003090
Dan Williamsa4456852007-07-09 11:56:43 -07003091 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3092 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3093 for (j = 0; j < conf->raid_disks; j++)
3094 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003095 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003096 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3097 break;
3098 if (j == conf->raid_disks) {
3099 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3100 set_bit(STRIPE_HANDLE, &sh2->state);
3101 }
3102 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003103
Dan Williamsa4456852007-07-09 11:56:43 -07003104 }
NeilBrowna2e08552007-09-11 15:23:36 -07003105 /* done submitting copies, wait for them to complete */
3106 if (tx) {
3107 async_tx_ack(tx);
3108 dma_wait_for_async_tx(tx);
3109 }
Dan Williamsa4456852007-07-09 11:56:43 -07003110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112/*
3113 * handle_stripe - do things to a stripe.
3114 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003115 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3116 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003118 * return some read requests which now have data
3119 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 * schedule a read on some buffers
3121 * schedule a write of some buffers
3122 * return confirmation of parity correctness
3123 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 */
Dan Williamsa4456852007-07-09 11:56:43 -07003125
NeilBrownacfe7262011-07-27 11:00:36 +10003126static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003127{
NeilBrownd1688a62011-10-11 16:49:52 +11003128 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003129 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003130 struct r5dev *dev;
3131 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003132 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003133
NeilBrownacfe7262011-07-27 11:00:36 +10003134 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003135
NeilBrownacfe7262011-07-27 11:00:36 +10003136 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3137 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3138 s->failed_num[0] = -1;
3139 s->failed_num[1] = -1;
3140
3141 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003142 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003143 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003144 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003145 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003146 sector_t first_bad;
3147 int bad_sectors;
3148 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003149
NeilBrown16a53ec2006-06-26 00:27:38 -07003150 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003151
Dan Williams45b42332007-07-09 11:56:43 -07003152 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003153 i, dev->flags,
3154 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003155 /* maybe we can reply to a read
3156 *
3157 * new wantfill requests are only permitted while
3158 * ops_complete_biofill is guaranteed to be inactive
3159 */
3160 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3161 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3162 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003163
3164 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003165 if (test_bit(R5_LOCKED, &dev->flags))
3166 s->locked++;
3167 if (test_bit(R5_UPTODATE, &dev->flags))
3168 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003169 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003170 s->compute++;
3171 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003172 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003173
NeilBrownacfe7262011-07-27 11:00:36 +10003174 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003175 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003176 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003177 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003178 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003179 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003180 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003181 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003182 }
Dan Williamsa4456852007-07-09 11:56:43 -07003183 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003184 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003185 /* Prefer to use the replacement for reads, but only
3186 * if it is recovered enough and has no bad blocks.
3187 */
3188 rdev = rcu_dereference(conf->disks[i].replacement);
3189 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3190 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3191 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3192 &first_bad, &bad_sectors))
3193 set_bit(R5_ReadRepl, &dev->flags);
3194 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003195 if (rdev)
3196 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003197 rdev = rcu_dereference(conf->disks[i].rdev);
3198 clear_bit(R5_ReadRepl, &dev->flags);
3199 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003200 if (rdev && test_bit(Faulty, &rdev->flags))
3201 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003202 if (rdev) {
3203 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3204 &first_bad, &bad_sectors);
3205 if (s->blocked_rdev == NULL
3206 && (test_bit(Blocked, &rdev->flags)
3207 || is_bad < 0)) {
3208 if (is_bad < 0)
3209 set_bit(BlockedBadBlocks,
3210 &rdev->flags);
3211 s->blocked_rdev = rdev;
3212 atomic_inc(&rdev->nr_pending);
3213 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003214 }
NeilBrown415e72d2010-06-17 17:25:21 +10003215 clear_bit(R5_Insync, &dev->flags);
3216 if (!rdev)
3217 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003218 else if (is_bad) {
3219 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003220 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3221 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003222 /* treat as in-sync, but with a read error
3223 * which we can now try to correct
3224 */
3225 set_bit(R5_Insync, &dev->flags);
3226 set_bit(R5_ReadError, &dev->flags);
3227 }
3228 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003229 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003230 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003231 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003232 set_bit(R5_Insync, &dev->flags);
3233 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3234 test_bit(R5_Expanded, &dev->flags))
3235 /* If we've reshaped into here, we assume it is Insync.
3236 * We will shortly update recovery_offset to make
3237 * it official.
3238 */
3239 set_bit(R5_Insync, &dev->flags);
3240
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003241 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003242 /* This flag does not apply to '.replacement'
3243 * only to .rdev, so make sure to check that*/
3244 struct md_rdev *rdev2 = rcu_dereference(
3245 conf->disks[i].rdev);
3246 if (rdev2 == rdev)
3247 clear_bit(R5_Insync, &dev->flags);
3248 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003249 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003250 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003251 } else
3252 clear_bit(R5_WriteError, &dev->flags);
3253 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003254 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003255 /* This flag does not apply to '.replacement'
3256 * only to .rdev, so make sure to check that*/
3257 struct md_rdev *rdev2 = rcu_dereference(
3258 conf->disks[i].rdev);
3259 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003260 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003261 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003262 } else
3263 clear_bit(R5_MadeGood, &dev->flags);
3264 }
NeilBrown977df362011-12-23 10:17:53 +11003265 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3266 struct md_rdev *rdev2 = rcu_dereference(
3267 conf->disks[i].replacement);
3268 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3269 s->handle_bad_blocks = 1;
3270 atomic_inc(&rdev2->nr_pending);
3271 } else
3272 clear_bit(R5_MadeGoodRepl, &dev->flags);
3273 }
NeilBrown415e72d2010-06-17 17:25:21 +10003274 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003275 /* The ReadError flag will just be confusing now */
3276 clear_bit(R5_ReadError, &dev->flags);
3277 clear_bit(R5_ReWrite, &dev->flags);
3278 }
NeilBrown415e72d2010-06-17 17:25:21 +10003279 if (test_bit(R5_ReadError, &dev->flags))
3280 clear_bit(R5_Insync, &dev->flags);
3281 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003282 if (s->failed < 2)
3283 s->failed_num[s->failed] = i;
3284 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003285 if (rdev && !test_bit(Faulty, &rdev->flags))
3286 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003287 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003288 }
NeilBrownc4c16632011-07-26 11:34:20 +10003289 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003290 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3291 /* If there is a failed device being replaced,
3292 * we must be recovering.
3293 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003294 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003295 * else we can only be replacing
3296 * sync and recovery both need to read all devices, and so
3297 * use the same flag.
3298 */
3299 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003300 sh->sector >= conf->mddev->recovery_cp ||
3301 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003302 s->syncing = 1;
3303 else
3304 s->replacing = 1;
3305 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003306 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003307}
NeilBrownf4168852007-02-28 20:11:53 -08003308
NeilBrowncc940152011-07-26 11:35:35 +10003309static void handle_stripe(struct stripe_head *sh)
3310{
3311 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003312 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003313 int i;
NeilBrown84789552011-07-27 11:00:36 +10003314 int prexor;
3315 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003316 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003317
3318 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003319 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003320 /* already being handled, ensure it gets handled
3321 * again when current action finishes */
3322 set_bit(STRIPE_HANDLE, &sh->state);
3323 return;
3324 }
3325
3326 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3327 set_bit(STRIPE_SYNCING, &sh->state);
3328 clear_bit(STRIPE_INSYNC, &sh->state);
3329 }
3330 clear_bit(STRIPE_DELAYED, &sh->state);
3331
3332 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3333 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3334 (unsigned long long)sh->sector, sh->state,
3335 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3336 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003337
NeilBrownacfe7262011-07-27 11:00:36 +10003338 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003339
NeilBrownbc2607f2011-07-28 11:39:22 +10003340 if (s.handle_bad_blocks) {
3341 set_bit(STRIPE_HANDLE, &sh->state);
3342 goto finish;
3343 }
3344
NeilBrown474af965fe2011-07-27 11:00:36 +10003345 if (unlikely(s.blocked_rdev)) {
3346 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003347 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003348 set_bit(STRIPE_HANDLE, &sh->state);
3349 goto finish;
3350 }
3351 /* There is nothing for the blocked_rdev to block */
3352 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3353 s.blocked_rdev = NULL;
3354 }
3355
3356 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3357 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3358 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3359 }
3360
3361 pr_debug("locked=%d uptodate=%d to_read=%d"
3362 " to_write=%d failed=%d failed_num=%d,%d\n",
3363 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3364 s.failed_num[0], s.failed_num[1]);
3365 /* check if the array has lost more than max_degraded devices and,
3366 * if so, some requests might need to be failed.
3367 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003368 if (s.failed > conf->max_degraded) {
3369 sh->check_state = 0;
3370 sh->reconstruct_state = 0;
3371 if (s.to_read+s.to_write+s.written)
3372 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003373 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003374 handle_failed_sync(conf, sh, &s);
3375 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003376
3377 /*
3378 * might be able to return some write requests if the parity blocks
3379 * are safe, or on a failed drive
3380 */
3381 pdev = &sh->dev[sh->pd_idx];
3382 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3383 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3384 qdev = &sh->dev[sh->qd_idx];
3385 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3386 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3387 || conf->level < 6;
3388
3389 if (s.written &&
3390 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3391 && !test_bit(R5_LOCKED, &pdev->flags)
3392 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3393 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3394 && !test_bit(R5_LOCKED, &qdev->flags)
3395 && test_bit(R5_UPTODATE, &qdev->flags)))))
3396 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3397
3398 /* Now we might consider reading some blocks, either to check/generate
3399 * parity, or to satisfy requests
3400 * or to load a block that is being partially written.
3401 */
3402 if (s.to_read || s.non_overwrite
3403 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003404 || (s.syncing && (s.uptodate + s.compute < disks))
3405 || s.replacing
3406 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003407 handle_stripe_fill(sh, &s, disks);
3408
NeilBrown84789552011-07-27 11:00:36 +10003409 /* Now we check to see if any write operations have recently
3410 * completed
3411 */
3412 prexor = 0;
3413 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3414 prexor = 1;
3415 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3416 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3417 sh->reconstruct_state = reconstruct_state_idle;
3418
3419 /* All the 'written' buffers and the parity block are ready to
3420 * be written back to disk
3421 */
3422 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3423 BUG_ON(sh->qd_idx >= 0 &&
3424 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3425 for (i = disks; i--; ) {
3426 struct r5dev *dev = &sh->dev[i];
3427 if (test_bit(R5_LOCKED, &dev->flags) &&
3428 (i == sh->pd_idx || i == sh->qd_idx ||
3429 dev->written)) {
3430 pr_debug("Writing block %d\n", i);
3431 set_bit(R5_Wantwrite, &dev->flags);
3432 if (prexor)
3433 continue;
3434 if (!test_bit(R5_Insync, &dev->flags) ||
3435 ((i == sh->pd_idx || i == sh->qd_idx) &&
3436 s.failed == 0))
3437 set_bit(STRIPE_INSYNC, &sh->state);
3438 }
3439 }
3440 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3441 s.dec_preread_active = 1;
3442 }
3443
3444 /* Now to consider new write requests and what else, if anything
3445 * should be read. We do not handle new writes when:
3446 * 1/ A 'write' operation (copy+xor) is already in flight.
3447 * 2/ A 'check' operation is in flight, as it may clobber the parity
3448 * block.
3449 */
3450 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3451 handle_stripe_dirtying(conf, sh, &s, disks);
3452
3453 /* maybe we need to check and possibly fix the parity for this stripe
3454 * Any reads will already have been scheduled, so we just see if enough
3455 * data is available. The parity check is held off while parity
3456 * dependent operations are in flight.
3457 */
3458 if (sh->check_state ||
3459 (s.syncing && s.locked == 0 &&
3460 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3461 !test_bit(STRIPE_INSYNC, &sh->state))) {
3462 if (conf->level == 6)
3463 handle_parity_checks6(conf, sh, &s, disks);
3464 else
3465 handle_parity_checks5(conf, sh, &s, disks);
3466 }
NeilBrownc5a31002011-07-27 11:00:36 +10003467
NeilBrown9a3e1102011-12-23 10:17:53 +11003468 if (s.replacing && s.locked == 0
3469 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3470 /* Write out to replacement devices where possible */
3471 for (i = 0; i < conf->raid_disks; i++)
3472 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3473 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3474 set_bit(R5_WantReplace, &sh->dev[i].flags);
3475 set_bit(R5_LOCKED, &sh->dev[i].flags);
3476 s.locked++;
3477 }
3478 set_bit(STRIPE_INSYNC, &sh->state);
3479 }
3480 if ((s.syncing || s.replacing) && s.locked == 0 &&
3481 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003482 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3483 clear_bit(STRIPE_SYNCING, &sh->state);
3484 }
3485
3486 /* If the failed drives are just a ReadError, then we might need
3487 * to progress the repair/check process
3488 */
3489 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3490 for (i = 0; i < s.failed; i++) {
3491 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3492 if (test_bit(R5_ReadError, &dev->flags)
3493 && !test_bit(R5_LOCKED, &dev->flags)
3494 && test_bit(R5_UPTODATE, &dev->flags)
3495 ) {
3496 if (!test_bit(R5_ReWrite, &dev->flags)) {
3497 set_bit(R5_Wantwrite, &dev->flags);
3498 set_bit(R5_ReWrite, &dev->flags);
3499 set_bit(R5_LOCKED, &dev->flags);
3500 s.locked++;
3501 } else {
3502 /* let's read it back */
3503 set_bit(R5_Wantread, &dev->flags);
3504 set_bit(R5_LOCKED, &dev->flags);
3505 s.locked++;
3506 }
3507 }
3508 }
3509
3510
NeilBrown3687c062011-07-27 11:00:36 +10003511 /* Finish reconstruct operations initiated by the expansion process */
3512 if (sh->reconstruct_state == reconstruct_state_result) {
3513 struct stripe_head *sh_src
3514 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3515 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3516 /* sh cannot be written until sh_src has been read.
3517 * so arrange for sh to be delayed a little
3518 */
3519 set_bit(STRIPE_DELAYED, &sh->state);
3520 set_bit(STRIPE_HANDLE, &sh->state);
3521 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3522 &sh_src->state))
3523 atomic_inc(&conf->preread_active_stripes);
3524 release_stripe(sh_src);
3525 goto finish;
3526 }
3527 if (sh_src)
3528 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003529
NeilBrown3687c062011-07-27 11:00:36 +10003530 sh->reconstruct_state = reconstruct_state_idle;
3531 clear_bit(STRIPE_EXPANDING, &sh->state);
3532 for (i = conf->raid_disks; i--; ) {
3533 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3534 set_bit(R5_LOCKED, &sh->dev[i].flags);
3535 s.locked++;
3536 }
3537 }
3538
3539 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3540 !sh->reconstruct_state) {
3541 /* Need to write out all blocks after computing parity */
3542 sh->disks = conf->raid_disks;
3543 stripe_set_idx(sh->sector, conf, 0, sh);
3544 schedule_reconstruction(sh, &s, 1, 1);
3545 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3546 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3547 atomic_dec(&conf->reshape_stripes);
3548 wake_up(&conf->wait_for_overlap);
3549 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3550 }
3551
3552 if (s.expanding && s.locked == 0 &&
3553 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3554 handle_stripe_expansion(conf, sh);
3555
3556finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003557 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003558 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003559 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003560
NeilBrownbc2607f2011-07-28 11:39:22 +10003561 if (s.handle_bad_blocks)
3562 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003563 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003564 struct r5dev *dev = &sh->dev[i];
3565 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3566 /* We own a safe reference to the rdev */
3567 rdev = conf->disks[i].rdev;
3568 if (!rdev_set_badblocks(rdev, sh->sector,
3569 STRIPE_SECTORS, 0))
3570 md_error(conf->mddev, rdev);
3571 rdev_dec_pending(rdev, conf->mddev);
3572 }
NeilBrownb84db562011-07-28 11:39:23 +10003573 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3574 rdev = conf->disks[i].rdev;
3575 rdev_clear_badblocks(rdev, sh->sector,
3576 STRIPE_SECTORS);
3577 rdev_dec_pending(rdev, conf->mddev);
3578 }
NeilBrown977df362011-12-23 10:17:53 +11003579 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3580 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003581 if (!rdev)
3582 /* rdev have been moved down */
3583 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003584 rdev_clear_badblocks(rdev, sh->sector,
3585 STRIPE_SECTORS);
3586 rdev_dec_pending(rdev, conf->mddev);
3587 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003588 }
3589
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003590 if (s.ops_request)
3591 raid_run_ops(sh, s.ops_request);
3592
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003593 ops_run_io(sh, &s);
3594
NeilBrownc5709ef2011-07-26 11:35:20 +10003595 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003596 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003597 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003598 * have actually been submitted.
3599 */
3600 atomic_dec(&conf->preread_active_stripes);
3601 if (atomic_read(&conf->preread_active_stripes) <
3602 IO_THRESHOLD)
3603 md_wakeup_thread(conf->mddev->thread);
3604 }
3605
NeilBrownc5709ef2011-07-26 11:35:20 +10003606 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003607
Dan Williams257a4b42011-11-08 16:22:06 +11003608 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003609}
3610
NeilBrownd1688a62011-10-11 16:49:52 +11003611static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3614 while (!list_empty(&conf->delayed_list)) {
3615 struct list_head *l = conf->delayed_list.next;
3616 struct stripe_head *sh;
3617 sh = list_entry(l, struct stripe_head, lru);
3618 list_del_init(l);
3619 clear_bit(STRIPE_DELAYED, &sh->state);
3620 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3621 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003622 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
NeilBrown482c0832011-04-18 18:25:42 +10003624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
NeilBrownd1688a62011-10-11 16:49:52 +11003627static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003628{
3629 /* device_lock is held */
3630 struct list_head head;
3631 list_add(&head, &conf->bitmap_list);
3632 list_del_init(&conf->bitmap_list);
3633 while (!list_empty(&head)) {
3634 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3635 list_del_init(&sh->lru);
3636 atomic_inc(&sh->count);
3637 __release_stripe(conf, sh);
3638 }
3639}
3640
NeilBrownfd01b882011-10-11 16:47:53 +11003641int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003642{
NeilBrownd1688a62011-10-11 16:49:52 +11003643 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003644
3645 /* No difference between reads and writes. Just check
3646 * how busy the stripe_cache is
3647 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003648
NeilBrownf022b2f2006-10-03 01:15:56 -07003649 if (conf->inactive_blocked)
3650 return 1;
3651 if (conf->quiesce)
3652 return 1;
3653 if (list_empty_careful(&conf->inactive_list))
3654 return 1;
3655
3656 return 0;
3657}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003658EXPORT_SYMBOL_GPL(md_raid5_congested);
3659
3660static int raid5_congested(void *data, int bits)
3661{
NeilBrownfd01b882011-10-11 16:47:53 +11003662 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003663
3664 return mddev_congested(mddev, bits) ||
3665 md_raid5_congested(mddev, bits);
3666}
NeilBrownf022b2f2006-10-03 01:15:56 -07003667
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003668/* We want read requests to align with chunks where possible,
3669 * but write requests don't need to.
3670 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003671static int raid5_mergeable_bvec(struct request_queue *q,
3672 struct bvec_merge_data *bvm,
3673 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003674{
NeilBrownfd01b882011-10-11 16:47:53 +11003675 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003676 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003677 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003678 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003679 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003680
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003681 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003682 return biovec->bv_len; /* always allow writes to be mergeable */
3683
Andre Noll664e7c42009-06-18 08:45:27 +10003684 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3685 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003686 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3687 if (max < 0) max = 0;
3688 if (max <= biovec->bv_len && bio_sectors == 0)
3689 return biovec->bv_len;
3690 else
3691 return max;
3692}
3693
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003694
NeilBrownfd01b882011-10-11 16:47:53 +11003695static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003696{
3697 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003698 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003699 unsigned int bio_sectors = bio->bi_size >> 9;
3700
Andre Noll664e7c42009-06-18 08:45:27 +10003701 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3702 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003703 return chunk_sectors >=
3704 ((sector & (chunk_sectors - 1)) + bio_sectors);
3705}
3706
3707/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003708 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3709 * later sampled by raid5d.
3710 */
NeilBrownd1688a62011-10-11 16:49:52 +11003711static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003712{
3713 unsigned long flags;
3714
3715 spin_lock_irqsave(&conf->device_lock, flags);
3716
3717 bi->bi_next = conf->retry_read_aligned_list;
3718 conf->retry_read_aligned_list = bi;
3719
3720 spin_unlock_irqrestore(&conf->device_lock, flags);
3721 md_wakeup_thread(conf->mddev->thread);
3722}
3723
3724
NeilBrownd1688a62011-10-11 16:49:52 +11003725static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003726{
3727 struct bio *bi;
3728
3729 bi = conf->retry_read_aligned;
3730 if (bi) {
3731 conf->retry_read_aligned = NULL;
3732 return bi;
3733 }
3734 bi = conf->retry_read_aligned_list;
3735 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003736 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003737 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003738 /*
3739 * this sets the active strip count to 1 and the processed
3740 * strip count to zero (upper 8 bits)
3741 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003742 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003743 }
3744
3745 return bi;
3746}
3747
3748
3749/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003750 * The "raid5_align_endio" should check if the read succeeded and if it
3751 * did, call bio_endio on the original bio (having bio_put the new bio
3752 * first).
3753 * If the read failed..
3754 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003755static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003756{
3757 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003758 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003759 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003761 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003762
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003763 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003764
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003765 rdev = (void*)raid_bi->bi_next;
3766 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003767 mddev = rdev->mddev;
3768 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003769
3770 rdev_dec_pending(rdev, conf->mddev);
3771
3772 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003773 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003774 if (atomic_dec_and_test(&conf->active_aligned_reads))
3775 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003776 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003777 }
3778
3779
Dan Williams45b42332007-07-09 11:56:43 -07003780 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003781
3782 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003783}
3784
Neil Brown387bb172007-02-08 14:20:29 -08003785static int bio_fits_rdev(struct bio *bi)
3786{
Jens Axboe165125e2007-07-24 09:28:11 +02003787 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003788
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003789 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003790 return 0;
3791 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003792 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003793 return 0;
3794
3795 if (q->merge_bvec_fn)
3796 /* it's too hard to apply the merge_bvec_fn at this stage,
3797 * just just give up
3798 */
3799 return 0;
3800
3801 return 1;
3802}
3803
3804
NeilBrownfd01b882011-10-11 16:47:53 +11003805static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003806{
NeilBrownd1688a62011-10-11 16:49:52 +11003807 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003808 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003809 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003810 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003811 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003812
3813 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003814 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815 return 0;
3816 }
3817 /*
NeilBrowna167f662010-10-26 18:31:13 +11003818 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003819 */
NeilBrowna167f662010-10-26 18:31:13 +11003820 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003821 if (!align_bi)
3822 return 0;
3823 /*
3824 * set bi_end_io to a new function, and set bi_private to the
3825 * original bio.
3826 */
3827 align_bi->bi_end_io = raid5_align_endio;
3828 align_bi->bi_private = raid_bio;
3829 /*
3830 * compute position
3831 */
NeilBrown112bf892009-03-31 14:39:38 +11003832 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3833 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003834 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003835
NeilBrown671488c2011-12-23 10:17:52 +11003836 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003837 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003838 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3839 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3840 rdev->recovery_offset < end_sector) {
3841 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3842 if (rdev &&
3843 (test_bit(Faulty, &rdev->flags) ||
3844 !(test_bit(In_sync, &rdev->flags) ||
3845 rdev->recovery_offset >= end_sector)))
3846 rdev = NULL;
3847 }
3848 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003849 sector_t first_bad;
3850 int bad_sectors;
3851
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003852 atomic_inc(&rdev->nr_pending);
3853 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854 raid_bio->bi_next = (void*)rdev;
3855 align_bi->bi_bdev = rdev->bdev;
3856 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003857
NeilBrown31c176e2011-07-28 11:39:22 +10003858 if (!bio_fits_rdev(align_bi) ||
3859 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3860 &first_bad, &bad_sectors)) {
3861 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003862 bio_put(align_bi);
3863 rdev_dec_pending(rdev, mddev);
3864 return 0;
3865 }
3866
majianpengd3f94022012-06-12 08:31:10 +08003867 /* No reshape active, so we can trust rdev->data_offset */
3868 align_bi->bi_sector += rdev->data_offset;
3869
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003870 spin_lock_irq(&conf->device_lock);
3871 wait_event_lock_irq(conf->wait_for_stripe,
3872 conf->quiesce == 0,
3873 conf->device_lock, /* nothing */);
3874 atomic_inc(&conf->active_aligned_reads);
3875 spin_unlock_irq(&conf->device_lock);
3876
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003877 generic_make_request(align_bi);
3878 return 1;
3879 } else {
3880 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003881 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003882 return 0;
3883 }
3884}
3885
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003886/* __get_priority_stripe - get the next stripe to process
3887 *
3888 * Full stripe writes are allowed to pass preread active stripes up until
3889 * the bypass_threshold is exceeded. In general the bypass_count
3890 * increments when the handle_list is handled before the hold_list; however, it
3891 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3892 * stripe with in flight i/o. The bypass_count will be reset when the
3893 * head of the hold_list has changed, i.e. the head was promoted to the
3894 * handle_list.
3895 */
NeilBrownd1688a62011-10-11 16:49:52 +11003896static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003897{
3898 struct stripe_head *sh;
3899
3900 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3901 __func__,
3902 list_empty(&conf->handle_list) ? "empty" : "busy",
3903 list_empty(&conf->hold_list) ? "empty" : "busy",
3904 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3905
3906 if (!list_empty(&conf->handle_list)) {
3907 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3908
3909 if (list_empty(&conf->hold_list))
3910 conf->bypass_count = 0;
3911 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3912 if (conf->hold_list.next == conf->last_hold)
3913 conf->bypass_count++;
3914 else {
3915 conf->last_hold = conf->hold_list.next;
3916 conf->bypass_count -= conf->bypass_threshold;
3917 if (conf->bypass_count < 0)
3918 conf->bypass_count = 0;
3919 }
3920 }
3921 } else if (!list_empty(&conf->hold_list) &&
3922 ((conf->bypass_threshold &&
3923 conf->bypass_count > conf->bypass_threshold) ||
3924 atomic_read(&conf->pending_full_writes) == 0)) {
3925 sh = list_entry(conf->hold_list.next,
3926 typeof(*sh), lru);
3927 conf->bypass_count -= conf->bypass_threshold;
3928 if (conf->bypass_count < 0)
3929 conf->bypass_count = 0;
3930 } else
3931 return NULL;
3932
3933 list_del_init(&sh->lru);
3934 atomic_inc(&sh->count);
3935 BUG_ON(atomic_read(&sh->count) != 1);
3936 return sh;
3937}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003938
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003939static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
NeilBrownd1688a62011-10-11 16:49:52 +11003941 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003942 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 sector_t new_sector;
3944 sector_t logical_sector, last_sector;
3945 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003946 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003947 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003948 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Tejun Heoe9c74692010-09-03 11:56:18 +02003950 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3951 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003952 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003953 }
3954
NeilBrown3d310eb2005-06-21 17:17:26 -07003955 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003956
NeilBrown802ba062006-12-13 00:34:13 -08003957 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003958 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003959 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003960 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003961
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3963 last_sector = bi->bi_sector + (bi->bi_size>>9);
3964 bi->bi_next = NULL;
3965 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003966
NeilBrown7c13edc2011-04-18 18:25:43 +10003967 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3969 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003970 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003971 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003972
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003973 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003974 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003975 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003976 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003977 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003978 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003979 * 64bit on a 32bit platform, and so it might be
3980 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003981 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003982 * the lock is dropped, so once we get a reference
3983 * to the stripe that we think it is, we will have
3984 * to check again.
3985 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003986 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003987 if (mddev->delta_disks < 0
3988 ? logical_sector < conf->reshape_progress
3989 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003990 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003991 previous = 1;
3992 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003993 if (mddev->delta_disks < 0
3994 ? logical_sector < conf->reshape_safe
3995 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003996 spin_unlock_irq(&conf->device_lock);
3997 schedule();
3998 goto retry;
3999 }
4000 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004001 spin_unlock_irq(&conf->device_lock);
4002 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004003 data_disks = disks - conf->max_degraded;
4004
NeilBrown112bf892009-03-31 14:39:38 +11004005 new_sector = raid5_compute_sector(conf, logical_sector,
4006 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004007 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004008 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 (unsigned long long)new_sector,
4010 (unsigned long long)logical_sector);
4011
NeilBrownb5663ba2009-03-31 14:39:38 +11004012 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004013 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004015 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004016 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004017 * stripe, so we must do the range check again.
4018 * Expansion could still move past after this
4019 * test, but as we are holding a reference to
4020 * 'sh', we know that if that happens,
4021 * STRIPE_EXPANDING will get set and the expansion
4022 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004023 */
4024 int must_retry = 0;
4025 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004026 if (mddev->delta_disks < 0
4027 ? logical_sector >= conf->reshape_progress
4028 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004029 /* mismatch, need to try again */
4030 must_retry = 1;
4031 spin_unlock_irq(&conf->device_lock);
4032 if (must_retry) {
4033 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004034 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004035 goto retry;
4036 }
4037 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004038
Namhyung Kimffd96e32011-07-18 17:38:51 +10004039 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004040 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004041 logical_sector < mddev->suspend_hi) {
4042 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004043 /* As the suspend_* range is controlled by
4044 * userspace, we want an interruptible
4045 * wait.
4046 */
4047 flush_signals(current);
4048 prepare_to_wait(&conf->wait_for_overlap,
4049 &w, TASK_INTERRUPTIBLE);
4050 if (logical_sector >= mddev->suspend_lo &&
4051 logical_sector < mddev->suspend_hi)
4052 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004053 goto retry;
4054 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004055
4056 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004057 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004058 /* Stripe is busy expanding or
4059 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 * and wait a while
4061 */
NeilBrown482c0832011-04-18 18:25:42 +10004062 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 release_stripe(sh);
4064 schedule();
4065 goto retry;
4066 }
4067 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004068 set_bit(STRIPE_HANDLE, &sh->state);
4069 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004070 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004071 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4072 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 } else {
4075 /* cannot get stripe for read-ahead, just give-up */
4076 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4077 finish_wait(&conf->wait_for_overlap, &w);
4078 break;
4079 }
4080
4081 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004082 if (!plugged)
4083 md_wakeup_thread(mddev->thread);
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004086 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004087 spin_unlock_irq(&conf->device_lock);
4088 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
NeilBrown16a53ec2006-06-26 00:27:38 -07004090 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004092
Neil Brown0e13fe232008-06-28 08:31:20 +10004093 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095}
4096
NeilBrownfd01b882011-10-11 16:47:53 +11004097static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004098
NeilBrownfd01b882011-10-11 16:47:53 +11004099static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{
NeilBrown52c03292006-06-26 00:27:43 -07004101 /* reshaping is quite different to recovery/resync so it is
4102 * handled quite separately ... here.
4103 *
4104 * On each call to sync_request, we gather one chunk worth of
4105 * destination stripes and flag them as expanding.
4106 * Then we find all the source stripes and request reads.
4107 * As the reads complete, handle_stripe will copy the data
4108 * into the destination stripe and release that stripe.
4109 */
NeilBrownd1688a62011-10-11 16:49:52 +11004110 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004112 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004113 int raid_disks = conf->previous_raid_disks;
4114 int data_disks = raid_disks - conf->max_degraded;
4115 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004116 int i;
4117 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004118 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004119 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004120 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004121 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004122
NeilBrownfef9c612009-03-31 15:16:46 +11004123 if (sector_nr == 0) {
4124 /* If restarting in the middle, skip the initial sectors */
4125 if (mddev->delta_disks < 0 &&
4126 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4127 sector_nr = raid5_size(mddev, 0, 0)
4128 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004129 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004130 conf->reshape_progress > 0)
4131 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004132 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004133 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004134 mddev->curr_resync_completed = sector_nr;
4135 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004136 *skipped = 1;
4137 return sector_nr;
4138 }
NeilBrown52c03292006-06-26 00:27:43 -07004139 }
4140
NeilBrown7a661382009-03-31 15:21:40 +11004141 /* We need to process a full chunk at a time.
4142 * If old and new chunk sizes differ, we need to process the
4143 * largest of these
4144 */
Andre Noll664e7c42009-06-18 08:45:27 +10004145 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4146 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004147 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004148 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004149
NeilBrown52c03292006-06-26 00:27:43 -07004150 /* we update the metadata when there is more than 3Meg
4151 * in the block range (that is rather arbitrary, should
4152 * probably be time based) or when the data about to be
4153 * copied would over-write the source of the data at
4154 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004155 * i.e. one new_stripe along from reshape_progress new_maps
4156 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004157 */
NeilBrownfef9c612009-03-31 15:16:46 +11004158 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004159 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004160 readpos = conf->reshape_progress;
4161 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004162 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004163 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004164 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004165 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004166 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004167 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004168 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004169 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004170 readpos -= min_t(sector_t, reshape_sectors, readpos);
4171 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004172 }
NeilBrown52c03292006-06-26 00:27:43 -07004173
NeilBrownc8f517c2009-03-31 15:28:40 +11004174 /* 'writepos' is the most advanced device address we might write.
4175 * 'readpos' is the least advanced device address we might read.
4176 * 'safepos' is the least address recorded in the metadata as having
4177 * been reshaped.
4178 * If 'readpos' is behind 'writepos', then there is no way that we can
4179 * ensure safety in the face of a crash - that must be done by userspace
4180 * making a backup of the data. So in that case there is no particular
4181 * rush to update metadata.
4182 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4183 * update the metadata to advance 'safepos' to match 'readpos' so that
4184 * we can be safe in the event of a crash.
4185 * So we insist on updating metadata if safepos is behind writepos and
4186 * readpos is beyond writepos.
4187 * In any case, update the metadata every 10 seconds.
4188 * Maybe that number should be configurable, but I'm not sure it is
4189 * worth it.... maybe it could be a multiple of safemode_delay???
4190 */
NeilBrownfef9c612009-03-31 15:16:46 +11004191 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004192 ? (safepos > writepos && readpos < writepos)
4193 : (safepos < writepos && readpos > writepos)) ||
4194 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004195 /* Cannot proceed until we've updated the superblock... */
4196 wait_event(conf->wait_for_overlap,
4197 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004198 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004199 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004200 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004201 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004202 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004203 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004204 kthread_should_stop());
4205 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004206 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004207 spin_unlock_irq(&conf->device_lock);
4208 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004209 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004210 }
4211
NeilBrownec32a2b2009-03-31 15:17:38 +11004212 if (mddev->delta_disks < 0) {
4213 BUG_ON(conf->reshape_progress == 0);
4214 stripe_addr = writepos;
4215 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004216 ~((sector_t)reshape_sectors - 1))
4217 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004218 != sector_nr);
4219 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004220 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004221 stripe_addr = sector_nr;
4222 }
NeilBrownab69ae12009-03-31 15:26:47 +11004223 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004224 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004225 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004226 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004227 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004228 set_bit(STRIPE_EXPANDING, &sh->state);
4229 atomic_inc(&conf->reshape_stripes);
4230 /* If any of this stripe is beyond the end of the old
4231 * array, then we need to zero those blocks
4232 */
4233 for (j=sh->disks; j--;) {
4234 sector_t s;
4235 if (j == sh->pd_idx)
4236 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004237 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004238 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004239 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004240 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004241 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004242 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004243 continue;
4244 }
4245 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4246 set_bit(R5_Expanded, &sh->dev[j].flags);
4247 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4248 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004249 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004250 set_bit(STRIPE_EXPAND_READY, &sh->state);
4251 set_bit(STRIPE_HANDLE, &sh->state);
4252 }
NeilBrownab69ae12009-03-31 15:26:47 +11004253 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004254 }
4255 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004256 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004257 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004258 else
NeilBrown7a661382009-03-31 15:21:40 +11004259 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004260 spin_unlock_irq(&conf->device_lock);
4261 /* Ok, those stripe are ready. We can start scheduling
4262 * reads on the source stripes.
4263 * The source stripes are determined by mapping the first and last
4264 * block on the destination stripes.
4265 */
NeilBrown52c03292006-06-26 00:27:43 -07004266 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004267 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004268 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004269 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004270 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004271 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004272 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004273 if (last_sector >= mddev->dev_sectors)
4274 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004275 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004276 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004277 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4278 set_bit(STRIPE_HANDLE, &sh->state);
4279 release_stripe(sh);
4280 first_sector += STRIPE_SECTORS;
4281 }
NeilBrownab69ae12009-03-31 15:26:47 +11004282 /* Now that the sources are clearly marked, we can release
4283 * the destination stripes
4284 */
4285 while (!list_empty(&stripes)) {
4286 sh = list_entry(stripes.next, struct stripe_head, lru);
4287 list_del_init(&sh->lru);
4288 release_stripe(sh);
4289 }
NeilBrownc6207272008-02-06 01:39:52 -08004290 /* If this takes us to the resync_max point where we have to pause,
4291 * then we need to write out the superblock.
4292 */
NeilBrown7a661382009-03-31 15:21:40 +11004293 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004294 if ((sector_nr - mddev->curr_resync_completed) * 2
4295 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004296 /* Cannot proceed until we've updated the superblock... */
4297 wait_event(conf->wait_for_overlap,
4298 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004299 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004300 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004301 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004302 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4303 md_wakeup_thread(mddev->thread);
4304 wait_event(mddev->sb_wait,
4305 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4306 || kthread_should_stop());
4307 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004308 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004309 spin_unlock_irq(&conf->device_lock);
4310 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004311 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004312 }
NeilBrown7a661382009-03-31 15:21:40 +11004313 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004314}
4315
4316/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004317static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004318{
NeilBrownd1688a62011-10-11 16:49:52 +11004319 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004320 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004321 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004322 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004323 int still_degraded = 0;
4324 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
NeilBrown72626682005-09-09 16:23:54 -07004326 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004328
NeilBrown29269552006-03-27 01:18:10 -08004329 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4330 end_reshape(conf);
4331 return 0;
4332 }
NeilBrown72626682005-09-09 16:23:54 -07004333
4334 if (mddev->curr_resync < max_sector) /* aborted */
4335 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4336 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004337 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004338 conf->fullsync = 0;
4339 bitmap_close_sync(mddev->bitmap);
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return 0;
4342 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004343
NeilBrown64bd6602009-08-03 10:59:58 +10004344 /* Allow raid5_quiesce to complete */
4345 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4346
NeilBrown52c03292006-06-26 00:27:43 -07004347 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4348 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004349
NeilBrownc6207272008-02-06 01:39:52 -08004350 /* No need to check resync_max as we never do more than one
4351 * stripe, and as resync_max will always be on a chunk boundary,
4352 * if the check in md_do_sync didn't fire, there is no chance
4353 * of overstepping resync_max here
4354 */
4355
NeilBrown16a53ec2006-06-26 00:27:38 -07004356 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 * to resync, then assert that we are finished, because there is
4358 * nothing we can do.
4359 */
NeilBrown3285edf2006-06-26 00:27:55 -07004360 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004361 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004362 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004363 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return rv;
4365 }
NeilBrown72626682005-09-09 16:23:54 -07004366 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004367 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004368 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4369 /* we can skip this block, and probably more */
4370 sync_blocks /= STRIPE_SECTORS;
4371 *skipped = 1;
4372 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
NeilBrownb47490c2008-02-06 01:39:50 -08004375 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4376
NeilBrowna8c906c2009-06-09 14:39:59 +10004377 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004379 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004381 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004383 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004385 /* Need to check if array will still be degraded after recovery/resync
4386 * We don't need to check the 'failed' flag as when that gets set,
4387 * recovery aborts.
4388 */
NeilBrownf001a702009-06-09 14:30:31 +10004389 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004390 if (conf->disks[i].rdev == NULL)
4391 still_degraded = 1;
4392
4393 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4394
NeilBrown83206d62011-07-26 11:19:49 +10004395 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
NeilBrown14425772009-10-16 15:55:25 +11004397 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 release_stripe(sh);
4399
4400 return STRIPE_SECTORS;
4401}
4402
NeilBrownd1688a62011-10-11 16:49:52 +11004403static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004404{
4405 /* We may not be able to submit a whole bio at once as there
4406 * may not be enough stripe_heads available.
4407 * We cannot pre-allocate enough stripe_heads as we may need
4408 * more than exist in the cache (if we allow ever large chunks).
4409 * So we do one stripe head at a time and record in
4410 * ->bi_hw_segments how many have been done.
4411 *
4412 * We *know* that this entire raid_bio is in one chunk, so
4413 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4414 */
4415 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004416 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004417 sector_t sector, logical_sector, last_sector;
4418 int scnt = 0;
4419 int remaining;
4420 int handled = 0;
4421
4422 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004423 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004424 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4426
4427 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004428 logical_sector += STRIPE_SECTORS,
4429 sector += STRIPE_SECTORS,
4430 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004431
Jens Axboe960e7392008-08-15 10:41:18 +02004432 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004433 /* already done this stripe */
4434 continue;
4435
NeilBrowna8c906c2009-06-09 14:39:59 +10004436 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004437
4438 if (!sh) {
4439 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004440 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004441 conf->retry_read_aligned = raid_bio;
4442 return handled;
4443 }
4444
Neil Brown387bb172007-02-08 14:20:29 -08004445 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4446 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004447 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004448 conf->retry_read_aligned = raid_bio;
4449 return handled;
4450 }
4451
Dan Williams36d1c642009-07-14 11:48:22 -07004452 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004453 release_stripe(sh);
4454 handled++;
4455 }
4456 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004457 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004458 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004459 if (remaining == 0)
4460 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004461 if (atomic_dec_and_test(&conf->active_aligned_reads))
4462 wake_up(&conf->wait_for_stripe);
4463 return handled;
4464}
4465
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467/*
4468 * This is our raid5 kernel thread.
4469 *
4470 * We scan the hash table for stripes which can be handled now.
4471 * During the scan, completed stripes are saved for us by the interrupt
4472 * handler, so that they will not have to wait for our next wakeup.
4473 */
NeilBrownfd01b882011-10-11 16:47:53 +11004474static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
4476 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004477 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004479 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Dan Williams45b42332007-07-09 11:56:43 -07004481 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004485 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 handled = 0;
4487 spin_lock_irq(&conf->device_lock);
4488 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004489 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
NeilBrown7c13edc2011-04-18 18:25:43 +10004491 if (atomic_read(&mddev->plug_cnt) == 0 &&
4492 !list_empty(&conf->bitmap_list)) {
4493 /* Now is a good time to flush some bitmap updates */
4494 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004495 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004496 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004497 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004498 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004499 activate_bit_delay(conf);
4500 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004501 if (atomic_read(&mddev->plug_cnt) == 0)
4502 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004503
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004504 while ((bio = remove_bio_from_retry(conf))) {
4505 int ok;
4506 spin_unlock_irq(&conf->device_lock);
4507 ok = retry_aligned_read(conf, bio);
4508 spin_lock_irq(&conf->device_lock);
4509 if (!ok)
4510 break;
4511 handled++;
4512 }
4513
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004514 sh = __get_priority_stripe(conf);
4515
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004516 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 spin_unlock_irq(&conf->device_lock);
4519
4520 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004521 handle_stripe(sh);
4522 release_stripe(sh);
4523 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
NeilBrownde393cd2011-07-28 11:31:48 +10004525 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4526 md_check_recovery(mddev);
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 spin_lock_irq(&conf->device_lock);
4529 }
Dan Williams45b42332007-07-09 11:56:43 -07004530 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
4532 spin_unlock_irq(&conf->device_lock);
4533
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004534 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004535 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Dan Williams45b42332007-07-09 11:56:43 -07004537 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538}
4539
NeilBrown3f294f42005-11-08 21:39:25 -08004540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004541raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004542{
NeilBrownd1688a62011-10-11 16:49:52 +11004543 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004544 if (conf)
4545 return sprintf(page, "%d\n", conf->max_nr_stripes);
4546 else
4547 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004548}
4549
NeilBrownc41d4ac2010-06-01 19:37:24 +10004550int
NeilBrownfd01b882011-10-11 16:47:53 +11004551raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004552{
NeilBrownd1688a62011-10-11 16:49:52 +11004553 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004554 int err;
4555
4556 if (size <= 16 || size > 32768)
4557 return -EINVAL;
4558 while (size < conf->max_nr_stripes) {
4559 if (drop_one_stripe(conf))
4560 conf->max_nr_stripes--;
4561 else
4562 break;
4563 }
4564 err = md_allow_write(mddev);
4565 if (err)
4566 return err;
4567 while (size > conf->max_nr_stripes) {
4568 if (grow_one_stripe(conf))
4569 conf->max_nr_stripes++;
4570 else break;
4571 }
4572 return 0;
4573}
4574EXPORT_SYMBOL(raid5_set_cache_size);
4575
NeilBrown3f294f42005-11-08 21:39:25 -08004576static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004577raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004578{
NeilBrownd1688a62011-10-11 16:49:52 +11004579 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004580 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004581 int err;
4582
NeilBrown3f294f42005-11-08 21:39:25 -08004583 if (len >= PAGE_SIZE)
4584 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004585 if (!conf)
4586 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004587
Dan Williams4ef197d82008-04-28 02:15:54 -07004588 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004589 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004590 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004591 if (err)
4592 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004593 return len;
4594}
NeilBrown007583c2005-11-08 21:39:30 -08004595
NeilBrown96de1e62005-11-08 21:39:39 -08004596static struct md_sysfs_entry
4597raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4598 raid5_show_stripe_cache_size,
4599 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004600
4601static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004602raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004603{
NeilBrownd1688a62011-10-11 16:49:52 +11004604 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004605 if (conf)
4606 return sprintf(page, "%d\n", conf->bypass_threshold);
4607 else
4608 return 0;
4609}
4610
4611static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004612raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004613{
NeilBrownd1688a62011-10-11 16:49:52 +11004614 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004615 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004616 if (len >= PAGE_SIZE)
4617 return -EINVAL;
4618 if (!conf)
4619 return -ENODEV;
4620
Dan Williams4ef197d82008-04-28 02:15:54 -07004621 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004622 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004623 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004624 return -EINVAL;
4625 conf->bypass_threshold = new;
4626 return len;
4627}
4628
4629static struct md_sysfs_entry
4630raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4631 S_IRUGO | S_IWUSR,
4632 raid5_show_preread_threshold,
4633 raid5_store_preread_threshold);
4634
4635static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004636stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004637{
NeilBrownd1688a62011-10-11 16:49:52 +11004638 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004639 if (conf)
4640 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4641 else
4642 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004643}
4644
NeilBrown96de1e62005-11-08 21:39:39 -08004645static struct md_sysfs_entry
4646raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004647
NeilBrown007583c2005-11-08 21:39:30 -08004648static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004649 &raid5_stripecache_size.attr,
4650 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004651 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004652 NULL,
4653};
NeilBrown007583c2005-11-08 21:39:30 -08004654static struct attribute_group raid5_attrs_group = {
4655 .name = NULL,
4656 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004657};
4658
Dan Williams80c3a6c2009-03-17 18:10:40 -07004659static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004660raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004661{
NeilBrownd1688a62011-10-11 16:49:52 +11004662 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004663
4664 if (!sectors)
4665 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004666 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004667 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004668 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004669
Andre Noll9d8f0362009-06-18 08:45:01 +10004670 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004671 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004672 return sectors * (raid_disks - conf->max_degraded);
4673}
4674
NeilBrownd1688a62011-10-11 16:49:52 +11004675static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004676{
4677 struct raid5_percpu *percpu;
4678 unsigned long cpu;
4679
4680 if (!conf->percpu)
4681 return;
4682
4683 get_online_cpus();
4684 for_each_possible_cpu(cpu) {
4685 percpu = per_cpu_ptr(conf->percpu, cpu);
4686 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004687 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004688 }
4689#ifdef CONFIG_HOTPLUG_CPU
4690 unregister_cpu_notifier(&conf->cpu_notify);
4691#endif
4692 put_online_cpus();
4693
4694 free_percpu(conf->percpu);
4695}
4696
NeilBrownd1688a62011-10-11 16:49:52 +11004697static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004698{
4699 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004700 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004701 kfree(conf->disks);
4702 kfree(conf->stripe_hashtbl);
4703 kfree(conf);
4704}
4705
Dan Williams36d1c642009-07-14 11:48:22 -07004706#ifdef CONFIG_HOTPLUG_CPU
4707static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4708 void *hcpu)
4709{
NeilBrownd1688a62011-10-11 16:49:52 +11004710 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004711 long cpu = (long)hcpu;
4712 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4713
4714 switch (action) {
4715 case CPU_UP_PREPARE:
4716 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004717 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004718 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004719 if (!percpu->scribble)
4720 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4721
4722 if (!percpu->scribble ||
4723 (conf->level == 6 && !percpu->spare_page)) {
4724 safe_put_page(percpu->spare_page);
4725 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004726 pr_err("%s: failed memory allocation for cpu%ld\n",
4727 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004728 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004729 }
4730 break;
4731 case CPU_DEAD:
4732 case CPU_DEAD_FROZEN:
4733 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004734 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004735 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004736 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004737 break;
4738 default:
4739 break;
4740 }
4741 return NOTIFY_OK;
4742}
4743#endif
4744
NeilBrownd1688a62011-10-11 16:49:52 +11004745static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004746{
4747 unsigned long cpu;
4748 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004749 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004750 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004751 int err;
4752
Dan Williams36d1c642009-07-14 11:48:22 -07004753 allcpus = alloc_percpu(struct raid5_percpu);
4754 if (!allcpus)
4755 return -ENOMEM;
4756 conf->percpu = allcpus;
4757
4758 get_online_cpus();
4759 err = 0;
4760 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004761 if (conf->level == 6) {
4762 spare_page = alloc_page(GFP_KERNEL);
4763 if (!spare_page) {
4764 err = -ENOMEM;
4765 break;
4766 }
4767 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4768 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004769 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004770 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004771 err = -ENOMEM;
4772 break;
4773 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004774 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004775 }
4776#ifdef CONFIG_HOTPLUG_CPU
4777 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4778 conf->cpu_notify.priority = 0;
4779 if (err == 0)
4780 err = register_cpu_notifier(&conf->cpu_notify);
4781#endif
4782 put_online_cpus();
4783
4784 return err;
4785}
4786
NeilBrownd1688a62011-10-11 16:49:52 +11004787static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
NeilBrownd1688a62011-10-11 16:49:52 +11004789 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004790 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004791 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
NeilBrown91adb562009-03-31 14:39:39 +11004794 if (mddev->new_level != 5
4795 && mddev->new_level != 4
4796 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004797 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004798 mdname(mddev), mddev->new_level);
4799 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 }
NeilBrown91adb562009-03-31 14:39:39 +11004801 if ((mddev->new_level == 5
4802 && !algorithm_valid_raid5(mddev->new_layout)) ||
4803 (mddev->new_level == 6
4804 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004805 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004806 mdname(mddev), mddev->new_layout);
4807 return ERR_PTR(-EIO);
4808 }
4809 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004810 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004811 mdname(mddev), mddev->raid_disks);
4812 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Andre Noll664e7c42009-06-18 08:45:27 +10004815 if (!mddev->new_chunk_sectors ||
4816 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4817 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004818 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4819 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004820 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004821 }
4822
NeilBrownd1688a62011-10-11 16:49:52 +11004823 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004824 if (conf == NULL)
4825 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004826 spin_lock_init(&conf->device_lock);
4827 init_waitqueue_head(&conf->wait_for_stripe);
4828 init_waitqueue_head(&conf->wait_for_overlap);
4829 INIT_LIST_HEAD(&conf->handle_list);
4830 INIT_LIST_HEAD(&conf->hold_list);
4831 INIT_LIST_HEAD(&conf->delayed_list);
4832 INIT_LIST_HEAD(&conf->bitmap_list);
4833 INIT_LIST_HEAD(&conf->inactive_list);
4834 atomic_set(&conf->active_stripes, 0);
4835 atomic_set(&conf->preread_active_stripes, 0);
4836 atomic_set(&conf->active_aligned_reads, 0);
4837 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004838 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004839
4840 conf->raid_disks = mddev->raid_disks;
4841 if (mddev->reshape_position == MaxSector)
4842 conf->previous_raid_disks = mddev->raid_disks;
4843 else
4844 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004845 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4846 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004847
NeilBrown5e5e3e72009-10-16 16:35:30 +11004848 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004849 GFP_KERNEL);
4850 if (!conf->disks)
4851 goto abort;
4852
4853 conf->mddev = mddev;
4854
4855 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4856 goto abort;
4857
Dan Williams36d1c642009-07-14 11:48:22 -07004858 conf->level = mddev->new_level;
4859 if (raid5_alloc_percpu(conf) != 0)
4860 goto abort;
4861
NeilBrown0c55e022010-05-03 14:09:02 +10004862 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004863
NeilBrowndafb20f2012-03-19 12:46:39 +11004864 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004865 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004866 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004867 || raid_disk < 0)
4868 continue;
4869 disk = conf->disks + raid_disk;
4870
NeilBrown17045f52011-12-23 10:17:53 +11004871 if (test_bit(Replacement, &rdev->flags)) {
4872 if (disk->replacement)
4873 goto abort;
4874 disk->replacement = rdev;
4875 } else {
4876 if (disk->rdev)
4877 goto abort;
4878 disk->rdev = rdev;
4879 }
NeilBrown91adb562009-03-31 14:39:39 +11004880
4881 if (test_bit(In_sync, &rdev->flags)) {
4882 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004883 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4884 " disk %d\n",
4885 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004886 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004887 /* Cannot rely on bitmap to complete recovery */
4888 conf->fullsync = 1;
4889 }
4890
Andre Noll09c9e5f2009-06-18 08:45:55 +10004891 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004892 conf->level = mddev->new_level;
4893 if (conf->level == 6)
4894 conf->max_degraded = 2;
4895 else
4896 conf->max_degraded = 1;
4897 conf->algorithm = mddev->new_layout;
4898 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004899 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004900 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004901 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004902 conf->prev_algo = mddev->layout;
4903 }
NeilBrown91adb562009-03-31 14:39:39 +11004904
4905 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004906 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004907 if (grow_stripes(conf, conf->max_nr_stripes)) {
4908 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004909 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4910 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004911 goto abort;
4912 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004913 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4914 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004915
NeilBrown0da3c612009-09-23 18:09:45 +10004916 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004917 if (!conf->thread) {
4918 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004919 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004920 mdname(mddev));
4921 goto abort;
4922 }
4923
4924 return conf;
4925
4926 abort:
4927 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004928 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004929 return ERR_PTR(-EIO);
4930 } else
4931 return ERR_PTR(-ENOMEM);
4932}
4933
NeilBrownc148ffd2009-11-13 17:47:00 +11004934
4935static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4936{
4937 switch (algo) {
4938 case ALGORITHM_PARITY_0:
4939 if (raid_disk < max_degraded)
4940 return 1;
4941 break;
4942 case ALGORITHM_PARITY_N:
4943 if (raid_disk >= raid_disks - max_degraded)
4944 return 1;
4945 break;
4946 case ALGORITHM_PARITY_0_6:
4947 if (raid_disk == 0 ||
4948 raid_disk == raid_disks - 1)
4949 return 1;
4950 break;
4951 case ALGORITHM_LEFT_ASYMMETRIC_6:
4952 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4953 case ALGORITHM_LEFT_SYMMETRIC_6:
4954 case ALGORITHM_RIGHT_SYMMETRIC_6:
4955 if (raid_disk == raid_disks - 1)
4956 return 1;
4957 }
4958 return 0;
4959}
4960
NeilBrownfd01b882011-10-11 16:47:53 +11004961static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004962{
NeilBrownd1688a62011-10-11 16:49:52 +11004963 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004964 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004965 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004966 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004967 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004968 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004969
Andre Noll8c6ac862009-06-18 08:48:06 +10004970 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004971 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004972 " -- starting background reconstruction\n",
4973 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004974 if (mddev->reshape_position != MaxSector) {
4975 /* Check that we can continue the reshape.
4976 * Currently only disks can change, it must
4977 * increase, and we must be past the point where
4978 * a stripe over-writes itself
4979 */
4980 sector_t here_new, here_old;
4981 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004982 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004983
NeilBrown88ce4932009-03-31 15:24:23 +11004984 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004985 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004986 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004987 mdname(mddev));
4988 return -EINVAL;
4989 }
NeilBrownf6705572006-03-27 01:18:11 -08004990 old_disks = mddev->raid_disks - mddev->delta_disks;
4991 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004992 * further up in new geometry must map after here in old
4993 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004994 */
4995 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004996 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004997 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004998 printk(KERN_ERR "md/raid:%s: reshape_position not "
4999 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005000 return -EINVAL;
5001 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005002 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005003 /* here_new is the stripe we will write to */
5004 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005005 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005006 (old_disks-max_degraded));
5007 /* here_old is the first stripe that we might need to read
5008 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005009 if (mddev->delta_disks == 0) {
5010 /* We cannot be sure it is safe to start an in-place
5011 * reshape. It is only safe if user-space if monitoring
5012 * and taking constant backups.
5013 * mdadm always starts a situation like this in
5014 * readonly mode so it can take control before
5015 * allowing any writes. So just check for that.
5016 */
5017 if ((here_new * mddev->new_chunk_sectors !=
5018 here_old * mddev->chunk_sectors) ||
5019 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005020 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5021 " in read-only mode - aborting\n",
5022 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005023 return -EINVAL;
5024 }
5025 } else if (mddev->delta_disks < 0
5026 ? (here_new * mddev->new_chunk_sectors <=
5027 here_old * mddev->chunk_sectors)
5028 : (here_new * mddev->new_chunk_sectors >=
5029 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005030 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005031 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5032 "auto-recovery - aborting.\n",
5033 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005034 return -EINVAL;
5035 }
NeilBrown0c55e022010-05-03 14:09:02 +10005036 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5037 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005038 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005039 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005040 BUG_ON(mddev->level != mddev->new_level);
5041 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005042 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005043 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005044 }
5045
NeilBrown245f46c2009-03-31 14:39:39 +11005046 if (mddev->private == NULL)
5047 conf = setup_conf(mddev);
5048 else
5049 conf = mddev->private;
5050
NeilBrown91adb562009-03-31 14:39:39 +11005051 if (IS_ERR(conf))
5052 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005053
NeilBrown91adb562009-03-31 14:39:39 +11005054 mddev->thread = conf->thread;
5055 conf->thread = NULL;
5056 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
NeilBrown17045f52011-12-23 10:17:53 +11005058 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5059 i++) {
5060 rdev = conf->disks[i].rdev;
5061 if (!rdev && conf->disks[i].replacement) {
5062 /* The replacement is all we have yet */
5063 rdev = conf->disks[i].replacement;
5064 conf->disks[i].replacement = NULL;
5065 clear_bit(Replacement, &rdev->flags);
5066 conf->disks[i].rdev = rdev;
5067 }
5068 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005069 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005070 if (conf->disks[i].replacement &&
5071 conf->reshape_progress != MaxSector) {
5072 /* replacements and reshape simply do not mix. */
5073 printk(KERN_ERR "md: cannot handle concurrent "
5074 "replacement and reshape.\n");
5075 goto abort;
5076 }
NeilBrown2f115882010-06-17 17:41:03 +10005077 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005078 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005079 continue;
5080 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005081 /* This disc is not fully in-sync. However if it
5082 * just stored parity (beyond the recovery_offset),
5083 * when we don't need to be concerned about the
5084 * array being dirty.
5085 * When reshape goes 'backwards', we never have
5086 * partially completed devices, so we only need
5087 * to worry about reshape going forwards.
5088 */
5089 /* Hack because v0.91 doesn't store recovery_offset properly. */
5090 if (mddev->major_version == 0 &&
5091 mddev->minor_version > 90)
5092 rdev->recovery_offset = reshape_offset;
5093
NeilBrownc148ffd2009-11-13 17:47:00 +11005094 if (rdev->recovery_offset < reshape_offset) {
5095 /* We need to check old and new layout */
5096 if (!only_parity(rdev->raid_disk,
5097 conf->algorithm,
5098 conf->raid_disks,
5099 conf->max_degraded))
5100 continue;
5101 }
5102 if (!only_parity(rdev->raid_disk,
5103 conf->prev_algo,
5104 conf->previous_raid_disks,
5105 conf->max_degraded))
5106 continue;
5107 dirty_parity_disks++;
5108 }
NeilBrown91adb562009-03-31 14:39:39 +11005109
NeilBrown17045f52011-12-23 10:17:53 +11005110 /*
5111 * 0 for a fully functional array, 1 or 2 for a degraded array.
5112 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005113 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
NeilBrown674806d2010-06-16 17:17:53 +10005115 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005116 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005118 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 goto abort;
5120 }
5121
NeilBrown91adb562009-03-31 14:39:39 +11005122 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005123 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005124 mddev->resync_max_sectors = mddev->dev_sectors;
5125
NeilBrownc148ffd2009-11-13 17:47:00 +11005126 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005128 if (mddev->ok_start_degraded)
5129 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005130 "md/raid:%s: starting dirty degraded array"
5131 " - data corruption possible.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005132 mdname(mddev));
5133 else {
5134 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005135 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005136 mdname(mddev));
5137 goto abort;
5138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005142 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5143 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005144 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5145 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 else
NeilBrown0c55e022010-05-03 14:09:02 +10005147 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5148 " out of %d devices, algorithm %d\n",
5149 mdname(mddev), conf->level,
5150 mddev->raid_disks - mddev->degraded,
5151 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
5153 print_raid5_conf(conf);
5154
NeilBrownfef9c612009-03-31 15:16:46 +11005155 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005156 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005157 atomic_set(&conf->reshape_stripes, 0);
5158 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5159 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5160 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5161 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5162 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005163 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005164 }
5165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
5167 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005168 if (mddev->to_remove == &raid5_attrs_group)
5169 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005170 else if (mddev->kobj.sd &&
5171 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005172 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005173 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005174 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005175 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5176
5177 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005178 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005179 /* read-ahead size must cover two whole stripes, which
5180 * is 2 * (datadisks) * chunksize where 'n' is the
5181 * number of raid devices
5182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5184 int stripe = data_disks *
5185 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5186 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5187 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005188
5189 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005190
5191 mddev->queue->backing_dev_info.congested_data = mddev;
5192 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005193
5194 chunk_size = mddev->chunk_sectors << 9;
5195 blk_queue_io_min(mddev->queue, chunk_size);
5196 blk_queue_io_opt(mddev->queue, chunk_size *
5197 (conf->raid_disks - conf->max_degraded));
5198
NeilBrowndafb20f2012-03-19 12:46:39 +11005199 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005200 disk_stack_limits(mddev->gendisk, rdev->bdev,
5201 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return 0;
5205abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005206 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005207 print_raid5_conf(conf);
5208 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005210 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return -EIO;
5212}
5213
NeilBrownfd01b882011-10-11 16:47:53 +11005214static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215{
NeilBrownd1688a62011-10-11 16:49:52 +11005216 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
NeilBrown01f96c02011-09-21 15:30:20 +10005218 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005219 if (mddev->queue)
5220 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005221 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005222 mddev->private = NULL;
5223 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 return 0;
5225}
5226
NeilBrownfd01b882011-10-11 16:47:53 +11005227static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
NeilBrownd1688a62011-10-11 16:49:52 +11005229 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 int i;
5231
Andre Noll9d8f0362009-06-18 08:45:01 +10005232 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5233 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005234 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 for (i = 0; i < conf->raid_disks; i++)
5236 seq_printf (seq, "%s",
5237 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005238 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
NeilBrownd1688a62011-10-11 16:49:52 +11005242static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
5244 int i;
5245 struct disk_info *tmp;
5246
NeilBrown0c55e022010-05-03 14:09:02 +10005247 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 if (!conf) {
5249 printk("(conf==NULL)\n");
5250 return;
5251 }
NeilBrown0c55e022010-05-03 14:09:02 +10005252 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5253 conf->raid_disks,
5254 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 for (i = 0; i < conf->raid_disks; i++) {
5257 char b[BDEVNAME_SIZE];
5258 tmp = conf->disks + i;
5259 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005260 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5261 i, !test_bit(Faulty, &tmp->rdev->flags),
5262 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264}
5265
NeilBrownfd01b882011-10-11 16:47:53 +11005266static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
5268 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005269 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005271 int count = 0;
5272 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 for (i = 0; i < conf->raid_disks; i++) {
5275 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005276 if (tmp->replacement
5277 && tmp->replacement->recovery_offset == MaxSector
5278 && !test_bit(Faulty, &tmp->replacement->flags)
5279 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5280 /* Replacement has just become active. */
5281 if (!tmp->rdev
5282 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5283 count++;
5284 if (tmp->rdev) {
5285 /* Replaced device not technically faulty,
5286 * but we need to be sure it gets removed
5287 * and never re-added.
5288 */
5289 set_bit(Faulty, &tmp->rdev->flags);
5290 sysfs_notify_dirent_safe(
5291 tmp->rdev->sysfs_state);
5292 }
5293 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5294 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005295 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005296 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005297 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005298 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005299 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 }
5301 }
NeilBrown6b965622010-08-18 11:56:59 +10005302 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005303 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005304 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005306 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307}
5308
NeilBrownb8321b62011-12-23 10:17:51 +11005309static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310{
NeilBrownd1688a62011-10-11 16:49:52 +11005311 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005313 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005314 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 struct disk_info *p = conf->disks + number;
5316
5317 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005318 if (rdev == p->rdev)
5319 rdevp = &p->rdev;
5320 else if (rdev == p->replacement)
5321 rdevp = &p->replacement;
5322 else
5323 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005324
NeilBrown657e3e42011-12-23 10:17:52 +11005325 if (number >= conf->raid_disks &&
5326 conf->reshape_progress == MaxSector)
5327 clear_bit(In_sync, &rdev->flags);
5328
5329 if (test_bit(In_sync, &rdev->flags) ||
5330 atomic_read(&rdev->nr_pending)) {
5331 err = -EBUSY;
5332 goto abort;
5333 }
5334 /* Only remove non-faulty devices if recovery
5335 * isn't possible.
5336 */
5337 if (!test_bit(Faulty, &rdev->flags) &&
5338 mddev->recovery_disabled != conf->recovery_disabled &&
5339 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005340 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005341 number < conf->raid_disks) {
5342 err = -EBUSY;
5343 goto abort;
5344 }
5345 *rdevp = NULL;
5346 synchronize_rcu();
5347 if (atomic_read(&rdev->nr_pending)) {
5348 /* lost the race, try later */
5349 err = -EBUSY;
5350 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005351 } else if (p->replacement) {
5352 /* We must have just cleared 'rdev' */
5353 p->rdev = p->replacement;
5354 clear_bit(Replacement, &p->replacement->flags);
5355 smp_mb(); /* Make sure other CPUs may see both as identical
5356 * but will never see neither - if they are careful
5357 */
5358 p->replacement = NULL;
5359 clear_bit(WantReplacement, &rdev->flags);
5360 } else
5361 /* We might have just removed the Replacement as faulty-
5362 * clear the bit just in case
5363 */
5364 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365abort:
5366
5367 print_raid5_conf(conf);
5368 return err;
5369}
5370
NeilBrownfd01b882011-10-11 16:47:53 +11005371static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372{
NeilBrownd1688a62011-10-11 16:49:52 +11005373 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005374 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 int disk;
5376 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005377 int first = 0;
5378 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
NeilBrown7f0da592011-07-28 11:39:22 +10005380 if (mddev->recovery_disabled == conf->recovery_disabled)
5381 return -EBUSY;
5382
NeilBrowndc10c642012-03-19 12:46:37 +11005383 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Neil Brown6c2fce22008-06-28 08:31:31 +10005387 if (rdev->raid_disk >= 0)
5388 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005391 * find the disk ... but prefer rdev->saved_raid_disk
5392 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005394 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005395 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005396 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5397 disk = rdev->saved_raid_disk;
5398 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005399 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005400 for ( ; disk <= last ; disk++) {
5401 p = conf->disks + disk;
5402 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005403 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005405 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005406 if (rdev->saved_raid_disk != disk)
5407 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005408 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 break;
5410 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005411 if (test_bit(WantReplacement, &p->rdev->flags) &&
5412 p->replacement == NULL) {
5413 clear_bit(In_sync, &rdev->flags);
5414 set_bit(Replacement, &rdev->flags);
5415 rdev->raid_disk = disk;
5416 err = 0;
5417 conf->fullsync = 1;
5418 rcu_assign_pointer(p->replacement, rdev);
5419 break;
5420 }
5421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424}
5425
NeilBrownfd01b882011-10-11 16:47:53 +11005426static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
5428 /* no resync is happening, and there is enough space
5429 * on all devices, so we can resize.
5430 * We need to make sure resync covers any new space.
5431 * If the array is shrinking we should possibly wait until
5432 * any io in the removed space completes, but it hardly seems
5433 * worth it.
5434 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005435 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005436 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5437 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005438 if (mddev->array_sectors >
5439 raid5_size(mddev, sectors, mddev->raid_disks))
5440 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005441 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005442 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005443 if (sectors > mddev->dev_sectors &&
5444 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005445 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5447 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005448 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005449 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 return 0;
5451}
5452
NeilBrownfd01b882011-10-11 16:47:53 +11005453static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005454{
5455 /* Can only proceed if there are plenty of stripe_heads.
5456 * We need a minimum of one full stripe,, and for sensible progress
5457 * it is best to have about 4 times that.
5458 * If we require 4 times, then the default 256 4K stripe_heads will
5459 * allow for chunk sizes up to 256K, which is probably OK.
5460 * If the chunk size is greater, user-space should request more
5461 * stripe_heads first.
5462 */
NeilBrownd1688a62011-10-11 16:49:52 +11005463 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005464 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5465 > conf->max_nr_stripes ||
5466 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5467 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005468 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5469 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005470 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5471 / STRIPE_SIZE)*4);
5472 return 0;
5473 }
5474 return 1;
5475}
5476
NeilBrownfd01b882011-10-11 16:47:53 +11005477static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005478{
NeilBrownd1688a62011-10-11 16:49:52 +11005479 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005480
NeilBrown88ce4932009-03-31 15:24:23 +11005481 if (mddev->delta_disks == 0 &&
5482 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005483 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005484 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005485 if (mddev->bitmap)
5486 /* Cannot grow a bitmap yet */
5487 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005488 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005489 return -EINVAL;
5490 if (mddev->delta_disks < 0) {
5491 /* We might be able to shrink, but the devices must
5492 * be made bigger first.
5493 * For raid6, 4 is the minimum size.
5494 * Otherwise 2 is the minimum
5495 */
5496 int min = 2;
5497 if (mddev->level == 6)
5498 min = 4;
5499 if (mddev->raid_disks + mddev->delta_disks < min)
5500 return -EINVAL;
5501 }
NeilBrown29269552006-03-27 01:18:10 -08005502
NeilBrown01ee22b2009-06-18 08:47:20 +10005503 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005504 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005505
NeilBrownec32a2b2009-03-31 15:17:38 +11005506 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005507}
5508
NeilBrownfd01b882011-10-11 16:47:53 +11005509static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005510{
NeilBrownd1688a62011-10-11 16:49:52 +11005511 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005512 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005513 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005514 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005515
NeilBrownf4168852007-02-28 20:11:53 -08005516 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005517 return -EBUSY;
5518
NeilBrown01ee22b2009-06-18 08:47:20 +10005519 if (!check_stripe_cache(mddev))
5520 return -ENOSPC;
5521
NeilBrowndafb20f2012-03-19 12:46:39 +11005522 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005523 if (!test_bit(In_sync, &rdev->flags)
5524 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005525 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005526
NeilBrownf4168852007-02-28 20:11:53 -08005527 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005528 /* Not enough devices even to make a degraded array
5529 * of that size
5530 */
5531 return -EINVAL;
5532
NeilBrownec32a2b2009-03-31 15:17:38 +11005533 /* Refuse to reduce size of the array. Any reductions in
5534 * array size must be through explicit setting of array_size
5535 * attribute.
5536 */
5537 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5538 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005539 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005540 "before number of disks\n", mdname(mddev));
5541 return -EINVAL;
5542 }
5543
NeilBrownf6705572006-03-27 01:18:11 -08005544 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005545 spin_lock_irq(&conf->device_lock);
5546 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005547 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005548 conf->prev_chunk_sectors = conf->chunk_sectors;
5549 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005550 conf->prev_algo = conf->algorithm;
5551 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005552 if (mddev->delta_disks < 0)
5553 conf->reshape_progress = raid5_size(mddev, 0, 0);
5554 else
5555 conf->reshape_progress = 0;
5556 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005557 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005558 spin_unlock_irq(&conf->device_lock);
5559
5560 /* Add some new drives, as many as will fit.
5561 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005562 * Don't add devices if we are reducing the number of
5563 * devices in the array. This is because it is not possible
5564 * to correctly record the "partially reconstructed" state of
5565 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005566 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005567 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005568 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005569 if (rdev->raid_disk < 0 &&
5570 !test_bit(Faulty, &rdev->flags)) {
5571 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005572 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005573 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005574 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005575 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005576 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005577
5578 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005579 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005580 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005581 } else if (rdev->raid_disk >= conf->previous_raid_disks
5582 && !test_bit(Faulty, &rdev->flags)) {
5583 /* This is a spare that was manually added */
5584 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005585 }
NeilBrown29269552006-03-27 01:18:10 -08005586
NeilBrown87a8dec2011-01-31 11:57:43 +11005587 /* When a reshape changes the number of devices,
5588 * ->degraded is measured against the larger of the
5589 * pre and post number of devices.
5590 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005591 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005592 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005593 spin_unlock_irqrestore(&conf->device_lock, flags);
5594 }
NeilBrown63c70c42006-03-27 01:18:13 -08005595 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005596 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005597 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005598
NeilBrown29269552006-03-27 01:18:10 -08005599 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5600 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5601 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5602 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5603 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005604 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005605 if (!mddev->sync_thread) {
5606 mddev->recovery = 0;
5607 spin_lock_irq(&conf->device_lock);
5608 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005609 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005610 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005611 spin_unlock_irq(&conf->device_lock);
5612 return -EAGAIN;
5613 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005614 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005615 md_wakeup_thread(mddev->sync_thread);
5616 md_new_event(mddev);
5617 return 0;
5618}
NeilBrown29269552006-03-27 01:18:10 -08005619
NeilBrownec32a2b2009-03-31 15:17:38 +11005620/* This is called from the reshape thread and should make any
5621 * changes needed in 'conf'
5622 */
NeilBrownd1688a62011-10-11 16:49:52 +11005623static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005624{
NeilBrown29269552006-03-27 01:18:10 -08005625
NeilBrownf6705572006-03-27 01:18:11 -08005626 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005627
NeilBrownf6705572006-03-27 01:18:11 -08005628 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005629 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005630 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005631 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005632 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005633
5634 /* read-ahead size must cover two whole stripes, which is
5635 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5636 */
NeilBrown4a5add42010-06-01 19:37:28 +10005637 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005638 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005639 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005640 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005641 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5642 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5643 }
NeilBrown29269552006-03-27 01:18:10 -08005644 }
NeilBrown29269552006-03-27 01:18:10 -08005645}
5646
NeilBrownec32a2b2009-03-31 15:17:38 +11005647/* This is called from the raid5d thread with mddev_lock held.
5648 * It makes config changes to the device.
5649 */
NeilBrownfd01b882011-10-11 16:47:53 +11005650static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005651{
NeilBrownd1688a62011-10-11 16:49:52 +11005652 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005653
5654 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5655
NeilBrownec32a2b2009-03-31 15:17:38 +11005656 if (mddev->delta_disks > 0) {
5657 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5658 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005659 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005660 } else {
5661 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005662 spin_lock_irq(&conf->device_lock);
5663 mddev->degraded = calc_degraded(conf);
5664 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005665 for (d = conf->raid_disks ;
5666 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005667 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005668 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005669 if (rdev &&
5670 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005671 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005672 rdev->raid_disk = -1;
5673 }
5674 }
NeilBrowncea9c222009-03-31 15:15:05 +11005675 }
NeilBrown88ce4932009-03-31 15:24:23 +11005676 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005677 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005678 mddev->reshape_position = MaxSector;
5679 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005680 }
5681}
5682
NeilBrownfd01b882011-10-11 16:47:53 +11005683static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005684{
NeilBrownd1688a62011-10-11 16:49:52 +11005685 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005686
5687 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005688 case 2: /* resume for a suspend */
5689 wake_up(&conf->wait_for_overlap);
5690 break;
5691
NeilBrown72626682005-09-09 16:23:54 -07005692 case 1: /* stop all writes */
5693 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005694 /* '2' tells resync/reshape to pause so that all
5695 * active stripes can drain
5696 */
5697 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005698 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005699 atomic_read(&conf->active_stripes) == 0 &&
5700 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005701 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005702 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005703 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005704 /* allow reshape to continue */
5705 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005706 break;
5707
5708 case 0: /* re-enable writes */
5709 spin_lock_irq(&conf->device_lock);
5710 conf->quiesce = 0;
5711 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005712 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005713 spin_unlock_irq(&conf->device_lock);
5714 break;
5715 }
NeilBrown72626682005-09-09 16:23:54 -07005716}
NeilBrownb15c2e52006-01-06 00:20:16 -08005717
NeilBrownd562b0c2009-03-31 14:39:39 +11005718
NeilBrownfd01b882011-10-11 16:47:53 +11005719static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005720{
NeilBrowne373ab12011-10-11 16:48:59 +11005721 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005722 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005723
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005724 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005725 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005726 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5727 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005728 return ERR_PTR(-EINVAL);
5729 }
5730
NeilBrowne373ab12011-10-11 16:48:59 +11005731 sectors = raid0_conf->strip_zone[0].zone_end;
5732 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005733 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005734 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005735 mddev->new_layout = ALGORITHM_PARITY_N;
5736 mddev->new_chunk_sectors = mddev->chunk_sectors;
5737 mddev->raid_disks += 1;
5738 mddev->delta_disks = 1;
5739 /* make sure it will be not marked as dirty */
5740 mddev->recovery_cp = MaxSector;
5741
5742 return setup_conf(mddev);
5743}
5744
5745
NeilBrownfd01b882011-10-11 16:47:53 +11005746static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005747{
5748 int chunksect;
5749
5750 if (mddev->raid_disks != 2 ||
5751 mddev->degraded > 1)
5752 return ERR_PTR(-EINVAL);
5753
5754 /* Should check if there are write-behind devices? */
5755
5756 chunksect = 64*2; /* 64K by default */
5757
5758 /* The array must be an exact multiple of chunksize */
5759 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5760 chunksect >>= 1;
5761
5762 if ((chunksect<<9) < STRIPE_SIZE)
5763 /* array size does not allow a suitable chunk size */
5764 return ERR_PTR(-EINVAL);
5765
5766 mddev->new_level = 5;
5767 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005768 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005769
5770 return setup_conf(mddev);
5771}
5772
NeilBrownfd01b882011-10-11 16:47:53 +11005773static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005774{
5775 int new_layout;
5776
5777 switch (mddev->layout) {
5778 case ALGORITHM_LEFT_ASYMMETRIC_6:
5779 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5780 break;
5781 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5782 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5783 break;
5784 case ALGORITHM_LEFT_SYMMETRIC_6:
5785 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5786 break;
5787 case ALGORITHM_RIGHT_SYMMETRIC_6:
5788 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5789 break;
5790 case ALGORITHM_PARITY_0_6:
5791 new_layout = ALGORITHM_PARITY_0;
5792 break;
5793 case ALGORITHM_PARITY_N:
5794 new_layout = ALGORITHM_PARITY_N;
5795 break;
5796 default:
5797 return ERR_PTR(-EINVAL);
5798 }
5799 mddev->new_level = 5;
5800 mddev->new_layout = new_layout;
5801 mddev->delta_disks = -1;
5802 mddev->raid_disks -= 1;
5803 return setup_conf(mddev);
5804}
5805
NeilBrownd562b0c2009-03-31 14:39:39 +11005806
NeilBrownfd01b882011-10-11 16:47:53 +11005807static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005808{
NeilBrown88ce4932009-03-31 15:24:23 +11005809 /* For a 2-drive array, the layout and chunk size can be changed
5810 * immediately as not restriping is needed.
5811 * For larger arrays we record the new value - after validation
5812 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005813 */
NeilBrownd1688a62011-10-11 16:49:52 +11005814 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005815 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005816
NeilBrown597a7112009-06-18 08:47:42 +10005817 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005818 return -EINVAL;
5819 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005820 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005821 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005822 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005823 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005824 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005825 /* not factor of array size */
5826 return -EINVAL;
5827 }
5828
5829 /* They look valid */
5830
NeilBrown88ce4932009-03-31 15:24:23 +11005831 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005832 /* can make the change immediately */
5833 if (mddev->new_layout >= 0) {
5834 conf->algorithm = mddev->new_layout;
5835 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005836 }
5837 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005838 conf->chunk_sectors = new_chunk ;
5839 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005840 }
5841 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5842 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005843 }
NeilBrown50ac1682009-06-18 08:47:55 +10005844 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005845}
5846
NeilBrownfd01b882011-10-11 16:47:53 +11005847static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005848{
NeilBrown597a7112009-06-18 08:47:42 +10005849 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005850
NeilBrown597a7112009-06-18 08:47:42 +10005851 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005852 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005853 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005854 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005855 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005856 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005857 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005858 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005859 /* not factor of array size */
5860 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005861 }
NeilBrown88ce4932009-03-31 15:24:23 +11005862
5863 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005864 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005865}
5866
NeilBrownfd01b882011-10-11 16:47:53 +11005867static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005868{
5869 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005870 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005871 * raid1 - if there are two drives. We need to know the chunk size
5872 * raid4 - trivial - just use a raid4 layout.
5873 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005874 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005875 if (mddev->level == 0)
5876 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005877 if (mddev->level == 1)
5878 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005879 if (mddev->level == 4) {
5880 mddev->new_layout = ALGORITHM_PARITY_N;
5881 mddev->new_level = 5;
5882 return setup_conf(mddev);
5883 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005884 if (mddev->level == 6)
5885 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005886
5887 return ERR_PTR(-EINVAL);
5888}
5889
NeilBrownfd01b882011-10-11 16:47:53 +11005890static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005891{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005892 /* raid4 can take over:
5893 * raid0 - if there is only one strip zone
5894 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005895 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005896 if (mddev->level == 0)
5897 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005898 if (mddev->level == 5 &&
5899 mddev->layout == ALGORITHM_PARITY_N) {
5900 mddev->new_layout = 0;
5901 mddev->new_level = 4;
5902 return setup_conf(mddev);
5903 }
5904 return ERR_PTR(-EINVAL);
5905}
NeilBrownd562b0c2009-03-31 14:39:39 +11005906
NeilBrown84fc4b52011-10-11 16:49:58 +11005907static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005908
NeilBrownfd01b882011-10-11 16:47:53 +11005909static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005910{
5911 /* Currently can only take over a raid5. We map the
5912 * personality to an equivalent raid6 personality
5913 * with the Q block at the end.
5914 */
5915 int new_layout;
5916
5917 if (mddev->pers != &raid5_personality)
5918 return ERR_PTR(-EINVAL);
5919 if (mddev->degraded > 1)
5920 return ERR_PTR(-EINVAL);
5921 if (mddev->raid_disks > 253)
5922 return ERR_PTR(-EINVAL);
5923 if (mddev->raid_disks < 3)
5924 return ERR_PTR(-EINVAL);
5925
5926 switch (mddev->layout) {
5927 case ALGORITHM_LEFT_ASYMMETRIC:
5928 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5929 break;
5930 case ALGORITHM_RIGHT_ASYMMETRIC:
5931 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5932 break;
5933 case ALGORITHM_LEFT_SYMMETRIC:
5934 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5935 break;
5936 case ALGORITHM_RIGHT_SYMMETRIC:
5937 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5938 break;
5939 case ALGORITHM_PARITY_0:
5940 new_layout = ALGORITHM_PARITY_0_6;
5941 break;
5942 case ALGORITHM_PARITY_N:
5943 new_layout = ALGORITHM_PARITY_N;
5944 break;
5945 default:
5946 return ERR_PTR(-EINVAL);
5947 }
5948 mddev->new_level = 6;
5949 mddev->new_layout = new_layout;
5950 mddev->delta_disks = 1;
5951 mddev->raid_disks += 1;
5952 return setup_conf(mddev);
5953}
5954
5955
NeilBrown84fc4b52011-10-11 16:49:58 +11005956static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005957{
5958 .name = "raid6",
5959 .level = 6,
5960 .owner = THIS_MODULE,
5961 .make_request = make_request,
5962 .run = run,
5963 .stop = stop,
5964 .status = status,
5965 .error_handler = error,
5966 .hot_add_disk = raid5_add_disk,
5967 .hot_remove_disk= raid5_remove_disk,
5968 .spare_active = raid5_spare_active,
5969 .sync_request = sync_request,
5970 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005971 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005972 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005973 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005974 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005975 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005976 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005977};
NeilBrown84fc4b52011-10-11 16:49:58 +11005978static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
5980 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005981 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 .owner = THIS_MODULE,
5983 .make_request = make_request,
5984 .run = run,
5985 .stop = stop,
5986 .status = status,
5987 .error_handler = error,
5988 .hot_add_disk = raid5_add_disk,
5989 .hot_remove_disk= raid5_remove_disk,
5990 .spare_active = raid5_spare_active,
5991 .sync_request = sync_request,
5992 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005993 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005994 .check_reshape = raid5_check_reshape,
5995 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005996 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005997 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005998 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999};
6000
NeilBrown84fc4b52011-10-11 16:49:58 +11006001static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002{
NeilBrown2604b702006-01-06 00:20:36 -08006003 .name = "raid4",
6004 .level = 4,
6005 .owner = THIS_MODULE,
6006 .make_request = make_request,
6007 .run = run,
6008 .stop = stop,
6009 .status = status,
6010 .error_handler = error,
6011 .hot_add_disk = raid5_add_disk,
6012 .hot_remove_disk= raid5_remove_disk,
6013 .spare_active = raid5_spare_active,
6014 .sync_request = sync_request,
6015 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006016 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006017 .check_reshape = raid5_check_reshape,
6018 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006019 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006020 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006021 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006022};
6023
6024static int __init raid5_init(void)
6025{
NeilBrown16a53ec2006-06-26 00:27:38 -07006026 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006027 register_md_personality(&raid5_personality);
6028 register_md_personality(&raid4_personality);
6029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
NeilBrown2604b702006-01-06 00:20:36 -08006032static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
NeilBrown16a53ec2006-06-26 00:27:38 -07006034 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006035 unregister_md_personality(&raid5_personality);
6036 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037}
6038
6039module_init(raid5_init);
6040module_exit(raid5_exit);
6041MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006042MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006044MODULE_ALIAS("md-raid5");
6045MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006046MODULE_ALIAS("md-level-5");
6047MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006048MODULE_ALIAS("md-personality-8"); /* RAID6 */
6049MODULE_ALIAS("md-raid6");
6050MODULE_ALIAS("md-level-6");
6051
6052/* This used to be two separate modules, they were: */
6053MODULE_ALIAS("raid5");
6054MODULE_ALIAS("raid6");