blob: 73a58007eb9994fdeae34604b48c1d61767309a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Namhyung Kim9b2dc8b2011-06-13 14:48:22 +0900129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
Dan Williams66295422009-10-19 18:09:32 -0700158 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrowne4424fe2009-10-16 16:27:34 +1100160 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700161 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 return slot;
169}
170
Dan Williamsa4456852007-07-09 11:56:43 -0700171static void return_io(struct bio *return_bi)
172{
173 struct bio *bi = return_bi;
174 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700175
176 return_bi = bi->bi_next;
177 bi->bi_next = NULL;
178 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000179 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700180 bi = return_bi;
181 }
182}
183
NeilBrownd1688a62011-10-11 16:49:52 +1100184static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dan Williams600aa102008-06-28 08:32:05 +1000186static int stripe_operations_active(struct stripe_head *sh)
187{
188 return sh->check_state || sh->reconstruct_state ||
189 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
190 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
191}
192
NeilBrownd1688a62011-10-11 16:49:52 +1100193static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200196 BUG_ON(!list_empty(&sh->lru));
197 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (test_bit(STRIPE_HANDLE, &sh->state)) {
Shaohua Li5bbbd742012-07-03 15:57:19 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state) &&
200 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000202 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
203 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700204 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000205 else {
Shaohua Li5bbbd742012-07-03 15:57:19 +1000206 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrown72626682005-09-09 16:23:54 -0700207 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 md_wakeup_thread(conf->mddev->thread);
211 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000212 BUG_ON(stripe_operations_active(sh));
majianpeng41fe75f2012-03-13 11:21:25 +1100213 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
214 if (atomic_dec_return(&conf->preread_active_stripes)
215 < IO_THRESHOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800218 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
219 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800221 if (conf->retry_read_aligned)
222 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 }
226}
NeilBrownd0dabf72009-03-31 14:39:38 +1100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static void release_stripe(struct stripe_head *sh)
229{
NeilBrownd1688a62011-10-11 16:49:52 +1100230 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 spin_lock_irqsave(&conf->device_lock, flags);
234 __release_stripe(conf, sh);
235 spin_unlock_irqrestore(&conf->device_lock, flags);
236}
237
NeilBrownfccddba2006-01-06 00:20:33 -0800238static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Dan Williams45b42332007-07-09 11:56:43 -0700240 pr_debug("remove_hash(), stripe %llu\n",
241 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
NeilBrownfccddba2006-01-06 00:20:33 -0800243 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
NeilBrownd1688a62011-10-11 16:49:52 +1100246static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
NeilBrownfccddba2006-01-06 00:20:33 -0800248 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Dan Williams45b42332007-07-09 11:56:43 -0700250 pr_debug("insert_hash(), stripe %llu\n",
251 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
NeilBrownfccddba2006-01-06 00:20:33 -0800253 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100258static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct stripe_head *sh = NULL;
261 struct list_head *first;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (list_empty(&conf->inactive_list))
264 goto out;
265 first = conf->inactive_list.next;
266 sh = list_entry(first, struct stripe_head, lru);
267 list_del_init(first);
268 remove_hash(sh);
269 atomic_inc(&conf->active_stripes);
270out:
271 return sh;
272}
273
NeilBrowne4e11e32010-06-16 16:45:16 +1000274static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct page *p;
277 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000278 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
NeilBrowne4e11e32010-06-16 16:45:16 +1000280 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 p = sh->dev[i].page;
282 if (!p)
283 continue;
284 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800285 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
NeilBrowne4e11e32010-06-16 16:45:16 +1000289static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000292 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
NeilBrowne4e11e32010-06-16 16:45:16 +1000294 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct page *page;
296
297 if (!(page = alloc_page(GFP_KERNEL))) {
298 return 1;
299 }
300 sh->dev[i].page = page;
301 }
302 return 0;
303}
304
NeilBrown784052e2009-03-31 15:19:07 +1100305static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100306static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100307 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
NeilBrownb5663ba2009-03-31 14:39:38 +1100309static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
NeilBrownd1688a62011-10-11 16:49:52 +1100311 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800312 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200314 BUG_ON(atomic_read(&sh->count) != 0);
315 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000316 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700317
Dan Williams45b42332007-07-09 11:56:43 -0700318 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 (unsigned long long)sh->sector);
320
321 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700322
NeilBrown86b42c72009-03-31 15:19:03 +1100323 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100324 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100326 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sh->state = 0;
328
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329
330 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct r5dev *dev = &sh->dev[i];
332
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000339 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100342 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 insert_hash(conf, sh);
345}
346
NeilBrownd1688a62011-10-11 16:49:52 +1100347static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100348 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800351 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Dan Williams45b42332007-07-09 11:56:43 -0700353 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800354 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100355 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700357 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return NULL;
359}
360
NeilBrown674806d2010-06-16 17:17:53 +1000361/*
362 * Need to check if array has failed when deciding whether to:
363 * - start an array
364 * - remove non-faulty devices
365 * - add a spare
366 * - allow a reshape
367 * This determination is simple when no reshape is happening.
368 * However if there is a reshape, we need to carefully check
369 * both the before and after sections.
370 * This is because some failed devices may only affect one
371 * of the two sections, and some non-in_sync devices may
372 * be insync in the section most affected by failed devices.
373 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100374static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000375{
NeilBrown908f4fb2011-12-23 10:17:50 +1100376 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000377 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000378
379 rcu_read_lock();
380 degraded = 0;
381 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100382 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000383 if (!rdev || test_bit(Faulty, &rdev->flags))
384 degraded++;
385 else if (test_bit(In_sync, &rdev->flags))
386 ;
387 else
388 /* not in-sync or faulty.
389 * If the reshape increases the number of devices,
390 * this is being recovered by the reshape, so
391 * this 'previous' section is not in_sync.
392 * If the number of devices is being reduced however,
393 * the device can only be part of the array if
394 * we are reverting a reshape, so this section will
395 * be in-sync.
396 */
397 if (conf->raid_disks >= conf->previous_raid_disks)
398 degraded++;
399 }
400 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100401 if (conf->raid_disks == conf->previous_raid_disks)
402 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000403 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100404 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000405 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100406 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000407 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100408 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000409 else if (test_bit(In_sync, &rdev->flags))
410 ;
411 else
412 /* not in-sync or faulty.
413 * If reshape increases the number of devices, this
414 * section has already been recovered, else it
415 * almost certainly hasn't.
416 */
417 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100418 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000419 }
420 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 if (degraded2 > degraded)
422 return degraded2;
423 return degraded;
424}
425
426static int has_failed(struct r5conf *conf)
427{
428 int degraded;
429
430 if (conf->mddev->reshape_position == MaxSector)
431 return conf->mddev->degraded > conf->max_degraded;
432
433 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000434 if (degraded > conf->max_degraded)
435 return 1;
436 return 0;
437}
438
NeilBrownb5663ba2009-03-31 14:39:38 +1100439static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100440get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000441 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct stripe_head *sh;
444
Dan Williams45b42332007-07-09 11:56:43 -0700445 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 spin_lock_irq(&conf->device_lock);
448
449 do {
NeilBrown72626682005-09-09 16:23:54 -0700450 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000451 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700452 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100453 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!sh) {
455 if (!conf->inactive_blocked)
456 sh = get_free_stripe(conf);
457 if (noblock && sh == NULL)
458 break;
459 if (!sh) {
460 conf->inactive_blocked = 1;
461 wait_event_lock_irq(conf->wait_for_stripe,
462 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800463 (atomic_read(&conf->active_stripes)
464 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 || !conf->inactive_blocked),
466 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000467 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 conf->inactive_blocked = 0;
469 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100470 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
472 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100473 BUG_ON(!list_empty(&sh->lru)
474 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 if (!test_bit(STRIPE_HANDLE, &sh->state))
477 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700478 if (list_empty(&sh->lru) &&
479 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700480 BUG();
481 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
484 } while (sh == NULL);
485
486 if (sh)
487 atomic_inc(&sh->count);
488
489 spin_unlock_irq(&conf->device_lock);
490 return sh;
491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void
494raid5_end_read_request(struct bio *bi, int error);
495static void
496raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700497
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000498static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700499{
NeilBrownd1688a62011-10-11 16:49:52 +1100500 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700501 int i, disks = sh->disks;
502
503 might_sleep();
504
505 for (i = disks; i--; ) {
506 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100507 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100508 struct bio *bi, *rbi;
509 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200510 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
511 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
512 rw = WRITE_FUA;
513 else
514 rw = WRITE;
515 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700516 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100517 else if (test_and_clear_bit(R5_WantReplace,
518 &sh->dev[i].flags)) {
519 rw = WRITE;
520 replace_only = 1;
521 } else
Dan Williams91c00922007-01-02 13:52:30 -0700522 continue;
523
524 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100525 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700526
527 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100528 rbi->bi_rw = rw;
529 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700530 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100531 rbi->bi_end_io = raid5_end_write_request;
532 } else
Dan Williams91c00922007-01-02 13:52:30 -0700533 bi->bi_end_io = raid5_end_read_request;
534
535 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100536 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100537 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
538 rdev = rcu_dereference(conf->disks[i].rdev);
539 if (!rdev) {
540 rdev = rrdev;
541 rrdev = NULL;
542 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100543 if (rw & WRITE) {
544 if (replace_only)
545 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100546 if (rdev == rrdev)
547 /* We raced and saw duplicates */
548 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100550 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100551 rdev = rrdev;
552 rrdev = NULL;
553 }
NeilBrown977df362011-12-23 10:17:53 +1100554
Dan Williams91c00922007-01-02 13:52:30 -0700555 if (rdev && test_bit(Faulty, &rdev->flags))
556 rdev = NULL;
557 if (rdev)
558 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100559 if (rrdev && test_bit(Faulty, &rrdev->flags))
560 rrdev = NULL;
561 if (rrdev)
562 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700563 rcu_read_unlock();
564
NeilBrown73e92e52011-07-28 11:39:22 +1000565 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100566 * need to check for writes. We never accept write errors
567 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000568 */
569 while ((rw & WRITE) && rdev &&
570 test_bit(WriteErrorSeen, &rdev->flags)) {
571 sector_t first_bad;
572 int bad_sectors;
573 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
574 &first_bad, &bad_sectors);
575 if (!bad)
576 break;
577
578 if (bad < 0) {
579 set_bit(BlockedBadBlocks, &rdev->flags);
580 if (!conf->mddev->external &&
581 conf->mddev->flags) {
582 /* It is very unlikely, but we might
583 * still need to write out the
584 * bad block log - better give it
585 * a chance*/
586 md_check_recovery(conf->mddev);
587 }
majianpeng8d936982012-07-03 12:11:54 +1000588 /*
589 * Because md_wait_for_blocked_rdev
590 * will dec nr_pending, we must
591 * increment it first.
592 */
593 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000594 md_wait_for_blocked_rdev(rdev, conf->mddev);
595 } else {
596 /* Acknowledged bad block - skip the write */
597 rdev_dec_pending(rdev, conf->mddev);
598 rdev = NULL;
599 }
600 }
601
Dan Williams91c00922007-01-02 13:52:30 -0700602 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100603 if (s->syncing || s->expanding || s->expanded
604 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700605 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
606
Dan Williams2b7497f2008-06-28 08:31:52 +1000607 set_bit(STRIPE_IO_STARTED, &sh->state);
608
Dan Williams91c00922007-01-02 13:52:30 -0700609 bi->bi_bdev = rdev->bdev;
610 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700611 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700612 bi->bi_rw, i);
613 atomic_inc(&sh->count);
614 bi->bi_sector = sh->sector + rdev->data_offset;
615 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700616 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700617 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
618 bi->bi_io_vec[0].bv_offset = 0;
619 bi->bi_size = STRIPE_SIZE;
620 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100621 if (rrdev)
622 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700623 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100624 }
625 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100626 if (s->syncing || s->expanding || s->expanded
627 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100628 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
629
630 set_bit(STRIPE_IO_STARTED, &sh->state);
631
632 rbi->bi_bdev = rrdev->bdev;
633 pr_debug("%s: for %llu schedule op %ld on "
634 "replacement disc %d\n",
635 __func__, (unsigned long long)sh->sector,
636 rbi->bi_rw, i);
637 atomic_inc(&sh->count);
638 rbi->bi_sector = sh->sector + rrdev->data_offset;
639 rbi->bi_flags = 1 << BIO_UPTODATE;
640 rbi->bi_idx = 0;
641 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
642 rbi->bi_io_vec[0].bv_offset = 0;
643 rbi->bi_size = STRIPE_SIZE;
644 rbi->bi_next = NULL;
645 generic_make_request(rbi);
646 }
647 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000648 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700649 set_bit(STRIPE_DEGRADED, &sh->state);
650 pr_debug("skip op %ld on disc %d for sector %llu\n",
651 bi->bi_rw, i, (unsigned long long)sh->sector);
652 clear_bit(R5_LOCKED, &sh->dev[i].flags);
653 set_bit(STRIPE_HANDLE, &sh->state);
654 }
655 }
656}
657
658static struct dma_async_tx_descriptor *
659async_copy_data(int frombio, struct bio *bio, struct page *page,
660 sector_t sector, struct dma_async_tx_descriptor *tx)
661{
662 struct bio_vec *bvl;
663 struct page *bio_page;
664 int i;
665 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700666 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700667 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700668
669 if (bio->bi_sector >= sector)
670 page_offset = (signed)(bio->bi_sector - sector) * 512;
671 else
672 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700673
Dan Williams0403e382009-09-08 17:42:50 -0700674 if (frombio)
675 flags |= ASYNC_TX_FENCE;
676 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
677
Dan Williams91c00922007-01-02 13:52:30 -0700678 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000679 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700680 int clen;
681 int b_offset = 0;
682
683 if (page_offset < 0) {
684 b_offset = -page_offset;
685 page_offset += b_offset;
686 len -= b_offset;
687 }
688
689 if (len > 0 && page_offset + len > STRIPE_SIZE)
690 clen = STRIPE_SIZE - page_offset;
691 else
692 clen = len;
693
694 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000695 b_offset += bvl->bv_offset;
696 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700697 if (frombio)
698 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700699 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700700 else
701 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700702 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700703 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700704 /* chain the operations */
705 submit.depend_tx = tx;
706
Dan Williams91c00922007-01-02 13:52:30 -0700707 if (clen < len) /* hit end of page */
708 break;
709 page_offset += len;
710 }
711
712 return tx;
713}
714
715static void ops_complete_biofill(void *stripe_head_ref)
716{
717 struct stripe_head *sh = stripe_head_ref;
718 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100719 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700720 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700721
Harvey Harrisone46b2722008-04-28 02:15:50 -0700722 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700723 (unsigned long long)sh->sector);
724
725 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000726 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700727 for (i = sh->disks; i--; ) {
728 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700729
730 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700731 /* and check if we need to reply to a read request,
732 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000733 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700734 */
735 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700736 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700737
Dan Williams91c00922007-01-02 13:52:30 -0700738 BUG_ON(!dev->read);
739 rbi = dev->read;
740 dev->read = NULL;
741 while (rbi && rbi->bi_sector <
742 dev->sector + STRIPE_SECTORS) {
743 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200744 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700745 rbi->bi_next = return_bi;
746 return_bi = rbi;
747 }
Dan Williams91c00922007-01-02 13:52:30 -0700748 rbi = rbi2;
749 }
750 }
751 }
Dan Williams83de75c2008-06-28 08:31:58 +1000752 spin_unlock_irq(&conf->device_lock);
753 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700754
755 return_io(return_bi);
756
Dan Williamse4d84902007-09-24 10:06:13 -0700757 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700758 release_stripe(sh);
759}
760
761static void ops_run_biofill(struct stripe_head *sh)
762{
763 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100764 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700765 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700766 int i;
767
Harvey Harrisone46b2722008-04-28 02:15:50 -0700768 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700769 (unsigned long long)sh->sector);
770
771 for (i = sh->disks; i--; ) {
772 struct r5dev *dev = &sh->dev[i];
773 if (test_bit(R5_Wantfill, &dev->flags)) {
774 struct bio *rbi;
775 spin_lock_irq(&conf->device_lock);
776 dev->read = rbi = dev->toread;
777 dev->toread = NULL;
778 spin_unlock_irq(&conf->device_lock);
779 while (rbi && rbi->bi_sector <
780 dev->sector + STRIPE_SECTORS) {
781 tx = async_copy_data(0, rbi, dev->page,
782 dev->sector, tx);
783 rbi = r5_next_bio(rbi, dev->sector);
784 }
785 }
786 }
787
788 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700789 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
790 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700791}
792
Dan Williams4e7d2c02009-08-29 19:13:11 -0700793static void mark_target_uptodate(struct stripe_head *sh, int target)
794{
795 struct r5dev *tgt;
796
797 if (target < 0)
798 return;
799
800 tgt = &sh->dev[target];
801 set_bit(R5_UPTODATE, &tgt->flags);
802 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
803 clear_bit(R5_Wantcompute, &tgt->flags);
804}
805
Dan Williamsac6b53b2009-07-14 13:40:19 -0700806static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700807{
808 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700809
Harvey Harrisone46b2722008-04-28 02:15:50 -0700810 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700811 (unsigned long long)sh->sector);
812
Dan Williamsac6b53b2009-07-14 13:40:19 -0700813 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700814 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700815 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700816
Dan Williamsecc65c92008-06-28 08:31:57 +1000817 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
818 if (sh->check_state == check_state_compute_run)
819 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700820 set_bit(STRIPE_HANDLE, &sh->state);
821 release_stripe(sh);
822}
823
Dan Williamsd6f38f32009-07-14 11:50:52 -0700824/* return a pointer to the address conversion region of the scribble buffer */
825static addr_conv_t *to_addr_conv(struct stripe_head *sh,
826 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700827{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700828 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
829}
830
831static struct dma_async_tx_descriptor *
832ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
833{
Dan Williams91c00922007-01-02 13:52:30 -0700834 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700835 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700836 int target = sh->ops.target;
837 struct r5dev *tgt = &sh->dev[target];
838 struct page *xor_dest = tgt->page;
839 int count = 0;
840 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700841 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700842 int i;
843
844 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700845 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700846 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
847
848 for (i = disks; i--; )
849 if (i != target)
850 xor_srcs[count++] = sh->dev[i].page;
851
852 atomic_inc(&sh->count);
853
Dan Williams0403e382009-09-08 17:42:50 -0700854 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700855 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700856 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700857 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700858 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700859 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700860
Dan Williams91c00922007-01-02 13:52:30 -0700861 return tx;
862}
863
Dan Williamsac6b53b2009-07-14 13:40:19 -0700864/* set_syndrome_sources - populate source buffers for gen_syndrome
865 * @srcs - (struct page *) array of size sh->disks
866 * @sh - stripe_head to parse
867 *
868 * Populates srcs in proper layout order for the stripe and returns the
869 * 'count' of sources to be used in a call to async_gen_syndrome. The P
870 * destination buffer is recorded in srcs[count] and the Q destination
871 * is recorded in srcs[count+1]].
872 */
873static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
874{
875 int disks = sh->disks;
876 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
877 int d0_idx = raid6_d0(sh);
878 int count;
879 int i;
880
881 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100882 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700883
884 count = 0;
885 i = d0_idx;
886 do {
887 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
888
889 srcs[slot] = sh->dev[i].page;
890 i = raid6_next_disk(i, disks);
891 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700892
NeilBrowne4424fe2009-10-16 16:27:34 +1100893 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700894}
895
896static struct dma_async_tx_descriptor *
897ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
898{
899 int disks = sh->disks;
900 struct page **blocks = percpu->scribble;
901 int target;
902 int qd_idx = sh->qd_idx;
903 struct dma_async_tx_descriptor *tx;
904 struct async_submit_ctl submit;
905 struct r5dev *tgt;
906 struct page *dest;
907 int i;
908 int count;
909
910 if (sh->ops.target < 0)
911 target = sh->ops.target2;
912 else if (sh->ops.target2 < 0)
913 target = sh->ops.target;
914 else
915 /* we should only have one valid target */
916 BUG();
917 BUG_ON(target < 0);
918 pr_debug("%s: stripe %llu block: %d\n",
919 __func__, (unsigned long long)sh->sector, target);
920
921 tgt = &sh->dev[target];
922 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
923 dest = tgt->page;
924
925 atomic_inc(&sh->count);
926
927 if (target == qd_idx) {
928 count = set_syndrome_sources(blocks, sh);
929 blocks[count] = NULL; /* regenerating p is not necessary */
930 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700931 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
932 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700933 to_addr_conv(sh, percpu));
934 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
935 } else {
936 /* Compute any data- or p-drive using XOR */
937 count = 0;
938 for (i = disks; i-- ; ) {
939 if (i == target || i == qd_idx)
940 continue;
941 blocks[count++] = sh->dev[i].page;
942 }
943
Dan Williams0403e382009-09-08 17:42:50 -0700944 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
945 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700946 to_addr_conv(sh, percpu));
947 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
948 }
949
950 return tx;
951}
952
953static struct dma_async_tx_descriptor *
954ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
955{
956 int i, count, disks = sh->disks;
957 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
958 int d0_idx = raid6_d0(sh);
959 int faila = -1, failb = -1;
960 int target = sh->ops.target;
961 int target2 = sh->ops.target2;
962 struct r5dev *tgt = &sh->dev[target];
963 struct r5dev *tgt2 = &sh->dev[target2];
964 struct dma_async_tx_descriptor *tx;
965 struct page **blocks = percpu->scribble;
966 struct async_submit_ctl submit;
967
968 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
969 __func__, (unsigned long long)sh->sector, target, target2);
970 BUG_ON(target < 0 || target2 < 0);
971 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
972 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
973
Dan Williams6c910a72009-09-16 12:24:54 -0700974 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700975 * slot number conversion for 'faila' and 'failb'
976 */
977 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100978 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 count = 0;
980 i = d0_idx;
981 do {
982 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
983
984 blocks[slot] = sh->dev[i].page;
985
986 if (i == target)
987 faila = slot;
988 if (i == target2)
989 failb = slot;
990 i = raid6_next_disk(i, disks);
991 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992
993 BUG_ON(faila == failb);
994 if (failb < faila)
995 swap(faila, failb);
996 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
997 __func__, (unsigned long long)sh->sector, faila, failb);
998
999 atomic_inc(&sh->count);
1000
1001 if (failb == syndrome_disks+1) {
1002 /* Q disk is one of the missing disks */
1003 if (faila == syndrome_disks) {
1004 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001005 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1006 ops_complete_compute, sh,
1007 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001008 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009 STRIPE_SIZE, &submit);
1010 } else {
1011 struct page *dest;
1012 int data_target;
1013 int qd_idx = sh->qd_idx;
1014
1015 /* Missing D+Q: recompute D from P, then recompute Q */
1016 if (target == qd_idx)
1017 data_target = target2;
1018 else
1019 data_target = target;
1020
1021 count = 0;
1022 for (i = disks; i-- ; ) {
1023 if (i == data_target || i == qd_idx)
1024 continue;
1025 blocks[count++] = sh->dev[i].page;
1026 }
1027 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001028 init_async_submit(&submit,
1029 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1030 NULL, NULL, NULL,
1031 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001032 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1033 &submit);
1034
1035 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001036 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1037 ops_complete_compute, sh,
1038 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001039 return async_gen_syndrome(blocks, 0, count+2,
1040 STRIPE_SIZE, &submit);
1041 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001043 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1044 ops_complete_compute, sh,
1045 to_addr_conv(sh, percpu));
1046 if (failb == syndrome_disks) {
1047 /* We're missing D+P. */
1048 return async_raid6_datap_recov(syndrome_disks+2,
1049 STRIPE_SIZE, faila,
1050 blocks, &submit);
1051 } else {
1052 /* We're missing D+D. */
1053 return async_raid6_2data_recov(syndrome_disks+2,
1054 STRIPE_SIZE, faila, failb,
1055 blocks, &submit);
1056 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057 }
1058}
1059
1060
Dan Williams91c00922007-01-02 13:52:30 -07001061static void ops_complete_prexor(void *stripe_head_ref)
1062{
1063 struct stripe_head *sh = stripe_head_ref;
1064
Harvey Harrisone46b2722008-04-28 02:15:50 -07001065 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001066 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001067}
1068
1069static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001070ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1071 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001072{
Dan Williams91c00922007-01-02 13:52:30 -07001073 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001074 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001075 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001076 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001077
1078 /* existing parity data subtracted */
1079 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1080
Harvey Harrisone46b2722008-04-28 02:15:50 -07001081 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001082 (unsigned long long)sh->sector);
1083
1084 for (i = disks; i--; ) {
1085 struct r5dev *dev = &sh->dev[i];
1086 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001087 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001088 xor_srcs[count++] = dev->page;
1089 }
1090
Dan Williams0403e382009-09-08 17:42:50 -07001091 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001092 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001093 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001094
1095 return tx;
1096}
1097
1098static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001099ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001100{
1101 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001102 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001103
Harvey Harrisone46b2722008-04-28 02:15:50 -07001104 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001105 (unsigned long long)sh->sector);
1106
1107 for (i = disks; i--; ) {
1108 struct r5dev *dev = &sh->dev[i];
1109 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001110
Dan Williamsd8ee0722008-06-28 08:32:06 +10001111 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001112 struct bio *wbi;
1113
NeilBrowncbe47ec2011-07-26 11:20:35 +10001114 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001115 chosen = dev->towrite;
1116 dev->towrite = NULL;
1117 BUG_ON(dev->written);
1118 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001119 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001120
1121 while (wbi && wbi->bi_sector <
1122 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001123 if (wbi->bi_rw & REQ_FUA)
1124 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001125 tx = async_copy_data(1, wbi, dev->page,
1126 dev->sector, tx);
1127 wbi = r5_next_bio(wbi, dev->sector);
1128 }
1129 }
1130 }
1131
1132 return tx;
1133}
1134
Dan Williamsac6b53b2009-07-14 13:40:19 -07001135static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001136{
1137 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001138 int disks = sh->disks;
1139 int pd_idx = sh->pd_idx;
1140 int qd_idx = sh->qd_idx;
1141 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001142 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001143
Harvey Harrisone46b2722008-04-28 02:15:50 -07001144 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001145 (unsigned long long)sh->sector);
1146
Tejun Heoe9c74692010-09-03 11:56:18 +02001147 for (i = disks; i--; )
1148 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1149
Dan Williams91c00922007-01-02 13:52:30 -07001150 for (i = disks; i--; ) {
1151 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001152
Tejun Heoe9c74692010-09-03 11:56:18 +02001153 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001154 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001155 if (fua)
1156 set_bit(R5_WantFUA, &dev->flags);
1157 }
Dan Williams91c00922007-01-02 13:52:30 -07001158 }
1159
Dan Williamsd8ee0722008-06-28 08:32:06 +10001160 if (sh->reconstruct_state == reconstruct_state_drain_run)
1161 sh->reconstruct_state = reconstruct_state_drain_result;
1162 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1163 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1164 else {
1165 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1166 sh->reconstruct_state = reconstruct_state_result;
1167 }
Dan Williams91c00922007-01-02 13:52:30 -07001168
1169 set_bit(STRIPE_HANDLE, &sh->state);
1170 release_stripe(sh);
1171}
1172
1173static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001174ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1175 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001176{
Dan Williams91c00922007-01-02 13:52:30 -07001177 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001178 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001179 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001180 int count = 0, pd_idx = sh->pd_idx, i;
1181 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001182 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001183 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001184
Harvey Harrisone46b2722008-04-28 02:15:50 -07001185 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001186 (unsigned long long)sh->sector);
1187
1188 /* check if prexor is active which means only process blocks
1189 * that are part of a read-modify-write (written)
1190 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001191 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1192 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001193 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1194 for (i = disks; i--; ) {
1195 struct r5dev *dev = &sh->dev[i];
1196 if (dev->written)
1197 xor_srcs[count++] = dev->page;
1198 }
1199 } else {
1200 xor_dest = sh->dev[pd_idx].page;
1201 for (i = disks; i--; ) {
1202 struct r5dev *dev = &sh->dev[i];
1203 if (i != pd_idx)
1204 xor_srcs[count++] = dev->page;
1205 }
1206 }
1207
Dan Williams91c00922007-01-02 13:52:30 -07001208 /* 1/ if we prexor'd then the dest is reused as a source
1209 * 2/ if we did not prexor then we are redoing the parity
1210 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1211 * for the synchronous xor case
1212 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001213 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001214 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1215
1216 atomic_inc(&sh->count);
1217
Dan Williamsac6b53b2009-07-14 13:40:19 -07001218 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001219 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001220 if (unlikely(count == 1))
1221 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1222 else
1223 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001224}
1225
Dan Williamsac6b53b2009-07-14 13:40:19 -07001226static void
1227ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1228 struct dma_async_tx_descriptor *tx)
1229{
1230 struct async_submit_ctl submit;
1231 struct page **blocks = percpu->scribble;
1232 int count;
1233
1234 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1235
1236 count = set_syndrome_sources(blocks, sh);
1237
1238 atomic_inc(&sh->count);
1239
1240 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1241 sh, to_addr_conv(sh, percpu));
1242 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001243}
1244
1245static void ops_complete_check(void *stripe_head_ref)
1246{
1247 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001248
Harvey Harrisone46b2722008-04-28 02:15:50 -07001249 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001250 (unsigned long long)sh->sector);
1251
Dan Williamsecc65c92008-06-28 08:31:57 +10001252 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001253 set_bit(STRIPE_HANDLE, &sh->state);
1254 release_stripe(sh);
1255}
1256
Dan Williamsac6b53b2009-07-14 13:40:19 -07001257static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001258{
Dan Williams91c00922007-01-02 13:52:30 -07001259 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001260 int pd_idx = sh->pd_idx;
1261 int qd_idx = sh->qd_idx;
1262 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001263 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001264 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001265 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001266 int count;
1267 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001268
Harvey Harrisone46b2722008-04-28 02:15:50 -07001269 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001270 (unsigned long long)sh->sector);
1271
Dan Williamsac6b53b2009-07-14 13:40:19 -07001272 count = 0;
1273 xor_dest = sh->dev[pd_idx].page;
1274 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001275 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276 if (i == pd_idx || i == qd_idx)
1277 continue;
1278 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001279 }
1280
Dan Williamsd6f38f32009-07-14 11:50:52 -07001281 init_async_submit(&submit, 0, NULL, NULL, NULL,
1282 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001283 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001284 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001285
Dan Williams91c00922007-01-02 13:52:30 -07001286 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001287 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1288 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001289}
1290
Dan Williamsac6b53b2009-07-14 13:40:19 -07001291static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1292{
1293 struct page **srcs = percpu->scribble;
1294 struct async_submit_ctl submit;
1295 int count;
1296
1297 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1298 (unsigned long long)sh->sector, checkp);
1299
1300 count = set_syndrome_sources(srcs, sh);
1301 if (!checkp)
1302 srcs[count] = NULL;
1303
1304 atomic_inc(&sh->count);
1305 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1306 sh, to_addr_conv(sh, percpu));
1307 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1308 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1309}
1310
Dan Williams417b8d42009-10-16 16:25:22 +11001311static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001312{
1313 int overlap_clear = 0, i, disks = sh->disks;
1314 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001315 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001317 struct raid5_percpu *percpu;
1318 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001319
Dan Williamsd6f38f32009-07-14 11:50:52 -07001320 cpu = get_cpu();
1321 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001322 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001323 ops_run_biofill(sh);
1324 overlap_clear++;
1325 }
1326
Dan Williams7b3a8712008-06-28 08:32:09 +10001327 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328 if (level < 6)
1329 tx = ops_run_compute5(sh, percpu);
1330 else {
1331 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1332 tx = ops_run_compute6_1(sh, percpu);
1333 else
1334 tx = ops_run_compute6_2(sh, percpu);
1335 }
1336 /* terminate the chain if reconstruct is not set to be run */
1337 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001338 async_tx_ack(tx);
1339 }
Dan Williams91c00922007-01-02 13:52:30 -07001340
Dan Williams600aa102008-06-28 08:32:05 +10001341 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001342 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001343
Dan Williams600aa102008-06-28 08:32:05 +10001344 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001345 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001346 overlap_clear++;
1347 }
1348
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1350 if (level < 6)
1351 ops_run_reconstruct5(sh, percpu, tx);
1352 else
1353 ops_run_reconstruct6(sh, percpu, tx);
1354 }
Dan Williams91c00922007-01-02 13:52:30 -07001355
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1357 if (sh->check_state == check_state_run)
1358 ops_run_check_p(sh, percpu);
1359 else if (sh->check_state == check_state_run_q)
1360 ops_run_check_pq(sh, percpu, 0);
1361 else if (sh->check_state == check_state_run_pq)
1362 ops_run_check_pq(sh, percpu, 1);
1363 else
1364 BUG();
1365 }
Dan Williams91c00922007-01-02 13:52:30 -07001366
Dan Williams91c00922007-01-02 13:52:30 -07001367 if (overlap_clear)
1368 for (i = disks; i--; ) {
1369 struct r5dev *dev = &sh->dev[i];
1370 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1371 wake_up(&sh->raid_conf->wait_for_overlap);
1372 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001374}
1375
Dan Williams417b8d42009-10-16 16:25:22 +11001376#ifdef CONFIG_MULTICORE_RAID456
1377static void async_run_ops(void *param, async_cookie_t cookie)
1378{
1379 struct stripe_head *sh = param;
1380 unsigned long ops_request = sh->ops.request;
1381
1382 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1383 wake_up(&sh->ops.wait_for_ops);
1384
1385 __raid_run_ops(sh, ops_request);
1386 release_stripe(sh);
1387}
1388
1389static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1390{
1391 /* since handle_stripe can be called outside of raid5d context
1392 * we need to ensure sh->ops.request is de-staged before another
1393 * request arrives
1394 */
1395 wait_event(sh->ops.wait_for_ops,
1396 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1397 sh->ops.request = ops_request;
1398
1399 atomic_inc(&sh->count);
1400 async_schedule(async_run_ops, sh);
1401}
1402#else
1403#define raid_run_ops __raid_run_ops
1404#endif
1405
NeilBrownd1688a62011-10-11 16:49:52 +11001406static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001409 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001410 if (!sh)
1411 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001412
NeilBrown3f294f42005-11-08 21:39:25 -08001413 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001414 #ifdef CONFIG_MULTICORE_RAID456
1415 init_waitqueue_head(&sh->ops.wait_for_ops);
1416 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001417
NeilBrowne4e11e32010-06-16 16:45:16 +10001418 if (grow_buffers(sh)) {
1419 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001420 kmem_cache_free(conf->slab_cache, sh);
1421 return 0;
1422 }
1423 /* we just created an active stripe so... */
1424 atomic_set(&sh->count, 1);
1425 atomic_inc(&conf->active_stripes);
1426 INIT_LIST_HEAD(&sh->lru);
1427 release_stripe(sh);
1428 return 1;
1429}
1430
NeilBrownd1688a62011-10-11 16:49:52 +11001431static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001432{
Christoph Lametere18b8902006-12-06 20:33:20 -08001433 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001434 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
NeilBrownf4be6b42010-06-01 19:37:25 +10001436 if (conf->mddev->gendisk)
1437 sprintf(conf->cache_name[0],
1438 "raid%d-%s", conf->level, mdname(conf->mddev));
1439 else
1440 sprintf(conf->cache_name[0],
1441 "raid%d-%p", conf->level, conf->mddev);
1442 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1443
NeilBrownad01c9e2006-03-27 01:18:07 -08001444 conf->active_name = 0;
1445 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001447 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!sc)
1449 return 1;
1450 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001451 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001452 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001453 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
NeilBrown29269552006-03-27 01:18:10 -08001457
Dan Williamsd6f38f32009-07-14 11:50:52 -07001458/**
1459 * scribble_len - return the required size of the scribble region
1460 * @num - total number of disks in the array
1461 *
1462 * The size must be enough to contain:
1463 * 1/ a struct page pointer for each device in the array +2
1464 * 2/ room to convert each entry in (1) to its corresponding dma
1465 * (dma_map_page()) or page (page_address()) address.
1466 *
1467 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1468 * calculate over all devices (not just the data blocks), using zeros in place
1469 * of the P and Q blocks.
1470 */
1471static size_t scribble_len(int num)
1472{
1473 size_t len;
1474
1475 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1476
1477 return len;
1478}
1479
NeilBrownd1688a62011-10-11 16:49:52 +11001480static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001481{
1482 /* Make all the stripes able to hold 'newsize' devices.
1483 * New slots in each stripe get 'page' set to a new page.
1484 *
1485 * This happens in stages:
1486 * 1/ create a new kmem_cache and allocate the required number of
1487 * stripe_heads.
1488 * 2/ gather all the old stripe_heads and tranfer the pages across
1489 * to the new stripe_heads. This will have the side effect of
1490 * freezing the array as once all stripe_heads have been collected,
1491 * no IO will be possible. Old stripe heads are freed once their
1492 * pages have been transferred over, and the old kmem_cache is
1493 * freed when all stripes are done.
1494 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1495 * we simple return a failre status - no need to clean anything up.
1496 * 4/ allocate new pages for the new slots in the new stripe_heads.
1497 * If this fails, we don't bother trying the shrink the
1498 * stripe_heads down again, we just leave them as they are.
1499 * As each stripe_head is processed the new one is released into
1500 * active service.
1501 *
1502 * Once step2 is started, we cannot afford to wait for a write,
1503 * so we use GFP_NOIO allocations.
1504 */
1505 struct stripe_head *osh, *nsh;
1506 LIST_HEAD(newstripes);
1507 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001508 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001509 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001510 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001511 int i;
1512
1513 if (newsize <= conf->pool_size)
1514 return 0; /* never bother to shrink */
1515
Dan Williamsb5470dc2008-06-27 21:44:04 -07001516 err = md_allow_write(conf->mddev);
1517 if (err)
1518 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001519
NeilBrownad01c9e2006-03-27 01:18:07 -08001520 /* Step 1 */
1521 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1522 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001523 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001524 if (!sc)
1525 return -ENOMEM;
1526
1527 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001528 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001529 if (!nsh)
1530 break;
1531
NeilBrownad01c9e2006-03-27 01:18:07 -08001532 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001533 #ifdef CONFIG_MULTICORE_RAID456
1534 init_waitqueue_head(&nsh->ops.wait_for_ops);
1535 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001536
1537 list_add(&nsh->lru, &newstripes);
1538 }
1539 if (i) {
1540 /* didn't get enough, give up */
1541 while (!list_empty(&newstripes)) {
1542 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1543 list_del(&nsh->lru);
1544 kmem_cache_free(sc, nsh);
1545 }
1546 kmem_cache_destroy(sc);
1547 return -ENOMEM;
1548 }
1549 /* Step 2 - Must use GFP_NOIO now.
1550 * OK, we have enough stripes, start collecting inactive
1551 * stripes and copying them over
1552 */
1553 list_for_each_entry(nsh, &newstripes, lru) {
1554 spin_lock_irq(&conf->device_lock);
1555 wait_event_lock_irq(conf->wait_for_stripe,
1556 !list_empty(&conf->inactive_list),
1557 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001558 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001559 osh = get_free_stripe(conf);
1560 spin_unlock_irq(&conf->device_lock);
1561 atomic_set(&nsh->count, 1);
1562 for(i=0; i<conf->pool_size; i++)
1563 nsh->dev[i].page = osh->dev[i].page;
1564 for( ; i<newsize; i++)
1565 nsh->dev[i].page = NULL;
1566 kmem_cache_free(conf->slab_cache, osh);
1567 }
1568 kmem_cache_destroy(conf->slab_cache);
1569
1570 /* Step 3.
1571 * At this point, we are holding all the stripes so the array
1572 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001573 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001574 */
1575 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1576 if (ndisks) {
1577 for (i=0; i<conf->raid_disks; i++)
1578 ndisks[i] = conf->disks[i];
1579 kfree(conf->disks);
1580 conf->disks = ndisks;
1581 } else
1582 err = -ENOMEM;
1583
Dan Williamsd6f38f32009-07-14 11:50:52 -07001584 get_online_cpus();
1585 conf->scribble_len = scribble_len(newsize);
1586 for_each_present_cpu(cpu) {
1587 struct raid5_percpu *percpu;
1588 void *scribble;
1589
1590 percpu = per_cpu_ptr(conf->percpu, cpu);
1591 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1592
1593 if (scribble) {
1594 kfree(percpu->scribble);
1595 percpu->scribble = scribble;
1596 } else {
1597 err = -ENOMEM;
1598 break;
1599 }
1600 }
1601 put_online_cpus();
1602
NeilBrownad01c9e2006-03-27 01:18:07 -08001603 /* Step 4, return new stripes to service */
1604 while(!list_empty(&newstripes)) {
1605 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1606 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001607
NeilBrownad01c9e2006-03-27 01:18:07 -08001608 for (i=conf->raid_disks; i < newsize; i++)
1609 if (nsh->dev[i].page == NULL) {
1610 struct page *p = alloc_page(GFP_NOIO);
1611 nsh->dev[i].page = p;
1612 if (!p)
1613 err = -ENOMEM;
1614 }
1615 release_stripe(nsh);
1616 }
1617 /* critical section pass, GFP_NOIO no longer needed */
1618
1619 conf->slab_cache = sc;
1620 conf->active_name = 1-conf->active_name;
1621 conf->pool_size = newsize;
1622 return err;
1623}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
NeilBrownd1688a62011-10-11 16:49:52 +11001625static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 struct stripe_head *sh;
1628
NeilBrown3f294f42005-11-08 21:39:25 -08001629 spin_lock_irq(&conf->device_lock);
1630 sh = get_free_stripe(conf);
1631 spin_unlock_irq(&conf->device_lock);
1632 if (!sh)
1633 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001634 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001635 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001636 kmem_cache_free(conf->slab_cache, sh);
1637 atomic_dec(&conf->active_stripes);
1638 return 1;
1639}
1640
NeilBrownd1688a62011-10-11 16:49:52 +11001641static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001642{
1643 while (drop_one_stripe(conf))
1644 ;
1645
NeilBrown29fc7e32006-02-03 03:03:41 -08001646 if (conf->slab_cache)
1647 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 conf->slab_cache = NULL;
1649}
1650
NeilBrown6712ecf2007-09-27 12:47:43 +02001651static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
NeilBrown99c0fb52009-03-31 14:39:38 +11001653 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001654 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001655 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001657 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001658 struct md_rdev *rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 for (i=0 ; i<disks; i++)
1662 if (bi == &sh->dev[i].req)
1663 break;
1664
Dan Williams45b42332007-07-09 11:56:43 -07001665 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1666 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 uptodate);
1668 if (i == disks) {
1669 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
NeilBrown14a75d32011-12-23 10:17:52 +11001672 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001673 /* If replacement finished while this request was outstanding,
1674 * 'replacement' might be NULL already.
1675 * In that case it moved down to 'rdev'.
1676 * rdev is not removed until all requests are finished.
1677 */
NeilBrown14a75d32011-12-23 10:17:52 +11001678 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001679 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001680 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001684 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001685 /* Note that this cannot happen on a
1686 * replacement device. We just fail those on
1687 * any error
1688 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001689 printk_ratelimited(
1690 KERN_INFO
1691 "md/raid:%s: read error corrected"
1692 " (%lu sectors at %llu on %s)\n",
1693 mdname(conf->mddev), STRIPE_SECTORS,
1694 (unsigned long long)(sh->sector
1695 + rdev->data_offset),
1696 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001697 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001698 clear_bit(R5_ReadError, &sh->dev[i].flags);
1699 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1700 }
NeilBrown14a75d32011-12-23 10:17:52 +11001701 if (atomic_read(&rdev->read_errors))
1702 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001704 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001705 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001708 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001709 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1710 printk_ratelimited(
1711 KERN_WARNING
1712 "md/raid:%s: read error on replacement device "
1713 "(sector %llu on %s).\n",
1714 mdname(conf->mddev),
1715 (unsigned long long)(sh->sector
1716 + rdev->data_offset),
1717 bdn);
1718 else if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001719 printk_ratelimited(
1720 KERN_WARNING
1721 "md/raid:%s: read error not correctable "
1722 "(sector %llu on %s).\n",
1723 mdname(conf->mddev),
1724 (unsigned long long)(sh->sector
1725 + rdev->data_offset),
1726 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001727 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001728 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001729 printk_ratelimited(
1730 KERN_WARNING
1731 "md/raid:%s: read error NOT corrected!! "
1732 "(sector %llu on %s).\n",
1733 mdname(conf->mddev),
1734 (unsigned long long)(sh->sector
1735 + rdev->data_offset),
1736 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001737 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001738 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001739 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001740 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001741 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001742 else
1743 retry = 1;
1744 if (retry)
1745 set_bit(R5_ReadError, &sh->dev[i].flags);
1746 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001747 clear_bit(R5_ReadError, &sh->dev[i].flags);
1748 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001749 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
NeilBrown14a75d32011-12-23 10:17:52 +11001752 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1754 set_bit(STRIPE_HANDLE, &sh->state);
1755 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
NeilBrownd710e132008-10-13 11:55:12 +11001758static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
NeilBrown99c0fb52009-03-31 14:39:38 +11001760 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001761 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001762 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001763 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001765 sector_t first_bad;
1766 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001767 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
NeilBrown977df362011-12-23 10:17:53 +11001769 for (i = 0 ; i < disks; i++) {
1770 if (bi == &sh->dev[i].req) {
1771 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 break;
NeilBrown977df362011-12-23 10:17:53 +11001773 }
1774 if (bi == &sh->dev[i].rreq) {
1775 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001776 if (rdev)
1777 replacement = 1;
1778 else
1779 /* rdev was removed and 'replacement'
1780 * replaced it. rdev is not removed
1781 * until all requests are finished.
1782 */
1783 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001784 break;
1785 }
1786 }
Dan Williams45b42332007-07-09 11:56:43 -07001787 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1789 uptodate);
1790 if (i == disks) {
1791 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001792 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
NeilBrown977df362011-12-23 10:17:53 +11001795 if (replacement) {
1796 if (!uptodate)
1797 md_error(conf->mddev, rdev);
1798 else if (is_badblock(rdev, sh->sector,
1799 STRIPE_SECTORS,
1800 &first_bad, &bad_sectors))
1801 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1802 } else {
1803 if (!uptodate) {
1804 set_bit(WriteErrorSeen, &rdev->flags);
1805 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001806 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1807 set_bit(MD_RECOVERY_NEEDED,
1808 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001809 } else if (is_badblock(rdev, sh->sector,
1810 STRIPE_SECTORS,
1811 &first_bad, &bad_sectors))
1812 set_bit(R5_MadeGood, &sh->dev[i].flags);
1813 }
1814 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrown977df362011-12-23 10:17:53 +11001816 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1817 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001819 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
NeilBrown784052e2009-03-31 15:19:07 +11001822static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
NeilBrown784052e2009-03-31 15:19:07 +11001824static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 struct r5dev *dev = &sh->dev[i];
1827
1828 bio_init(&dev->req);
1829 dev->req.bi_io_vec = &dev->vec;
1830 dev->req.bi_vcnt++;
1831 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001833 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
NeilBrown977df362011-12-23 10:17:53 +11001835 bio_init(&dev->rreq);
1836 dev->rreq.bi_io_vec = &dev->rvec;
1837 dev->rreq.bi_vcnt++;
1838 dev->rreq.bi_max_vecs++;
1839 dev->rreq.bi_private = sh;
1840 dev->rvec.bv_page = dev->page;
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001843 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
NeilBrownfd01b882011-10-11 16:47:53 +11001846static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001849 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001850 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001851 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
NeilBrown908f4fb2011-12-23 10:17:50 +11001853 spin_lock_irqsave(&conf->device_lock, flags);
1854 clear_bit(In_sync, &rdev->flags);
1855 mddev->degraded = calc_degraded(conf);
1856 spin_unlock_irqrestore(&conf->device_lock, flags);
1857 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1858
NeilBrownde393cd2011-07-28 11:31:48 +10001859 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001860 set_bit(Faulty, &rdev->flags);
1861 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1862 printk(KERN_ALERT
1863 "md/raid:%s: Disk failure on %s, disabling device.\n"
1864 "md/raid:%s: Operation continuing on %d devices.\n",
1865 mdname(mddev),
1866 bdevname(rdev->bdev, b),
1867 mdname(mddev),
1868 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871/*
1872 * Input: a 'big' sector number,
1873 * Output: index of the data and parity disk, and the sector # in them.
1874 */
NeilBrownd1688a62011-10-11 16:49:52 +11001875static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001876 int previous, int *dd_idx,
1877 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001879 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001880 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001882 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001883 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001885 int algorithm = previous ? conf->prev_algo
1886 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001887 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1888 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001889 int raid_disks = previous ? conf->previous_raid_disks
1890 : conf->raid_disks;
1891 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 /* First compute the information on this sector */
1894
1895 /*
1896 * Compute the chunk number and the sector offset inside the chunk
1897 */
1898 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1899 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /*
1902 * Compute the stripe number
1903 */
NeilBrown35f2a592010-04-20 14:13:34 +10001904 stripe = chunk_number;
1905 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001906 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
1908 * Select the parity disk based on the user selected algorithm.
1909 */
NeilBrown84789552011-07-27 11:00:36 +10001910 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001911 switch(conf->level) {
1912 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001913 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 break;
1915 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001916 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001918 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001919 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 (*dd_idx)++;
1921 break;
1922 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001923 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001924 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 (*dd_idx)++;
1926 break;
1927 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001928 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001929 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001932 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001933 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001935 case ALGORITHM_PARITY_0:
1936 pd_idx = 0;
1937 (*dd_idx)++;
1938 break;
1939 case ALGORITHM_PARITY_N:
1940 pd_idx = data_disks;
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001943 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001944 }
1945 break;
1946 case 6:
1947
NeilBrowne183eae2009-03-31 15:20:22 +11001948 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001949 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001950 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001951 qd_idx = pd_idx + 1;
1952 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001953 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001954 qd_idx = 0;
1955 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001956 (*dd_idx) += 2; /* D D P Q D */
1957 break;
1958 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001959 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001960 qd_idx = pd_idx + 1;
1961 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001962 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001963 qd_idx = 0;
1964 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001965 (*dd_idx) += 2; /* D D P Q D */
1966 break;
1967 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001968 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001969 qd_idx = (pd_idx + 1) % raid_disks;
1970 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001971 break;
1972 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001973 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001974 qd_idx = (pd_idx + 1) % raid_disks;
1975 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001976 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001977
1978 case ALGORITHM_PARITY_0:
1979 pd_idx = 0;
1980 qd_idx = 1;
1981 (*dd_idx) += 2;
1982 break;
1983 case ALGORITHM_PARITY_N:
1984 pd_idx = data_disks;
1985 qd_idx = data_disks + 1;
1986 break;
1987
1988 case ALGORITHM_ROTATING_ZERO_RESTART:
1989 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1990 * of blocks for computing Q is different.
1991 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001992 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001993 qd_idx = pd_idx + 1;
1994 if (pd_idx == raid_disks-1) {
1995 (*dd_idx)++; /* Q D D D P */
1996 qd_idx = 0;
1997 } else if (*dd_idx >= pd_idx)
1998 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001999 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002000 break;
2001
2002 case ALGORITHM_ROTATING_N_RESTART:
2003 /* Same a left_asymmetric, by first stripe is
2004 * D D D P Q rather than
2005 * Q D D D P
2006 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002007 stripe2 += 1;
2008 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 qd_idx = pd_idx + 1;
2010 if (pd_idx == raid_disks-1) {
2011 (*dd_idx)++; /* Q D D D P */
2012 qd_idx = 0;
2013 } else if (*dd_idx >= pd_idx)
2014 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002015 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002016 break;
2017
2018 case ALGORITHM_ROTATING_N_CONTINUE:
2019 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002020 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002021 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2022 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002023 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002024 break;
2025
2026 case ALGORITHM_LEFT_ASYMMETRIC_6:
2027 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 if (*dd_idx >= pd_idx)
2030 (*dd_idx)++;
2031 qd_idx = raid_disks - 1;
2032 break;
2033
2034 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002036 if (*dd_idx >= pd_idx)
2037 (*dd_idx)++;
2038 qd_idx = raid_disks - 1;
2039 break;
2040
2041 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002042 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002043 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2044 qd_idx = raid_disks - 1;
2045 break;
2046
2047 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002048 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002049 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2050 qd_idx = raid_disks - 1;
2051 break;
2052
2053 case ALGORITHM_PARITY_0_6:
2054 pd_idx = 0;
2055 (*dd_idx)++;
2056 qd_idx = raid_disks - 1;
2057 break;
2058
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002060 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002061 }
2062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
NeilBrown911d4ee2009-03-31 14:39:38 +11002065 if (sh) {
2066 sh->pd_idx = pd_idx;
2067 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002068 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Finally, compute the new sector number
2072 */
2073 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2074 return new_sector;
2075}
2076
2077
NeilBrown784052e2009-03-31 15:19:07 +11002078static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
NeilBrownd1688a62011-10-11 16:49:52 +11002080 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002081 int raid_disks = sh->disks;
2082 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002084 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2085 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002086 int algorithm = previous ? conf->prev_algo
2087 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 sector_t stripe;
2089 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002090 sector_t chunk_number;
2091 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002093 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
NeilBrown16a53ec2006-06-26 00:27:38 -07002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2097 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
NeilBrown16a53ec2006-06-26 00:27:38 -07002099 if (i == sh->pd_idx)
2100 return 0;
2101 switch(conf->level) {
2102 case 4: break;
2103 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002104 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 case ALGORITHM_LEFT_ASYMMETRIC:
2106 case ALGORITHM_RIGHT_ASYMMETRIC:
2107 if (i > sh->pd_idx)
2108 i--;
2109 break;
2110 case ALGORITHM_LEFT_SYMMETRIC:
2111 case ALGORITHM_RIGHT_SYMMETRIC:
2112 if (i < sh->pd_idx)
2113 i += raid_disks;
2114 i -= (sh->pd_idx + 1);
2115 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 case ALGORITHM_PARITY_0:
2117 i -= 1;
2118 break;
2119 case ALGORITHM_PARITY_N:
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002122 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002123 }
2124 break;
2125 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002126 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002128 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002129 case ALGORITHM_LEFT_ASYMMETRIC:
2130 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002131 case ALGORITHM_ROTATING_ZERO_RESTART:
2132 case ALGORITHM_ROTATING_N_RESTART:
2133 if (sh->pd_idx == raid_disks-1)
2134 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002135 else if (i > sh->pd_idx)
2136 i -= 2; /* D D P Q D */
2137 break;
2138 case ALGORITHM_LEFT_SYMMETRIC:
2139 case ALGORITHM_RIGHT_SYMMETRIC:
2140 if (sh->pd_idx == raid_disks-1)
2141 i--; /* Q D D D P */
2142 else {
2143 /* D D P Q D */
2144 if (i < sh->pd_idx)
2145 i += raid_disks;
2146 i -= (sh->pd_idx + 2);
2147 }
2148 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 case ALGORITHM_PARITY_0:
2150 i -= 2;
2151 break;
2152 case ALGORITHM_PARITY_N:
2153 break;
2154 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002155 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002156 if (sh->pd_idx == 0)
2157 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002158 else {
2159 /* D D Q P D */
2160 if (i < sh->pd_idx)
2161 i += raid_disks;
2162 i -= (sh->pd_idx + 1);
2163 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002164 break;
2165 case ALGORITHM_LEFT_ASYMMETRIC_6:
2166 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2167 if (i > sh->pd_idx)
2168 i--;
2169 break;
2170 case ALGORITHM_LEFT_SYMMETRIC_6:
2171 case ALGORITHM_RIGHT_SYMMETRIC_6:
2172 if (i < sh->pd_idx)
2173 i += data_disks + 1;
2174 i -= (sh->pd_idx + 1);
2175 break;
2176 case ALGORITHM_PARITY_0_6:
2177 i -= 1;
2178 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002179 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002180 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002181 }
2182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
2185 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002186 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
NeilBrown112bf892009-03-31 14:39:38 +11002188 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002189 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002190 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2191 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002192 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2193 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195 }
2196 return r_sector;
2197}
2198
2199
Dan Williams600aa102008-06-28 08:32:05 +10002200static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002201schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002202 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002203{
2204 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002205 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002206 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002207
Dan Williamse33129d2007-01-02 13:52:30 -07002208 if (rcw) {
2209 /* if we are not expanding this is a proper write request, and
2210 * there will be bios with new data to be drained into the
2211 * stripe cache
2212 */
2213 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002214 sh->reconstruct_state = reconstruct_state_drain_run;
2215 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2216 } else
2217 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002218
Dan Williamsac6b53b2009-07-14 13:40:19 -07002219 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002220
2221 for (i = disks; i--; ) {
2222 struct r5dev *dev = &sh->dev[i];
2223
2224 if (dev->towrite) {
2225 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002226 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002227 if (!expand)
2228 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002229 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002230 }
2231 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002232 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002233 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002234 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002235 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002236 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002237 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2238 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2239
Dan Williamsd8ee0722008-06-28 08:32:06 +10002240 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002241 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2242 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002243 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002244
2245 for (i = disks; i--; ) {
2246 struct r5dev *dev = &sh->dev[i];
2247 if (i == pd_idx)
2248 continue;
2249
Dan Williamse33129d2007-01-02 13:52:30 -07002250 if (dev->towrite &&
2251 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002252 test_bit(R5_Wantcompute, &dev->flags))) {
2253 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002254 set_bit(R5_LOCKED, &dev->flags);
2255 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002256 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002257 }
2258 }
2259 }
2260
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002261 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002262 * are in flight
2263 */
2264 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2265 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002266 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002267
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002268 if (level == 6) {
2269 int qd_idx = sh->qd_idx;
2270 struct r5dev *dev = &sh->dev[qd_idx];
2271
2272 set_bit(R5_LOCKED, &dev->flags);
2273 clear_bit(R5_UPTODATE, &dev->flags);
2274 s->locked++;
2275 }
2276
Dan Williams600aa102008-06-28 08:32:05 +10002277 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002278 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002279 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002280}
NeilBrown16a53ec2006-06-26 00:27:38 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/*
2283 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002284 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * The bi_next chain must be in order.
2286 */
2287static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2288{
2289 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002290 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002291 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
NeilBrowncbe47ec2011-07-26 11:20:35 +10002293 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 (unsigned long long)bi->bi_sector,
2295 (unsigned long long)sh->sector);
2296
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002299 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002301 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2302 firstwrite = 1;
2303 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 bip = &sh->dev[dd_idx].toread;
2305 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2306 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2307 goto overlap;
2308 bip = & (*bip)->bi_next;
2309 }
2310 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2311 goto overlap;
2312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002313 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (*bip)
2315 bi->bi_next = *bip;
2316 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002317 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (forwrite) {
2320 /* check if page is covered */
2321 sector_t sector = sh->dev[dd_idx].sector;
2322 for (bi=sh->dev[dd_idx].towrite;
2323 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2324 bi && bi->bi_sector <= sector;
2325 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2326 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2327 sector = bi->bi_sector + (bi->bi_size>>9);
2328 }
2329 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2330 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2331 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002332 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002333
2334 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2335 (unsigned long long)(*bip)->bi_sector,
2336 (unsigned long long)sh->sector, dd_idx);
2337
2338 if (conf->mddev->bitmap && firstwrite) {
2339 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2340 STRIPE_SECTORS, 0);
2341 sh->bm_seq = conf->seq_flush+1;
2342 set_bit(STRIPE_BIT_DELAY, &sh->state);
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 1;
2345
2346 overlap:
2347 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2348 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350}
2351
NeilBrownd1688a62011-10-11 16:49:52 +11002352static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002353
NeilBrownd1688a62011-10-11 16:49:52 +11002354static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002355 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002356{
NeilBrown784052e2009-03-31 15:19:07 +11002357 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002358 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002359 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002360 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002361 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002362
NeilBrown112bf892009-03-31 14:39:38 +11002363 raid5_compute_sector(conf,
2364 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002365 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002366 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002367 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002368}
2369
Dan Williamsa4456852007-07-09 11:56:43 -07002370static void
NeilBrownd1688a62011-10-11 16:49:52 +11002371handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002372 struct stripe_head_state *s, int disks,
2373 struct bio **return_bi)
2374{
2375 int i;
2376 for (i = disks; i--; ) {
2377 struct bio *bi;
2378 int bitmap_end = 0;
2379
2380 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002381 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002382 rcu_read_lock();
2383 rdev = rcu_dereference(conf->disks[i].rdev);
2384 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002385 atomic_inc(&rdev->nr_pending);
2386 else
2387 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002388 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002389 if (rdev) {
2390 if (!rdev_set_badblocks(
2391 rdev,
2392 sh->sector,
2393 STRIPE_SECTORS, 0))
2394 md_error(conf->mddev, rdev);
2395 rdev_dec_pending(rdev, conf->mddev);
2396 }
Dan Williamsa4456852007-07-09 11:56:43 -07002397 }
2398 spin_lock_irq(&conf->device_lock);
2399 /* fail all writes first */
2400 bi = sh->dev[i].towrite;
2401 sh->dev[i].towrite = NULL;
2402 if (bi) {
2403 s->to_write--;
2404 bitmap_end = 1;
2405 }
2406
2407 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2408 wake_up(&conf->wait_for_overlap);
2409
2410 while (bi && bi->bi_sector <
2411 sh->dev[i].sector + STRIPE_SECTORS) {
2412 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2413 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002414 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002415 md_write_end(conf->mddev);
2416 bi->bi_next = *return_bi;
2417 *return_bi = bi;
2418 }
2419 bi = nextbi;
2420 }
2421 /* and fail all 'written' */
2422 bi = sh->dev[i].written;
2423 sh->dev[i].written = NULL;
2424 if (bi) bitmap_end = 1;
2425 while (bi && bi->bi_sector <
2426 sh->dev[i].sector + STRIPE_SECTORS) {
2427 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2428 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002429 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002430 md_write_end(conf->mddev);
2431 bi->bi_next = *return_bi;
2432 *return_bi = bi;
2433 }
2434 bi = bi2;
2435 }
2436
Dan Williamsb5e98d62007-01-02 13:52:31 -07002437 /* fail any reads if this device is non-operational and
2438 * the data has not reached the cache yet.
2439 */
2440 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2441 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2442 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002443 bi = sh->dev[i].toread;
2444 sh->dev[i].toread = NULL;
2445 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2446 wake_up(&conf->wait_for_overlap);
2447 if (bi) s->to_read--;
2448 while (bi && bi->bi_sector <
2449 sh->dev[i].sector + STRIPE_SECTORS) {
2450 struct bio *nextbi =
2451 r5_next_bio(bi, sh->dev[i].sector);
2452 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002453 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002454 bi->bi_next = *return_bi;
2455 *return_bi = bi;
2456 }
2457 bi = nextbi;
2458 }
2459 }
2460 spin_unlock_irq(&conf->device_lock);
2461 if (bitmap_end)
2462 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2463 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002464 /* If we were in the middle of a write the parity block might
2465 * still be locked - so just clear all R5_LOCKED flags
2466 */
2467 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002468 }
2469
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002470 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2471 if (atomic_dec_and_test(&conf->pending_full_writes))
2472 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002473}
2474
NeilBrown7f0da592011-07-28 11:39:22 +10002475static void
NeilBrownd1688a62011-10-11 16:49:52 +11002476handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002477 struct stripe_head_state *s)
2478{
2479 int abort = 0;
2480 int i;
2481
NeilBrown7f0da592011-07-28 11:39:22 +10002482 clear_bit(STRIPE_SYNCING, &sh->state);
2483 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002484 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002485 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002486 * Don't even need to abort as that is handled elsewhere
2487 * if needed, and not always wanted e.g. if there is a known
2488 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002489 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002490 * non-sync devices, or abort the recovery
2491 */
NeilBrown18b98372012-04-01 23:48:38 +10002492 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2493 /* During recovery devices cannot be removed, so
2494 * locking and refcounting of rdevs is not needed
2495 */
2496 for (i = 0; i < conf->raid_disks; i++) {
2497 struct md_rdev *rdev = conf->disks[i].rdev;
2498 if (rdev
2499 && !test_bit(Faulty, &rdev->flags)
2500 && !test_bit(In_sync, &rdev->flags)
2501 && !rdev_set_badblocks(rdev, sh->sector,
2502 STRIPE_SECTORS, 0))
2503 abort = 1;
2504 rdev = conf->disks[i].replacement;
2505 if (rdev
2506 && !test_bit(Faulty, &rdev->flags)
2507 && !test_bit(In_sync, &rdev->flags)
2508 && !rdev_set_badblocks(rdev, sh->sector,
2509 STRIPE_SECTORS, 0))
2510 abort = 1;
2511 }
2512 if (abort)
2513 conf->recovery_disabled =
2514 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002515 }
NeilBrown18b98372012-04-01 23:48:38 +10002516 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002517}
2518
NeilBrown9a3e1102011-12-23 10:17:53 +11002519static int want_replace(struct stripe_head *sh, int disk_idx)
2520{
2521 struct md_rdev *rdev;
2522 int rv = 0;
2523 /* Doing recovery so rcu locking not required */
2524 rdev = sh->raid_conf->disks[disk_idx].replacement;
2525 if (rdev
2526 && !test_bit(Faulty, &rdev->flags)
2527 && !test_bit(In_sync, &rdev->flags)
2528 && (rdev->recovery_offset <= sh->sector
2529 || rdev->mddev->recovery_cp <= sh->sector))
2530 rv = 1;
2531
2532 return rv;
2533}
2534
NeilBrown93b3dbc2011-07-27 11:00:36 +10002535/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002536 * to be read or computed to satisfy a request.
2537 *
2538 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002539 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002540 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002541static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2542 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002543{
2544 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002545 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2546 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002547
Dan Williamsf38e1212007-01-02 13:52:30 -07002548 /* is the data in this block needed, and can we get it? */
2549 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002550 !test_bit(R5_UPTODATE, &dev->flags) &&
2551 (dev->toread ||
2552 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2553 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002554 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002555 (s->failed >= 1 && fdev[0]->toread) ||
2556 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002557 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2558 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2559 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002560 /* we would like to get this block, possibly by computing it,
2561 * otherwise read it if the backing disk is insync
2562 */
2563 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2564 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2565 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002566 (s->failed && (disk_idx == s->failed_num[0] ||
2567 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002568 /* have disk failed, and we're requested to fetch it;
2569 * do compute it
2570 */
2571 pr_debug("Computing stripe %llu block %d\n",
2572 (unsigned long long)sh->sector, disk_idx);
2573 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2574 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2575 set_bit(R5_Wantcompute, &dev->flags);
2576 sh->ops.target = disk_idx;
2577 sh->ops.target2 = -1; /* no 2nd target */
2578 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002579 /* Careful: from this point on 'uptodate' is in the eye
2580 * of raid_run_ops which services 'compute' operations
2581 * before writes. R5_Wantcompute flags a block that will
2582 * be R5_UPTODATE by the time it is needed for a
2583 * subsequent operation.
2584 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002585 s->uptodate++;
2586 return 1;
2587 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2588 /* Computing 2-failure is *very* expensive; only
2589 * do it if failed >= 2
2590 */
2591 int other;
2592 for (other = disks; other--; ) {
2593 if (other == disk_idx)
2594 continue;
2595 if (!test_bit(R5_UPTODATE,
2596 &sh->dev[other].flags))
2597 break;
2598 }
2599 BUG_ON(other < 0);
2600 pr_debug("Computing stripe %llu blocks %d,%d\n",
2601 (unsigned long long)sh->sector,
2602 disk_idx, other);
2603 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2604 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2605 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2606 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2607 sh->ops.target = disk_idx;
2608 sh->ops.target2 = other;
2609 s->uptodate += 2;
2610 s->req_compute = 1;
2611 return 1;
2612 } else if (test_bit(R5_Insync, &dev->flags)) {
2613 set_bit(R5_LOCKED, &dev->flags);
2614 set_bit(R5_Wantread, &dev->flags);
2615 s->locked++;
2616 pr_debug("Reading block %d (sync=%d)\n",
2617 disk_idx, s->syncing);
2618 }
2619 }
2620
2621 return 0;
2622}
2623
2624/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002625 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002626 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002627static void handle_stripe_fill(struct stripe_head *sh,
2628 struct stripe_head_state *s,
2629 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002630{
2631 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002632
2633 /* look for blocks to read/compute, skip this if a compute
2634 * is already in flight, or if the stripe contents are in the
2635 * midst of changing due to a write
2636 */
2637 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2638 !sh->reconstruct_state)
2639 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002640 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002641 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002642 set_bit(STRIPE_HANDLE, &sh->state);
2643}
2644
2645
Dan Williams1fe797e2008-06-28 09:16:30 +10002646/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002647 * any written block on an uptodate or failed drive can be returned.
2648 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2649 * never LOCKED, so we don't need to test 'failed' directly.
2650 */
NeilBrownd1688a62011-10-11 16:49:52 +11002651static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002652 struct stripe_head *sh, int disks, struct bio **return_bi)
2653{
2654 int i;
2655 struct r5dev *dev;
2656
2657 for (i = disks; i--; )
2658 if (sh->dev[i].written) {
2659 dev = &sh->dev[i];
2660 if (!test_bit(R5_LOCKED, &dev->flags) &&
2661 test_bit(R5_UPTODATE, &dev->flags)) {
2662 /* We can return any write requests */
2663 struct bio *wbi, *wbi2;
2664 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002665 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002666 spin_lock_irq(&conf->device_lock);
2667 wbi = dev->written;
2668 dev->written = NULL;
2669 while (wbi && wbi->bi_sector <
2670 dev->sector + STRIPE_SECTORS) {
2671 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002672 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002673 md_write_end(conf->mddev);
2674 wbi->bi_next = *return_bi;
2675 *return_bi = wbi;
2676 }
2677 wbi = wbi2;
2678 }
2679 if (dev->towrite == NULL)
2680 bitmap_end = 1;
2681 spin_unlock_irq(&conf->device_lock);
2682 if (bitmap_end)
2683 bitmap_endwrite(conf->mddev->bitmap,
2684 sh->sector,
2685 STRIPE_SECTORS,
2686 !test_bit(STRIPE_DEGRADED, &sh->state),
2687 0);
2688 }
2689 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002690
2691 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2692 if (atomic_dec_and_test(&conf->pending_full_writes))
2693 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002694}
2695
NeilBrownd1688a62011-10-11 16:49:52 +11002696static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002697 struct stripe_head *sh,
2698 struct stripe_head_state *s,
2699 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002700{
2701 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002702 if (conf->max_degraded == 2) {
2703 /* RAID6 requires 'rcw' in current implementation
2704 * Calculate the real rcw later - for now fake it
2705 * look like rcw is cheaper
2706 */
2707 rcw = 1; rmw = 2;
2708 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002709 /* would I have to read this buffer for read_modify_write */
2710 struct r5dev *dev = &sh->dev[i];
2711 if ((dev->towrite || i == sh->pd_idx) &&
2712 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002713 !(test_bit(R5_UPTODATE, &dev->flags) ||
2714 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002715 if (test_bit(R5_Insync, &dev->flags))
2716 rmw++;
2717 else
2718 rmw += 2*disks; /* cannot read it */
2719 }
2720 /* Would I have to read this buffer for reconstruct_write */
2721 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2722 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002723 !(test_bit(R5_UPTODATE, &dev->flags) ||
2724 test_bit(R5_Wantcompute, &dev->flags))) {
2725 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002726 else
2727 rcw += 2*disks;
2728 }
2729 }
Dan Williams45b42332007-07-09 11:56:43 -07002730 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002731 (unsigned long long)sh->sector, rmw, rcw);
2732 set_bit(STRIPE_HANDLE, &sh->state);
2733 if (rmw < rcw && rmw > 0)
2734 /* prefer read-modify-write, but need to get some data */
2735 for (i = disks; i--; ) {
2736 struct r5dev *dev = &sh->dev[i];
2737 if ((dev->towrite || i == sh->pd_idx) &&
2738 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002739 !(test_bit(R5_UPTODATE, &dev->flags) ||
2740 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002741 test_bit(R5_Insync, &dev->flags)) {
2742 if (
2743 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002744 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002745 "%d for r-m-w\n", i);
2746 set_bit(R5_LOCKED, &dev->flags);
2747 set_bit(R5_Wantread, &dev->flags);
2748 s->locked++;
2749 } else {
2750 set_bit(STRIPE_DELAYED, &sh->state);
2751 set_bit(STRIPE_HANDLE, &sh->state);
2752 }
2753 }
2754 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002755 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002756 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002757 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002758 for (i = disks; i--; ) {
2759 struct r5dev *dev = &sh->dev[i];
2760 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002761 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002762 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002763 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002764 test_bit(R5_Wantcompute, &dev->flags))) {
2765 rcw++;
2766 if (!test_bit(R5_Insync, &dev->flags))
2767 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002768 if (
2769 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002770 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002771 "%d for Reconstruct\n", i);
2772 set_bit(R5_LOCKED, &dev->flags);
2773 set_bit(R5_Wantread, &dev->flags);
2774 s->locked++;
2775 } else {
2776 set_bit(STRIPE_DELAYED, &sh->state);
2777 set_bit(STRIPE_HANDLE, &sh->state);
2778 }
2779 }
2780 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002781 }
Dan Williamsa4456852007-07-09 11:56:43 -07002782 /* now if nothing is locked, and if we have enough data,
2783 * we can start a write request
2784 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002785 /* since handle_stripe can be called at any time we need to handle the
2786 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002787 * subsequent call wants to start a write request. raid_run_ops only
2788 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002789 * simultaneously. If this is not the case then new writes need to be
2790 * held off until the compute completes.
2791 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002792 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2793 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2794 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002795 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002796}
2797
NeilBrownd1688a62011-10-11 16:49:52 +11002798static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002799 struct stripe_head_state *s, int disks)
2800{
Dan Williamsecc65c92008-06-28 08:31:57 +10002801 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002802
Dan Williamsbd2ab672008-04-10 21:29:27 -07002803 set_bit(STRIPE_HANDLE, &sh->state);
2804
Dan Williamsecc65c92008-06-28 08:31:57 +10002805 switch (sh->check_state) {
2806 case check_state_idle:
2807 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002808 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002809 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002810 sh->check_state = check_state_run;
2811 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002812 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002813 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002814 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002815 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002816 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002817 /* fall through */
2818 case check_state_compute_result:
2819 sh->check_state = check_state_idle;
2820 if (!dev)
2821 dev = &sh->dev[sh->pd_idx];
2822
2823 /* check that a write has not made the stripe insync */
2824 if (test_bit(STRIPE_INSYNC, &sh->state))
2825 break;
2826
2827 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002828 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2829 BUG_ON(s->uptodate != disks);
2830
2831 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002832 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002833 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002834
Dan Williamsa4456852007-07-09 11:56:43 -07002835 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002836 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002837 break;
2838 case check_state_run:
2839 break; /* we will be called again upon completion */
2840 case check_state_check_result:
2841 sh->check_state = check_state_idle;
2842
2843 /* if a failure occurred during the check operation, leave
2844 * STRIPE_INSYNC not set and let the stripe be handled again
2845 */
2846 if (s->failed)
2847 break;
2848
2849 /* handle a successful check operation, if parity is correct
2850 * we are done. Otherwise update the mismatch count and repair
2851 * parity if !MD_RECOVERY_CHECK
2852 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002853 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002854 /* parity is correct (on disc,
2855 * not in buffer any more)
2856 */
2857 set_bit(STRIPE_INSYNC, &sh->state);
2858 else {
2859 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2860 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2861 /* don't try to repair!! */
2862 set_bit(STRIPE_INSYNC, &sh->state);
2863 else {
2864 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002865 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002866 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2867 set_bit(R5_Wantcompute,
2868 &sh->dev[sh->pd_idx].flags);
2869 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002870 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002871 s->uptodate++;
2872 }
2873 }
2874 break;
2875 case check_state_compute_run:
2876 break;
2877 default:
2878 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2879 __func__, sh->check_state,
2880 (unsigned long long) sh->sector);
2881 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002882 }
2883}
2884
2885
NeilBrownd1688a62011-10-11 16:49:52 +11002886static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002887 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002888 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002889{
Dan Williamsa4456852007-07-09 11:56:43 -07002890 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002891 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002892 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002893
2894 set_bit(STRIPE_HANDLE, &sh->state);
2895
2896 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002897
Dan Williamsa4456852007-07-09 11:56:43 -07002898 /* Want to check and possibly repair P and Q.
2899 * However there could be one 'failed' device, in which
2900 * case we can only check one of them, possibly using the
2901 * other to generate missing data
2902 */
2903
Dan Williamsd82dfee2009-07-14 13:40:57 -07002904 switch (sh->check_state) {
2905 case check_state_idle:
2906 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002907 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002908 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002909 * makes sense to check P (If anything else were failed,
2910 * we would have used P to recreate it).
2911 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002912 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002913 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002914 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002915 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002916 * anything, so it makes sense to check it
2917 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002918 if (sh->check_state == check_state_run)
2919 sh->check_state = check_state_run_pq;
2920 else
2921 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002922 }
Dan Williams36d1c642009-07-14 11:48:22 -07002923
Dan Williamsd82dfee2009-07-14 13:40:57 -07002924 /* discard potentially stale zero_sum_result */
2925 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002926
Dan Williamsd82dfee2009-07-14 13:40:57 -07002927 if (sh->check_state == check_state_run) {
2928 /* async_xor_zero_sum destroys the contents of P */
2929 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2930 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002931 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002932 if (sh->check_state >= check_state_run &&
2933 sh->check_state <= check_state_run_pq) {
2934 /* async_syndrome_zero_sum preserves P and Q, so
2935 * no need to mark them !uptodate here
2936 */
2937 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2938 break;
2939 }
Dan Williams36d1c642009-07-14 11:48:22 -07002940
Dan Williamsd82dfee2009-07-14 13:40:57 -07002941 /* we have 2-disk failure */
2942 BUG_ON(s->failed != 2);
2943 /* fall through */
2944 case check_state_compute_result:
2945 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002946
Dan Williamsd82dfee2009-07-14 13:40:57 -07002947 /* check that a write has not made the stripe insync */
2948 if (test_bit(STRIPE_INSYNC, &sh->state))
2949 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002950
2951 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002952 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002953 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002954 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002955 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002956 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002957 s->locked++;
2958 set_bit(R5_LOCKED, &dev->flags);
2959 set_bit(R5_Wantwrite, &dev->flags);
2960 }
2961 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002962 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002963 s->locked++;
2964 set_bit(R5_LOCKED, &dev->flags);
2965 set_bit(R5_Wantwrite, &dev->flags);
2966 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002967 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002968 dev = &sh->dev[pd_idx];
2969 s->locked++;
2970 set_bit(R5_LOCKED, &dev->flags);
2971 set_bit(R5_Wantwrite, &dev->flags);
2972 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002973 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002974 dev = &sh->dev[qd_idx];
2975 s->locked++;
2976 set_bit(R5_LOCKED, &dev->flags);
2977 set_bit(R5_Wantwrite, &dev->flags);
2978 }
2979 clear_bit(STRIPE_DEGRADED, &sh->state);
2980
2981 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002982 break;
2983 case check_state_run:
2984 case check_state_run_q:
2985 case check_state_run_pq:
2986 break; /* we will be called again upon completion */
2987 case check_state_check_result:
2988 sh->check_state = check_state_idle;
2989
2990 /* handle a successful check operation, if parity is correct
2991 * we are done. Otherwise update the mismatch count and repair
2992 * parity if !MD_RECOVERY_CHECK
2993 */
2994 if (sh->ops.zero_sum_result == 0) {
2995 /* both parities are correct */
2996 if (!s->failed)
2997 set_bit(STRIPE_INSYNC, &sh->state);
2998 else {
2999 /* in contrast to the raid5 case we can validate
3000 * parity, but still have a failure to write
3001 * back
3002 */
3003 sh->check_state = check_state_compute_result;
3004 /* Returning at this point means that we may go
3005 * off and bring p and/or q uptodate again so
3006 * we make sure to check zero_sum_result again
3007 * to verify if p or q need writeback
3008 */
3009 }
3010 } else {
3011 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3012 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3013 /* don't try to repair!! */
3014 set_bit(STRIPE_INSYNC, &sh->state);
3015 else {
3016 int *target = &sh->ops.target;
3017
3018 sh->ops.target = -1;
3019 sh->ops.target2 = -1;
3020 sh->check_state = check_state_compute_run;
3021 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3022 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3023 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3024 set_bit(R5_Wantcompute,
3025 &sh->dev[pd_idx].flags);
3026 *target = pd_idx;
3027 target = &sh->ops.target2;
3028 s->uptodate++;
3029 }
3030 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3031 set_bit(R5_Wantcompute,
3032 &sh->dev[qd_idx].flags);
3033 *target = qd_idx;
3034 s->uptodate++;
3035 }
3036 }
3037 }
3038 break;
3039 case check_state_compute_run:
3040 break;
3041 default:
3042 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3043 __func__, sh->check_state,
3044 (unsigned long long) sh->sector);
3045 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003046 }
3047}
3048
NeilBrownd1688a62011-10-11 16:49:52 +11003049static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003050{
3051 int i;
3052
3053 /* We have read all the blocks in this stripe and now we need to
3054 * copy some of them into a target stripe for expand.
3055 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003056 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003057 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3058 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003059 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003060 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003061 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003062 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003063
NeilBrown784052e2009-03-31 15:19:07 +11003064 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003065 sector_t s = raid5_compute_sector(conf, bn, 0,
3066 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003067 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003068 if (sh2 == NULL)
3069 /* so far only the early blocks of this stripe
3070 * have been requested. When later blocks
3071 * get requested, we will try again
3072 */
3073 continue;
3074 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3075 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3076 /* must have already done this block */
3077 release_stripe(sh2);
3078 continue;
3079 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003080
3081 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003082 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003083 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003084 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003085 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003086
Dan Williamsa4456852007-07-09 11:56:43 -07003087 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3088 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3089 for (j = 0; j < conf->raid_disks; j++)
3090 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003091 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003092 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3093 break;
3094 if (j == conf->raid_disks) {
3095 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3096 set_bit(STRIPE_HANDLE, &sh2->state);
3097 }
3098 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003099
Dan Williamsa4456852007-07-09 11:56:43 -07003100 }
NeilBrowna2e08552007-09-11 15:23:36 -07003101 /* done submitting copies, wait for them to complete */
3102 if (tx) {
3103 async_tx_ack(tx);
3104 dma_wait_for_async_tx(tx);
3105 }
Dan Williamsa4456852007-07-09 11:56:43 -07003106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108/*
3109 * handle_stripe - do things to a stripe.
3110 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003111 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3112 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003114 * return some read requests which now have data
3115 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 * schedule a read on some buffers
3117 * schedule a write of some buffers
3118 * return confirmation of parity correctness
3119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 */
Dan Williamsa4456852007-07-09 11:56:43 -07003121
NeilBrownacfe7262011-07-27 11:00:36 +10003122static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003123{
NeilBrownd1688a62011-10-11 16:49:52 +11003124 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003125 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003126 struct r5dev *dev;
3127 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003128 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003129
NeilBrownacfe7262011-07-27 11:00:36 +10003130 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003131
NeilBrownacfe7262011-07-27 11:00:36 +10003132 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3133 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3134 s->failed_num[0] = -1;
3135 s->failed_num[1] = -1;
3136
3137 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003138 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003139 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003140 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003141 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003142 sector_t first_bad;
3143 int bad_sectors;
3144 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003145
NeilBrown16a53ec2006-06-26 00:27:38 -07003146 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003147
Dan Williams45b42332007-07-09 11:56:43 -07003148 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003149 i, dev->flags,
3150 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003151 /* maybe we can reply to a read
3152 *
3153 * new wantfill requests are only permitted while
3154 * ops_complete_biofill is guaranteed to be inactive
3155 */
3156 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3157 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3158 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003159
3160 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003161 if (test_bit(R5_LOCKED, &dev->flags))
3162 s->locked++;
3163 if (test_bit(R5_UPTODATE, &dev->flags))
3164 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003165 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003166 s->compute++;
3167 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003168 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003169
NeilBrownacfe7262011-07-27 11:00:36 +10003170 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003171 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003172 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003173 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003174 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003175 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003176 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003177 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003178 }
Dan Williamsa4456852007-07-09 11:56:43 -07003179 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003180 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003181 /* Prefer to use the replacement for reads, but only
3182 * if it is recovered enough and has no bad blocks.
3183 */
3184 rdev = rcu_dereference(conf->disks[i].replacement);
3185 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3186 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3187 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3188 &first_bad, &bad_sectors))
3189 set_bit(R5_ReadRepl, &dev->flags);
3190 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003191 if (rdev)
3192 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003193 rdev = rcu_dereference(conf->disks[i].rdev);
3194 clear_bit(R5_ReadRepl, &dev->flags);
3195 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003196 if (rdev && test_bit(Faulty, &rdev->flags))
3197 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003198 if (rdev) {
3199 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3200 &first_bad, &bad_sectors);
3201 if (s->blocked_rdev == NULL
3202 && (test_bit(Blocked, &rdev->flags)
3203 || is_bad < 0)) {
3204 if (is_bad < 0)
3205 set_bit(BlockedBadBlocks,
3206 &rdev->flags);
3207 s->blocked_rdev = rdev;
3208 atomic_inc(&rdev->nr_pending);
3209 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003210 }
NeilBrown415e72d2010-06-17 17:25:21 +10003211 clear_bit(R5_Insync, &dev->flags);
3212 if (!rdev)
3213 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003214 else if (is_bad) {
3215 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003216 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3217 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003218 /* treat as in-sync, but with a read error
3219 * which we can now try to correct
3220 */
3221 set_bit(R5_Insync, &dev->flags);
3222 set_bit(R5_ReadError, &dev->flags);
3223 }
3224 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003225 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003226 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003227 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003228 set_bit(R5_Insync, &dev->flags);
3229 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3230 test_bit(R5_Expanded, &dev->flags))
3231 /* If we've reshaped into here, we assume it is Insync.
3232 * We will shortly update recovery_offset to make
3233 * it official.
3234 */
3235 set_bit(R5_Insync, &dev->flags);
3236
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003237 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003238 /* This flag does not apply to '.replacement'
3239 * only to .rdev, so make sure to check that*/
3240 struct md_rdev *rdev2 = rcu_dereference(
3241 conf->disks[i].rdev);
3242 if (rdev2 == rdev)
3243 clear_bit(R5_Insync, &dev->flags);
3244 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003245 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003246 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003247 } else
3248 clear_bit(R5_WriteError, &dev->flags);
3249 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003250 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003251 /* This flag does not apply to '.replacement'
3252 * only to .rdev, so make sure to check that*/
3253 struct md_rdev *rdev2 = rcu_dereference(
3254 conf->disks[i].rdev);
3255 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003256 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003257 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003258 } else
3259 clear_bit(R5_MadeGood, &dev->flags);
3260 }
NeilBrown977df362011-12-23 10:17:53 +11003261 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3262 struct md_rdev *rdev2 = rcu_dereference(
3263 conf->disks[i].replacement);
3264 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3265 s->handle_bad_blocks = 1;
3266 atomic_inc(&rdev2->nr_pending);
3267 } else
3268 clear_bit(R5_MadeGoodRepl, &dev->flags);
3269 }
NeilBrown415e72d2010-06-17 17:25:21 +10003270 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003271 /* The ReadError flag will just be confusing now */
3272 clear_bit(R5_ReadError, &dev->flags);
3273 clear_bit(R5_ReWrite, &dev->flags);
3274 }
NeilBrown415e72d2010-06-17 17:25:21 +10003275 if (test_bit(R5_ReadError, &dev->flags))
3276 clear_bit(R5_Insync, &dev->flags);
3277 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003278 if (s->failed < 2)
3279 s->failed_num[s->failed] = i;
3280 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003281 if (rdev && !test_bit(Faulty, &rdev->flags))
3282 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003283 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003284 }
NeilBrownc4c16632011-07-26 11:34:20 +10003285 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003286 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3287 /* If there is a failed device being replaced,
3288 * we must be recovering.
3289 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003290 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003291 * else we can only be replacing
3292 * sync and recovery both need to read all devices, and so
3293 * use the same flag.
3294 */
3295 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003296 sh->sector >= conf->mddev->recovery_cp ||
3297 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003298 s->syncing = 1;
3299 else
3300 s->replacing = 1;
3301 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003303}
NeilBrownf4168852007-02-28 20:11:53 -08003304
NeilBrowncc940152011-07-26 11:35:35 +10003305static void handle_stripe(struct stripe_head *sh)
3306{
3307 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003308 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003309 int i;
NeilBrown84789552011-07-27 11:00:36 +10003310 int prexor;
3311 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003312 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003313
3314 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003315 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003316 /* already being handled, ensure it gets handled
3317 * again when current action finishes */
3318 set_bit(STRIPE_HANDLE, &sh->state);
3319 return;
3320 }
3321
3322 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3323 set_bit(STRIPE_SYNCING, &sh->state);
3324 clear_bit(STRIPE_INSYNC, &sh->state);
3325 }
3326 clear_bit(STRIPE_DELAYED, &sh->state);
3327
3328 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3329 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3330 (unsigned long long)sh->sector, sh->state,
3331 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3332 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003333
NeilBrownacfe7262011-07-27 11:00:36 +10003334 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003335
NeilBrownbc2607f2011-07-28 11:39:22 +10003336 if (s.handle_bad_blocks) {
3337 set_bit(STRIPE_HANDLE, &sh->state);
3338 goto finish;
3339 }
3340
NeilBrown474af965fe2011-07-27 11:00:36 +10003341 if (unlikely(s.blocked_rdev)) {
3342 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003343 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003344 set_bit(STRIPE_HANDLE, &sh->state);
3345 goto finish;
3346 }
3347 /* There is nothing for the blocked_rdev to block */
3348 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3349 s.blocked_rdev = NULL;
3350 }
3351
3352 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3353 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3354 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3355 }
3356
3357 pr_debug("locked=%d uptodate=%d to_read=%d"
3358 " to_write=%d failed=%d failed_num=%d,%d\n",
3359 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3360 s.failed_num[0], s.failed_num[1]);
3361 /* check if the array has lost more than max_degraded devices and,
3362 * if so, some requests might need to be failed.
3363 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003364 if (s.failed > conf->max_degraded) {
3365 sh->check_state = 0;
3366 sh->reconstruct_state = 0;
3367 if (s.to_read+s.to_write+s.written)
3368 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003369 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003370 handle_failed_sync(conf, sh, &s);
3371 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003372
3373 /*
3374 * might be able to return some write requests if the parity blocks
3375 * are safe, or on a failed drive
3376 */
3377 pdev = &sh->dev[sh->pd_idx];
3378 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3379 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3380 qdev = &sh->dev[sh->qd_idx];
3381 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3382 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3383 || conf->level < 6;
3384
3385 if (s.written &&
3386 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3387 && !test_bit(R5_LOCKED, &pdev->flags)
3388 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3389 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3390 && !test_bit(R5_LOCKED, &qdev->flags)
3391 && test_bit(R5_UPTODATE, &qdev->flags)))))
3392 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3393
3394 /* Now we might consider reading some blocks, either to check/generate
3395 * parity, or to satisfy requests
3396 * or to load a block that is being partially written.
3397 */
3398 if (s.to_read || s.non_overwrite
3399 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003400 || (s.syncing && (s.uptodate + s.compute < disks))
3401 || s.replacing
3402 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003403 handle_stripe_fill(sh, &s, disks);
3404
NeilBrown84789552011-07-27 11:00:36 +10003405 /* Now we check to see if any write operations have recently
3406 * completed
3407 */
3408 prexor = 0;
3409 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3410 prexor = 1;
3411 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3412 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3413 sh->reconstruct_state = reconstruct_state_idle;
3414
3415 /* All the 'written' buffers and the parity block are ready to
3416 * be written back to disk
3417 */
3418 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3419 BUG_ON(sh->qd_idx >= 0 &&
3420 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3421 for (i = disks; i--; ) {
3422 struct r5dev *dev = &sh->dev[i];
3423 if (test_bit(R5_LOCKED, &dev->flags) &&
3424 (i == sh->pd_idx || i == sh->qd_idx ||
3425 dev->written)) {
3426 pr_debug("Writing block %d\n", i);
3427 set_bit(R5_Wantwrite, &dev->flags);
3428 if (prexor)
3429 continue;
3430 if (!test_bit(R5_Insync, &dev->flags) ||
3431 ((i == sh->pd_idx || i == sh->qd_idx) &&
3432 s.failed == 0))
3433 set_bit(STRIPE_INSYNC, &sh->state);
3434 }
3435 }
3436 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3437 s.dec_preread_active = 1;
3438 }
3439
3440 /* Now to consider new write requests and what else, if anything
3441 * should be read. We do not handle new writes when:
3442 * 1/ A 'write' operation (copy+xor) is already in flight.
3443 * 2/ A 'check' operation is in flight, as it may clobber the parity
3444 * block.
3445 */
3446 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3447 handle_stripe_dirtying(conf, sh, &s, disks);
3448
3449 /* maybe we need to check and possibly fix the parity for this stripe
3450 * Any reads will already have been scheduled, so we just see if enough
3451 * data is available. The parity check is held off while parity
3452 * dependent operations are in flight.
3453 */
3454 if (sh->check_state ||
3455 (s.syncing && s.locked == 0 &&
3456 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3457 !test_bit(STRIPE_INSYNC, &sh->state))) {
3458 if (conf->level == 6)
3459 handle_parity_checks6(conf, sh, &s, disks);
3460 else
3461 handle_parity_checks5(conf, sh, &s, disks);
3462 }
NeilBrownc5a31002011-07-27 11:00:36 +10003463
NeilBrown9a3e1102011-12-23 10:17:53 +11003464 if (s.replacing && s.locked == 0
3465 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3466 /* Write out to replacement devices where possible */
3467 for (i = 0; i < conf->raid_disks; i++)
3468 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3469 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3470 set_bit(R5_WantReplace, &sh->dev[i].flags);
3471 set_bit(R5_LOCKED, &sh->dev[i].flags);
3472 s.locked++;
3473 }
3474 set_bit(STRIPE_INSYNC, &sh->state);
3475 }
3476 if ((s.syncing || s.replacing) && s.locked == 0 &&
3477 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003478 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3479 clear_bit(STRIPE_SYNCING, &sh->state);
3480 }
3481
3482 /* If the failed drives are just a ReadError, then we might need
3483 * to progress the repair/check process
3484 */
3485 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3486 for (i = 0; i < s.failed; i++) {
3487 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3488 if (test_bit(R5_ReadError, &dev->flags)
3489 && !test_bit(R5_LOCKED, &dev->flags)
3490 && test_bit(R5_UPTODATE, &dev->flags)
3491 ) {
3492 if (!test_bit(R5_ReWrite, &dev->flags)) {
3493 set_bit(R5_Wantwrite, &dev->flags);
3494 set_bit(R5_ReWrite, &dev->flags);
3495 set_bit(R5_LOCKED, &dev->flags);
3496 s.locked++;
3497 } else {
3498 /* let's read it back */
3499 set_bit(R5_Wantread, &dev->flags);
3500 set_bit(R5_LOCKED, &dev->flags);
3501 s.locked++;
3502 }
3503 }
3504 }
3505
3506
NeilBrown3687c062011-07-27 11:00:36 +10003507 /* Finish reconstruct operations initiated by the expansion process */
3508 if (sh->reconstruct_state == reconstruct_state_result) {
3509 struct stripe_head *sh_src
3510 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3511 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3512 /* sh cannot be written until sh_src has been read.
3513 * so arrange for sh to be delayed a little
3514 */
3515 set_bit(STRIPE_DELAYED, &sh->state);
3516 set_bit(STRIPE_HANDLE, &sh->state);
3517 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3518 &sh_src->state))
3519 atomic_inc(&conf->preread_active_stripes);
3520 release_stripe(sh_src);
3521 goto finish;
3522 }
3523 if (sh_src)
3524 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003525
NeilBrown3687c062011-07-27 11:00:36 +10003526 sh->reconstruct_state = reconstruct_state_idle;
3527 clear_bit(STRIPE_EXPANDING, &sh->state);
3528 for (i = conf->raid_disks; i--; ) {
3529 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3530 set_bit(R5_LOCKED, &sh->dev[i].flags);
3531 s.locked++;
3532 }
3533 }
3534
3535 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3536 !sh->reconstruct_state) {
3537 /* Need to write out all blocks after computing parity */
3538 sh->disks = conf->raid_disks;
3539 stripe_set_idx(sh->sector, conf, 0, sh);
3540 schedule_reconstruction(sh, &s, 1, 1);
3541 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3542 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3543 atomic_dec(&conf->reshape_stripes);
3544 wake_up(&conf->wait_for_overlap);
3545 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3546 }
3547
3548 if (s.expanding && s.locked == 0 &&
3549 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3550 handle_stripe_expansion(conf, sh);
3551
3552finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003553 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003554 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003555 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003556
NeilBrownbc2607f2011-07-28 11:39:22 +10003557 if (s.handle_bad_blocks)
3558 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003559 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003560 struct r5dev *dev = &sh->dev[i];
3561 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3562 /* We own a safe reference to the rdev */
3563 rdev = conf->disks[i].rdev;
3564 if (!rdev_set_badblocks(rdev, sh->sector,
3565 STRIPE_SECTORS, 0))
3566 md_error(conf->mddev, rdev);
3567 rdev_dec_pending(rdev, conf->mddev);
3568 }
NeilBrownb84db562011-07-28 11:39:23 +10003569 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3570 rdev = conf->disks[i].rdev;
3571 rdev_clear_badblocks(rdev, sh->sector,
3572 STRIPE_SECTORS);
3573 rdev_dec_pending(rdev, conf->mddev);
3574 }
NeilBrown977df362011-12-23 10:17:53 +11003575 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3576 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003577 if (!rdev)
3578 /* rdev have been moved down */
3579 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003580 rdev_clear_badblocks(rdev, sh->sector,
3581 STRIPE_SECTORS);
3582 rdev_dec_pending(rdev, conf->mddev);
3583 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003584 }
3585
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003586 if (s.ops_request)
3587 raid_run_ops(sh, s.ops_request);
3588
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003589 ops_run_io(sh, &s);
3590
NeilBrownc5709ef2011-07-26 11:35:20 +10003591 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003592 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003593 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003594 * have actually been submitted.
3595 */
3596 atomic_dec(&conf->preread_active_stripes);
3597 if (atomic_read(&conf->preread_active_stripes) <
3598 IO_THRESHOLD)
3599 md_wakeup_thread(conf->mddev->thread);
3600 }
3601
NeilBrownc5709ef2011-07-26 11:35:20 +10003602 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003603
Dan Williams257a4b42011-11-08 16:22:06 +11003604 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003605}
3606
NeilBrownd1688a62011-10-11 16:49:52 +11003607static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3610 while (!list_empty(&conf->delayed_list)) {
3611 struct list_head *l = conf->delayed_list.next;
3612 struct stripe_head *sh;
3613 sh = list_entry(l, struct stripe_head, lru);
3614 list_del_init(l);
3615 clear_bit(STRIPE_DELAYED, &sh->state);
3616 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3617 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003618 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
NeilBrown482c0832011-04-18 18:25:42 +10003620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
NeilBrownd1688a62011-10-11 16:49:52 +11003623static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003624{
3625 /* device_lock is held */
3626 struct list_head head;
3627 list_add(&head, &conf->bitmap_list);
3628 list_del_init(&conf->bitmap_list);
3629 while (!list_empty(&head)) {
3630 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3631 list_del_init(&sh->lru);
3632 atomic_inc(&sh->count);
3633 __release_stripe(conf, sh);
3634 }
3635}
3636
NeilBrownfd01b882011-10-11 16:47:53 +11003637int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003638{
NeilBrownd1688a62011-10-11 16:49:52 +11003639 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003640
3641 /* No difference between reads and writes. Just check
3642 * how busy the stripe_cache is
3643 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003644
NeilBrownf022b2f2006-10-03 01:15:56 -07003645 if (conf->inactive_blocked)
3646 return 1;
3647 if (conf->quiesce)
3648 return 1;
3649 if (list_empty_careful(&conf->inactive_list))
3650 return 1;
3651
3652 return 0;
3653}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003654EXPORT_SYMBOL_GPL(md_raid5_congested);
3655
3656static int raid5_congested(void *data, int bits)
3657{
NeilBrownfd01b882011-10-11 16:47:53 +11003658 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003659
3660 return mddev_congested(mddev, bits) ||
3661 md_raid5_congested(mddev, bits);
3662}
NeilBrownf022b2f2006-10-03 01:15:56 -07003663
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003664/* We want read requests to align with chunks where possible,
3665 * but write requests don't need to.
3666 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003667static int raid5_mergeable_bvec(struct request_queue *q,
3668 struct bvec_merge_data *bvm,
3669 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003670{
NeilBrownfd01b882011-10-11 16:47:53 +11003671 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003672 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003673 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003674 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003675 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003676
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003677 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003678 return biovec->bv_len; /* always allow writes to be mergeable */
3679
Andre Noll664e7c42009-06-18 08:45:27 +10003680 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3681 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003682 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3683 if (max < 0) max = 0;
3684 if (max <= biovec->bv_len && bio_sectors == 0)
3685 return biovec->bv_len;
3686 else
3687 return max;
3688}
3689
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003690
NeilBrownfd01b882011-10-11 16:47:53 +11003691static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003692{
3693 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003694 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003695 unsigned int bio_sectors = bio->bi_size >> 9;
3696
Andre Noll664e7c42009-06-18 08:45:27 +10003697 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3698 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003699 return chunk_sectors >=
3700 ((sector & (chunk_sectors - 1)) + bio_sectors);
3701}
3702
3703/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003704 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3705 * later sampled by raid5d.
3706 */
NeilBrownd1688a62011-10-11 16:49:52 +11003707static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003708{
3709 unsigned long flags;
3710
3711 spin_lock_irqsave(&conf->device_lock, flags);
3712
3713 bi->bi_next = conf->retry_read_aligned_list;
3714 conf->retry_read_aligned_list = bi;
3715
3716 spin_unlock_irqrestore(&conf->device_lock, flags);
3717 md_wakeup_thread(conf->mddev->thread);
3718}
3719
3720
NeilBrownd1688a62011-10-11 16:49:52 +11003721static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003722{
3723 struct bio *bi;
3724
3725 bi = conf->retry_read_aligned;
3726 if (bi) {
3727 conf->retry_read_aligned = NULL;
3728 return bi;
3729 }
3730 bi = conf->retry_read_aligned_list;
3731 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003732 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003733 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003734 /*
3735 * this sets the active strip count to 1 and the processed
3736 * strip count to zero (upper 8 bits)
3737 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003738 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003739 }
3740
3741 return bi;
3742}
3743
3744
3745/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003746 * The "raid5_align_endio" should check if the read succeeded and if it
3747 * did, call bio_endio on the original bio (having bio_put the new bio
3748 * first).
3749 * If the read failed..
3750 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003751static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003752{
3753 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003754 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003755 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003756 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003757 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003758
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003759 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003761 rdev = (void*)raid_bi->bi_next;
3762 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003763 mddev = rdev->mddev;
3764 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003765
3766 rdev_dec_pending(rdev, conf->mddev);
3767
3768 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003769 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003770 if (atomic_dec_and_test(&conf->active_aligned_reads))
3771 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003772 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003773 }
3774
3775
Dan Williams45b42332007-07-09 11:56:43 -07003776 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003777
3778 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003779}
3780
Neil Brown387bb172007-02-08 14:20:29 -08003781static int bio_fits_rdev(struct bio *bi)
3782{
Jens Axboe165125e2007-07-24 09:28:11 +02003783 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003784
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003785 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003786 return 0;
3787 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003788 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003789 return 0;
3790
3791 if (q->merge_bvec_fn)
3792 /* it's too hard to apply the merge_bvec_fn at this stage,
3793 * just just give up
3794 */
3795 return 0;
3796
3797 return 1;
3798}
3799
3800
NeilBrownfd01b882011-10-11 16:47:53 +11003801static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003802{
NeilBrownd1688a62011-10-11 16:49:52 +11003803 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003804 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003805 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003806 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003807 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003808
3809 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003810 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811 return 0;
3812 }
3813 /*
NeilBrowna167f662010-10-26 18:31:13 +11003814 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815 */
NeilBrowna167f662010-10-26 18:31:13 +11003816 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003817 if (!align_bi)
3818 return 0;
3819 /*
3820 * set bi_end_io to a new function, and set bi_private to the
3821 * original bio.
3822 */
3823 align_bi->bi_end_io = raid5_align_endio;
3824 align_bi->bi_private = raid_bio;
3825 /*
3826 * compute position
3827 */
NeilBrown112bf892009-03-31 14:39:38 +11003828 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3829 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003830 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003831
NeilBrown671488c2011-12-23 10:17:52 +11003832 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003833 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003834 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3835 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3836 rdev->recovery_offset < end_sector) {
3837 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3838 if (rdev &&
3839 (test_bit(Faulty, &rdev->flags) ||
3840 !(test_bit(In_sync, &rdev->flags) ||
3841 rdev->recovery_offset >= end_sector)))
3842 rdev = NULL;
3843 }
3844 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003845 sector_t first_bad;
3846 int bad_sectors;
3847
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003848 atomic_inc(&rdev->nr_pending);
3849 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003850 raid_bio->bi_next = (void*)rdev;
3851 align_bi->bi_bdev = rdev->bdev;
3852 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003853
NeilBrown31c176e2011-07-28 11:39:22 +10003854 if (!bio_fits_rdev(align_bi) ||
3855 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3856 &first_bad, &bad_sectors)) {
3857 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003858 bio_put(align_bi);
3859 rdev_dec_pending(rdev, mddev);
3860 return 0;
3861 }
3862
majianpengd3f94022012-06-12 08:31:10 +08003863 /* No reshape active, so we can trust rdev->data_offset */
3864 align_bi->bi_sector += rdev->data_offset;
3865
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003866 spin_lock_irq(&conf->device_lock);
3867 wait_event_lock_irq(conf->wait_for_stripe,
3868 conf->quiesce == 0,
3869 conf->device_lock, /* nothing */);
3870 atomic_inc(&conf->active_aligned_reads);
3871 spin_unlock_irq(&conf->device_lock);
3872
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003873 generic_make_request(align_bi);
3874 return 1;
3875 } else {
3876 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003877 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003878 return 0;
3879 }
3880}
3881
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003882/* __get_priority_stripe - get the next stripe to process
3883 *
3884 * Full stripe writes are allowed to pass preread active stripes up until
3885 * the bypass_threshold is exceeded. In general the bypass_count
3886 * increments when the handle_list is handled before the hold_list; however, it
3887 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3888 * stripe with in flight i/o. The bypass_count will be reset when the
3889 * head of the hold_list has changed, i.e. the head was promoted to the
3890 * handle_list.
3891 */
NeilBrownd1688a62011-10-11 16:49:52 +11003892static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003893{
3894 struct stripe_head *sh;
3895
3896 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3897 __func__,
3898 list_empty(&conf->handle_list) ? "empty" : "busy",
3899 list_empty(&conf->hold_list) ? "empty" : "busy",
3900 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3901
3902 if (!list_empty(&conf->handle_list)) {
3903 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3904
3905 if (list_empty(&conf->hold_list))
3906 conf->bypass_count = 0;
3907 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3908 if (conf->hold_list.next == conf->last_hold)
3909 conf->bypass_count++;
3910 else {
3911 conf->last_hold = conf->hold_list.next;
3912 conf->bypass_count -= conf->bypass_threshold;
3913 if (conf->bypass_count < 0)
3914 conf->bypass_count = 0;
3915 }
3916 }
3917 } else if (!list_empty(&conf->hold_list) &&
3918 ((conf->bypass_threshold &&
3919 conf->bypass_count > conf->bypass_threshold) ||
3920 atomic_read(&conf->pending_full_writes) == 0)) {
3921 sh = list_entry(conf->hold_list.next,
3922 typeof(*sh), lru);
3923 conf->bypass_count -= conf->bypass_threshold;
3924 if (conf->bypass_count < 0)
3925 conf->bypass_count = 0;
3926 } else
3927 return NULL;
3928
3929 list_del_init(&sh->lru);
3930 atomic_inc(&sh->count);
3931 BUG_ON(atomic_read(&sh->count) != 1);
3932 return sh;
3933}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003935static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
NeilBrownd1688a62011-10-11 16:49:52 +11003937 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003938 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 sector_t new_sector;
3940 sector_t logical_sector, last_sector;
3941 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003942 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003943 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003944 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Tejun Heoe9c74692010-09-03 11:56:18 +02003946 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3947 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003948 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003949 }
3950
NeilBrown3d310eb2005-06-21 17:17:26 -07003951 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003952
NeilBrown802ba062006-12-13 00:34:13 -08003953 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003954 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003955 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003956 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3959 last_sector = bi->bi_sector + (bi->bi_size>>9);
3960 bi->bi_next = NULL;
3961 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003962
NeilBrown7c13edc2011-04-18 18:25:43 +10003963 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3965 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003966 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003967 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003968
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003969 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003970 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003971 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003972 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003973 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003974 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003975 * 64bit on a 32bit platform, and so it might be
3976 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003977 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003978 * the lock is dropped, so once we get a reference
3979 * to the stripe that we think it is, we will have
3980 * to check again.
3981 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003982 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003983 if (mddev->delta_disks < 0
3984 ? logical_sector < conf->reshape_progress
3985 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003986 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003987 previous = 1;
3988 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003989 if (mddev->delta_disks < 0
3990 ? logical_sector < conf->reshape_safe
3991 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003992 spin_unlock_irq(&conf->device_lock);
3993 schedule();
3994 goto retry;
3995 }
3996 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003997 spin_unlock_irq(&conf->device_lock);
3998 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003999 data_disks = disks - conf->max_degraded;
4000
NeilBrown112bf892009-03-31 14:39:38 +11004001 new_sector = raid5_compute_sector(conf, logical_sector,
4002 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004003 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004004 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 (unsigned long long)new_sector,
4006 (unsigned long long)logical_sector);
4007
NeilBrownb5663ba2009-03-31 14:39:38 +11004008 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004009 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004011 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004012 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004013 * stripe, so we must do the range check again.
4014 * Expansion could still move past after this
4015 * test, but as we are holding a reference to
4016 * 'sh', we know that if that happens,
4017 * STRIPE_EXPANDING will get set and the expansion
4018 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004019 */
4020 int must_retry = 0;
4021 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004022 if (mddev->delta_disks < 0
4023 ? logical_sector >= conf->reshape_progress
4024 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004025 /* mismatch, need to try again */
4026 must_retry = 1;
4027 spin_unlock_irq(&conf->device_lock);
4028 if (must_retry) {
4029 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004030 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004031 goto retry;
4032 }
4033 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004034
Namhyung Kimffd96e32011-07-18 17:38:51 +10004035 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004036 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004037 logical_sector < mddev->suspend_hi) {
4038 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004039 /* As the suspend_* range is controlled by
4040 * userspace, we want an interruptible
4041 * wait.
4042 */
4043 flush_signals(current);
4044 prepare_to_wait(&conf->wait_for_overlap,
4045 &w, TASK_INTERRUPTIBLE);
4046 if (logical_sector >= mddev->suspend_lo &&
4047 logical_sector < mddev->suspend_hi)
4048 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004049 goto retry;
4050 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004051
4052 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004053 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004054 /* Stripe is busy expanding or
4055 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 * and wait a while
4057 */
NeilBrown482c0832011-04-18 18:25:42 +10004058 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 release_stripe(sh);
4060 schedule();
4061 goto retry;
4062 }
4063 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004064 set_bit(STRIPE_HANDLE, &sh->state);
4065 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004066 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004067 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4068 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 } else {
4071 /* cannot get stripe for read-ahead, just give-up */
4072 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4073 finish_wait(&conf->wait_for_overlap, &w);
4074 break;
4075 }
4076
4077 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004078 if (!plugged)
4079 md_wakeup_thread(mddev->thread);
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004082 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004083 spin_unlock_irq(&conf->device_lock);
4084 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
NeilBrown16a53ec2006-06-26 00:27:38 -07004086 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004088
Neil Brown0e13fe232008-06-28 08:31:20 +10004089 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
NeilBrownfd01b882011-10-11 16:47:53 +11004093static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004094
NeilBrownfd01b882011-10-11 16:47:53 +11004095static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
NeilBrown52c03292006-06-26 00:27:43 -07004097 /* reshaping is quite different to recovery/resync so it is
4098 * handled quite separately ... here.
4099 *
4100 * On each call to sync_request, we gather one chunk worth of
4101 * destination stripes and flag them as expanding.
4102 * Then we find all the source stripes and request reads.
4103 * As the reads complete, handle_stripe will copy the data
4104 * into the destination stripe and release that stripe.
4105 */
NeilBrownd1688a62011-10-11 16:49:52 +11004106 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004108 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004109 int raid_disks = conf->previous_raid_disks;
4110 int data_disks = raid_disks - conf->max_degraded;
4111 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004112 int i;
4113 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004114 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004115 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004116 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004117 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004118
NeilBrownfef9c612009-03-31 15:16:46 +11004119 if (sector_nr == 0) {
4120 /* If restarting in the middle, skip the initial sectors */
4121 if (mddev->delta_disks < 0 &&
4122 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4123 sector_nr = raid5_size(mddev, 0, 0)
4124 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004125 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004126 conf->reshape_progress > 0)
4127 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004128 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004129 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004130 mddev->curr_resync_completed = sector_nr;
4131 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004132 *skipped = 1;
4133 return sector_nr;
4134 }
NeilBrown52c03292006-06-26 00:27:43 -07004135 }
4136
NeilBrown7a661382009-03-31 15:21:40 +11004137 /* We need to process a full chunk at a time.
4138 * If old and new chunk sizes differ, we need to process the
4139 * largest of these
4140 */
Andre Noll664e7c42009-06-18 08:45:27 +10004141 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4142 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004143 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004144 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004145
NeilBrown52c03292006-06-26 00:27:43 -07004146 /* we update the metadata when there is more than 3Meg
4147 * in the block range (that is rather arbitrary, should
4148 * probably be time based) or when the data about to be
4149 * copied would over-write the source of the data at
4150 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004151 * i.e. one new_stripe along from reshape_progress new_maps
4152 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004153 */
NeilBrownfef9c612009-03-31 15:16:46 +11004154 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004155 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004156 readpos = conf->reshape_progress;
4157 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004158 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004159 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004160 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004161 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004162 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004163 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004164 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004165 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004166 readpos -= min_t(sector_t, reshape_sectors, readpos);
4167 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004168 }
NeilBrown52c03292006-06-26 00:27:43 -07004169
NeilBrownc8f517c2009-03-31 15:28:40 +11004170 /* 'writepos' is the most advanced device address we might write.
4171 * 'readpos' is the least advanced device address we might read.
4172 * 'safepos' is the least address recorded in the metadata as having
4173 * been reshaped.
4174 * If 'readpos' is behind 'writepos', then there is no way that we can
4175 * ensure safety in the face of a crash - that must be done by userspace
4176 * making a backup of the data. So in that case there is no particular
4177 * rush to update metadata.
4178 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4179 * update the metadata to advance 'safepos' to match 'readpos' so that
4180 * we can be safe in the event of a crash.
4181 * So we insist on updating metadata if safepos is behind writepos and
4182 * readpos is beyond writepos.
4183 * In any case, update the metadata every 10 seconds.
4184 * Maybe that number should be configurable, but I'm not sure it is
4185 * worth it.... maybe it could be a multiple of safemode_delay???
4186 */
NeilBrownfef9c612009-03-31 15:16:46 +11004187 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004188 ? (safepos > writepos && readpos < writepos)
4189 : (safepos < writepos && readpos > writepos)) ||
4190 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004191 /* Cannot proceed until we've updated the superblock... */
4192 wait_event(conf->wait_for_overlap,
4193 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004194 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004195 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004196 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004197 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004198 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004199 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004200 kthread_should_stop());
4201 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004202 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004203 spin_unlock_irq(&conf->device_lock);
4204 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004205 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004206 }
4207
NeilBrownec32a2b2009-03-31 15:17:38 +11004208 if (mddev->delta_disks < 0) {
4209 BUG_ON(conf->reshape_progress == 0);
4210 stripe_addr = writepos;
4211 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004212 ~((sector_t)reshape_sectors - 1))
4213 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004214 != sector_nr);
4215 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004216 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004217 stripe_addr = sector_nr;
4218 }
NeilBrownab69ae12009-03-31 15:26:47 +11004219 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004220 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004221 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004222 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004223 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004224 set_bit(STRIPE_EXPANDING, &sh->state);
4225 atomic_inc(&conf->reshape_stripes);
4226 /* If any of this stripe is beyond the end of the old
4227 * array, then we need to zero those blocks
4228 */
4229 for (j=sh->disks; j--;) {
4230 sector_t s;
4231 if (j == sh->pd_idx)
4232 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004233 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004234 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004235 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004236 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004237 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004238 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004239 continue;
4240 }
4241 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4242 set_bit(R5_Expanded, &sh->dev[j].flags);
4243 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4244 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004245 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004246 set_bit(STRIPE_EXPAND_READY, &sh->state);
4247 set_bit(STRIPE_HANDLE, &sh->state);
4248 }
NeilBrownab69ae12009-03-31 15:26:47 +11004249 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004250 }
4251 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004252 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004253 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004254 else
NeilBrown7a661382009-03-31 15:21:40 +11004255 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004256 spin_unlock_irq(&conf->device_lock);
4257 /* Ok, those stripe are ready. We can start scheduling
4258 * reads on the source stripes.
4259 * The source stripes are determined by mapping the first and last
4260 * block on the destination stripes.
4261 */
NeilBrown52c03292006-06-26 00:27:43 -07004262 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004263 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004264 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004265 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004266 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004267 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004268 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004269 if (last_sector >= mddev->dev_sectors)
4270 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004271 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004272 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004273 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4274 set_bit(STRIPE_HANDLE, &sh->state);
4275 release_stripe(sh);
4276 first_sector += STRIPE_SECTORS;
4277 }
NeilBrownab69ae12009-03-31 15:26:47 +11004278 /* Now that the sources are clearly marked, we can release
4279 * the destination stripes
4280 */
4281 while (!list_empty(&stripes)) {
4282 sh = list_entry(stripes.next, struct stripe_head, lru);
4283 list_del_init(&sh->lru);
4284 release_stripe(sh);
4285 }
NeilBrownc6207272008-02-06 01:39:52 -08004286 /* If this takes us to the resync_max point where we have to pause,
4287 * then we need to write out the superblock.
4288 */
NeilBrown7a661382009-03-31 15:21:40 +11004289 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004290 if ((sector_nr - mddev->curr_resync_completed) * 2
4291 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004292 /* Cannot proceed until we've updated the superblock... */
4293 wait_event(conf->wait_for_overlap,
4294 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004295 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004296 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004297 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004298 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4299 md_wakeup_thread(mddev->thread);
4300 wait_event(mddev->sb_wait,
4301 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4302 || kthread_should_stop());
4303 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004304 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004305 spin_unlock_irq(&conf->device_lock);
4306 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004307 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004308 }
NeilBrown7a661382009-03-31 15:21:40 +11004309 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004310}
4311
4312/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004313static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004314{
NeilBrownd1688a62011-10-11 16:49:52 +11004315 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004316 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004317 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004318 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004319 int still_degraded = 0;
4320 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
NeilBrown72626682005-09-09 16:23:54 -07004322 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004324
NeilBrown29269552006-03-27 01:18:10 -08004325 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4326 end_reshape(conf);
4327 return 0;
4328 }
NeilBrown72626682005-09-09 16:23:54 -07004329
4330 if (mddev->curr_resync < max_sector) /* aborted */
4331 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4332 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004333 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004334 conf->fullsync = 0;
4335 bitmap_close_sync(mddev->bitmap);
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return 0;
4338 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004339
NeilBrown64bd6602009-08-03 10:59:58 +10004340 /* Allow raid5_quiesce to complete */
4341 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4342
NeilBrown52c03292006-06-26 00:27:43 -07004343 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4344 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004345
NeilBrownc6207272008-02-06 01:39:52 -08004346 /* No need to check resync_max as we never do more than one
4347 * stripe, and as resync_max will always be on a chunk boundary,
4348 * if the check in md_do_sync didn't fire, there is no chance
4349 * of overstepping resync_max here
4350 */
4351
NeilBrown16a53ec2006-06-26 00:27:38 -07004352 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 * to resync, then assert that we are finished, because there is
4354 * nothing we can do.
4355 */
NeilBrown3285edf2006-06-26 00:27:55 -07004356 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004357 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004358 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004359 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return rv;
4361 }
NeilBrown72626682005-09-09 16:23:54 -07004362 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004363 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004364 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4365 /* we can skip this block, and probably more */
4366 sync_blocks /= STRIPE_SECTORS;
4367 *skipped = 1;
4368 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
NeilBrownb47490c2008-02-06 01:39:50 -08004371 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4372
NeilBrowna8c906c2009-06-09 14:39:59 +10004373 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004375 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004377 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004379 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004381 /* Need to check if array will still be degraded after recovery/resync
4382 * We don't need to check the 'failed' flag as when that gets set,
4383 * recovery aborts.
4384 */
NeilBrownf001a702009-06-09 14:30:31 +10004385 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004386 if (conf->disks[i].rdev == NULL)
4387 still_degraded = 1;
4388
4389 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4390
NeilBrown83206d62011-07-26 11:19:49 +10004391 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
NeilBrown14425772009-10-16 15:55:25 +11004393 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 release_stripe(sh);
4395
4396 return STRIPE_SECTORS;
4397}
4398
NeilBrownd1688a62011-10-11 16:49:52 +11004399static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004400{
4401 /* We may not be able to submit a whole bio at once as there
4402 * may not be enough stripe_heads available.
4403 * We cannot pre-allocate enough stripe_heads as we may need
4404 * more than exist in the cache (if we allow ever large chunks).
4405 * So we do one stripe head at a time and record in
4406 * ->bi_hw_segments how many have been done.
4407 *
4408 * We *know* that this entire raid_bio is in one chunk, so
4409 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4410 */
4411 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004412 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004413 sector_t sector, logical_sector, last_sector;
4414 int scnt = 0;
4415 int remaining;
4416 int handled = 0;
4417
4418 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004419 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004420 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004421 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4422
4423 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004424 logical_sector += STRIPE_SECTORS,
4425 sector += STRIPE_SECTORS,
4426 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004427
Jens Axboe960e7392008-08-15 10:41:18 +02004428 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004429 /* already done this stripe */
4430 continue;
4431
NeilBrowna8c906c2009-06-09 14:39:59 +10004432 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004433
4434 if (!sh) {
4435 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004436 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004437 conf->retry_read_aligned = raid_bio;
4438 return handled;
4439 }
4440
Neil Brown387bb172007-02-08 14:20:29 -08004441 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4442 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004443 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004444 conf->retry_read_aligned = raid_bio;
4445 return handled;
4446 }
4447
Dan Williams36d1c642009-07-14 11:48:22 -07004448 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004449 release_stripe(sh);
4450 handled++;
4451 }
4452 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004453 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004454 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004455 if (remaining == 0)
4456 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004457 if (atomic_dec_and_test(&conf->active_aligned_reads))
4458 wake_up(&conf->wait_for_stripe);
4459 return handled;
4460}
4461
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463/*
4464 * This is our raid5 kernel thread.
4465 *
4466 * We scan the hash table for stripes which can be handled now.
4467 * During the scan, completed stripes are saved for us by the interrupt
4468 * handler, so that they will not have to wait for our next wakeup.
4469 */
NeilBrownfd01b882011-10-11 16:47:53 +11004470static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
4472 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004473 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004475 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Dan Williams45b42332007-07-09 11:56:43 -07004477 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004481 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 handled = 0;
4483 spin_lock_irq(&conf->device_lock);
4484 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004485 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
NeilBrown7c13edc2011-04-18 18:25:43 +10004487 if (atomic_read(&mddev->plug_cnt) == 0 &&
4488 !list_empty(&conf->bitmap_list)) {
4489 /* Now is a good time to flush some bitmap updates */
4490 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004491 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004492 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004493 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004494 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004495 activate_bit_delay(conf);
4496 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004497 if (atomic_read(&mddev->plug_cnt) == 0)
4498 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004499
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004500 while ((bio = remove_bio_from_retry(conf))) {
4501 int ok;
4502 spin_unlock_irq(&conf->device_lock);
4503 ok = retry_aligned_read(conf, bio);
4504 spin_lock_irq(&conf->device_lock);
4505 if (!ok)
4506 break;
4507 handled++;
4508 }
4509
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004510 sh = __get_priority_stripe(conf);
4511
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004512 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 spin_unlock_irq(&conf->device_lock);
4515
4516 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004517 handle_stripe(sh);
4518 release_stripe(sh);
4519 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
NeilBrownde393cd2011-07-28 11:31:48 +10004521 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4522 md_check_recovery(mddev);
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 spin_lock_irq(&conf->device_lock);
4525 }
Dan Williams45b42332007-07-09 11:56:43 -07004526 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528 spin_unlock_irq(&conf->device_lock);
4529
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004530 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004531 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Dan Williams45b42332007-07-09 11:56:43 -07004533 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534}
4535
NeilBrown3f294f42005-11-08 21:39:25 -08004536static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004537raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004538{
NeilBrownd1688a62011-10-11 16:49:52 +11004539 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004540 if (conf)
4541 return sprintf(page, "%d\n", conf->max_nr_stripes);
4542 else
4543 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004544}
4545
NeilBrownc41d4ac2010-06-01 19:37:24 +10004546int
NeilBrownfd01b882011-10-11 16:47:53 +11004547raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004548{
NeilBrownd1688a62011-10-11 16:49:52 +11004549 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004550 int err;
4551
4552 if (size <= 16 || size > 32768)
4553 return -EINVAL;
4554 while (size < conf->max_nr_stripes) {
4555 if (drop_one_stripe(conf))
4556 conf->max_nr_stripes--;
4557 else
4558 break;
4559 }
4560 err = md_allow_write(mddev);
4561 if (err)
4562 return err;
4563 while (size > conf->max_nr_stripes) {
4564 if (grow_one_stripe(conf))
4565 conf->max_nr_stripes++;
4566 else break;
4567 }
4568 return 0;
4569}
4570EXPORT_SYMBOL(raid5_set_cache_size);
4571
NeilBrown3f294f42005-11-08 21:39:25 -08004572static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004573raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004574{
NeilBrownd1688a62011-10-11 16:49:52 +11004575 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004576 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004577 int err;
4578
NeilBrown3f294f42005-11-08 21:39:25 -08004579 if (len >= PAGE_SIZE)
4580 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004581 if (!conf)
4582 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004583
Dan Williams4ef197d82008-04-28 02:15:54 -07004584 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004585 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004586 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004587 if (err)
4588 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004589 return len;
4590}
NeilBrown007583c2005-11-08 21:39:30 -08004591
NeilBrown96de1e62005-11-08 21:39:39 -08004592static struct md_sysfs_entry
4593raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4594 raid5_show_stripe_cache_size,
4595 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004596
4597static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004598raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004599{
NeilBrownd1688a62011-10-11 16:49:52 +11004600 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004601 if (conf)
4602 return sprintf(page, "%d\n", conf->bypass_threshold);
4603 else
4604 return 0;
4605}
4606
4607static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004608raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004609{
NeilBrownd1688a62011-10-11 16:49:52 +11004610 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004611 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004612 if (len >= PAGE_SIZE)
4613 return -EINVAL;
4614 if (!conf)
4615 return -ENODEV;
4616
Dan Williams4ef197d82008-04-28 02:15:54 -07004617 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004618 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004619 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004620 return -EINVAL;
4621 conf->bypass_threshold = new;
4622 return len;
4623}
4624
4625static struct md_sysfs_entry
4626raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4627 S_IRUGO | S_IWUSR,
4628 raid5_show_preread_threshold,
4629 raid5_store_preread_threshold);
4630
4631static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004632stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004633{
NeilBrownd1688a62011-10-11 16:49:52 +11004634 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004635 if (conf)
4636 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4637 else
4638 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004639}
4640
NeilBrown96de1e62005-11-08 21:39:39 -08004641static struct md_sysfs_entry
4642raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004643
NeilBrown007583c2005-11-08 21:39:30 -08004644static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004645 &raid5_stripecache_size.attr,
4646 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004647 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004648 NULL,
4649};
NeilBrown007583c2005-11-08 21:39:30 -08004650static struct attribute_group raid5_attrs_group = {
4651 .name = NULL,
4652 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004653};
4654
Dan Williams80c3a6c2009-03-17 18:10:40 -07004655static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004656raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004657{
NeilBrownd1688a62011-10-11 16:49:52 +11004658 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004659
4660 if (!sectors)
4661 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004662 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004663 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004664 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004665
Andre Noll9d8f0362009-06-18 08:45:01 +10004666 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004667 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004668 return sectors * (raid_disks - conf->max_degraded);
4669}
4670
NeilBrownd1688a62011-10-11 16:49:52 +11004671static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004672{
4673 struct raid5_percpu *percpu;
4674 unsigned long cpu;
4675
4676 if (!conf->percpu)
4677 return;
4678
4679 get_online_cpus();
4680 for_each_possible_cpu(cpu) {
4681 percpu = per_cpu_ptr(conf->percpu, cpu);
4682 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004683 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004684 }
4685#ifdef CONFIG_HOTPLUG_CPU
4686 unregister_cpu_notifier(&conf->cpu_notify);
4687#endif
4688 put_online_cpus();
4689
4690 free_percpu(conf->percpu);
4691}
4692
NeilBrownd1688a62011-10-11 16:49:52 +11004693static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004694{
4695 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004696 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004697 kfree(conf->disks);
4698 kfree(conf->stripe_hashtbl);
4699 kfree(conf);
4700}
4701
Dan Williams36d1c642009-07-14 11:48:22 -07004702#ifdef CONFIG_HOTPLUG_CPU
4703static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4704 void *hcpu)
4705{
NeilBrownd1688a62011-10-11 16:49:52 +11004706 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004707 long cpu = (long)hcpu;
4708 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4709
4710 switch (action) {
4711 case CPU_UP_PREPARE:
4712 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004713 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004714 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004715 if (!percpu->scribble)
4716 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4717
4718 if (!percpu->scribble ||
4719 (conf->level == 6 && !percpu->spare_page)) {
4720 safe_put_page(percpu->spare_page);
4721 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004722 pr_err("%s: failed memory allocation for cpu%ld\n",
4723 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004724 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004725 }
4726 break;
4727 case CPU_DEAD:
4728 case CPU_DEAD_FROZEN:
4729 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004730 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004731 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004732 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004733 break;
4734 default:
4735 break;
4736 }
4737 return NOTIFY_OK;
4738}
4739#endif
4740
NeilBrownd1688a62011-10-11 16:49:52 +11004741static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004742{
4743 unsigned long cpu;
4744 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004745 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004746 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004747 int err;
4748
Dan Williams36d1c642009-07-14 11:48:22 -07004749 allcpus = alloc_percpu(struct raid5_percpu);
4750 if (!allcpus)
4751 return -ENOMEM;
4752 conf->percpu = allcpus;
4753
4754 get_online_cpus();
4755 err = 0;
4756 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004757 if (conf->level == 6) {
4758 spare_page = alloc_page(GFP_KERNEL);
4759 if (!spare_page) {
4760 err = -ENOMEM;
4761 break;
4762 }
4763 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4764 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004765 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004766 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004767 err = -ENOMEM;
4768 break;
4769 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004770 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004771 }
4772#ifdef CONFIG_HOTPLUG_CPU
4773 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4774 conf->cpu_notify.priority = 0;
4775 if (err == 0)
4776 err = register_cpu_notifier(&conf->cpu_notify);
4777#endif
4778 put_online_cpus();
4779
4780 return err;
4781}
4782
NeilBrownd1688a62011-10-11 16:49:52 +11004783static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
NeilBrownd1688a62011-10-11 16:49:52 +11004785 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004786 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004787 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
NeilBrown91adb562009-03-31 14:39:39 +11004790 if (mddev->new_level != 5
4791 && mddev->new_level != 4
4792 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004793 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004794 mdname(mddev), mddev->new_level);
4795 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
NeilBrown91adb562009-03-31 14:39:39 +11004797 if ((mddev->new_level == 5
4798 && !algorithm_valid_raid5(mddev->new_layout)) ||
4799 (mddev->new_level == 6
4800 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004801 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004802 mdname(mddev), mddev->new_layout);
4803 return ERR_PTR(-EIO);
4804 }
4805 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004806 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004807 mdname(mddev), mddev->raid_disks);
4808 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Andre Noll664e7c42009-06-18 08:45:27 +10004811 if (!mddev->new_chunk_sectors ||
4812 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4813 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004814 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4815 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004816 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004817 }
4818
NeilBrownd1688a62011-10-11 16:49:52 +11004819 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004820 if (conf == NULL)
4821 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004822 spin_lock_init(&conf->device_lock);
4823 init_waitqueue_head(&conf->wait_for_stripe);
4824 init_waitqueue_head(&conf->wait_for_overlap);
4825 INIT_LIST_HEAD(&conf->handle_list);
4826 INIT_LIST_HEAD(&conf->hold_list);
4827 INIT_LIST_HEAD(&conf->delayed_list);
4828 INIT_LIST_HEAD(&conf->bitmap_list);
4829 INIT_LIST_HEAD(&conf->inactive_list);
4830 atomic_set(&conf->active_stripes, 0);
4831 atomic_set(&conf->preread_active_stripes, 0);
4832 atomic_set(&conf->active_aligned_reads, 0);
4833 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004834 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004835
4836 conf->raid_disks = mddev->raid_disks;
4837 if (mddev->reshape_position == MaxSector)
4838 conf->previous_raid_disks = mddev->raid_disks;
4839 else
4840 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004841 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4842 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004843
NeilBrown5e5e3e72009-10-16 16:35:30 +11004844 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004845 GFP_KERNEL);
4846 if (!conf->disks)
4847 goto abort;
4848
4849 conf->mddev = mddev;
4850
4851 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4852 goto abort;
4853
Dan Williams36d1c642009-07-14 11:48:22 -07004854 conf->level = mddev->new_level;
4855 if (raid5_alloc_percpu(conf) != 0)
4856 goto abort;
4857
NeilBrown0c55e022010-05-03 14:09:02 +10004858 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004859
NeilBrowndafb20f2012-03-19 12:46:39 +11004860 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004861 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004862 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004863 || raid_disk < 0)
4864 continue;
4865 disk = conf->disks + raid_disk;
4866
NeilBrown17045f52011-12-23 10:17:53 +11004867 if (test_bit(Replacement, &rdev->flags)) {
4868 if (disk->replacement)
4869 goto abort;
4870 disk->replacement = rdev;
4871 } else {
4872 if (disk->rdev)
4873 goto abort;
4874 disk->rdev = rdev;
4875 }
NeilBrown91adb562009-03-31 14:39:39 +11004876
4877 if (test_bit(In_sync, &rdev->flags)) {
4878 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004879 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4880 " disk %d\n",
4881 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004882 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004883 /* Cannot rely on bitmap to complete recovery */
4884 conf->fullsync = 1;
4885 }
4886
Andre Noll09c9e5f2009-06-18 08:45:55 +10004887 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004888 conf->level = mddev->new_level;
4889 if (conf->level == 6)
4890 conf->max_degraded = 2;
4891 else
4892 conf->max_degraded = 1;
4893 conf->algorithm = mddev->new_layout;
4894 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004895 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004896 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004897 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004898 conf->prev_algo = mddev->layout;
4899 }
NeilBrown91adb562009-03-31 14:39:39 +11004900
4901 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004902 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004903 if (grow_stripes(conf, conf->max_nr_stripes)) {
4904 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004905 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4906 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004907 goto abort;
4908 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004909 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4910 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004911
NeilBrown0da3c612009-09-23 18:09:45 +10004912 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004913 if (!conf->thread) {
4914 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004915 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004916 mdname(mddev));
4917 goto abort;
4918 }
4919
4920 return conf;
4921
4922 abort:
4923 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004924 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004925 return ERR_PTR(-EIO);
4926 } else
4927 return ERR_PTR(-ENOMEM);
4928}
4929
NeilBrownc148ffd2009-11-13 17:47:00 +11004930
4931static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4932{
4933 switch (algo) {
4934 case ALGORITHM_PARITY_0:
4935 if (raid_disk < max_degraded)
4936 return 1;
4937 break;
4938 case ALGORITHM_PARITY_N:
4939 if (raid_disk >= raid_disks - max_degraded)
4940 return 1;
4941 break;
4942 case ALGORITHM_PARITY_0_6:
4943 if (raid_disk == 0 ||
4944 raid_disk == raid_disks - 1)
4945 return 1;
4946 break;
4947 case ALGORITHM_LEFT_ASYMMETRIC_6:
4948 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4949 case ALGORITHM_LEFT_SYMMETRIC_6:
4950 case ALGORITHM_RIGHT_SYMMETRIC_6:
4951 if (raid_disk == raid_disks - 1)
4952 return 1;
4953 }
4954 return 0;
4955}
4956
NeilBrownfd01b882011-10-11 16:47:53 +11004957static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004958{
NeilBrownd1688a62011-10-11 16:49:52 +11004959 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004960 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004961 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004962 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004963 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11004964 int i;
NeilBrown91adb562009-03-31 14:39:39 +11004965
Andre Noll8c6ac862009-06-18 08:48:06 +10004966 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004967 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004968 " -- starting background reconstruction\n",
4969 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004970 if (mddev->reshape_position != MaxSector) {
4971 /* Check that we can continue the reshape.
4972 * Currently only disks can change, it must
4973 * increase, and we must be past the point where
4974 * a stripe over-writes itself
4975 */
4976 sector_t here_new, here_old;
4977 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004978 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004979
NeilBrown88ce4932009-03-31 15:24:23 +11004980 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004981 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004982 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004983 mdname(mddev));
4984 return -EINVAL;
4985 }
NeilBrownf6705572006-03-27 01:18:11 -08004986 old_disks = mddev->raid_disks - mddev->delta_disks;
4987 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004988 * further up in new geometry must map after here in old
4989 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004990 */
4991 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004992 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004993 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004994 printk(KERN_ERR "md/raid:%s: reshape_position not "
4995 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004996 return -EINVAL;
4997 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004998 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004999 /* here_new is the stripe we will write to */
5000 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005001 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005002 (old_disks-max_degraded));
5003 /* here_old is the first stripe that we might need to read
5004 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005005 if (mddev->delta_disks == 0) {
5006 /* We cannot be sure it is safe to start an in-place
5007 * reshape. It is only safe if user-space if monitoring
5008 * and taking constant backups.
5009 * mdadm always starts a situation like this in
5010 * readonly mode so it can take control before
5011 * allowing any writes. So just check for that.
5012 */
5013 if ((here_new * mddev->new_chunk_sectors !=
5014 here_old * mddev->chunk_sectors) ||
5015 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005016 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5017 " in read-only mode - aborting\n",
5018 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005019 return -EINVAL;
5020 }
5021 } else if (mddev->delta_disks < 0
5022 ? (here_new * mddev->new_chunk_sectors <=
5023 here_old * mddev->chunk_sectors)
5024 : (here_new * mddev->new_chunk_sectors >=
5025 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005026 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005027 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5028 "auto-recovery - aborting.\n",
5029 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005030 return -EINVAL;
5031 }
NeilBrown0c55e022010-05-03 14:09:02 +10005032 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5033 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005034 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005035 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005036 BUG_ON(mddev->level != mddev->new_level);
5037 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005038 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005039 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005040 }
5041
NeilBrown245f46c2009-03-31 14:39:39 +11005042 if (mddev->private == NULL)
5043 conf = setup_conf(mddev);
5044 else
5045 conf = mddev->private;
5046
NeilBrown91adb562009-03-31 14:39:39 +11005047 if (IS_ERR(conf))
5048 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005049
NeilBrown91adb562009-03-31 14:39:39 +11005050 mddev->thread = conf->thread;
5051 conf->thread = NULL;
5052 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
NeilBrown17045f52011-12-23 10:17:53 +11005054 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5055 i++) {
5056 rdev = conf->disks[i].rdev;
5057 if (!rdev && conf->disks[i].replacement) {
5058 /* The replacement is all we have yet */
5059 rdev = conf->disks[i].replacement;
5060 conf->disks[i].replacement = NULL;
5061 clear_bit(Replacement, &rdev->flags);
5062 conf->disks[i].rdev = rdev;
5063 }
5064 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005065 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005066 if (conf->disks[i].replacement &&
5067 conf->reshape_progress != MaxSector) {
5068 /* replacements and reshape simply do not mix. */
5069 printk(KERN_ERR "md: cannot handle concurrent "
5070 "replacement and reshape.\n");
5071 goto abort;
5072 }
NeilBrown2f115882010-06-17 17:41:03 +10005073 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005074 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005075 continue;
5076 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005077 /* This disc is not fully in-sync. However if it
5078 * just stored parity (beyond the recovery_offset),
5079 * when we don't need to be concerned about the
5080 * array being dirty.
5081 * When reshape goes 'backwards', we never have
5082 * partially completed devices, so we only need
5083 * to worry about reshape going forwards.
5084 */
5085 /* Hack because v0.91 doesn't store recovery_offset properly. */
5086 if (mddev->major_version == 0 &&
5087 mddev->minor_version > 90)
5088 rdev->recovery_offset = reshape_offset;
5089
NeilBrownc148ffd2009-11-13 17:47:00 +11005090 if (rdev->recovery_offset < reshape_offset) {
5091 /* We need to check old and new layout */
5092 if (!only_parity(rdev->raid_disk,
5093 conf->algorithm,
5094 conf->raid_disks,
5095 conf->max_degraded))
5096 continue;
5097 }
5098 if (!only_parity(rdev->raid_disk,
5099 conf->prev_algo,
5100 conf->previous_raid_disks,
5101 conf->max_degraded))
5102 continue;
5103 dirty_parity_disks++;
5104 }
NeilBrown91adb562009-03-31 14:39:39 +11005105
NeilBrown17045f52011-12-23 10:17:53 +11005106 /*
5107 * 0 for a fully functional array, 1 or 2 for a degraded array.
5108 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005109 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
NeilBrown674806d2010-06-16 17:17:53 +10005111 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005112 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005114 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 goto abort;
5116 }
5117
NeilBrown91adb562009-03-31 14:39:39 +11005118 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005119 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005120 mddev->resync_max_sectors = mddev->dev_sectors;
5121
NeilBrownc148ffd2009-11-13 17:47:00 +11005122 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005124 if (mddev->ok_start_degraded)
5125 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005126 "md/raid:%s: starting dirty degraded array"
5127 " - data corruption possible.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005128 mdname(mddev));
5129 else {
5130 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005131 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005132 mdname(mddev));
5133 goto abort;
5134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 }
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005138 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5139 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005140 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5141 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 else
NeilBrown0c55e022010-05-03 14:09:02 +10005143 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5144 " out of %d devices, algorithm %d\n",
5145 mdname(mddev), conf->level,
5146 mddev->raid_disks - mddev->degraded,
5147 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 print_raid5_conf(conf);
5150
NeilBrownfef9c612009-03-31 15:16:46 +11005151 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005152 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005153 atomic_set(&conf->reshape_stripes, 0);
5154 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5155 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5156 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5157 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5158 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005159 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005160 }
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005164 if (mddev->to_remove == &raid5_attrs_group)
5165 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005166 else if (mddev->kobj.sd &&
5167 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005168 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005169 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005170 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005171 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5172
5173 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005174 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005175 /* read-ahead size must cover two whole stripes, which
5176 * is 2 * (datadisks) * chunksize where 'n' is the
5177 * number of raid devices
5178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5180 int stripe = data_disks *
5181 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5182 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5183 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005184
5185 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005186
5187 mddev->queue->backing_dev_info.congested_data = mddev;
5188 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005189
5190 chunk_size = mddev->chunk_sectors << 9;
5191 blk_queue_io_min(mddev->queue, chunk_size);
5192 blk_queue_io_opt(mddev->queue, chunk_size *
5193 (conf->raid_disks - conf->max_degraded));
5194
NeilBrowndafb20f2012-03-19 12:46:39 +11005195 rdev_for_each(rdev, mddev)
NeilBrown9f7c2222010-07-26 12:04:13 +10005196 disk_stack_limits(mddev->gendisk, rdev->bdev,
5197 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 return 0;
5201abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005202 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005203 print_raid5_conf(conf);
5204 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005206 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 return -EIO;
5208}
5209
NeilBrownfd01b882011-10-11 16:47:53 +11005210static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
NeilBrownd1688a62011-10-11 16:49:52 +11005212 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
NeilBrown01f96c02011-09-21 15:30:20 +10005214 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005215 if (mddev->queue)
5216 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005217 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005218 mddev->private = NULL;
5219 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return 0;
5221}
5222
NeilBrownfd01b882011-10-11 16:47:53 +11005223static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
NeilBrownd1688a62011-10-11 16:49:52 +11005225 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 int i;
5227
Andre Noll9d8f0362009-06-18 08:45:01 +10005228 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5229 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005230 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 for (i = 0; i < conf->raid_disks; i++)
5232 seq_printf (seq, "%s",
5233 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005234 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236}
5237
NeilBrownd1688a62011-10-11 16:49:52 +11005238static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239{
5240 int i;
5241 struct disk_info *tmp;
5242
NeilBrown0c55e022010-05-03 14:09:02 +10005243 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 if (!conf) {
5245 printk("(conf==NULL)\n");
5246 return;
5247 }
NeilBrown0c55e022010-05-03 14:09:02 +10005248 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5249 conf->raid_disks,
5250 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 for (i = 0; i < conf->raid_disks; i++) {
5253 char b[BDEVNAME_SIZE];
5254 tmp = conf->disks + i;
5255 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005256 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5257 i, !test_bit(Faulty, &tmp->rdev->flags),
5258 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
5260}
5261
NeilBrownfd01b882011-10-11 16:47:53 +11005262static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263{
5264 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005265 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005267 int count = 0;
5268 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
5270 for (i = 0; i < conf->raid_disks; i++) {
5271 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005272 if (tmp->replacement
5273 && tmp->replacement->recovery_offset == MaxSector
5274 && !test_bit(Faulty, &tmp->replacement->flags)
5275 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5276 /* Replacement has just become active. */
5277 if (!tmp->rdev
5278 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5279 count++;
5280 if (tmp->rdev) {
5281 /* Replaced device not technically faulty,
5282 * but we need to be sure it gets removed
5283 * and never re-added.
5284 */
5285 set_bit(Faulty, &tmp->rdev->flags);
5286 sysfs_notify_dirent_safe(
5287 tmp->rdev->sysfs_state);
5288 }
5289 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5290 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005291 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005292 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005293 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005294 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005295 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
5297 }
NeilBrown6b965622010-08-18 11:56:59 +10005298 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005299 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005300 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005302 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303}
5304
NeilBrownb8321b62011-12-23 10:17:51 +11005305static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306{
NeilBrownd1688a62011-10-11 16:49:52 +11005307 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005309 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005310 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 struct disk_info *p = conf->disks + number;
5312
5313 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005314 if (rdev == p->rdev)
5315 rdevp = &p->rdev;
5316 else if (rdev == p->replacement)
5317 rdevp = &p->replacement;
5318 else
5319 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005320
NeilBrown657e3e42011-12-23 10:17:52 +11005321 if (number >= conf->raid_disks &&
5322 conf->reshape_progress == MaxSector)
5323 clear_bit(In_sync, &rdev->flags);
5324
5325 if (test_bit(In_sync, &rdev->flags) ||
5326 atomic_read(&rdev->nr_pending)) {
5327 err = -EBUSY;
5328 goto abort;
5329 }
5330 /* Only remove non-faulty devices if recovery
5331 * isn't possible.
5332 */
5333 if (!test_bit(Faulty, &rdev->flags) &&
5334 mddev->recovery_disabled != conf->recovery_disabled &&
5335 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005336 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005337 number < conf->raid_disks) {
5338 err = -EBUSY;
5339 goto abort;
5340 }
5341 *rdevp = NULL;
5342 synchronize_rcu();
5343 if (atomic_read(&rdev->nr_pending)) {
5344 /* lost the race, try later */
5345 err = -EBUSY;
5346 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005347 } else if (p->replacement) {
5348 /* We must have just cleared 'rdev' */
5349 p->rdev = p->replacement;
5350 clear_bit(Replacement, &p->replacement->flags);
5351 smp_mb(); /* Make sure other CPUs may see both as identical
5352 * but will never see neither - if they are careful
5353 */
5354 p->replacement = NULL;
5355 clear_bit(WantReplacement, &rdev->flags);
5356 } else
5357 /* We might have just removed the Replacement as faulty-
5358 * clear the bit just in case
5359 */
5360 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361abort:
5362
5363 print_raid5_conf(conf);
5364 return err;
5365}
5366
NeilBrownfd01b882011-10-11 16:47:53 +11005367static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
NeilBrownd1688a62011-10-11 16:49:52 +11005369 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005370 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 int disk;
5372 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005373 int first = 0;
5374 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
NeilBrown7f0da592011-07-28 11:39:22 +10005376 if (mddev->recovery_disabled == conf->recovery_disabled)
5377 return -EBUSY;
5378
NeilBrowndc10c642012-03-19 12:46:37 +11005379 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382
Neil Brown6c2fce22008-06-28 08:31:31 +10005383 if (rdev->raid_disk >= 0)
5384 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005387 * find the disk ... but prefer rdev->saved_raid_disk
5388 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005390 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005391 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005392 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5393 disk = rdev->saved_raid_disk;
5394 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005395 disk = first;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005396 for ( ; disk <= last ; disk++) {
5397 p = conf->disks + disk;
5398 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005399 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005401 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005402 if (rdev->saved_raid_disk != disk)
5403 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005404 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 break;
5406 }
NeilBrown7bfec5f2011-12-23 10:17:53 +11005407 if (test_bit(WantReplacement, &p->rdev->flags) &&
5408 p->replacement == NULL) {
5409 clear_bit(In_sync, &rdev->flags);
5410 set_bit(Replacement, &rdev->flags);
5411 rdev->raid_disk = disk;
5412 err = 0;
5413 conf->fullsync = 1;
5414 rcu_assign_pointer(p->replacement, rdev);
5415 break;
5416 }
5417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
NeilBrownfd01b882011-10-11 16:47:53 +11005422static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
5424 /* no resync is happening, and there is enough space
5425 * on all devices, so we can resize.
5426 * We need to make sure resync covers any new space.
5427 * If the array is shrinking we should possibly wait until
5428 * any io in the removed space completes, but it hardly seems
5429 * worth it.
5430 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005431 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005432 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5433 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005434 if (mddev->array_sectors >
5435 raid5_size(mddev, sectors, mddev->raid_disks))
5436 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005437 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005438 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005439 if (sectors > mddev->dev_sectors &&
5440 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005441 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5443 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005444 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005445 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 return 0;
5447}
5448
NeilBrownfd01b882011-10-11 16:47:53 +11005449static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005450{
5451 /* Can only proceed if there are plenty of stripe_heads.
5452 * We need a minimum of one full stripe,, and for sensible progress
5453 * it is best to have about 4 times that.
5454 * If we require 4 times, then the default 256 4K stripe_heads will
5455 * allow for chunk sizes up to 256K, which is probably OK.
5456 * If the chunk size is greater, user-space should request more
5457 * stripe_heads first.
5458 */
NeilBrownd1688a62011-10-11 16:49:52 +11005459 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005460 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5461 > conf->max_nr_stripes ||
5462 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5463 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005464 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5465 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005466 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5467 / STRIPE_SIZE)*4);
5468 return 0;
5469 }
5470 return 1;
5471}
5472
NeilBrownfd01b882011-10-11 16:47:53 +11005473static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005474{
NeilBrownd1688a62011-10-11 16:49:52 +11005475 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005476
NeilBrown88ce4932009-03-31 15:24:23 +11005477 if (mddev->delta_disks == 0 &&
5478 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005479 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005480 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005481 if (mddev->bitmap)
5482 /* Cannot grow a bitmap yet */
5483 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005484 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005485 return -EINVAL;
5486 if (mddev->delta_disks < 0) {
5487 /* We might be able to shrink, but the devices must
5488 * be made bigger first.
5489 * For raid6, 4 is the minimum size.
5490 * Otherwise 2 is the minimum
5491 */
5492 int min = 2;
5493 if (mddev->level == 6)
5494 min = 4;
5495 if (mddev->raid_disks + mddev->delta_disks < min)
5496 return -EINVAL;
5497 }
NeilBrown29269552006-03-27 01:18:10 -08005498
NeilBrown01ee22b2009-06-18 08:47:20 +10005499 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005500 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005501
NeilBrownec32a2b2009-03-31 15:17:38 +11005502 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005503}
5504
NeilBrownfd01b882011-10-11 16:47:53 +11005505static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005506{
NeilBrownd1688a62011-10-11 16:49:52 +11005507 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005508 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005509 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005510 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005511
NeilBrownf4168852007-02-28 20:11:53 -08005512 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005513 return -EBUSY;
5514
NeilBrown01ee22b2009-06-18 08:47:20 +10005515 if (!check_stripe_cache(mddev))
5516 return -ENOSPC;
5517
NeilBrowndafb20f2012-03-19 12:46:39 +11005518 rdev_for_each(rdev, mddev)
NeilBrown469518a2011-01-31 11:57:43 +11005519 if (!test_bit(In_sync, &rdev->flags)
5520 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005521 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005522
NeilBrownf4168852007-02-28 20:11:53 -08005523 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005524 /* Not enough devices even to make a degraded array
5525 * of that size
5526 */
5527 return -EINVAL;
5528
NeilBrownec32a2b2009-03-31 15:17:38 +11005529 /* Refuse to reduce size of the array. Any reductions in
5530 * array size must be through explicit setting of array_size
5531 * attribute.
5532 */
5533 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5534 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005535 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005536 "before number of disks\n", mdname(mddev));
5537 return -EINVAL;
5538 }
5539
NeilBrownf6705572006-03-27 01:18:11 -08005540 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005541 spin_lock_irq(&conf->device_lock);
5542 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005543 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005544 conf->prev_chunk_sectors = conf->chunk_sectors;
5545 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005546 conf->prev_algo = conf->algorithm;
5547 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005548 if (mddev->delta_disks < 0)
5549 conf->reshape_progress = raid5_size(mddev, 0, 0);
5550 else
5551 conf->reshape_progress = 0;
5552 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005553 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005554 spin_unlock_irq(&conf->device_lock);
5555
5556 /* Add some new drives, as many as will fit.
5557 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005558 * Don't add devices if we are reducing the number of
5559 * devices in the array. This is because it is not possible
5560 * to correctly record the "partially reconstructed" state of
5561 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005562 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005563 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005564 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005565 if (rdev->raid_disk < 0 &&
5566 !test_bit(Faulty, &rdev->flags)) {
5567 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005568 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005569 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005570 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005571 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005572 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005573
5574 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005575 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005576 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005577 } else if (rdev->raid_disk >= conf->previous_raid_disks
5578 && !test_bit(Faulty, &rdev->flags)) {
5579 /* This is a spare that was manually added */
5580 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005581 }
NeilBrown29269552006-03-27 01:18:10 -08005582
NeilBrown87a8dec2011-01-31 11:57:43 +11005583 /* When a reshape changes the number of devices,
5584 * ->degraded is measured against the larger of the
5585 * pre and post number of devices.
5586 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005587 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005588 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005589 spin_unlock_irqrestore(&conf->device_lock, flags);
5590 }
NeilBrown63c70c42006-03-27 01:18:13 -08005591 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005592 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005593 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005594
NeilBrown29269552006-03-27 01:18:10 -08005595 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5596 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5597 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5598 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5599 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005600 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005601 if (!mddev->sync_thread) {
5602 mddev->recovery = 0;
5603 spin_lock_irq(&conf->device_lock);
5604 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005605 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005606 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005607 spin_unlock_irq(&conf->device_lock);
5608 return -EAGAIN;
5609 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005610 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005611 md_wakeup_thread(mddev->sync_thread);
5612 md_new_event(mddev);
5613 return 0;
5614}
NeilBrown29269552006-03-27 01:18:10 -08005615
NeilBrownec32a2b2009-03-31 15:17:38 +11005616/* This is called from the reshape thread and should make any
5617 * changes needed in 'conf'
5618 */
NeilBrownd1688a62011-10-11 16:49:52 +11005619static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005620{
NeilBrown29269552006-03-27 01:18:10 -08005621
NeilBrownf6705572006-03-27 01:18:11 -08005622 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005623
NeilBrownf6705572006-03-27 01:18:11 -08005624 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005625 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005626 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005627 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005628 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005629
5630 /* read-ahead size must cover two whole stripes, which is
5631 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5632 */
NeilBrown4a5add42010-06-01 19:37:28 +10005633 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005634 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005635 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005636 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005637 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5638 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5639 }
NeilBrown29269552006-03-27 01:18:10 -08005640 }
NeilBrown29269552006-03-27 01:18:10 -08005641}
5642
NeilBrownec32a2b2009-03-31 15:17:38 +11005643/* This is called from the raid5d thread with mddev_lock held.
5644 * It makes config changes to the device.
5645 */
NeilBrownfd01b882011-10-11 16:47:53 +11005646static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005647{
NeilBrownd1688a62011-10-11 16:49:52 +11005648 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005649
5650 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5651
NeilBrownec32a2b2009-03-31 15:17:38 +11005652 if (mddev->delta_disks > 0) {
5653 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5654 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005655 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005656 } else {
5657 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005658 spin_lock_irq(&conf->device_lock);
5659 mddev->degraded = calc_degraded(conf);
5660 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005661 for (d = conf->raid_disks ;
5662 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005663 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005664 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005665 if (rdev &&
5666 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005667 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005668 rdev->raid_disk = -1;
5669 }
5670 }
NeilBrowncea9c222009-03-31 15:15:05 +11005671 }
NeilBrown88ce4932009-03-31 15:24:23 +11005672 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005673 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005674 mddev->reshape_position = MaxSector;
5675 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005676 }
5677}
5678
NeilBrownfd01b882011-10-11 16:47:53 +11005679static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005680{
NeilBrownd1688a62011-10-11 16:49:52 +11005681 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005682
5683 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005684 case 2: /* resume for a suspend */
5685 wake_up(&conf->wait_for_overlap);
5686 break;
5687
NeilBrown72626682005-09-09 16:23:54 -07005688 case 1: /* stop all writes */
5689 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005690 /* '2' tells resync/reshape to pause so that all
5691 * active stripes can drain
5692 */
5693 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005694 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005695 atomic_read(&conf->active_stripes) == 0 &&
5696 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005697 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005698 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005699 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005700 /* allow reshape to continue */
5701 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005702 break;
5703
5704 case 0: /* re-enable writes */
5705 spin_lock_irq(&conf->device_lock);
5706 conf->quiesce = 0;
5707 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005708 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005709 spin_unlock_irq(&conf->device_lock);
5710 break;
5711 }
NeilBrown72626682005-09-09 16:23:54 -07005712}
NeilBrownb15c2e52006-01-06 00:20:16 -08005713
NeilBrownd562b0c2009-03-31 14:39:39 +11005714
NeilBrownfd01b882011-10-11 16:47:53 +11005715static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005716{
NeilBrowne373ab12011-10-11 16:48:59 +11005717 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005718 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005719
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005720 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005721 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005722 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5723 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005724 return ERR_PTR(-EINVAL);
5725 }
5726
NeilBrowne373ab12011-10-11 16:48:59 +11005727 sectors = raid0_conf->strip_zone[0].zone_end;
5728 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005729 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005730 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005731 mddev->new_layout = ALGORITHM_PARITY_N;
5732 mddev->new_chunk_sectors = mddev->chunk_sectors;
5733 mddev->raid_disks += 1;
5734 mddev->delta_disks = 1;
5735 /* make sure it will be not marked as dirty */
5736 mddev->recovery_cp = MaxSector;
5737
5738 return setup_conf(mddev);
5739}
5740
5741
NeilBrownfd01b882011-10-11 16:47:53 +11005742static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005743{
5744 int chunksect;
5745
5746 if (mddev->raid_disks != 2 ||
5747 mddev->degraded > 1)
5748 return ERR_PTR(-EINVAL);
5749
5750 /* Should check if there are write-behind devices? */
5751
5752 chunksect = 64*2; /* 64K by default */
5753
5754 /* The array must be an exact multiple of chunksize */
5755 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5756 chunksect >>= 1;
5757
5758 if ((chunksect<<9) < STRIPE_SIZE)
5759 /* array size does not allow a suitable chunk size */
5760 return ERR_PTR(-EINVAL);
5761
5762 mddev->new_level = 5;
5763 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005764 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005765
5766 return setup_conf(mddev);
5767}
5768
NeilBrownfd01b882011-10-11 16:47:53 +11005769static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005770{
5771 int new_layout;
5772
5773 switch (mddev->layout) {
5774 case ALGORITHM_LEFT_ASYMMETRIC_6:
5775 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5776 break;
5777 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5778 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5779 break;
5780 case ALGORITHM_LEFT_SYMMETRIC_6:
5781 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5782 break;
5783 case ALGORITHM_RIGHT_SYMMETRIC_6:
5784 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5785 break;
5786 case ALGORITHM_PARITY_0_6:
5787 new_layout = ALGORITHM_PARITY_0;
5788 break;
5789 case ALGORITHM_PARITY_N:
5790 new_layout = ALGORITHM_PARITY_N;
5791 break;
5792 default:
5793 return ERR_PTR(-EINVAL);
5794 }
5795 mddev->new_level = 5;
5796 mddev->new_layout = new_layout;
5797 mddev->delta_disks = -1;
5798 mddev->raid_disks -= 1;
5799 return setup_conf(mddev);
5800}
5801
NeilBrownd562b0c2009-03-31 14:39:39 +11005802
NeilBrownfd01b882011-10-11 16:47:53 +11005803static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005804{
NeilBrown88ce4932009-03-31 15:24:23 +11005805 /* For a 2-drive array, the layout and chunk size can be changed
5806 * immediately as not restriping is needed.
5807 * For larger arrays we record the new value - after validation
5808 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005809 */
NeilBrownd1688a62011-10-11 16:49:52 +11005810 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005811 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005812
NeilBrown597a7112009-06-18 08:47:42 +10005813 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005814 return -EINVAL;
5815 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005816 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005817 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005818 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005819 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005820 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005821 /* not factor of array size */
5822 return -EINVAL;
5823 }
5824
5825 /* They look valid */
5826
NeilBrown88ce4932009-03-31 15:24:23 +11005827 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005828 /* can make the change immediately */
5829 if (mddev->new_layout >= 0) {
5830 conf->algorithm = mddev->new_layout;
5831 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005832 }
5833 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005834 conf->chunk_sectors = new_chunk ;
5835 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005836 }
5837 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5838 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005839 }
NeilBrown50ac1682009-06-18 08:47:55 +10005840 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005841}
5842
NeilBrownfd01b882011-10-11 16:47:53 +11005843static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005844{
NeilBrown597a7112009-06-18 08:47:42 +10005845 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005846
NeilBrown597a7112009-06-18 08:47:42 +10005847 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005848 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005849 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005850 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005851 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005852 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005853 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005854 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005855 /* not factor of array size */
5856 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005857 }
NeilBrown88ce4932009-03-31 15:24:23 +11005858
5859 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005860 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005861}
5862
NeilBrownfd01b882011-10-11 16:47:53 +11005863static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005864{
5865 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005866 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005867 * raid1 - if there are two drives. We need to know the chunk size
5868 * raid4 - trivial - just use a raid4 layout.
5869 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005870 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005871 if (mddev->level == 0)
5872 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005873 if (mddev->level == 1)
5874 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005875 if (mddev->level == 4) {
5876 mddev->new_layout = ALGORITHM_PARITY_N;
5877 mddev->new_level = 5;
5878 return setup_conf(mddev);
5879 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005880 if (mddev->level == 6)
5881 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005882
5883 return ERR_PTR(-EINVAL);
5884}
5885
NeilBrownfd01b882011-10-11 16:47:53 +11005886static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005887{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005888 /* raid4 can take over:
5889 * raid0 - if there is only one strip zone
5890 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005891 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005892 if (mddev->level == 0)
5893 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005894 if (mddev->level == 5 &&
5895 mddev->layout == ALGORITHM_PARITY_N) {
5896 mddev->new_layout = 0;
5897 mddev->new_level = 4;
5898 return setup_conf(mddev);
5899 }
5900 return ERR_PTR(-EINVAL);
5901}
NeilBrownd562b0c2009-03-31 14:39:39 +11005902
NeilBrown84fc4b52011-10-11 16:49:58 +11005903static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005904
NeilBrownfd01b882011-10-11 16:47:53 +11005905static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005906{
5907 /* Currently can only take over a raid5. We map the
5908 * personality to an equivalent raid6 personality
5909 * with the Q block at the end.
5910 */
5911 int new_layout;
5912
5913 if (mddev->pers != &raid5_personality)
5914 return ERR_PTR(-EINVAL);
5915 if (mddev->degraded > 1)
5916 return ERR_PTR(-EINVAL);
5917 if (mddev->raid_disks > 253)
5918 return ERR_PTR(-EINVAL);
5919 if (mddev->raid_disks < 3)
5920 return ERR_PTR(-EINVAL);
5921
5922 switch (mddev->layout) {
5923 case ALGORITHM_LEFT_ASYMMETRIC:
5924 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5925 break;
5926 case ALGORITHM_RIGHT_ASYMMETRIC:
5927 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5928 break;
5929 case ALGORITHM_LEFT_SYMMETRIC:
5930 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5931 break;
5932 case ALGORITHM_RIGHT_SYMMETRIC:
5933 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5934 break;
5935 case ALGORITHM_PARITY_0:
5936 new_layout = ALGORITHM_PARITY_0_6;
5937 break;
5938 case ALGORITHM_PARITY_N:
5939 new_layout = ALGORITHM_PARITY_N;
5940 break;
5941 default:
5942 return ERR_PTR(-EINVAL);
5943 }
5944 mddev->new_level = 6;
5945 mddev->new_layout = new_layout;
5946 mddev->delta_disks = 1;
5947 mddev->raid_disks += 1;
5948 return setup_conf(mddev);
5949}
5950
5951
NeilBrown84fc4b52011-10-11 16:49:58 +11005952static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005953{
5954 .name = "raid6",
5955 .level = 6,
5956 .owner = THIS_MODULE,
5957 .make_request = make_request,
5958 .run = run,
5959 .stop = stop,
5960 .status = status,
5961 .error_handler = error,
5962 .hot_add_disk = raid5_add_disk,
5963 .hot_remove_disk= raid5_remove_disk,
5964 .spare_active = raid5_spare_active,
5965 .sync_request = sync_request,
5966 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005967 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005968 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005969 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005970 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005971 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005972 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005973};
NeilBrown84fc4b52011-10-11 16:49:58 +11005974static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975{
5976 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005977 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 .owner = THIS_MODULE,
5979 .make_request = make_request,
5980 .run = run,
5981 .stop = stop,
5982 .status = status,
5983 .error_handler = error,
5984 .hot_add_disk = raid5_add_disk,
5985 .hot_remove_disk= raid5_remove_disk,
5986 .spare_active = raid5_spare_active,
5987 .sync_request = sync_request,
5988 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005989 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005990 .check_reshape = raid5_check_reshape,
5991 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005992 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005993 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005994 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995};
5996
NeilBrown84fc4b52011-10-11 16:49:58 +11005997static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
NeilBrown2604b702006-01-06 00:20:36 -08005999 .name = "raid4",
6000 .level = 4,
6001 .owner = THIS_MODULE,
6002 .make_request = make_request,
6003 .run = run,
6004 .stop = stop,
6005 .status = status,
6006 .error_handler = error,
6007 .hot_add_disk = raid5_add_disk,
6008 .hot_remove_disk= raid5_remove_disk,
6009 .spare_active = raid5_spare_active,
6010 .sync_request = sync_request,
6011 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006012 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006013 .check_reshape = raid5_check_reshape,
6014 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006015 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006016 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006017 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006018};
6019
6020static int __init raid5_init(void)
6021{
NeilBrown16a53ec2006-06-26 00:27:38 -07006022 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006023 register_md_personality(&raid5_personality);
6024 register_md_personality(&raid4_personality);
6025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026}
6027
NeilBrown2604b702006-01-06 00:20:36 -08006028static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
NeilBrown16a53ec2006-06-26 00:27:38 -07006030 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006031 unregister_md_personality(&raid5_personality);
6032 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033}
6034
6035module_init(raid5_init);
6036module_exit(raid5_exit);
6037MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006038MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006040MODULE_ALIAS("md-raid5");
6041MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006042MODULE_ALIAS("md-level-5");
6043MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006044MODULE_ALIAS("md-personality-8"); /* RAID6 */
6045MODULE_ALIAS("md-raid6");
6046MODULE_ALIAS("md-level-6");
6047
6048/* This used to be two separate modules, they were: */
6049MODULE_ALIAS("raid5");
6050MODULE_ALIAS("raid6");