blob: c276ad09ace9ebd42e19bdcbca03ae44cf3781f9 [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) {
NeilBrown18c80532014-01-16 09:35:38 +11001808 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11001809 set_bit(WriteErrorSeen, &rdev->flags);
1810 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001811 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1812 set_bit(MD_RECOVERY_NEEDED,
1813 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001814 } else if (is_badblock(rdev, sh->sector,
1815 STRIPE_SECTORS,
1816 &first_bad, &bad_sectors))
1817 set_bit(R5_MadeGood, &sh->dev[i].flags);
1818 }
1819 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
NeilBrown977df362011-12-23 10:17:53 +11001821 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1822 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001824 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
NeilBrown784052e2009-03-31 15:19:07 +11001827static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
NeilBrown784052e2009-03-31 15:19:07 +11001829static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct r5dev *dev = &sh->dev[i];
1832
1833 bio_init(&dev->req);
1834 dev->req.bi_io_vec = &dev->vec;
1835 dev->req.bi_vcnt++;
1836 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001838 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
NeilBrown977df362011-12-23 10:17:53 +11001840 bio_init(&dev->rreq);
1841 dev->rreq.bi_io_vec = &dev->rvec;
1842 dev->rreq.bi_vcnt++;
1843 dev->rreq.bi_max_vecs++;
1844 dev->rreq.bi_private = sh;
1845 dev->rvec.bv_page = dev->page;
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001848 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
NeilBrownfd01b882011-10-11 16:47:53 +11001851static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001854 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001855 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001856 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
NeilBrown908f4fb2011-12-23 10:17:50 +11001858 spin_lock_irqsave(&conf->device_lock, flags);
1859 clear_bit(In_sync, &rdev->flags);
1860 mddev->degraded = calc_degraded(conf);
1861 spin_unlock_irqrestore(&conf->device_lock, flags);
1862 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1863
NeilBrownde393cd2011-07-28 11:31:48 +10001864 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001865 set_bit(Faulty, &rdev->flags);
1866 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1867 printk(KERN_ALERT
1868 "md/raid:%s: Disk failure on %s, disabling device.\n"
1869 "md/raid:%s: Operation continuing on %d devices.\n",
1870 mdname(mddev),
1871 bdevname(rdev->bdev, b),
1872 mdname(mddev),
1873 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001874}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876/*
1877 * Input: a 'big' sector number,
1878 * Output: index of the data and parity disk, and the sector # in them.
1879 */
NeilBrownd1688a62011-10-11 16:49:52 +11001880static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001881 int previous, int *dd_idx,
1882 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001884 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001885 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001887 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001888 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001890 int algorithm = previous ? conf->prev_algo
1891 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001892 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1893 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001894 int raid_disks = previous ? conf->previous_raid_disks
1895 : conf->raid_disks;
1896 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* First compute the information on this sector */
1899
1900 /*
1901 * Compute the chunk number and the sector offset inside the chunk
1902 */
1903 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1904 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /*
1907 * Compute the stripe number
1908 */
NeilBrown35f2a592010-04-20 14:13:34 +10001909 stripe = chunk_number;
1910 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001911 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /*
1913 * Select the parity disk based on the user selected algorithm.
1914 */
NeilBrown84789552011-07-27 11:00:36 +10001915 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001916 switch(conf->level) {
1917 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001918 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001919 break;
1920 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001921 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001923 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001924 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 (*dd_idx)++;
1926 break;
1927 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001928 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001929 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 (*dd_idx)++;
1931 break;
1932 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001933 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001934 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
1936 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001937 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001938 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001940 case ALGORITHM_PARITY_0:
1941 pd_idx = 0;
1942 (*dd_idx)++;
1943 break;
1944 case ALGORITHM_PARITY_N:
1945 pd_idx = data_disks;
1946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001948 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001949 }
1950 break;
1951 case 6:
1952
NeilBrowne183eae2009-03-31 15:20:22 +11001953 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001954 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001955 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001956 qd_idx = pd_idx + 1;
1957 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001958 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001959 qd_idx = 0;
1960 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001961 (*dd_idx) += 2; /* D D P Q D */
1962 break;
1963 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001964 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001965 qd_idx = pd_idx + 1;
1966 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001967 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001968 qd_idx = 0;
1969 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001970 (*dd_idx) += 2; /* D D P Q D */
1971 break;
1972 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001973 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001974 qd_idx = (pd_idx + 1) % raid_disks;
1975 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001976 break;
1977 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001978 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001979 qd_idx = (pd_idx + 1) % raid_disks;
1980 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001981 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001982
1983 case ALGORITHM_PARITY_0:
1984 pd_idx = 0;
1985 qd_idx = 1;
1986 (*dd_idx) += 2;
1987 break;
1988 case ALGORITHM_PARITY_N:
1989 pd_idx = data_disks;
1990 qd_idx = data_disks + 1;
1991 break;
1992
1993 case ALGORITHM_ROTATING_ZERO_RESTART:
1994 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1995 * of blocks for computing Q is different.
1996 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001997 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001998 qd_idx = pd_idx + 1;
1999 if (pd_idx == raid_disks-1) {
2000 (*dd_idx)++; /* Q D D D P */
2001 qd_idx = 0;
2002 } else if (*dd_idx >= pd_idx)
2003 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002004 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002005 break;
2006
2007 case ALGORITHM_ROTATING_N_RESTART:
2008 /* Same a left_asymmetric, by first stripe is
2009 * D D D P Q rather than
2010 * Q D D D P
2011 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 stripe2 += 1;
2013 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002014 qd_idx = pd_idx + 1;
2015 if (pd_idx == raid_disks-1) {
2016 (*dd_idx)++; /* Q D D D P */
2017 qd_idx = 0;
2018 } else if (*dd_idx >= pd_idx)
2019 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002020 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002021 break;
2022
2023 case ALGORITHM_ROTATING_N_CONTINUE:
2024 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002025 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002026 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2027 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002028 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 break;
2030
2031 case ALGORITHM_LEFT_ASYMMETRIC_6:
2032 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002033 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002034 if (*dd_idx >= pd_idx)
2035 (*dd_idx)++;
2036 qd_idx = raid_disks - 1;
2037 break;
2038
2039 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002041 if (*dd_idx >= pd_idx)
2042 (*dd_idx)++;
2043 qd_idx = raid_disks - 1;
2044 break;
2045
2046 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002047 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002048 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2049 qd_idx = raid_disks - 1;
2050 break;
2051
2052 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002053 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002054 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2055 qd_idx = raid_disks - 1;
2056 break;
2057
2058 case ALGORITHM_PARITY_0_6:
2059 pd_idx = 0;
2060 (*dd_idx)++;
2061 qd_idx = raid_disks - 1;
2062 break;
2063
NeilBrown16a53ec2006-06-26 00:27:38 -07002064 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002065 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002066 }
2067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069
NeilBrown911d4ee2009-03-31 14:39:38 +11002070 if (sh) {
2071 sh->pd_idx = pd_idx;
2072 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002073 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Finally, compute the new sector number
2077 */
2078 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2079 return new_sector;
2080}
2081
2082
NeilBrown784052e2009-03-31 15:19:07 +11002083static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
NeilBrownd1688a62011-10-11 16:49:52 +11002085 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002086 int raid_disks = sh->disks;
2087 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002089 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2090 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002091 int algorithm = previous ? conf->prev_algo
2092 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 sector_t stripe;
2094 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002095 sector_t chunk_number;
2096 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002098 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
NeilBrown16a53ec2006-06-26 00:27:38 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2102 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
NeilBrown16a53ec2006-06-26 00:27:38 -07002104 if (i == sh->pd_idx)
2105 return 0;
2106 switch(conf->level) {
2107 case 4: break;
2108 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002109 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 case ALGORITHM_LEFT_ASYMMETRIC:
2111 case ALGORITHM_RIGHT_ASYMMETRIC:
2112 if (i > sh->pd_idx)
2113 i--;
2114 break;
2115 case ALGORITHM_LEFT_SYMMETRIC:
2116 case ALGORITHM_RIGHT_SYMMETRIC:
2117 if (i < sh->pd_idx)
2118 i += raid_disks;
2119 i -= (sh->pd_idx + 1);
2120 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 case ALGORITHM_PARITY_0:
2122 i -= 1;
2123 break;
2124 case ALGORITHM_PARITY_N:
2125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002127 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002128 }
2129 break;
2130 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002131 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002132 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002133 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002134 case ALGORITHM_LEFT_ASYMMETRIC:
2135 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002136 case ALGORITHM_ROTATING_ZERO_RESTART:
2137 case ALGORITHM_ROTATING_N_RESTART:
2138 if (sh->pd_idx == raid_disks-1)
2139 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002140 else if (i > sh->pd_idx)
2141 i -= 2; /* D D P Q D */
2142 break;
2143 case ALGORITHM_LEFT_SYMMETRIC:
2144 case ALGORITHM_RIGHT_SYMMETRIC:
2145 if (sh->pd_idx == raid_disks-1)
2146 i--; /* Q D D D P */
2147 else {
2148 /* D D P Q D */
2149 if (i < sh->pd_idx)
2150 i += raid_disks;
2151 i -= (sh->pd_idx + 2);
2152 }
2153 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002154 case ALGORITHM_PARITY_0:
2155 i -= 2;
2156 break;
2157 case ALGORITHM_PARITY_N:
2158 break;
2159 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002160 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002161 if (sh->pd_idx == 0)
2162 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002163 else {
2164 /* D D Q P D */
2165 if (i < sh->pd_idx)
2166 i += raid_disks;
2167 i -= (sh->pd_idx + 1);
2168 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002169 break;
2170 case ALGORITHM_LEFT_ASYMMETRIC_6:
2171 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2172 if (i > sh->pd_idx)
2173 i--;
2174 break;
2175 case ALGORITHM_LEFT_SYMMETRIC_6:
2176 case ALGORITHM_RIGHT_SYMMETRIC_6:
2177 if (i < sh->pd_idx)
2178 i += data_disks + 1;
2179 i -= (sh->pd_idx + 1);
2180 break;
2181 case ALGORITHM_PARITY_0_6:
2182 i -= 1;
2183 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002184 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002185 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002186 }
2187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189
2190 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002191 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
NeilBrown112bf892009-03-31 14:39:38 +11002193 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002194 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002195 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2196 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002197 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2198 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return 0;
2200 }
2201 return r_sector;
2202}
2203
2204
Dan Williams600aa102008-06-28 08:32:05 +10002205static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002206schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002207 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002208{
2209 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002210 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002211 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002212
Dan Williamse33129d2007-01-02 13:52:30 -07002213 if (rcw) {
2214 /* if we are not expanding this is a proper write request, and
2215 * there will be bios with new data to be drained into the
2216 * stripe cache
2217 */
2218 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002219 sh->reconstruct_state = reconstruct_state_drain_run;
2220 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2221 } else
2222 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002223
Dan Williamsac6b53b2009-07-14 13:40:19 -07002224 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002225
2226 for (i = disks; i--; ) {
2227 struct r5dev *dev = &sh->dev[i];
2228
2229 if (dev->towrite) {
2230 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002231 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002232 if (!expand)
2233 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002234 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002235 }
2236 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002237 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002238 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002239 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002240 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002241 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002242 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2243 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2244
Dan Williamsd8ee0722008-06-28 08:32:06 +10002245 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002246 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2247 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002248 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002249
2250 for (i = disks; i--; ) {
2251 struct r5dev *dev = &sh->dev[i];
2252 if (i == pd_idx)
2253 continue;
2254
Dan Williamse33129d2007-01-02 13:52:30 -07002255 if (dev->towrite &&
2256 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002257 test_bit(R5_Wantcompute, &dev->flags))) {
2258 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002259 set_bit(R5_LOCKED, &dev->flags);
2260 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002261 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002262 }
2263 }
2264 }
2265
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002266 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002267 * are in flight
2268 */
2269 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2270 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002271 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002272
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002273 if (level == 6) {
2274 int qd_idx = sh->qd_idx;
2275 struct r5dev *dev = &sh->dev[qd_idx];
2276
2277 set_bit(R5_LOCKED, &dev->flags);
2278 clear_bit(R5_UPTODATE, &dev->flags);
2279 s->locked++;
2280 }
2281
Dan Williams600aa102008-06-28 08:32:05 +10002282 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002283 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002284 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002285}
NeilBrown16a53ec2006-06-26 00:27:38 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287/*
2288 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002289 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * The bi_next chain must be in order.
2291 */
2292static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2293{
2294 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002295 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002296 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
NeilBrowncbe47ec2011-07-26 11:20:35 +10002298 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 (unsigned long long)bi->bi_sector,
2300 (unsigned long long)sh->sector);
2301
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002304 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002306 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2307 firstwrite = 1;
2308 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 bip = &sh->dev[dd_idx].toread;
2310 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2311 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2312 goto overlap;
2313 bip = & (*bip)->bi_next;
2314 }
2315 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2316 goto overlap;
2317
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002318 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (*bip)
2320 bi->bi_next = *bip;
2321 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002322 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (forwrite) {
2325 /* check if page is covered */
2326 sector_t sector = sh->dev[dd_idx].sector;
2327 for (bi=sh->dev[dd_idx].towrite;
2328 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2329 bi && bi->bi_sector <= sector;
2330 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2331 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2332 sector = bi->bi_sector + (bi->bi_size>>9);
2333 }
2334 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2335 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2336 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002337 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002338
2339 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2340 (unsigned long long)(*bip)->bi_sector,
2341 (unsigned long long)sh->sector, dd_idx);
2342
2343 if (conf->mddev->bitmap && firstwrite) {
2344 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2345 STRIPE_SECTORS, 0);
2346 sh->bm_seq = conf->seq_flush+1;
2347 set_bit(STRIPE_BIT_DELAY, &sh->state);
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 1;
2350
2351 overlap:
2352 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2353 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return 0;
2355}
2356
NeilBrownd1688a62011-10-11 16:49:52 +11002357static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002358
NeilBrownd1688a62011-10-11 16:49:52 +11002359static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002360 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002361{
NeilBrown784052e2009-03-31 15:19:07 +11002362 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002363 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002364 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002365 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002366 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002367
NeilBrown112bf892009-03-31 14:39:38 +11002368 raid5_compute_sector(conf,
2369 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002370 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002371 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002372 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002373}
2374
Dan Williamsa4456852007-07-09 11:56:43 -07002375static void
NeilBrownd1688a62011-10-11 16:49:52 +11002376handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002377 struct stripe_head_state *s, int disks,
2378 struct bio **return_bi)
2379{
2380 int i;
2381 for (i = disks; i--; ) {
2382 struct bio *bi;
2383 int bitmap_end = 0;
2384
2385 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002386 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002387 rcu_read_lock();
2388 rdev = rcu_dereference(conf->disks[i].rdev);
2389 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002390 atomic_inc(&rdev->nr_pending);
2391 else
2392 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002393 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002394 if (rdev) {
2395 if (!rdev_set_badblocks(
2396 rdev,
2397 sh->sector,
2398 STRIPE_SECTORS, 0))
2399 md_error(conf->mddev, rdev);
2400 rdev_dec_pending(rdev, conf->mddev);
2401 }
Dan Williamsa4456852007-07-09 11:56:43 -07002402 }
2403 spin_lock_irq(&conf->device_lock);
2404 /* fail all writes first */
2405 bi = sh->dev[i].towrite;
2406 sh->dev[i].towrite = NULL;
2407 if (bi) {
2408 s->to_write--;
2409 bitmap_end = 1;
2410 }
2411
2412 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2413 wake_up(&conf->wait_for_overlap);
2414
2415 while (bi && bi->bi_sector <
2416 sh->dev[i].sector + STRIPE_SECTORS) {
2417 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2418 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002419 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002420 md_write_end(conf->mddev);
2421 bi->bi_next = *return_bi;
2422 *return_bi = bi;
2423 }
2424 bi = nextbi;
2425 }
2426 /* and fail all 'written' */
2427 bi = sh->dev[i].written;
2428 sh->dev[i].written = NULL;
2429 if (bi) bitmap_end = 1;
2430 while (bi && bi->bi_sector <
2431 sh->dev[i].sector + STRIPE_SECTORS) {
2432 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2433 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002434 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002435 md_write_end(conf->mddev);
2436 bi->bi_next = *return_bi;
2437 *return_bi = bi;
2438 }
2439 bi = bi2;
2440 }
2441
Dan Williamsb5e98d62007-01-02 13:52:31 -07002442 /* fail any reads if this device is non-operational and
2443 * the data has not reached the cache yet.
2444 */
2445 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2446 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2447 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002448 bi = sh->dev[i].toread;
2449 sh->dev[i].toread = NULL;
2450 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2451 wake_up(&conf->wait_for_overlap);
2452 if (bi) s->to_read--;
2453 while (bi && bi->bi_sector <
2454 sh->dev[i].sector + STRIPE_SECTORS) {
2455 struct bio *nextbi =
2456 r5_next_bio(bi, sh->dev[i].sector);
2457 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002458 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002459 bi->bi_next = *return_bi;
2460 *return_bi = bi;
2461 }
2462 bi = nextbi;
2463 }
2464 }
2465 spin_unlock_irq(&conf->device_lock);
2466 if (bitmap_end)
2467 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2468 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002469 /* If we were in the middle of a write the parity block might
2470 * still be locked - so just clear all R5_LOCKED flags
2471 */
2472 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002473 }
2474
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002475 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2476 if (atomic_dec_and_test(&conf->pending_full_writes))
2477 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002478}
2479
NeilBrown7f0da592011-07-28 11:39:22 +10002480static void
NeilBrownd1688a62011-10-11 16:49:52 +11002481handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002482 struct stripe_head_state *s)
2483{
2484 int abort = 0;
2485 int i;
2486
NeilBrown7f0da592011-07-28 11:39:22 +10002487 clear_bit(STRIPE_SYNCING, &sh->state);
2488 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002489 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002490 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002491 * Don't even need to abort as that is handled elsewhere
2492 * if needed, and not always wanted e.g. if there is a known
2493 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002494 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002495 * non-sync devices, or abort the recovery
2496 */
NeilBrown18b98372012-04-01 23:48:38 +10002497 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2498 /* During recovery devices cannot be removed, so
2499 * locking and refcounting of rdevs is not needed
2500 */
2501 for (i = 0; i < conf->raid_disks; i++) {
2502 struct md_rdev *rdev = conf->disks[i].rdev;
2503 if (rdev
2504 && !test_bit(Faulty, &rdev->flags)
2505 && !test_bit(In_sync, &rdev->flags)
2506 && !rdev_set_badblocks(rdev, sh->sector,
2507 STRIPE_SECTORS, 0))
2508 abort = 1;
2509 rdev = conf->disks[i].replacement;
2510 if (rdev
2511 && !test_bit(Faulty, &rdev->flags)
2512 && !test_bit(In_sync, &rdev->flags)
2513 && !rdev_set_badblocks(rdev, sh->sector,
2514 STRIPE_SECTORS, 0))
2515 abort = 1;
2516 }
2517 if (abort)
2518 conf->recovery_disabled =
2519 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002520 }
NeilBrown18b98372012-04-01 23:48:38 +10002521 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002522}
2523
NeilBrown9a3e1102011-12-23 10:17:53 +11002524static int want_replace(struct stripe_head *sh, int disk_idx)
2525{
2526 struct md_rdev *rdev;
2527 int rv = 0;
2528 /* Doing recovery so rcu locking not required */
2529 rdev = sh->raid_conf->disks[disk_idx].replacement;
2530 if (rdev
2531 && !test_bit(Faulty, &rdev->flags)
2532 && !test_bit(In_sync, &rdev->flags)
2533 && (rdev->recovery_offset <= sh->sector
2534 || rdev->mddev->recovery_cp <= sh->sector))
2535 rv = 1;
2536
2537 return rv;
2538}
2539
NeilBrown93b3dbc2011-07-27 11:00:36 +10002540/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002541 * to be read or computed to satisfy a request.
2542 *
2543 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002544 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002545 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002546static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2547 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002548{
2549 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002550 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2551 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002552
Dan Williamsf38e1212007-01-02 13:52:30 -07002553 /* is the data in this block needed, and can we get it? */
2554 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002555 !test_bit(R5_UPTODATE, &dev->flags) &&
2556 (dev->toread ||
2557 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2558 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002559 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002560 (s->failed >= 1 && fdev[0]->toread) ||
2561 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002562 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2563 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2564 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002565 /* we would like to get this block, possibly by computing it,
2566 * otherwise read it if the backing disk is insync
2567 */
2568 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2569 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2570 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002571 (s->failed && (disk_idx == s->failed_num[0] ||
2572 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002573 /* have disk failed, and we're requested to fetch it;
2574 * do compute it
2575 */
2576 pr_debug("Computing stripe %llu block %d\n",
2577 (unsigned long long)sh->sector, disk_idx);
2578 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2579 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2580 set_bit(R5_Wantcompute, &dev->flags);
2581 sh->ops.target = disk_idx;
2582 sh->ops.target2 = -1; /* no 2nd target */
2583 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002584 /* Careful: from this point on 'uptodate' is in the eye
2585 * of raid_run_ops which services 'compute' operations
2586 * before writes. R5_Wantcompute flags a block that will
2587 * be R5_UPTODATE by the time it is needed for a
2588 * subsequent operation.
2589 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002590 s->uptodate++;
2591 return 1;
2592 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2593 /* Computing 2-failure is *very* expensive; only
2594 * do it if failed >= 2
2595 */
2596 int other;
2597 for (other = disks; other--; ) {
2598 if (other == disk_idx)
2599 continue;
2600 if (!test_bit(R5_UPTODATE,
2601 &sh->dev[other].flags))
2602 break;
2603 }
2604 BUG_ON(other < 0);
2605 pr_debug("Computing stripe %llu blocks %d,%d\n",
2606 (unsigned long long)sh->sector,
2607 disk_idx, other);
2608 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2609 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2610 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2611 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2612 sh->ops.target = disk_idx;
2613 sh->ops.target2 = other;
2614 s->uptodate += 2;
2615 s->req_compute = 1;
2616 return 1;
2617 } else if (test_bit(R5_Insync, &dev->flags)) {
2618 set_bit(R5_LOCKED, &dev->flags);
2619 set_bit(R5_Wantread, &dev->flags);
2620 s->locked++;
2621 pr_debug("Reading block %d (sync=%d)\n",
2622 disk_idx, s->syncing);
2623 }
2624 }
2625
2626 return 0;
2627}
2628
2629/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002630 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002631 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002632static void handle_stripe_fill(struct stripe_head *sh,
2633 struct stripe_head_state *s,
2634 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002635{
2636 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002637
2638 /* look for blocks to read/compute, skip this if a compute
2639 * is already in flight, or if the stripe contents are in the
2640 * midst of changing due to a write
2641 */
2642 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2643 !sh->reconstruct_state)
2644 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002645 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002646 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002647 set_bit(STRIPE_HANDLE, &sh->state);
2648}
2649
2650
Dan Williams1fe797e2008-06-28 09:16:30 +10002651/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002652 * any written block on an uptodate or failed drive can be returned.
2653 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2654 * never LOCKED, so we don't need to test 'failed' directly.
2655 */
NeilBrownd1688a62011-10-11 16:49:52 +11002656static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002657 struct stripe_head *sh, int disks, struct bio **return_bi)
2658{
2659 int i;
2660 struct r5dev *dev;
2661
2662 for (i = disks; i--; )
2663 if (sh->dev[i].written) {
2664 dev = &sh->dev[i];
2665 if (!test_bit(R5_LOCKED, &dev->flags) &&
2666 test_bit(R5_UPTODATE, &dev->flags)) {
2667 /* We can return any write requests */
2668 struct bio *wbi, *wbi2;
2669 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002670 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002671 spin_lock_irq(&conf->device_lock);
2672 wbi = dev->written;
2673 dev->written = NULL;
2674 while (wbi && wbi->bi_sector <
2675 dev->sector + STRIPE_SECTORS) {
2676 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002677 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002678 md_write_end(conf->mddev);
2679 wbi->bi_next = *return_bi;
2680 *return_bi = wbi;
2681 }
2682 wbi = wbi2;
2683 }
2684 if (dev->towrite == NULL)
2685 bitmap_end = 1;
2686 spin_unlock_irq(&conf->device_lock);
2687 if (bitmap_end)
2688 bitmap_endwrite(conf->mddev->bitmap,
2689 sh->sector,
2690 STRIPE_SECTORS,
2691 !test_bit(STRIPE_DEGRADED, &sh->state),
2692 0);
2693 }
2694 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002695
2696 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2697 if (atomic_dec_and_test(&conf->pending_full_writes))
2698 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002699}
2700
NeilBrownd1688a62011-10-11 16:49:52 +11002701static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002702 struct stripe_head *sh,
2703 struct stripe_head_state *s,
2704 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002705{
2706 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002707 if (conf->max_degraded == 2) {
2708 /* RAID6 requires 'rcw' in current implementation
2709 * Calculate the real rcw later - for now fake it
2710 * look like rcw is cheaper
2711 */
2712 rcw = 1; rmw = 2;
2713 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002714 /* would I have to read this buffer for read_modify_write */
2715 struct r5dev *dev = &sh->dev[i];
2716 if ((dev->towrite || i == sh->pd_idx) &&
2717 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002718 !(test_bit(R5_UPTODATE, &dev->flags) ||
2719 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002720 if (test_bit(R5_Insync, &dev->flags))
2721 rmw++;
2722 else
2723 rmw += 2*disks; /* cannot read it */
2724 }
2725 /* Would I have to read this buffer for reconstruct_write */
2726 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2727 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002728 !(test_bit(R5_UPTODATE, &dev->flags) ||
2729 test_bit(R5_Wantcompute, &dev->flags))) {
2730 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002731 else
2732 rcw += 2*disks;
2733 }
2734 }
Dan Williams45b42332007-07-09 11:56:43 -07002735 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002736 (unsigned long long)sh->sector, rmw, rcw);
2737 set_bit(STRIPE_HANDLE, &sh->state);
2738 if (rmw < rcw && rmw > 0)
2739 /* prefer read-modify-write, but need to get some data */
2740 for (i = disks; i--; ) {
2741 struct r5dev *dev = &sh->dev[i];
2742 if ((dev->towrite || i == sh->pd_idx) &&
2743 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002744 !(test_bit(R5_UPTODATE, &dev->flags) ||
2745 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002746 test_bit(R5_Insync, &dev->flags)) {
2747 if (
2748 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002749 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002750 "%d for r-m-w\n", i);
2751 set_bit(R5_LOCKED, &dev->flags);
2752 set_bit(R5_Wantread, &dev->flags);
2753 s->locked++;
2754 } else {
2755 set_bit(STRIPE_DELAYED, &sh->state);
2756 set_bit(STRIPE_HANDLE, &sh->state);
2757 }
2758 }
2759 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002760 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002761 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002762 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002763 for (i = disks; i--; ) {
2764 struct r5dev *dev = &sh->dev[i];
2765 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002766 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002767 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002768 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002769 test_bit(R5_Wantcompute, &dev->flags))) {
2770 rcw++;
2771 if (!test_bit(R5_Insync, &dev->flags))
2772 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002773 if (
2774 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002775 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002776 "%d for Reconstruct\n", i);
2777 set_bit(R5_LOCKED, &dev->flags);
2778 set_bit(R5_Wantread, &dev->flags);
2779 s->locked++;
2780 } else {
2781 set_bit(STRIPE_DELAYED, &sh->state);
2782 set_bit(STRIPE_HANDLE, &sh->state);
2783 }
2784 }
2785 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002786 }
Dan Williamsa4456852007-07-09 11:56:43 -07002787 /* now if nothing is locked, and if we have enough data,
2788 * we can start a write request
2789 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002790 /* since handle_stripe can be called at any time we need to handle the
2791 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002792 * subsequent call wants to start a write request. raid_run_ops only
2793 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002794 * simultaneously. If this is not the case then new writes need to be
2795 * held off until the compute completes.
2796 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002797 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2798 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2799 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002800 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002801}
2802
NeilBrownd1688a62011-10-11 16:49:52 +11002803static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002804 struct stripe_head_state *s, int disks)
2805{
Dan Williamsecc65c92008-06-28 08:31:57 +10002806 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002807
Dan Williamsbd2ab672008-04-10 21:29:27 -07002808 set_bit(STRIPE_HANDLE, &sh->state);
2809
Dan Williamsecc65c92008-06-28 08:31:57 +10002810 switch (sh->check_state) {
2811 case check_state_idle:
2812 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002813 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002814 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002815 sh->check_state = check_state_run;
2816 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002817 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002818 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002819 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002820 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002821 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002822 /* fall through */
2823 case check_state_compute_result:
2824 sh->check_state = check_state_idle;
2825 if (!dev)
2826 dev = &sh->dev[sh->pd_idx];
2827
2828 /* check that a write has not made the stripe insync */
2829 if (test_bit(STRIPE_INSYNC, &sh->state))
2830 break;
2831
2832 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002833 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2834 BUG_ON(s->uptodate != disks);
2835
2836 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002837 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002838 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002839
Dan Williamsa4456852007-07-09 11:56:43 -07002840 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002841 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002842 break;
2843 case check_state_run:
2844 break; /* we will be called again upon completion */
2845 case check_state_check_result:
2846 sh->check_state = check_state_idle;
2847
2848 /* if a failure occurred during the check operation, leave
2849 * STRIPE_INSYNC not set and let the stripe be handled again
2850 */
2851 if (s->failed)
2852 break;
2853
2854 /* handle a successful check operation, if parity is correct
2855 * we are done. Otherwise update the mismatch count and repair
2856 * parity if !MD_RECOVERY_CHECK
2857 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002858 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002859 /* parity is correct (on disc,
2860 * not in buffer any more)
2861 */
2862 set_bit(STRIPE_INSYNC, &sh->state);
2863 else {
2864 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2865 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2866 /* don't try to repair!! */
2867 set_bit(STRIPE_INSYNC, &sh->state);
2868 else {
2869 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002870 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002871 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2872 set_bit(R5_Wantcompute,
2873 &sh->dev[sh->pd_idx].flags);
2874 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002875 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002876 s->uptodate++;
2877 }
2878 }
2879 break;
2880 case check_state_compute_run:
2881 break;
2882 default:
2883 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2884 __func__, sh->check_state,
2885 (unsigned long long) sh->sector);
2886 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002887 }
2888}
2889
2890
NeilBrownd1688a62011-10-11 16:49:52 +11002891static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002892 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002893 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002894{
Dan Williamsa4456852007-07-09 11:56:43 -07002895 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002896 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002897 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002898
2899 set_bit(STRIPE_HANDLE, &sh->state);
2900
2901 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002902
Dan Williamsa4456852007-07-09 11:56:43 -07002903 /* Want to check and possibly repair P and Q.
2904 * However there could be one 'failed' device, in which
2905 * case we can only check one of them, possibly using the
2906 * other to generate missing data
2907 */
2908
Dan Williamsd82dfee2009-07-14 13:40:57 -07002909 switch (sh->check_state) {
2910 case check_state_idle:
2911 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002912 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002913 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002914 * makes sense to check P (If anything else were failed,
2915 * we would have used P to recreate it).
2916 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002917 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002918 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002919 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002920 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002921 * anything, so it makes sense to check it
2922 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002923 if (sh->check_state == check_state_run)
2924 sh->check_state = check_state_run_pq;
2925 else
2926 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002927 }
Dan Williams36d1c642009-07-14 11:48:22 -07002928
Dan Williamsd82dfee2009-07-14 13:40:57 -07002929 /* discard potentially stale zero_sum_result */
2930 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002931
Dan Williamsd82dfee2009-07-14 13:40:57 -07002932 if (sh->check_state == check_state_run) {
2933 /* async_xor_zero_sum destroys the contents of P */
2934 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2935 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002936 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002937 if (sh->check_state >= check_state_run &&
2938 sh->check_state <= check_state_run_pq) {
2939 /* async_syndrome_zero_sum preserves P and Q, so
2940 * no need to mark them !uptodate here
2941 */
2942 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2943 break;
2944 }
Dan Williams36d1c642009-07-14 11:48:22 -07002945
Dan Williamsd82dfee2009-07-14 13:40:57 -07002946 /* we have 2-disk failure */
2947 BUG_ON(s->failed != 2);
2948 /* fall through */
2949 case check_state_compute_result:
2950 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002951
Dan Williamsd82dfee2009-07-14 13:40:57 -07002952 /* check that a write has not made the stripe insync */
2953 if (test_bit(STRIPE_INSYNC, &sh->state))
2954 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002955
2956 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002957 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002958 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002959 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002960 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002961 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002962 s->locked++;
2963 set_bit(R5_LOCKED, &dev->flags);
2964 set_bit(R5_Wantwrite, &dev->flags);
2965 }
2966 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002967 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002968 s->locked++;
2969 set_bit(R5_LOCKED, &dev->flags);
2970 set_bit(R5_Wantwrite, &dev->flags);
2971 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002972 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002973 dev = &sh->dev[pd_idx];
2974 s->locked++;
2975 set_bit(R5_LOCKED, &dev->flags);
2976 set_bit(R5_Wantwrite, &dev->flags);
2977 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002978 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002979 dev = &sh->dev[qd_idx];
2980 s->locked++;
2981 set_bit(R5_LOCKED, &dev->flags);
2982 set_bit(R5_Wantwrite, &dev->flags);
2983 }
2984 clear_bit(STRIPE_DEGRADED, &sh->state);
2985
2986 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002987 break;
2988 case check_state_run:
2989 case check_state_run_q:
2990 case check_state_run_pq:
2991 break; /* we will be called again upon completion */
2992 case check_state_check_result:
2993 sh->check_state = check_state_idle;
2994
2995 /* handle a successful check operation, if parity is correct
2996 * we are done. Otherwise update the mismatch count and repair
2997 * parity if !MD_RECOVERY_CHECK
2998 */
2999 if (sh->ops.zero_sum_result == 0) {
3000 /* both parities are correct */
3001 if (!s->failed)
3002 set_bit(STRIPE_INSYNC, &sh->state);
3003 else {
3004 /* in contrast to the raid5 case we can validate
3005 * parity, but still have a failure to write
3006 * back
3007 */
3008 sh->check_state = check_state_compute_result;
3009 /* Returning at this point means that we may go
3010 * off and bring p and/or q uptodate again so
3011 * we make sure to check zero_sum_result again
3012 * to verify if p or q need writeback
3013 */
3014 }
3015 } else {
3016 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3017 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3018 /* don't try to repair!! */
3019 set_bit(STRIPE_INSYNC, &sh->state);
3020 else {
3021 int *target = &sh->ops.target;
3022
3023 sh->ops.target = -1;
3024 sh->ops.target2 = -1;
3025 sh->check_state = check_state_compute_run;
3026 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3027 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3028 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3029 set_bit(R5_Wantcompute,
3030 &sh->dev[pd_idx].flags);
3031 *target = pd_idx;
3032 target = &sh->ops.target2;
3033 s->uptodate++;
3034 }
3035 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3036 set_bit(R5_Wantcompute,
3037 &sh->dev[qd_idx].flags);
3038 *target = qd_idx;
3039 s->uptodate++;
3040 }
3041 }
3042 }
3043 break;
3044 case check_state_compute_run:
3045 break;
3046 default:
3047 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3048 __func__, sh->check_state,
3049 (unsigned long long) sh->sector);
3050 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003051 }
3052}
3053
NeilBrownd1688a62011-10-11 16:49:52 +11003054static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003055{
3056 int i;
3057
3058 /* We have read all the blocks in this stripe and now we need to
3059 * copy some of them into a target stripe for expand.
3060 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003061 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003062 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3063 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003064 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003065 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003066 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003067 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003068
NeilBrown784052e2009-03-31 15:19:07 +11003069 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003070 sector_t s = raid5_compute_sector(conf, bn, 0,
3071 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003072 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003073 if (sh2 == NULL)
3074 /* so far only the early blocks of this stripe
3075 * have been requested. When later blocks
3076 * get requested, we will try again
3077 */
3078 continue;
3079 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3080 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3081 /* must have already done this block */
3082 release_stripe(sh2);
3083 continue;
3084 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003085
3086 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003087 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003088 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003089 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003090 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003091
Dan Williamsa4456852007-07-09 11:56:43 -07003092 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3093 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3094 for (j = 0; j < conf->raid_disks; j++)
3095 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003096 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003097 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3098 break;
3099 if (j == conf->raid_disks) {
3100 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3101 set_bit(STRIPE_HANDLE, &sh2->state);
3102 }
3103 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003104
Dan Williamsa4456852007-07-09 11:56:43 -07003105 }
NeilBrowna2e08552007-09-11 15:23:36 -07003106 /* done submitting copies, wait for them to complete */
3107 if (tx) {
3108 async_tx_ack(tx);
3109 dma_wait_for_async_tx(tx);
3110 }
Dan Williamsa4456852007-07-09 11:56:43 -07003111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113/*
3114 * handle_stripe - do things to a stripe.
3115 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003116 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3117 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003119 * return some read requests which now have data
3120 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 * schedule a read on some buffers
3122 * schedule a write of some buffers
3123 * return confirmation of parity correctness
3124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
Dan Williamsa4456852007-07-09 11:56:43 -07003126
NeilBrownacfe7262011-07-27 11:00:36 +10003127static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003128{
NeilBrownd1688a62011-10-11 16:49:52 +11003129 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003130 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003131 struct r5dev *dev;
3132 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003133 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003134
NeilBrownacfe7262011-07-27 11:00:36 +10003135 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003136
NeilBrownacfe7262011-07-27 11:00:36 +10003137 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3138 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3139 s->failed_num[0] = -1;
3140 s->failed_num[1] = -1;
3141
3142 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003143 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003144 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003145 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003146 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003147 sector_t first_bad;
3148 int bad_sectors;
3149 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003150
NeilBrown16a53ec2006-06-26 00:27:38 -07003151 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003152
Dan Williams45b42332007-07-09 11:56:43 -07003153 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003154 i, dev->flags,
3155 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003156 /* maybe we can reply to a read
3157 *
3158 * new wantfill requests are only permitted while
3159 * ops_complete_biofill is guaranteed to be inactive
3160 */
3161 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3162 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3163 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003164
3165 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003166 if (test_bit(R5_LOCKED, &dev->flags))
3167 s->locked++;
3168 if (test_bit(R5_UPTODATE, &dev->flags))
3169 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003170 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003171 s->compute++;
3172 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003173 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003174
NeilBrownacfe7262011-07-27 11:00:36 +10003175 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003176 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003177 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003178 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003179 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003180 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003181 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003182 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003183 }
Dan Williamsa4456852007-07-09 11:56:43 -07003184 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003185 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003186 /* Prefer to use the replacement for reads, but only
3187 * if it is recovered enough and has no bad blocks.
3188 */
3189 rdev = rcu_dereference(conf->disks[i].replacement);
3190 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3191 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3192 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3193 &first_bad, &bad_sectors))
3194 set_bit(R5_ReadRepl, &dev->flags);
3195 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003196 if (rdev)
3197 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003198 rdev = rcu_dereference(conf->disks[i].rdev);
3199 clear_bit(R5_ReadRepl, &dev->flags);
3200 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003201 if (rdev && test_bit(Faulty, &rdev->flags))
3202 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003203 if (rdev) {
3204 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3205 &first_bad, &bad_sectors);
3206 if (s->blocked_rdev == NULL
3207 && (test_bit(Blocked, &rdev->flags)
3208 || is_bad < 0)) {
3209 if (is_bad < 0)
3210 set_bit(BlockedBadBlocks,
3211 &rdev->flags);
3212 s->blocked_rdev = rdev;
3213 atomic_inc(&rdev->nr_pending);
3214 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003215 }
NeilBrown415e72d2010-06-17 17:25:21 +10003216 clear_bit(R5_Insync, &dev->flags);
3217 if (!rdev)
3218 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003219 else if (is_bad) {
3220 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003221 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3222 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003223 /* treat as in-sync, but with a read error
3224 * which we can now try to correct
3225 */
3226 set_bit(R5_Insync, &dev->flags);
3227 set_bit(R5_ReadError, &dev->flags);
3228 }
3229 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003230 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003231 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003232 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003233 set_bit(R5_Insync, &dev->flags);
3234 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3235 test_bit(R5_Expanded, &dev->flags))
3236 /* If we've reshaped into here, we assume it is Insync.
3237 * We will shortly update recovery_offset to make
3238 * it official.
3239 */
3240 set_bit(R5_Insync, &dev->flags);
3241
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003242 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003243 /* This flag does not apply to '.replacement'
3244 * only to .rdev, so make sure to check that*/
3245 struct md_rdev *rdev2 = rcu_dereference(
3246 conf->disks[i].rdev);
3247 if (rdev2 == rdev)
3248 clear_bit(R5_Insync, &dev->flags);
3249 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003250 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003251 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003252 } else
3253 clear_bit(R5_WriteError, &dev->flags);
3254 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003255 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003256 /* This flag does not apply to '.replacement'
3257 * only to .rdev, so make sure to check that*/
3258 struct md_rdev *rdev2 = rcu_dereference(
3259 conf->disks[i].rdev);
3260 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003261 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003262 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003263 } else
3264 clear_bit(R5_MadeGood, &dev->flags);
3265 }
NeilBrown977df362011-12-23 10:17:53 +11003266 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3267 struct md_rdev *rdev2 = rcu_dereference(
3268 conf->disks[i].replacement);
3269 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3270 s->handle_bad_blocks = 1;
3271 atomic_inc(&rdev2->nr_pending);
3272 } else
3273 clear_bit(R5_MadeGoodRepl, &dev->flags);
3274 }
NeilBrown415e72d2010-06-17 17:25:21 +10003275 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 /* The ReadError flag will just be confusing now */
3277 clear_bit(R5_ReadError, &dev->flags);
3278 clear_bit(R5_ReWrite, &dev->flags);
3279 }
NeilBrown415e72d2010-06-17 17:25:21 +10003280 if (test_bit(R5_ReadError, &dev->flags))
3281 clear_bit(R5_Insync, &dev->flags);
3282 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003283 if (s->failed < 2)
3284 s->failed_num[s->failed] = i;
3285 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003286 if (rdev && !test_bit(Faulty, &rdev->flags))
3287 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003288 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003289 }
NeilBrownc4c16632011-07-26 11:34:20 +10003290 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003291 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3292 /* If there is a failed device being replaced,
3293 * we must be recovering.
3294 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003295 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003296 * else we can only be replacing
3297 * sync and recovery both need to read all devices, and so
3298 * use the same flag.
3299 */
3300 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003301 sh->sector >= conf->mddev->recovery_cp ||
3302 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003303 s->syncing = 1;
3304 else
3305 s->replacing = 1;
3306 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003307 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003308}
NeilBrownf4168852007-02-28 20:11:53 -08003309
NeilBrowncc940152011-07-26 11:35:35 +10003310static void handle_stripe(struct stripe_head *sh)
3311{
3312 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003313 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003314 int i;
NeilBrown84789552011-07-27 11:00:36 +10003315 int prexor;
3316 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003317 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003318
3319 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003320 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003321 /* already being handled, ensure it gets handled
3322 * again when current action finishes */
3323 set_bit(STRIPE_HANDLE, &sh->state);
3324 return;
3325 }
3326
3327 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3328 set_bit(STRIPE_SYNCING, &sh->state);
3329 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrown0761d072013-07-22 12:57:21 +10003330 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrowncc940152011-07-26 11:35:35 +10003331 }
3332 clear_bit(STRIPE_DELAYED, &sh->state);
3333
3334 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3335 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3336 (unsigned long long)sh->sector, sh->state,
3337 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3338 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003339
NeilBrownacfe7262011-07-27 11:00:36 +10003340 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003341
NeilBrownbc2607f2011-07-28 11:39:22 +10003342 if (s.handle_bad_blocks) {
3343 set_bit(STRIPE_HANDLE, &sh->state);
3344 goto finish;
3345 }
3346
NeilBrown474af965fe2011-07-27 11:00:36 +10003347 if (unlikely(s.blocked_rdev)) {
3348 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003349 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003350 set_bit(STRIPE_HANDLE, &sh->state);
3351 goto finish;
3352 }
3353 /* There is nothing for the blocked_rdev to block */
3354 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3355 s.blocked_rdev = NULL;
3356 }
3357
3358 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3359 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3360 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3361 }
3362
3363 pr_debug("locked=%d uptodate=%d to_read=%d"
3364 " to_write=%d failed=%d failed_num=%d,%d\n",
3365 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3366 s.failed_num[0], s.failed_num[1]);
3367 /* check if the array has lost more than max_degraded devices and,
3368 * if so, some requests might need to be failed.
3369 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003370 if (s.failed > conf->max_degraded) {
3371 sh->check_state = 0;
3372 sh->reconstruct_state = 0;
3373 if (s.to_read+s.to_write+s.written)
3374 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003375 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003376 handle_failed_sync(conf, sh, &s);
3377 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003378
3379 /*
3380 * might be able to return some write requests if the parity blocks
3381 * are safe, or on a failed drive
3382 */
3383 pdev = &sh->dev[sh->pd_idx];
3384 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3385 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3386 qdev = &sh->dev[sh->qd_idx];
3387 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3388 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3389 || conf->level < 6;
3390
3391 if (s.written &&
3392 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3393 && !test_bit(R5_LOCKED, &pdev->flags)
3394 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3395 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3396 && !test_bit(R5_LOCKED, &qdev->flags)
3397 && test_bit(R5_UPTODATE, &qdev->flags)))))
3398 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3399
3400 /* Now we might consider reading some blocks, either to check/generate
3401 * parity, or to satisfy requests
3402 * or to load a block that is being partially written.
3403 */
3404 if (s.to_read || s.non_overwrite
3405 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003406 || (s.syncing && (s.uptodate + s.compute < disks))
3407 || s.replacing
3408 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003409 handle_stripe_fill(sh, &s, disks);
3410
NeilBrown84789552011-07-27 11:00:36 +10003411 /* Now we check to see if any write operations have recently
3412 * completed
3413 */
3414 prexor = 0;
3415 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3416 prexor = 1;
3417 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3418 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3419 sh->reconstruct_state = reconstruct_state_idle;
3420
3421 /* All the 'written' buffers and the parity block are ready to
3422 * be written back to disk
3423 */
3424 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3425 BUG_ON(sh->qd_idx >= 0 &&
3426 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3427 for (i = disks; i--; ) {
3428 struct r5dev *dev = &sh->dev[i];
3429 if (test_bit(R5_LOCKED, &dev->flags) &&
3430 (i == sh->pd_idx || i == sh->qd_idx ||
3431 dev->written)) {
3432 pr_debug("Writing block %d\n", i);
3433 set_bit(R5_Wantwrite, &dev->flags);
3434 if (prexor)
3435 continue;
NeilBrowna0e5b9d2014-08-13 09:57:07 +10003436 if (s.failed > 1)
3437 continue;
NeilBrown84789552011-07-27 11:00:36 +10003438 if (!test_bit(R5_Insync, &dev->flags) ||
3439 ((i == sh->pd_idx || i == sh->qd_idx) &&
3440 s.failed == 0))
3441 set_bit(STRIPE_INSYNC, &sh->state);
3442 }
3443 }
3444 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3445 s.dec_preread_active = 1;
3446 }
3447
3448 /* Now to consider new write requests and what else, if anything
3449 * should be read. We do not handle new writes when:
3450 * 1/ A 'write' operation (copy+xor) is already in flight.
3451 * 2/ A 'check' operation is in flight, as it may clobber the parity
3452 * block.
3453 */
3454 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3455 handle_stripe_dirtying(conf, sh, &s, disks);
3456
3457 /* maybe we need to check and possibly fix the parity for this stripe
3458 * Any reads will already have been scheduled, so we just see if enough
3459 * data is available. The parity check is held off while parity
3460 * dependent operations are in flight.
3461 */
3462 if (sh->check_state ||
3463 (s.syncing && s.locked == 0 &&
3464 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3465 !test_bit(STRIPE_INSYNC, &sh->state))) {
3466 if (conf->level == 6)
3467 handle_parity_checks6(conf, sh, &s, disks);
3468 else
3469 handle_parity_checks5(conf, sh, &s, disks);
3470 }
NeilBrownc5a31002011-07-27 11:00:36 +10003471
NeilBrown0761d072013-07-22 12:57:21 +10003472 if ((s.replacing || s.syncing) && s.locked == 0
3473 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3474 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003475 /* Write out to replacement devices where possible */
3476 for (i = 0; i < conf->raid_disks; i++)
NeilBrown0761d072013-07-22 12:57:21 +10003477 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3478 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003479 set_bit(R5_WantReplace, &sh->dev[i].flags);
3480 set_bit(R5_LOCKED, &sh->dev[i].flags);
3481 s.locked++;
3482 }
NeilBrown0761d072013-07-22 12:57:21 +10003483 if (s.replacing)
3484 set_bit(STRIPE_INSYNC, &sh->state);
3485 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003486 }
3487 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrown0761d072013-07-22 12:57:21 +10003488 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003489 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003490 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3491 clear_bit(STRIPE_SYNCING, &sh->state);
3492 }
3493
3494 /* If the failed drives are just a ReadError, then we might need
3495 * to progress the repair/check process
3496 */
3497 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3498 for (i = 0; i < s.failed; i++) {
3499 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3500 if (test_bit(R5_ReadError, &dev->flags)
3501 && !test_bit(R5_LOCKED, &dev->flags)
3502 && test_bit(R5_UPTODATE, &dev->flags)
3503 ) {
3504 if (!test_bit(R5_ReWrite, &dev->flags)) {
3505 set_bit(R5_Wantwrite, &dev->flags);
3506 set_bit(R5_ReWrite, &dev->flags);
3507 set_bit(R5_LOCKED, &dev->flags);
3508 s.locked++;
3509 } else {
3510 /* let's read it back */
3511 set_bit(R5_Wantread, &dev->flags);
3512 set_bit(R5_LOCKED, &dev->flags);
3513 s.locked++;
3514 }
3515 }
3516 }
3517
3518
NeilBrown3687c062011-07-27 11:00:36 +10003519 /* Finish reconstruct operations initiated by the expansion process */
3520 if (sh->reconstruct_state == reconstruct_state_result) {
3521 struct stripe_head *sh_src
3522 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3523 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3524 /* sh cannot be written until sh_src has been read.
3525 * so arrange for sh to be delayed a little
3526 */
3527 set_bit(STRIPE_DELAYED, &sh->state);
3528 set_bit(STRIPE_HANDLE, &sh->state);
3529 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3530 &sh_src->state))
3531 atomic_inc(&conf->preread_active_stripes);
3532 release_stripe(sh_src);
3533 goto finish;
3534 }
3535 if (sh_src)
3536 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003537
NeilBrown3687c062011-07-27 11:00:36 +10003538 sh->reconstruct_state = reconstruct_state_idle;
3539 clear_bit(STRIPE_EXPANDING, &sh->state);
3540 for (i = conf->raid_disks; i--; ) {
3541 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3542 set_bit(R5_LOCKED, &sh->dev[i].flags);
3543 s.locked++;
3544 }
3545 }
3546
3547 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3548 !sh->reconstruct_state) {
3549 /* Need to write out all blocks after computing parity */
3550 sh->disks = conf->raid_disks;
3551 stripe_set_idx(sh->sector, conf, 0, sh);
3552 schedule_reconstruction(sh, &s, 1, 1);
3553 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3554 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3555 atomic_dec(&conf->reshape_stripes);
3556 wake_up(&conf->wait_for_overlap);
3557 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3558 }
3559
3560 if (s.expanding && s.locked == 0 &&
3561 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3562 handle_stripe_expansion(conf, sh);
3563
3564finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003565 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003566 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003567 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003568
NeilBrownbc2607f2011-07-28 11:39:22 +10003569 if (s.handle_bad_blocks)
3570 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003571 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003572 struct r5dev *dev = &sh->dev[i];
3573 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3574 /* We own a safe reference to the rdev */
3575 rdev = conf->disks[i].rdev;
3576 if (!rdev_set_badblocks(rdev, sh->sector,
3577 STRIPE_SECTORS, 0))
3578 md_error(conf->mddev, rdev);
3579 rdev_dec_pending(rdev, conf->mddev);
3580 }
NeilBrownb84db562011-07-28 11:39:23 +10003581 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3582 rdev = conf->disks[i].rdev;
3583 rdev_clear_badblocks(rdev, sh->sector,
3584 STRIPE_SECTORS);
3585 rdev_dec_pending(rdev, conf->mddev);
3586 }
NeilBrown977df362011-12-23 10:17:53 +11003587 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3588 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003589 if (!rdev)
3590 /* rdev have been moved down */
3591 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003592 rdev_clear_badblocks(rdev, sh->sector,
3593 STRIPE_SECTORS);
3594 rdev_dec_pending(rdev, conf->mddev);
3595 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003596 }
3597
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003598 if (s.ops_request)
3599 raid_run_ops(sh, s.ops_request);
3600
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003601 ops_run_io(sh, &s);
3602
NeilBrownc5709ef2011-07-26 11:35:20 +10003603 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003604 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003605 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003606 * have actually been submitted.
3607 */
3608 atomic_dec(&conf->preread_active_stripes);
3609 if (atomic_read(&conf->preread_active_stripes) <
3610 IO_THRESHOLD)
3611 md_wakeup_thread(conf->mddev->thread);
3612 }
3613
NeilBrownc5709ef2011-07-26 11:35:20 +10003614 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003615
Dan Williams257a4b42011-11-08 16:22:06 +11003616 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003617}
3618
NeilBrownd1688a62011-10-11 16:49:52 +11003619static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3622 while (!list_empty(&conf->delayed_list)) {
3623 struct list_head *l = conf->delayed_list.next;
3624 struct stripe_head *sh;
3625 sh = list_entry(l, struct stripe_head, lru);
3626 list_del_init(l);
3627 clear_bit(STRIPE_DELAYED, &sh->state);
3628 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3629 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003630 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
NeilBrown482c0832011-04-18 18:25:42 +10003632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
NeilBrownd1688a62011-10-11 16:49:52 +11003635static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003636{
3637 /* device_lock is held */
3638 struct list_head head;
3639 list_add(&head, &conf->bitmap_list);
3640 list_del_init(&conf->bitmap_list);
3641 while (!list_empty(&head)) {
3642 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3643 list_del_init(&sh->lru);
3644 atomic_inc(&sh->count);
3645 __release_stripe(conf, sh);
3646 }
3647}
3648
NeilBrownfd01b882011-10-11 16:47:53 +11003649int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003650{
NeilBrownd1688a62011-10-11 16:49:52 +11003651 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003652
3653 /* No difference between reads and writes. Just check
3654 * how busy the stripe_cache is
3655 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003656
NeilBrownf022b2f2006-10-03 01:15:56 -07003657 if (conf->inactive_blocked)
3658 return 1;
3659 if (conf->quiesce)
3660 return 1;
3661 if (list_empty_careful(&conf->inactive_list))
3662 return 1;
3663
3664 return 0;
3665}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003666EXPORT_SYMBOL_GPL(md_raid5_congested);
3667
3668static int raid5_congested(void *data, int bits)
3669{
NeilBrownfd01b882011-10-11 16:47:53 +11003670 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003671
3672 return mddev_congested(mddev, bits) ||
3673 md_raid5_congested(mddev, bits);
3674}
NeilBrownf022b2f2006-10-03 01:15:56 -07003675
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003676/* We want read requests to align with chunks where possible,
3677 * but write requests don't need to.
3678 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003679static int raid5_mergeable_bvec(struct request_queue *q,
3680 struct bvec_merge_data *bvm,
3681 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003682{
NeilBrownfd01b882011-10-11 16:47:53 +11003683 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003684 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003685 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003686 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003687 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003688
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003689 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003690 return biovec->bv_len; /* always allow writes to be mergeable */
3691
Andre Noll664e7c42009-06-18 08:45:27 +10003692 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3693 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003694 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3695 if (max < 0) max = 0;
3696 if (max <= biovec->bv_len && bio_sectors == 0)
3697 return biovec->bv_len;
3698 else
3699 return max;
3700}
3701
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003702
NeilBrownfd01b882011-10-11 16:47:53 +11003703static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003704{
3705 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003706 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003707 unsigned int bio_sectors = bio->bi_size >> 9;
3708
Andre Noll664e7c42009-06-18 08:45:27 +10003709 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3710 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003711 return chunk_sectors >=
3712 ((sector & (chunk_sectors - 1)) + bio_sectors);
3713}
3714
3715/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003716 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3717 * later sampled by raid5d.
3718 */
NeilBrownd1688a62011-10-11 16:49:52 +11003719static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003720{
3721 unsigned long flags;
3722
3723 spin_lock_irqsave(&conf->device_lock, flags);
3724
3725 bi->bi_next = conf->retry_read_aligned_list;
3726 conf->retry_read_aligned_list = bi;
3727
3728 spin_unlock_irqrestore(&conf->device_lock, flags);
3729 md_wakeup_thread(conf->mddev->thread);
3730}
3731
3732
NeilBrownd1688a62011-10-11 16:49:52 +11003733static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003734{
3735 struct bio *bi;
3736
3737 bi = conf->retry_read_aligned;
3738 if (bi) {
3739 conf->retry_read_aligned = NULL;
3740 return bi;
3741 }
3742 bi = conf->retry_read_aligned_list;
3743 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003744 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003745 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003746 /*
3747 * this sets the active strip count to 1 and the processed
3748 * strip count to zero (upper 8 bits)
3749 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003750 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003751 }
3752
3753 return bi;
3754}
3755
3756
3757/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003758 * The "raid5_align_endio" should check if the read succeeded and if it
3759 * did, call bio_endio on the original bio (having bio_put the new bio
3760 * first).
3761 * If the read failed..
3762 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003763static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003764{
3765 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003766 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003767 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003768 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003769 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003770
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003771 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003772
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003773 rdev = (void*)raid_bi->bi_next;
3774 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003775 mddev = rdev->mddev;
3776 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003777
3778 rdev_dec_pending(rdev, conf->mddev);
3779
3780 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003781 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003782 if (atomic_dec_and_test(&conf->active_aligned_reads))
3783 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003784 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003785 }
3786
3787
Dan Williams45b42332007-07-09 11:56:43 -07003788 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003789
3790 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003791}
3792
Neil Brown387bb172007-02-08 14:20:29 -08003793static int bio_fits_rdev(struct bio *bi)
3794{
Jens Axboe165125e2007-07-24 09:28:11 +02003795 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003796
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003797 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003798 return 0;
3799 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003800 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003801 return 0;
3802
3803 if (q->merge_bvec_fn)
3804 /* it's too hard to apply the merge_bvec_fn at this stage,
3805 * just just give up
3806 */
3807 return 0;
3808
3809 return 1;
3810}
3811
3812
NeilBrownfd01b882011-10-11 16:47:53 +11003813static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003814{
NeilBrownd1688a62011-10-11 16:49:52 +11003815 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003816 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003817 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003818 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003819 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003820
3821 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003822 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003823 return 0;
3824 }
3825 /*
NeilBrowna167f662010-10-26 18:31:13 +11003826 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003827 */
NeilBrowna167f662010-10-26 18:31:13 +11003828 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003829 if (!align_bi)
3830 return 0;
3831 /*
3832 * set bi_end_io to a new function, and set bi_private to the
3833 * original bio.
3834 */
3835 align_bi->bi_end_io = raid5_align_endio;
3836 align_bi->bi_private = raid_bio;
3837 /*
3838 * compute position
3839 */
NeilBrown112bf892009-03-31 14:39:38 +11003840 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3841 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003842 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003843
NeilBrown671488c2011-12-23 10:17:52 +11003844 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003845 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003846 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3847 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3848 rdev->recovery_offset < end_sector) {
3849 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3850 if (rdev &&
3851 (test_bit(Faulty, &rdev->flags) ||
3852 !(test_bit(In_sync, &rdev->flags) ||
3853 rdev->recovery_offset >= end_sector)))
3854 rdev = NULL;
3855 }
3856 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003857 sector_t first_bad;
3858 int bad_sectors;
3859
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003860 atomic_inc(&rdev->nr_pending);
3861 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003862 raid_bio->bi_next = (void*)rdev;
3863 align_bi->bi_bdev = rdev->bdev;
3864 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003865
NeilBrown31c176e2011-07-28 11:39:22 +10003866 if (!bio_fits_rdev(align_bi) ||
3867 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3868 &first_bad, &bad_sectors)) {
3869 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003870 bio_put(align_bi);
3871 rdev_dec_pending(rdev, mddev);
3872 return 0;
3873 }
3874
majianpengd3f94022012-06-12 08:31:10 +08003875 /* No reshape active, so we can trust rdev->data_offset */
3876 align_bi->bi_sector += rdev->data_offset;
3877
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003878 spin_lock_irq(&conf->device_lock);
3879 wait_event_lock_irq(conf->wait_for_stripe,
3880 conf->quiesce == 0,
3881 conf->device_lock, /* nothing */);
3882 atomic_inc(&conf->active_aligned_reads);
3883 spin_unlock_irq(&conf->device_lock);
3884
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003885 generic_make_request(align_bi);
3886 return 1;
3887 } else {
3888 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003889 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003890 return 0;
3891 }
3892}
3893
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003894/* __get_priority_stripe - get the next stripe to process
3895 *
3896 * Full stripe writes are allowed to pass preread active stripes up until
3897 * the bypass_threshold is exceeded. In general the bypass_count
3898 * increments when the handle_list is handled before the hold_list; however, it
3899 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3900 * stripe with in flight i/o. The bypass_count will be reset when the
3901 * head of the hold_list has changed, i.e. the head was promoted to the
3902 * handle_list.
3903 */
NeilBrownd1688a62011-10-11 16:49:52 +11003904static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003905{
3906 struct stripe_head *sh;
3907
3908 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3909 __func__,
3910 list_empty(&conf->handle_list) ? "empty" : "busy",
3911 list_empty(&conf->hold_list) ? "empty" : "busy",
3912 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3913
3914 if (!list_empty(&conf->handle_list)) {
3915 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3916
3917 if (list_empty(&conf->hold_list))
3918 conf->bypass_count = 0;
3919 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3920 if (conf->hold_list.next == conf->last_hold)
3921 conf->bypass_count++;
3922 else {
3923 conf->last_hold = conf->hold_list.next;
3924 conf->bypass_count -= conf->bypass_threshold;
3925 if (conf->bypass_count < 0)
3926 conf->bypass_count = 0;
3927 }
3928 }
3929 } else if (!list_empty(&conf->hold_list) &&
3930 ((conf->bypass_threshold &&
3931 conf->bypass_count > conf->bypass_threshold) ||
3932 atomic_read(&conf->pending_full_writes) == 0)) {
3933 sh = list_entry(conf->hold_list.next,
3934 typeof(*sh), lru);
3935 conf->bypass_count -= conf->bypass_threshold;
3936 if (conf->bypass_count < 0)
3937 conf->bypass_count = 0;
3938 } else
3939 return NULL;
3940
3941 list_del_init(&sh->lru);
3942 atomic_inc(&sh->count);
3943 BUG_ON(atomic_read(&sh->count) != 1);
3944 return sh;
3945}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003946
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003947static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
NeilBrownd1688a62011-10-11 16:49:52 +11003949 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003950 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 sector_t new_sector;
3952 sector_t logical_sector, last_sector;
3953 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003954 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003955 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003956 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Tejun Heoe9c74692010-09-03 11:56:18 +02003958 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3959 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003960 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003961 }
3962
NeilBrown3d310eb2005-06-21 17:17:26 -07003963 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003964
NeilBrown802ba062006-12-13 00:34:13 -08003965 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003966 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003967 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003968 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3971 last_sector = bi->bi_sector + (bi->bi_size>>9);
3972 bi->bi_next = NULL;
3973 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003974
NeilBrown7c13edc2011-04-18 18:25:43 +10003975 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3977 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003978 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003979 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003980
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003981 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003982 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003983 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003984 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003985 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003986 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003987 * 64bit on a 32bit platform, and so it might be
3988 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003989 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003990 * the lock is dropped, so once we get a reference
3991 * to the stripe that we think it is, we will have
3992 * to check again.
3993 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003994 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003995 if (mddev->delta_disks < 0
3996 ? logical_sector < conf->reshape_progress
3997 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003998 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003999 previous = 1;
4000 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11004001 if (mddev->delta_disks < 0
4002 ? logical_sector < conf->reshape_safe
4003 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004004 spin_unlock_irq(&conf->device_lock);
4005 schedule();
4006 goto retry;
4007 }
4008 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004009 spin_unlock_irq(&conf->device_lock);
4010 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004011 data_disks = disks - conf->max_degraded;
4012
NeilBrown112bf892009-03-31 14:39:38 +11004013 new_sector = raid5_compute_sector(conf, logical_sector,
4014 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004015 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004016 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 (unsigned long long)new_sector,
4018 (unsigned long long)logical_sector);
4019
NeilBrownb5663ba2009-03-31 14:39:38 +11004020 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004021 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004023 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004024 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004025 * stripe, so we must do the range check again.
4026 * Expansion could still move past after this
4027 * test, but as we are holding a reference to
4028 * 'sh', we know that if that happens,
4029 * STRIPE_EXPANDING will get set and the expansion
4030 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004031 */
4032 int must_retry = 0;
4033 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004034 if (mddev->delta_disks < 0
4035 ? logical_sector >= conf->reshape_progress
4036 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004037 /* mismatch, need to try again */
4038 must_retry = 1;
4039 spin_unlock_irq(&conf->device_lock);
4040 if (must_retry) {
4041 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004042 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004043 goto retry;
4044 }
4045 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004046
Namhyung Kimffd96e32011-07-18 17:38:51 +10004047 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004048 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004049 logical_sector < mddev->suspend_hi) {
4050 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004051 /* As the suspend_* range is controlled by
4052 * userspace, we want an interruptible
4053 * wait.
4054 */
4055 flush_signals(current);
4056 prepare_to_wait(&conf->wait_for_overlap,
4057 &w, TASK_INTERRUPTIBLE);
4058 if (logical_sector >= mddev->suspend_lo &&
4059 logical_sector < mddev->suspend_hi)
4060 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004061 goto retry;
4062 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004063
4064 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004065 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004066 /* Stripe is busy expanding or
4067 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 * and wait a while
4069 */
NeilBrown482c0832011-04-18 18:25:42 +10004070 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 release_stripe(sh);
4072 schedule();
4073 goto retry;
4074 }
4075 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004076 set_bit(STRIPE_HANDLE, &sh->state);
4077 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004078 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004079 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4080 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 } else {
4083 /* cannot get stripe for read-ahead, just give-up */
4084 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4085 finish_wait(&conf->wait_for_overlap, &w);
4086 break;
4087 }
4088
4089 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004090 if (!plugged)
4091 md_wakeup_thread(mddev->thread);
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004094 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004095 spin_unlock_irq(&conf->device_lock);
4096 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
NeilBrown16a53ec2006-06-26 00:27:38 -07004098 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004100
Neil Brown0e13fe232008-06-28 08:31:20 +10004101 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
NeilBrownfd01b882011-10-11 16:47:53 +11004105static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004106
NeilBrownfd01b882011-10-11 16:47:53 +11004107static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
NeilBrown52c03292006-06-26 00:27:43 -07004109 /* reshaping is quite different to recovery/resync so it is
4110 * handled quite separately ... here.
4111 *
4112 * On each call to sync_request, we gather one chunk worth of
4113 * destination stripes and flag them as expanding.
4114 * Then we find all the source stripes and request reads.
4115 * As the reads complete, handle_stripe will copy the data
4116 * into the destination stripe and release that stripe.
4117 */
NeilBrownd1688a62011-10-11 16:49:52 +11004118 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004120 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004121 int raid_disks = conf->previous_raid_disks;
4122 int data_disks = raid_disks - conf->max_degraded;
4123 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004124 int i;
4125 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004126 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004127 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004128 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004129 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004130
NeilBrownfef9c612009-03-31 15:16:46 +11004131 if (sector_nr == 0) {
4132 /* If restarting in the middle, skip the initial sectors */
4133 if (mddev->delta_disks < 0 &&
4134 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4135 sector_nr = raid5_size(mddev, 0, 0)
4136 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004137 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004138 conf->reshape_progress > 0)
4139 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004140 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004141 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004142 mddev->curr_resync_completed = sector_nr;
4143 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004144 *skipped = 1;
4145 return sector_nr;
4146 }
NeilBrown52c03292006-06-26 00:27:43 -07004147 }
4148
NeilBrown7a661382009-03-31 15:21:40 +11004149 /* We need to process a full chunk at a time.
4150 * If old and new chunk sizes differ, we need to process the
4151 * largest of these
4152 */
Andre Noll664e7c42009-06-18 08:45:27 +10004153 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4154 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004155 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004156 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004157
NeilBrown52c03292006-06-26 00:27:43 -07004158 /* we update the metadata when there is more than 3Meg
4159 * in the block range (that is rather arbitrary, should
4160 * probably be time based) or when the data about to be
4161 * copied would over-write the source of the data at
4162 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004163 * i.e. one new_stripe along from reshape_progress new_maps
4164 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004165 */
NeilBrownfef9c612009-03-31 15:16:46 +11004166 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004167 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004168 readpos = conf->reshape_progress;
4169 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004170 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004171 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004172 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004173 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004174 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004175 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004176 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004177 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004178 readpos -= min_t(sector_t, reshape_sectors, readpos);
4179 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004180 }
NeilBrown52c03292006-06-26 00:27:43 -07004181
NeilBrownc8f517c2009-03-31 15:28:40 +11004182 /* 'writepos' is the most advanced device address we might write.
4183 * 'readpos' is the least advanced device address we might read.
4184 * 'safepos' is the least address recorded in the metadata as having
4185 * been reshaped.
4186 * If 'readpos' is behind 'writepos', then there is no way that we can
4187 * ensure safety in the face of a crash - that must be done by userspace
4188 * making a backup of the data. So in that case there is no particular
4189 * rush to update metadata.
4190 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4191 * update the metadata to advance 'safepos' to match 'readpos' so that
4192 * we can be safe in the event of a crash.
4193 * So we insist on updating metadata if safepos is behind writepos and
4194 * readpos is beyond writepos.
4195 * In any case, update the metadata every 10 seconds.
4196 * Maybe that number should be configurable, but I'm not sure it is
4197 * worth it.... maybe it could be a multiple of safemode_delay???
4198 */
NeilBrownfef9c612009-03-31 15:16:46 +11004199 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004200 ? (safepos > writepos && readpos < writepos)
4201 : (safepos < writepos && readpos > writepos)) ||
4202 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004203 /* Cannot proceed until we've updated the superblock... */
4204 wait_event(conf->wait_for_overlap,
4205 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004206 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004207 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004208 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004209 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004210 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004211 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004212 kthread_should_stop());
4213 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004214 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004215 spin_unlock_irq(&conf->device_lock);
4216 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004217 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004218 }
4219
NeilBrownec32a2b2009-03-31 15:17:38 +11004220 if (mddev->delta_disks < 0) {
4221 BUG_ON(conf->reshape_progress == 0);
4222 stripe_addr = writepos;
4223 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004224 ~((sector_t)reshape_sectors - 1))
4225 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004226 != sector_nr);
4227 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004228 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004229 stripe_addr = sector_nr;
4230 }
NeilBrownab69ae12009-03-31 15:26:47 +11004231 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004232 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004233 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004234 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004235 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004236 set_bit(STRIPE_EXPANDING, &sh->state);
4237 atomic_inc(&conf->reshape_stripes);
4238 /* If any of this stripe is beyond the end of the old
4239 * array, then we need to zero those blocks
4240 */
4241 for (j=sh->disks; j--;) {
4242 sector_t s;
4243 if (j == sh->pd_idx)
4244 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004245 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004246 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004247 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004248 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004249 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004250 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004251 continue;
4252 }
4253 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4254 set_bit(R5_Expanded, &sh->dev[j].flags);
4255 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4256 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004257 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004258 set_bit(STRIPE_EXPAND_READY, &sh->state);
4259 set_bit(STRIPE_HANDLE, &sh->state);
4260 }
NeilBrownab69ae12009-03-31 15:26:47 +11004261 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004262 }
4263 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004264 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004265 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004266 else
NeilBrown7a661382009-03-31 15:21:40 +11004267 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004268 spin_unlock_irq(&conf->device_lock);
4269 /* Ok, those stripe are ready. We can start scheduling
4270 * reads on the source stripes.
4271 * The source stripes are determined by mapping the first and last
4272 * block on the destination stripes.
4273 */
NeilBrown52c03292006-06-26 00:27:43 -07004274 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004275 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004276 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004277 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004278 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004279 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004280 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004281 if (last_sector >= mddev->dev_sectors)
4282 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004283 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004284 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004285 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4286 set_bit(STRIPE_HANDLE, &sh->state);
4287 release_stripe(sh);
4288 first_sector += STRIPE_SECTORS;
4289 }
NeilBrownab69ae12009-03-31 15:26:47 +11004290 /* Now that the sources are clearly marked, we can release
4291 * the destination stripes
4292 */
4293 while (!list_empty(&stripes)) {
4294 sh = list_entry(stripes.next, struct stripe_head, lru);
4295 list_del_init(&sh->lru);
4296 release_stripe(sh);
4297 }
NeilBrownc6207272008-02-06 01:39:52 -08004298 /* If this takes us to the resync_max point where we have to pause,
4299 * then we need to write out the superblock.
4300 */
NeilBrown7a661382009-03-31 15:21:40 +11004301 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004302 if ((sector_nr - mddev->curr_resync_completed) * 2
4303 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004304 /* Cannot proceed until we've updated the superblock... */
4305 wait_event(conf->wait_for_overlap,
4306 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004307 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004308 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004309 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004310 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4311 md_wakeup_thread(mddev->thread);
4312 wait_event(mddev->sb_wait,
4313 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4314 || kthread_should_stop());
4315 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004316 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004317 spin_unlock_irq(&conf->device_lock);
4318 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004319 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004320 }
NeilBrown7a661382009-03-31 15:21:40 +11004321 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004322}
4323
4324/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004325static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004326{
NeilBrownd1688a62011-10-11 16:49:52 +11004327 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004328 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004329 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004330 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004331 int still_degraded = 0;
4332 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
NeilBrown72626682005-09-09 16:23:54 -07004334 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004336
NeilBrown29269552006-03-27 01:18:10 -08004337 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4338 end_reshape(conf);
4339 return 0;
4340 }
NeilBrown72626682005-09-09 16:23:54 -07004341
4342 if (mddev->curr_resync < max_sector) /* aborted */
4343 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4344 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004345 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004346 conf->fullsync = 0;
4347 bitmap_close_sync(mddev->bitmap);
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return 0;
4350 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004351
NeilBrown64bd6602009-08-03 10:59:58 +10004352 /* Allow raid5_quiesce to complete */
4353 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4354
NeilBrown52c03292006-06-26 00:27:43 -07004355 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4356 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004357
NeilBrownc6207272008-02-06 01:39:52 -08004358 /* No need to check resync_max as we never do more than one
4359 * stripe, and as resync_max will always be on a chunk boundary,
4360 * if the check in md_do_sync didn't fire, there is no chance
4361 * of overstepping resync_max here
4362 */
4363
NeilBrown16a53ec2006-06-26 00:27:38 -07004364 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 * to resync, then assert that we are finished, because there is
4366 * nothing we can do.
4367 */
NeilBrown3285edf2006-06-26 00:27:55 -07004368 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004369 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004370 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004371 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return rv;
4373 }
NeilBrown72626682005-09-09 16:23:54 -07004374 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004375 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004376 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4377 /* we can skip this block, and probably more */
4378 sync_blocks /= STRIPE_SECTORS;
4379 *skipped = 1;
4380 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
NeilBrownb47490c2008-02-06 01:39:50 -08004383 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4384
NeilBrowna8c906c2009-06-09 14:39:59 +10004385 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004387 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004389 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004391 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004393 /* Need to check if array will still be degraded after recovery/resync
4394 * We don't need to check the 'failed' flag as when that gets set,
4395 * recovery aborts.
4396 */
NeilBrownf001a702009-06-09 14:30:31 +10004397 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004398 if (conf->disks[i].rdev == NULL)
4399 still_degraded = 1;
4400
4401 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4402
NeilBrown83206d62011-07-26 11:19:49 +10004403 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
NeilBrown14425772009-10-16 15:55:25 +11004405 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 release_stripe(sh);
4407
4408 return STRIPE_SECTORS;
4409}
4410
NeilBrownd1688a62011-10-11 16:49:52 +11004411static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004412{
4413 /* We may not be able to submit a whole bio at once as there
4414 * may not be enough stripe_heads available.
4415 * We cannot pre-allocate enough stripe_heads as we may need
4416 * more than exist in the cache (if we allow ever large chunks).
4417 * So we do one stripe head at a time and record in
4418 * ->bi_hw_segments how many have been done.
4419 *
4420 * We *know* that this entire raid_bio is in one chunk, so
4421 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4422 */
4423 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004424 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425 sector_t sector, logical_sector, last_sector;
4426 int scnt = 0;
4427 int remaining;
4428 int handled = 0;
4429
4430 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004431 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004432 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004433 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4434
4435 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004436 logical_sector += STRIPE_SECTORS,
4437 sector += STRIPE_SECTORS,
4438 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004439
Jens Axboe960e7392008-08-15 10:41:18 +02004440 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004441 /* already done this stripe */
4442 continue;
4443
NeilBrowna8c906c2009-06-09 14:39:59 +10004444 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004445
4446 if (!sh) {
4447 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004448 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004449 conf->retry_read_aligned = raid_bio;
4450 return handled;
4451 }
4452
Neil Brown387bb172007-02-08 14:20:29 -08004453 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4454 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004455 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004456 conf->retry_read_aligned = raid_bio;
4457 return handled;
4458 }
4459
Dan Williams36d1c642009-07-14 11:48:22 -07004460 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004461 release_stripe(sh);
4462 handled++;
4463 }
4464 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004465 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004466 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004467 if (remaining == 0)
4468 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004469 if (atomic_dec_and_test(&conf->active_aligned_reads))
4470 wake_up(&conf->wait_for_stripe);
4471 return handled;
4472}
4473
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475/*
4476 * This is our raid5 kernel thread.
4477 *
4478 * We scan the hash table for stripes which can be handled now.
4479 * During the scan, completed stripes are saved for us by the interrupt
4480 * handler, so that they will not have to wait for our next wakeup.
4481 */
NeilBrownfd01b882011-10-11 16:47:53 +11004482static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
4484 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004485 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004487 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Dan Williams45b42332007-07-09 11:56:43 -07004489 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004493 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 handled = 0;
4495 spin_lock_irq(&conf->device_lock);
4496 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004497 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
NeilBrown7c13edc2011-04-18 18:25:43 +10004499 if (atomic_read(&mddev->plug_cnt) == 0 &&
4500 !list_empty(&conf->bitmap_list)) {
4501 /* Now is a good time to flush some bitmap updates */
4502 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004503 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004504 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004505 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004506 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004507 activate_bit_delay(conf);
4508 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004509 if (atomic_read(&mddev->plug_cnt) == 0)
4510 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004511
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004512 while ((bio = remove_bio_from_retry(conf))) {
4513 int ok;
4514 spin_unlock_irq(&conf->device_lock);
4515 ok = retry_aligned_read(conf, bio);
4516 spin_lock_irq(&conf->device_lock);
4517 if (!ok)
4518 break;
4519 handled++;
4520 }
4521
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004522 sh = __get_priority_stripe(conf);
4523
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004524 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 spin_unlock_irq(&conf->device_lock);
4527
4528 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004529 handle_stripe(sh);
4530 release_stripe(sh);
4531 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
NeilBrownde393cd2011-07-28 11:31:48 +10004533 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4534 md_check_recovery(mddev);
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 spin_lock_irq(&conf->device_lock);
4537 }
Dan Williams45b42332007-07-09 11:56:43 -07004538 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 spin_unlock_irq(&conf->device_lock);
4541
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004542 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004543 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Dan Williams45b42332007-07-09 11:56:43 -07004545 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
NeilBrown3f294f42005-11-08 21:39:25 -08004548static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004549raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004550{
NeilBrownd1688a62011-10-11 16:49:52 +11004551 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004552 if (conf)
4553 return sprintf(page, "%d\n", conf->max_nr_stripes);
4554 else
4555 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004556}
4557
NeilBrownc41d4ac2010-06-01 19:37:24 +10004558int
NeilBrownfd01b882011-10-11 16:47:53 +11004559raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004560{
NeilBrownd1688a62011-10-11 16:49:52 +11004561 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004562 int err;
4563
4564 if (size <= 16 || size > 32768)
4565 return -EINVAL;
4566 while (size < conf->max_nr_stripes) {
4567 if (drop_one_stripe(conf))
4568 conf->max_nr_stripes--;
4569 else
4570 break;
4571 }
4572 err = md_allow_write(mddev);
4573 if (err)
4574 return err;
4575 while (size > conf->max_nr_stripes) {
4576 if (grow_one_stripe(conf))
4577 conf->max_nr_stripes++;
4578 else break;
4579 }
4580 return 0;
4581}
4582EXPORT_SYMBOL(raid5_set_cache_size);
4583
NeilBrown3f294f42005-11-08 21:39:25 -08004584static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004585raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004586{
NeilBrownd1688a62011-10-11 16:49:52 +11004587 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004588 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004589 int err;
4590
NeilBrown3f294f42005-11-08 21:39:25 -08004591 if (len >= PAGE_SIZE)
4592 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004593 if (!conf)
4594 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004595
Dan Williams4ef197d82008-04-28 02:15:54 -07004596 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004597 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004598 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004599 if (err)
4600 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004601 return len;
4602}
NeilBrown007583c2005-11-08 21:39:30 -08004603
NeilBrown96de1e62005-11-08 21:39:39 -08004604static struct md_sysfs_entry
4605raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4606 raid5_show_stripe_cache_size,
4607 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004608
4609static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004610raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004611{
NeilBrownd1688a62011-10-11 16:49:52 +11004612 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004613 if (conf)
4614 return sprintf(page, "%d\n", conf->bypass_threshold);
4615 else
4616 return 0;
4617}
4618
4619static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004620raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004621{
NeilBrownd1688a62011-10-11 16:49:52 +11004622 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004623 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004624 if (len >= PAGE_SIZE)
4625 return -EINVAL;
4626 if (!conf)
4627 return -ENODEV;
4628
Dan Williams4ef197d82008-04-28 02:15:54 -07004629 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004630 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004631 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004632 return -EINVAL;
4633 conf->bypass_threshold = new;
4634 return len;
4635}
4636
4637static struct md_sysfs_entry
4638raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4639 S_IRUGO | S_IWUSR,
4640 raid5_show_preread_threshold,
4641 raid5_store_preread_threshold);
4642
4643static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004644stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004645{
NeilBrownd1688a62011-10-11 16:49:52 +11004646 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004647 if (conf)
4648 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4649 else
4650 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004651}
4652
NeilBrown96de1e62005-11-08 21:39:39 -08004653static struct md_sysfs_entry
4654raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004655
NeilBrown007583c2005-11-08 21:39:30 -08004656static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004657 &raid5_stripecache_size.attr,
4658 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004659 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004660 NULL,
4661};
NeilBrown007583c2005-11-08 21:39:30 -08004662static struct attribute_group raid5_attrs_group = {
4663 .name = NULL,
4664 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004665};
4666
Dan Williams80c3a6c2009-03-17 18:10:40 -07004667static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004668raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004669{
NeilBrownd1688a62011-10-11 16:49:52 +11004670 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004671
4672 if (!sectors)
4673 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004674 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004675 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004676 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004677
Andre Noll9d8f0362009-06-18 08:45:01 +10004678 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004679 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004680 return sectors * (raid_disks - conf->max_degraded);
4681}
4682
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304683static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
4684{
4685 safe_put_page(percpu->spare_page);
4686 kfree(percpu->scribble);
4687 percpu->spare_page = NULL;
4688 percpu->scribble = NULL;
4689}
4690
4691static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
4692{
4693 if (conf->level == 6 && !percpu->spare_page)
4694 percpu->spare_page = alloc_page(GFP_KERNEL);
4695 if (!percpu->scribble)
4696 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4697
4698 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
4699 free_scratch_buffer(conf, percpu);
4700 return -ENOMEM;
4701 }
4702
4703 return 0;
4704}
4705
NeilBrownd1688a62011-10-11 16:49:52 +11004706static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004707{
Dan Williams36d1c642009-07-14 11:48:22 -07004708 unsigned long cpu;
4709
4710 if (!conf->percpu)
4711 return;
4712
Dan Williams36d1c642009-07-14 11:48:22 -07004713#ifdef CONFIG_HOTPLUG_CPU
4714 unregister_cpu_notifier(&conf->cpu_notify);
4715#endif
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304716
4717 get_online_cpus();
4718 for_each_possible_cpu(cpu)
4719 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07004720 put_online_cpus();
4721
4722 free_percpu(conf->percpu);
4723}
4724
NeilBrownd1688a62011-10-11 16:49:52 +11004725static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004726{
4727 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004728 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004729 kfree(conf->disks);
4730 kfree(conf->stripe_hashtbl);
4731 kfree(conf);
4732}
4733
Dan Williams36d1c642009-07-14 11:48:22 -07004734#ifdef CONFIG_HOTPLUG_CPU
4735static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4736 void *hcpu)
4737{
NeilBrownd1688a62011-10-11 16:49:52 +11004738 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004739 long cpu = (long)hcpu;
4740 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4741
4742 switch (action) {
4743 case CPU_UP_PREPARE:
4744 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304745 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07004746 pr_err("%s: failed memory allocation for cpu%ld\n",
4747 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004748 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004749 }
4750 break;
4751 case CPU_DEAD:
4752 case CPU_DEAD_FROZEN:
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304753 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07004754 break;
4755 default:
4756 break;
4757 }
4758 return NOTIFY_OK;
4759}
4760#endif
4761
NeilBrownd1688a62011-10-11 16:49:52 +11004762static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004763{
4764 unsigned long cpu;
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304765 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004766
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304767 conf->percpu = alloc_percpu(struct raid5_percpu);
4768 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07004769 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07004770
Dan Williams36d1c642009-07-14 11:48:22 -07004771#ifdef CONFIG_HOTPLUG_CPU
4772 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4773 conf->cpu_notify.priority = 0;
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304774 err = register_cpu_notifier(&conf->cpu_notify);
4775 if (err)
4776 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07004777#endif
Oleg Nesterovcd6f6b72014-02-06 03:42:45 +05304778
4779 get_online_cpus();
4780 for_each_present_cpu(cpu) {
4781 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
4782 if (err) {
4783 pr_err("%s: failed memory allocation for cpu%ld\n",
4784 __func__, cpu);
4785 break;
4786 }
4787 }
Dan Williams36d1c642009-07-14 11:48:22 -07004788 put_online_cpus();
4789
4790 return err;
4791}
4792
NeilBrownd1688a62011-10-11 16:49:52 +11004793static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
NeilBrownd1688a62011-10-11 16:49:52 +11004795 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004796 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004797 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
NeilBrown91adb562009-03-31 14:39:39 +11004800 if (mddev->new_level != 5
4801 && mddev->new_level != 4
4802 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004803 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004804 mdname(mddev), mddev->new_level);
4805 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
NeilBrown91adb562009-03-31 14:39:39 +11004807 if ((mddev->new_level == 5
4808 && !algorithm_valid_raid5(mddev->new_layout)) ||
4809 (mddev->new_level == 6
4810 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004811 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004812 mdname(mddev), mddev->new_layout);
4813 return ERR_PTR(-EIO);
4814 }
4815 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004816 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004817 mdname(mddev), mddev->raid_disks);
4818 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Andre Noll664e7c42009-06-18 08:45:27 +10004821 if (!mddev->new_chunk_sectors ||
4822 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4823 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004824 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4825 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004826 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004827 }
4828
NeilBrownd1688a62011-10-11 16:49:52 +11004829 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004830 if (conf == NULL)
4831 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004832 spin_lock_init(&conf->device_lock);
4833 init_waitqueue_head(&conf->wait_for_stripe);
4834 init_waitqueue_head(&conf->wait_for_overlap);
4835 INIT_LIST_HEAD(&conf->handle_list);
4836 INIT_LIST_HEAD(&conf->hold_list);
4837 INIT_LIST_HEAD(&conf->delayed_list);
4838 INIT_LIST_HEAD(&conf->bitmap_list);
4839 INIT_LIST_HEAD(&conf->inactive_list);
4840 atomic_set(&conf->active_stripes, 0);
4841 atomic_set(&conf->preread_active_stripes, 0);
4842 atomic_set(&conf->active_aligned_reads, 0);
4843 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004844 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004845
4846 conf->raid_disks = mddev->raid_disks;
4847 if (mddev->reshape_position == MaxSector)
4848 conf->previous_raid_disks = mddev->raid_disks;
4849 else
4850 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004851 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4852 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004853
NeilBrown5e5e3e72009-10-16 16:35:30 +11004854 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004855 GFP_KERNEL);
4856 if (!conf->disks)
4857 goto abort;
4858
4859 conf->mddev = mddev;
4860
4861 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4862 goto abort;
4863
Dan Williams36d1c642009-07-14 11:48:22 -07004864 conf->level = mddev->new_level;
4865 if (raid5_alloc_percpu(conf) != 0)
4866 goto abort;
4867
NeilBrown0c55e022010-05-03 14:09:02 +10004868 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004869
NeilBrowndafb20f2012-03-19 12:46:39 +11004870 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004871 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004872 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004873 || raid_disk < 0)
4874 continue;
4875 disk = conf->disks + raid_disk;
4876
NeilBrown17045f52011-12-23 10:17:53 +11004877 if (test_bit(Replacement, &rdev->flags)) {
4878 if (disk->replacement)
4879 goto abort;
4880 disk->replacement = rdev;
4881 } else {
4882 if (disk->rdev)
4883 goto abort;
4884 disk->rdev = rdev;
4885 }
NeilBrown91adb562009-03-31 14:39:39 +11004886
4887 if (test_bit(In_sync, &rdev->flags)) {
4888 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004889 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4890 " disk %d\n",
4891 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004892 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004893 /* Cannot rely on bitmap to complete recovery */
4894 conf->fullsync = 1;
4895 }
4896
Andre Noll09c9e5f2009-06-18 08:45:55 +10004897 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004898 conf->level = mddev->new_level;
4899 if (conf->level == 6)
4900 conf->max_degraded = 2;
4901 else
4902 conf->max_degraded = 1;
4903 conf->algorithm = mddev->new_layout;
4904 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004905 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004906 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004907 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004908 conf->prev_algo = mddev->layout;
4909 }
NeilBrown91adb562009-03-31 14:39:39 +11004910
4911 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004912 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004913 if (grow_stripes(conf, conf->max_nr_stripes)) {
4914 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004915 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4916 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004917 goto abort;
4918 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004919 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4920 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004921
NeilBrown0da3c612009-09-23 18:09:45 +10004922 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004923 if (!conf->thread) {
4924 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004925 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004926 mdname(mddev));
4927 goto abort;
4928 }
4929
4930 return conf;
4931
4932 abort:
4933 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004934 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004935 return ERR_PTR(-EIO);
4936 } else
4937 return ERR_PTR(-ENOMEM);
4938}
4939
NeilBrownc148ffd2009-11-13 17:47:00 +11004940
4941static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4942{
4943 switch (algo) {
4944 case ALGORITHM_PARITY_0:
4945 if (raid_disk < max_degraded)
4946 return 1;
4947 break;
4948 case ALGORITHM_PARITY_N:
4949 if (raid_disk >= raid_disks - max_degraded)
4950 return 1;
4951 break;
4952 case ALGORITHM_PARITY_0_6:
4953 if (raid_disk == 0 ||
4954 raid_disk == raid_disks - 1)
4955 return 1;
4956 break;
4957 case ALGORITHM_LEFT_ASYMMETRIC_6:
4958 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4959 case ALGORITHM_LEFT_SYMMETRIC_6:
4960 case ALGORITHM_RIGHT_SYMMETRIC_6:
4961 if (raid_disk == raid_disks - 1)
4962 return 1;
4963 }
4964 return 0;
4965}
4966
NeilBrownfd01b882011-10-11 16:47:53 +11004967static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004968{
NeilBrownd1688a62011-10-11 16:49:52 +11004969 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004970 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004971 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004972 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004973 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004974 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004975
Andre Noll8c6ac862009-06-18 08:48:06 +10004976 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004977 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004978 " -- starting background reconstruction\n",
4979 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004980 if (mddev->reshape_position != MaxSector) {
4981 /* Check that we can continue the reshape.
4982 * Currently only disks can change, it must
4983 * increase, and we must be past the point where
4984 * a stripe over-writes itself
4985 */
4986 sector_t here_new, here_old;
4987 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004988 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004989
NeilBrown88ce4932009-03-31 15:24:23 +11004990 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004991 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004992 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004993 mdname(mddev));
4994 return -EINVAL;
4995 }
NeilBrownf6705572006-03-27 01:18:11 -08004996 old_disks = mddev->raid_disks - mddev->delta_disks;
4997 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004998 * further up in new geometry must map after here in old
4999 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005000 */
5001 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005002 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005003 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005004 printk(KERN_ERR "md/raid:%s: reshape_position not "
5005 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005006 return -EINVAL;
5007 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005008 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005009 /* here_new is the stripe we will write to */
5010 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005011 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005012 (old_disks-max_degraded));
5013 /* here_old is the first stripe that we might need to read
5014 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005015 if (mddev->delta_disks == 0) {
5016 /* We cannot be sure it is safe to start an in-place
5017 * reshape. It is only safe if user-space if monitoring
5018 * and taking constant backups.
5019 * mdadm always starts a situation like this in
5020 * readonly mode so it can take control before
5021 * allowing any writes. So just check for that.
5022 */
5023 if ((here_new * mddev->new_chunk_sectors !=
5024 here_old * mddev->chunk_sectors) ||
5025 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005026 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5027 " in read-only mode - aborting\n",
5028 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005029 return -EINVAL;
5030 }
5031 } else if (mddev->delta_disks < 0
5032 ? (here_new * mddev->new_chunk_sectors <=
5033 here_old * mddev->chunk_sectors)
5034 : (here_new * mddev->new_chunk_sectors >=
5035 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005036 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005037 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5038 "auto-recovery - aborting.\n",
5039 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005040 return -EINVAL;
5041 }
NeilBrown0c55e022010-05-03 14:09:02 +10005042 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5043 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005044 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005045 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005046 BUG_ON(mddev->level != mddev->new_level);
5047 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005048 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005049 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005050 }
5051
NeilBrown245f46c2009-03-31 14:39:39 +11005052 if (mddev->private == NULL)
5053 conf = setup_conf(mddev);
5054 else
5055 conf = mddev->private;
5056
NeilBrown91adb562009-03-31 14:39:39 +11005057 if (IS_ERR(conf))
5058 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005059
NeilBrown91adb562009-03-31 14:39:39 +11005060 mddev->thread = conf->thread;
5061 conf->thread = NULL;
5062 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
NeilBrown17045f52011-12-23 10:17:53 +11005064 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5065 i++) {
5066 rdev = conf->disks[i].rdev;
5067 if (!rdev && conf->disks[i].replacement) {
5068 /* The replacement is all we have yet */
5069 rdev = conf->disks[i].replacement;
5070 conf->disks[i].replacement = NULL;
5071 clear_bit(Replacement, &rdev->flags);
5072 conf->disks[i].rdev = rdev;
5073 }
5074 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005075 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005076 if (conf->disks[i].replacement &&
5077 conf->reshape_progress != MaxSector) {
5078 /* replacements and reshape simply do not mix. */
5079 printk(KERN_ERR "md: cannot handle concurrent "
5080 "replacement and reshape.\n");
5081 goto abort;
5082 }
NeilBrown2f115882010-06-17 17:41:03 +10005083 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005084 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005085 continue;
5086 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005087 /* This disc is not fully in-sync. However if it
5088 * just stored parity (beyond the recovery_offset),
5089 * when we don't need to be concerned about the
5090 * array being dirty.
5091 * When reshape goes 'backwards', we never have
5092 * partially completed devices, so we only need
5093 * to worry about reshape going forwards.
5094 */
5095 /* Hack because v0.91 doesn't store recovery_offset properly. */
5096 if (mddev->major_version == 0 &&
5097 mddev->minor_version > 90)
5098 rdev->recovery_offset = reshape_offset;
5099
NeilBrownc148ffd2009-11-13 17:47:00 +11005100 if (rdev->recovery_offset < reshape_offset) {
5101 /* We need to check old and new layout */
5102 if (!only_parity(rdev->raid_disk,
5103 conf->algorithm,
5104 conf->raid_disks,
5105 conf->max_degraded))
5106 continue;
5107 }
5108 if (!only_parity(rdev->raid_disk,
5109 conf->prev_algo,
5110 conf->previous_raid_disks,
5111 conf->max_degraded))
5112 continue;
5113 dirty_parity_disks++;
5114 }
NeilBrown91adb562009-03-31 14:39:39 +11005115
NeilBrown17045f52011-12-23 10:17:53 +11005116 /*
5117 * 0 for a fully functional array, 1 or 2 for a degraded array.
5118 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005119 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
NeilBrown674806d2010-06-16 17:17:53 +10005121 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005122 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005124 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 goto abort;
5126 }
5127
NeilBrown91adb562009-03-31 14:39:39 +11005128 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005129 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005130 mddev->resync_max_sectors = mddev->dev_sectors;
5131
NeilBrownc148ffd2009-11-13 17:47:00 +11005132 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005134 if (mddev->ok_start_degraded)
5135 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005136 "md/raid:%s: starting dirty degraded array"
5137 " - data corruption possible.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005138 mdname(mddev));
5139 else {
5140 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005141 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005142 mdname(mddev));
5143 goto abort;
5144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 }
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005148 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5149 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005150 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5151 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 else
NeilBrown0c55e022010-05-03 14:09:02 +10005153 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5154 " out of %d devices, algorithm %d\n",
5155 mdname(mddev), conf->level,
5156 mddev->raid_disks - mddev->degraded,
5157 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
5159 print_raid5_conf(conf);
5160
NeilBrownfef9c612009-03-31 15:16:46 +11005161 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005162 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005163 atomic_set(&conf->reshape_stripes, 0);
5164 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5165 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5166 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5167 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5168 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005169 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005170 }
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
5173 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005174 if (mddev->to_remove == &raid5_attrs_group)
5175 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005176 else if (mddev->kobj.sd &&
5177 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005178 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005179 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005180 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005181 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5182
5183 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005184 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005185 /* read-ahead size must cover two whole stripes, which
5186 * is 2 * (datadisks) * chunksize where 'n' is the
5187 * number of raid devices
5188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5190 int stripe = data_disks *
5191 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5192 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5193 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005194
5195 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005196
5197 mddev->queue->backing_dev_info.congested_data = mddev;
5198 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005199
5200 chunk_size = mddev->chunk_sectors << 9;
5201 blk_queue_io_min(mddev->queue, chunk_size);
5202 blk_queue_io_opt(mddev->queue, chunk_size *
5203 (conf->raid_disks - conf->max_degraded));
5204
NeilBrowndafb20f2012-03-19 12:46:39 +11005205 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005206 disk_stack_limits(mddev->gendisk, rdev->bdev,
5207 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return 0;
5211abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005212 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005213 print_raid5_conf(conf);
5214 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005216 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 return -EIO;
5218}
5219
NeilBrownfd01b882011-10-11 16:47:53 +11005220static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
NeilBrownd1688a62011-10-11 16:49:52 +11005222 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
NeilBrown01f96c02011-09-21 15:30:20 +10005224 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005225 if (mddev->queue)
5226 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005227 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005228 mddev->private = NULL;
5229 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return 0;
5231}
5232
NeilBrownfd01b882011-10-11 16:47:53 +11005233static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
NeilBrownd1688a62011-10-11 16:49:52 +11005235 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 int i;
5237
Andre Noll9d8f0362009-06-18 08:45:01 +10005238 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5239 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005240 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 for (i = 0; i < conf->raid_disks; i++)
5242 seq_printf (seq, "%s",
5243 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005244 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246}
5247
NeilBrownd1688a62011-10-11 16:49:52 +11005248static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
5250 int i;
5251 struct disk_info *tmp;
5252
NeilBrown0c55e022010-05-03 14:09:02 +10005253 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 if (!conf) {
5255 printk("(conf==NULL)\n");
5256 return;
5257 }
NeilBrown0c55e022010-05-03 14:09:02 +10005258 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5259 conf->raid_disks,
5260 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
5262 for (i = 0; i < conf->raid_disks; i++) {
5263 char b[BDEVNAME_SIZE];
5264 tmp = conf->disks + i;
5265 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005266 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5267 i, !test_bit(Faulty, &tmp->rdev->flags),
5268 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 }
5270}
5271
NeilBrownfd01b882011-10-11 16:47:53 +11005272static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273{
5274 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005275 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005277 int count = 0;
5278 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
5280 for (i = 0; i < conf->raid_disks; i++) {
5281 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005282 if (tmp->replacement
5283 && tmp->replacement->recovery_offset == MaxSector
5284 && !test_bit(Faulty, &tmp->replacement->flags)
5285 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5286 /* Replacement has just become active. */
5287 if (!tmp->rdev
5288 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5289 count++;
5290 if (tmp->rdev) {
5291 /* Replaced device not technically faulty,
5292 * but we need to be sure it gets removed
5293 * and never re-added.
5294 */
5295 set_bit(Faulty, &tmp->rdev->flags);
5296 sysfs_notify_dirent_safe(
5297 tmp->rdev->sysfs_state);
5298 }
5299 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5300 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005301 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005302 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005303 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005304 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005305 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
5307 }
NeilBrown6b965622010-08-18 11:56:59 +10005308 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005309 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005310 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005312 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
NeilBrownb8321b62011-12-23 10:17:51 +11005315static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
NeilBrownd1688a62011-10-11 16:49:52 +11005317 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005319 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005320 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 struct disk_info *p = conf->disks + number;
5322
5323 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005324 if (rdev == p->rdev)
5325 rdevp = &p->rdev;
5326 else if (rdev == p->replacement)
5327 rdevp = &p->replacement;
5328 else
5329 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005330
NeilBrown657e3e42011-12-23 10:17:52 +11005331 if (number >= conf->raid_disks &&
5332 conf->reshape_progress == MaxSector)
5333 clear_bit(In_sync, &rdev->flags);
5334
5335 if (test_bit(In_sync, &rdev->flags) ||
5336 atomic_read(&rdev->nr_pending)) {
5337 err = -EBUSY;
5338 goto abort;
5339 }
5340 /* Only remove non-faulty devices if recovery
5341 * isn't possible.
5342 */
5343 if (!test_bit(Faulty, &rdev->flags) &&
5344 mddev->recovery_disabled != conf->recovery_disabled &&
5345 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005346 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005347 number < conf->raid_disks) {
5348 err = -EBUSY;
5349 goto abort;
5350 }
5351 *rdevp = NULL;
5352 synchronize_rcu();
5353 if (atomic_read(&rdev->nr_pending)) {
5354 /* lost the race, try later */
5355 err = -EBUSY;
5356 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005357 } else if (p->replacement) {
5358 /* We must have just cleared 'rdev' */
5359 p->rdev = p->replacement;
5360 clear_bit(Replacement, &p->replacement->flags);
5361 smp_mb(); /* Make sure other CPUs may see both as identical
5362 * but will never see neither - if they are careful
5363 */
5364 p->replacement = NULL;
5365 clear_bit(WantReplacement, &rdev->flags);
5366 } else
5367 /* We might have just removed the Replacement as faulty-
5368 * clear the bit just in case
5369 */
5370 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371abort:
5372
5373 print_raid5_conf(conf);
5374 return err;
5375}
5376
NeilBrownfd01b882011-10-11 16:47:53 +11005377static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
NeilBrownd1688a62011-10-11 16:49:52 +11005379 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005380 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 int disk;
5382 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005383 int first = 0;
5384 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
NeilBrown7f0da592011-07-28 11:39:22 +10005386 if (mddev->recovery_disabled == conf->recovery_disabled)
5387 return -EBUSY;
5388
NeilBrowndc10c642012-03-19 12:46:37 +11005389 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Neil Brown6c2fce22008-06-28 08:31:31 +10005393 if (rdev->raid_disk >= 0)
5394 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
5396 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005397 * find the disk ... but prefer rdev->saved_raid_disk
5398 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005400 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005401 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005402 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5403 disk = rdev->saved_raid_disk;
5404 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005405 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005406 for ( ; disk <= last ; disk++) {
5407 p = conf->disks + disk;
5408 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005409 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005411 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005412 if (rdev->saved_raid_disk != disk)
5413 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005414 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 break;
5416 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005417 if (test_bit(WantReplacement, &p->rdev->flags) &&
5418 p->replacement == NULL) {
5419 clear_bit(In_sync, &rdev->flags);
5420 set_bit(Replacement, &rdev->flags);
5421 rdev->raid_disk = disk;
5422 err = 0;
5423 conf->fullsync = 1;
5424 rcu_assign_pointer(p->replacement, rdev);
5425 break;
5426 }
5427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430}
5431
NeilBrownfd01b882011-10-11 16:47:53 +11005432static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
5434 /* no resync is happening, and there is enough space
5435 * on all devices, so we can resize.
5436 * We need to make sure resync covers any new space.
5437 * If the array is shrinking we should possibly wait until
5438 * any io in the removed space completes, but it hardly seems
5439 * worth it.
5440 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005441 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005442 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5443 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005444 if (mddev->array_sectors >
5445 raid5_size(mddev, sectors, mddev->raid_disks))
5446 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005447 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005448 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005449 if (sectors > mddev->dev_sectors &&
5450 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005451 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5453 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005454 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005455 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 return 0;
5457}
5458
NeilBrownfd01b882011-10-11 16:47:53 +11005459static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005460{
5461 /* Can only proceed if there are plenty of stripe_heads.
5462 * We need a minimum of one full stripe,, and for sensible progress
5463 * it is best to have about 4 times that.
5464 * If we require 4 times, then the default 256 4K stripe_heads will
5465 * allow for chunk sizes up to 256K, which is probably OK.
5466 * If the chunk size is greater, user-space should request more
5467 * stripe_heads first.
5468 */
NeilBrownd1688a62011-10-11 16:49:52 +11005469 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005470 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5471 > conf->max_nr_stripes ||
5472 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5473 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005474 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5475 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005476 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5477 / STRIPE_SIZE)*4);
5478 return 0;
5479 }
5480 return 1;
5481}
5482
NeilBrownfd01b882011-10-11 16:47:53 +11005483static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005484{
NeilBrownd1688a62011-10-11 16:49:52 +11005485 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005486
NeilBrown88ce4932009-03-31 15:24:23 +11005487 if (mddev->delta_disks == 0 &&
5488 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005489 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005490 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005491 if (mddev->bitmap)
5492 /* Cannot grow a bitmap yet */
5493 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005494 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005495 return -EINVAL;
5496 if (mddev->delta_disks < 0) {
5497 /* We might be able to shrink, but the devices must
5498 * be made bigger first.
5499 * For raid6, 4 is the minimum size.
5500 * Otherwise 2 is the minimum
5501 */
5502 int min = 2;
5503 if (mddev->level == 6)
5504 min = 4;
5505 if (mddev->raid_disks + mddev->delta_disks < min)
5506 return -EINVAL;
5507 }
NeilBrown29269552006-03-27 01:18:10 -08005508
NeilBrown01ee22b2009-06-18 08:47:20 +10005509 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005510 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005511
NeilBrownec32a2b2009-03-31 15:17:38 +11005512 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005513}
5514
NeilBrownfd01b882011-10-11 16:47:53 +11005515static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005516{
NeilBrownd1688a62011-10-11 16:49:52 +11005517 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005518 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005519 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005520 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005521
NeilBrownf4168852007-02-28 20:11:53 -08005522 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005523 return -EBUSY;
5524
NeilBrown01ee22b2009-06-18 08:47:20 +10005525 if (!check_stripe_cache(mddev))
5526 return -ENOSPC;
5527
NeilBrowndafb20f2012-03-19 12:46:39 +11005528 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005529 if (!test_bit(In_sync, &rdev->flags)
5530 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005531 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005532
NeilBrownf4168852007-02-28 20:11:53 -08005533 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005534 /* Not enough devices even to make a degraded array
5535 * of that size
5536 */
5537 return -EINVAL;
5538
NeilBrownec32a2b2009-03-31 15:17:38 +11005539 /* Refuse to reduce size of the array. Any reductions in
5540 * array size must be through explicit setting of array_size
5541 * attribute.
5542 */
5543 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5544 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005545 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005546 "before number of disks\n", mdname(mddev));
5547 return -EINVAL;
5548 }
5549
NeilBrownf6705572006-03-27 01:18:11 -08005550 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005551 spin_lock_irq(&conf->device_lock);
5552 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005553 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005554 conf->prev_chunk_sectors = conf->chunk_sectors;
5555 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005556 conf->prev_algo = conf->algorithm;
5557 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005558 if (mddev->delta_disks < 0)
5559 conf->reshape_progress = raid5_size(mddev, 0, 0);
5560 else
5561 conf->reshape_progress = 0;
5562 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005563 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005564 spin_unlock_irq(&conf->device_lock);
5565
5566 /* Add some new drives, as many as will fit.
5567 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005568 * Don't add devices if we are reducing the number of
5569 * devices in the array. This is because it is not possible
5570 * to correctly record the "partially reconstructed" state of
5571 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005572 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005573 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005574 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005575 if (rdev->raid_disk < 0 &&
5576 !test_bit(Faulty, &rdev->flags)) {
5577 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005578 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005579 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005580 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005581 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005582 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005583
5584 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005585 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005586 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005587 } else if (rdev->raid_disk >= conf->previous_raid_disks
5588 && !test_bit(Faulty, &rdev->flags)) {
5589 /* This is a spare that was manually added */
5590 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005591 }
NeilBrown29269552006-03-27 01:18:10 -08005592
NeilBrown87a8dec2011-01-31 11:57:43 +11005593 /* When a reshape changes the number of devices,
5594 * ->degraded is measured against the larger of the
5595 * pre and post number of devices.
5596 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005597 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005598 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005599 spin_unlock_irqrestore(&conf->device_lock, flags);
5600 }
NeilBrown63c70c42006-03-27 01:18:13 -08005601 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005602 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005603 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005604
NeilBrown29269552006-03-27 01:18:10 -08005605 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5606 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5607 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5608 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5609 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005610 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005611 if (!mddev->sync_thread) {
5612 mddev->recovery = 0;
5613 spin_lock_irq(&conf->device_lock);
5614 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005615 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005616 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005617 spin_unlock_irq(&conf->device_lock);
5618 return -EAGAIN;
5619 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005620 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005621 md_wakeup_thread(mddev->sync_thread);
5622 md_new_event(mddev);
5623 return 0;
5624}
NeilBrown29269552006-03-27 01:18:10 -08005625
NeilBrownec32a2b2009-03-31 15:17:38 +11005626/* This is called from the reshape thread and should make any
5627 * changes needed in 'conf'
5628 */
NeilBrownd1688a62011-10-11 16:49:52 +11005629static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005630{
NeilBrown29269552006-03-27 01:18:10 -08005631
NeilBrownf6705572006-03-27 01:18:11 -08005632 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005633
NeilBrownf6705572006-03-27 01:18:11 -08005634 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005635 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005636 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005637 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005638 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005639
5640 /* read-ahead size must cover two whole stripes, which is
5641 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5642 */
NeilBrown4a5add42010-06-01 19:37:28 +10005643 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005644 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005645 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005646 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005647 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5648 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5649 }
NeilBrown29269552006-03-27 01:18:10 -08005650 }
NeilBrown29269552006-03-27 01:18:10 -08005651}
5652
NeilBrownec32a2b2009-03-31 15:17:38 +11005653/* This is called from the raid5d thread with mddev_lock held.
5654 * It makes config changes to the device.
5655 */
NeilBrownfd01b882011-10-11 16:47:53 +11005656static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005657{
NeilBrownd1688a62011-10-11 16:49:52 +11005658 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005659
5660 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5661
NeilBrownec32a2b2009-03-31 15:17:38 +11005662 if (mddev->delta_disks > 0) {
5663 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5664 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005665 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005666 } else {
5667 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005668 spin_lock_irq(&conf->device_lock);
5669 mddev->degraded = calc_degraded(conf);
5670 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005671 for (d = conf->raid_disks ;
5672 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005673 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005674 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005675 if (rdev &&
5676 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005677 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005678 rdev->raid_disk = -1;
5679 }
5680 }
NeilBrowncea9c222009-03-31 15:15:05 +11005681 }
NeilBrown88ce4932009-03-31 15:24:23 +11005682 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005683 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005684 mddev->reshape_position = MaxSector;
5685 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005686 }
5687}
5688
NeilBrownfd01b882011-10-11 16:47:53 +11005689static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005690{
NeilBrownd1688a62011-10-11 16:49:52 +11005691 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005692
5693 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005694 case 2: /* resume for a suspend */
5695 wake_up(&conf->wait_for_overlap);
5696 break;
5697
NeilBrown72626682005-09-09 16:23:54 -07005698 case 1: /* stop all writes */
5699 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005700 /* '2' tells resync/reshape to pause so that all
5701 * active stripes can drain
5702 */
5703 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005704 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005705 atomic_read(&conf->active_stripes) == 0 &&
5706 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005707 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005708 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005709 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005710 /* allow reshape to continue */
5711 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005712 break;
5713
5714 case 0: /* re-enable writes */
5715 spin_lock_irq(&conf->device_lock);
5716 conf->quiesce = 0;
5717 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005718 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005719 spin_unlock_irq(&conf->device_lock);
5720 break;
5721 }
NeilBrown72626682005-09-09 16:23:54 -07005722}
NeilBrownb15c2e52006-01-06 00:20:16 -08005723
NeilBrownd562b0c2009-03-31 14:39:39 +11005724
NeilBrownfd01b882011-10-11 16:47:53 +11005725static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005726{
NeilBrowne373ab12011-10-11 16:48:59 +11005727 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005728 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005729
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005730 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005731 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005732 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5733 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005734 return ERR_PTR(-EINVAL);
5735 }
5736
NeilBrowne373ab12011-10-11 16:48:59 +11005737 sectors = raid0_conf->strip_zone[0].zone_end;
5738 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005739 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005740 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005741 mddev->new_layout = ALGORITHM_PARITY_N;
5742 mddev->new_chunk_sectors = mddev->chunk_sectors;
5743 mddev->raid_disks += 1;
5744 mddev->delta_disks = 1;
5745 /* make sure it will be not marked as dirty */
5746 mddev->recovery_cp = MaxSector;
5747
5748 return setup_conf(mddev);
5749}
5750
5751
NeilBrownfd01b882011-10-11 16:47:53 +11005752static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005753{
5754 int chunksect;
5755
5756 if (mddev->raid_disks != 2 ||
5757 mddev->degraded > 1)
5758 return ERR_PTR(-EINVAL);
5759
5760 /* Should check if there are write-behind devices? */
5761
5762 chunksect = 64*2; /* 64K by default */
5763
5764 /* The array must be an exact multiple of chunksize */
5765 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5766 chunksect >>= 1;
5767
5768 if ((chunksect<<9) < STRIPE_SIZE)
5769 /* array size does not allow a suitable chunk size */
5770 return ERR_PTR(-EINVAL);
5771
5772 mddev->new_level = 5;
5773 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005774 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005775
5776 return setup_conf(mddev);
5777}
5778
NeilBrownfd01b882011-10-11 16:47:53 +11005779static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005780{
5781 int new_layout;
5782
5783 switch (mddev->layout) {
5784 case ALGORITHM_LEFT_ASYMMETRIC_6:
5785 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5786 break;
5787 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5788 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5789 break;
5790 case ALGORITHM_LEFT_SYMMETRIC_6:
5791 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5792 break;
5793 case ALGORITHM_RIGHT_SYMMETRIC_6:
5794 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5795 break;
5796 case ALGORITHM_PARITY_0_6:
5797 new_layout = ALGORITHM_PARITY_0;
5798 break;
5799 case ALGORITHM_PARITY_N:
5800 new_layout = ALGORITHM_PARITY_N;
5801 break;
5802 default:
5803 return ERR_PTR(-EINVAL);
5804 }
5805 mddev->new_level = 5;
5806 mddev->new_layout = new_layout;
5807 mddev->delta_disks = -1;
5808 mddev->raid_disks -= 1;
5809 return setup_conf(mddev);
5810}
5811
NeilBrownd562b0c2009-03-31 14:39:39 +11005812
NeilBrownfd01b882011-10-11 16:47:53 +11005813static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005814{
NeilBrown88ce4932009-03-31 15:24:23 +11005815 /* For a 2-drive array, the layout and chunk size can be changed
5816 * immediately as not restriping is needed.
5817 * For larger arrays we record the new value - after validation
5818 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005819 */
NeilBrownd1688a62011-10-11 16:49:52 +11005820 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005821 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005822
NeilBrown597a7112009-06-18 08:47:42 +10005823 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005824 return -EINVAL;
5825 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005826 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005827 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005828 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005829 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005830 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005831 /* not factor of array size */
5832 return -EINVAL;
5833 }
5834
5835 /* They look valid */
5836
NeilBrown88ce4932009-03-31 15:24:23 +11005837 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005838 /* can make the change immediately */
5839 if (mddev->new_layout >= 0) {
5840 conf->algorithm = mddev->new_layout;
5841 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005842 }
5843 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005844 conf->chunk_sectors = new_chunk ;
5845 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005846 }
5847 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5848 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005849 }
NeilBrown50ac1682009-06-18 08:47:55 +10005850 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005851}
5852
NeilBrownfd01b882011-10-11 16:47:53 +11005853static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005854{
NeilBrown597a7112009-06-18 08:47:42 +10005855 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005856
NeilBrown597a7112009-06-18 08:47:42 +10005857 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005858 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005859 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005860 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005861 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005862 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005863 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005864 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005865 /* not factor of array size */
5866 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005867 }
NeilBrown88ce4932009-03-31 15:24:23 +11005868
5869 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005870 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005871}
5872
NeilBrownfd01b882011-10-11 16:47:53 +11005873static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005874{
5875 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005876 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005877 * raid1 - if there are two drives. We need to know the chunk size
5878 * raid4 - trivial - just use a raid4 layout.
5879 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005880 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005881 if (mddev->level == 0)
5882 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005883 if (mddev->level == 1)
5884 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005885 if (mddev->level == 4) {
5886 mddev->new_layout = ALGORITHM_PARITY_N;
5887 mddev->new_level = 5;
5888 return setup_conf(mddev);
5889 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005890 if (mddev->level == 6)
5891 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005892
5893 return ERR_PTR(-EINVAL);
5894}
5895
NeilBrownfd01b882011-10-11 16:47:53 +11005896static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005897{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005898 /* raid4 can take over:
5899 * raid0 - if there is only one strip zone
5900 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005901 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005902 if (mddev->level == 0)
5903 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005904 if (mddev->level == 5 &&
5905 mddev->layout == ALGORITHM_PARITY_N) {
5906 mddev->new_layout = 0;
5907 mddev->new_level = 4;
5908 return setup_conf(mddev);
5909 }
5910 return ERR_PTR(-EINVAL);
5911}
NeilBrownd562b0c2009-03-31 14:39:39 +11005912
NeilBrown84fc4b52011-10-11 16:49:58 +11005913static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005914
NeilBrownfd01b882011-10-11 16:47:53 +11005915static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005916{
5917 /* Currently can only take over a raid5. We map the
5918 * personality to an equivalent raid6 personality
5919 * with the Q block at the end.
5920 */
5921 int new_layout;
5922
5923 if (mddev->pers != &raid5_personality)
5924 return ERR_PTR(-EINVAL);
5925 if (mddev->degraded > 1)
5926 return ERR_PTR(-EINVAL);
5927 if (mddev->raid_disks > 253)
5928 return ERR_PTR(-EINVAL);
5929 if (mddev->raid_disks < 3)
5930 return ERR_PTR(-EINVAL);
5931
5932 switch (mddev->layout) {
5933 case ALGORITHM_LEFT_ASYMMETRIC:
5934 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5935 break;
5936 case ALGORITHM_RIGHT_ASYMMETRIC:
5937 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5938 break;
5939 case ALGORITHM_LEFT_SYMMETRIC:
5940 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5941 break;
5942 case ALGORITHM_RIGHT_SYMMETRIC:
5943 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5944 break;
5945 case ALGORITHM_PARITY_0:
5946 new_layout = ALGORITHM_PARITY_0_6;
5947 break;
5948 case ALGORITHM_PARITY_N:
5949 new_layout = ALGORITHM_PARITY_N;
5950 break;
5951 default:
5952 return ERR_PTR(-EINVAL);
5953 }
5954 mddev->new_level = 6;
5955 mddev->new_layout = new_layout;
5956 mddev->delta_disks = 1;
5957 mddev->raid_disks += 1;
5958 return setup_conf(mddev);
5959}
5960
5961
NeilBrown84fc4b52011-10-11 16:49:58 +11005962static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005963{
5964 .name = "raid6",
5965 .level = 6,
5966 .owner = THIS_MODULE,
5967 .make_request = make_request,
5968 .run = run,
5969 .stop = stop,
5970 .status = status,
5971 .error_handler = error,
5972 .hot_add_disk = raid5_add_disk,
5973 .hot_remove_disk= raid5_remove_disk,
5974 .spare_active = raid5_spare_active,
5975 .sync_request = sync_request,
5976 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005977 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005978 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005979 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005980 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005981 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005982 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005983};
NeilBrown84fc4b52011-10-11 16:49:58 +11005984static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985{
5986 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005987 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 .owner = THIS_MODULE,
5989 .make_request = make_request,
5990 .run = run,
5991 .stop = stop,
5992 .status = status,
5993 .error_handler = error,
5994 .hot_add_disk = raid5_add_disk,
5995 .hot_remove_disk= raid5_remove_disk,
5996 .spare_active = raid5_spare_active,
5997 .sync_request = sync_request,
5998 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005999 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006000 .check_reshape = raid5_check_reshape,
6001 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006002 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006003 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006004 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005};
6006
NeilBrown84fc4b52011-10-11 16:49:58 +11006007static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008{
NeilBrown2604b702006-01-06 00:20:36 -08006009 .name = "raid4",
6010 .level = 4,
6011 .owner = THIS_MODULE,
6012 .make_request = make_request,
6013 .run = run,
6014 .stop = stop,
6015 .status = status,
6016 .error_handler = error,
6017 .hot_add_disk = raid5_add_disk,
6018 .hot_remove_disk= raid5_remove_disk,
6019 .spare_active = raid5_spare_active,
6020 .sync_request = sync_request,
6021 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006022 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006023 .check_reshape = raid5_check_reshape,
6024 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006025 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006026 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006027 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006028};
6029
6030static int __init raid5_init(void)
6031{
NeilBrown16a53ec2006-06-26 00:27:38 -07006032 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006033 register_md_personality(&raid5_personality);
6034 register_md_personality(&raid4_personality);
6035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036}
6037
NeilBrown2604b702006-01-06 00:20:36 -08006038static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
NeilBrown16a53ec2006-06-26 00:27:38 -07006040 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006041 unregister_md_personality(&raid5_personality);
6042 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043}
6044
6045module_init(raid5_init);
6046module_exit(raid5_exit);
6047MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006048MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006050MODULE_ALIAS("md-raid5");
6051MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006052MODULE_ALIAS("md-level-5");
6053MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006054MODULE_ALIAS("md-personality-8"); /* RAID6 */
6055MODULE_ALIAS("md-raid6");
6056MODULE_ALIAS("md-level-6");
6057
6058/* This used to be two separate modules, they were: */
6059MODULE_ALIAS("raid5");
6060MODULE_ALIAS("raid6");