blob: 9e41ae37bd40828c6aa9961056d0288d48892ded [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 Lifab363b2012-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 Lifab363b2012-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)
Shaohua Li8811b592012-08-02 08:33:00 +1000474 && !test_bit(STRIPE_EXPANDING, &sh->state)
475 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else {
477 if (!test_bit(STRIPE_HANDLE, &sh->state))
478 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700479 if (list_empty(&sh->lru) &&
480 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700481 BUG();
482 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 }
485 } while (sh == NULL);
486
487 if (sh)
488 atomic_inc(&sh->count);
489
490 spin_unlock_irq(&conf->device_lock);
491 return sh;
492}
493
NeilBrown05616be2012-05-21 09:27:00 +1000494/* Determine if 'data_offset' or 'new_data_offset' should be used
495 * in this stripe_head.
496 */
497static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
498{
499 sector_t progress = conf->reshape_progress;
500 /* Need a memory barrier to make sure we see the value
501 * of conf->generation, or ->data_offset that was set before
502 * reshape_progress was updated.
503 */
504 smp_rmb();
505 if (progress == MaxSector)
506 return 0;
507 if (sh->generation == conf->generation - 1)
508 return 0;
509 /* We are in a reshape, and this is a new-generation stripe,
510 * so use new_data_offset.
511 */
512 return 1;
513}
514
NeilBrown6712ecf2007-09-27 12:47:43 +0200515static void
516raid5_end_read_request(struct bio *bi, int error);
517static void
518raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700519
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000520static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700521{
NeilBrownd1688a62011-10-11 16:49:52 +1100522 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700523 int i, disks = sh->disks;
524
525 might_sleep();
526
527 for (i = disks; i--; ) {
528 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100529 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100530 struct bio *bi, *rbi;
531 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200532 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
533 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
534 rw = WRITE_FUA;
535 else
536 rw = WRITE;
537 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700538 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100539 else if (test_and_clear_bit(R5_WantReplace,
540 &sh->dev[i].flags)) {
541 rw = WRITE;
542 replace_only = 1;
543 } else
Dan Williams91c00922007-01-02 13:52:30 -0700544 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000545 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
546 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700547
548 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100549 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700550
551 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100552 rbi->bi_rw = rw;
553 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700554 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100555 rbi->bi_end_io = raid5_end_write_request;
556 } else
Dan Williams91c00922007-01-02 13:52:30 -0700557 bi->bi_end_io = raid5_end_read_request;
558
559 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100560 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100561 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
562 rdev = rcu_dereference(conf->disks[i].rdev);
563 if (!rdev) {
564 rdev = rrdev;
565 rrdev = NULL;
566 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100567 if (rw & WRITE) {
568 if (replace_only)
569 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100570 if (rdev == rrdev)
571 /* We raced and saw duplicates */
572 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100573 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100574 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100575 rdev = rrdev;
576 rrdev = NULL;
577 }
NeilBrown977df362011-12-23 10:17:53 +1100578
Dan Williams91c00922007-01-02 13:52:30 -0700579 if (rdev && test_bit(Faulty, &rdev->flags))
580 rdev = NULL;
581 if (rdev)
582 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100583 if (rrdev && test_bit(Faulty, &rrdev->flags))
584 rrdev = NULL;
585 if (rrdev)
586 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700587 rcu_read_unlock();
588
NeilBrown73e92e52011-07-28 11:39:22 +1000589 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100590 * need to check for writes. We never accept write errors
591 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000592 */
593 while ((rw & WRITE) && rdev &&
594 test_bit(WriteErrorSeen, &rdev->flags)) {
595 sector_t first_bad;
596 int bad_sectors;
597 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
598 &first_bad, &bad_sectors);
599 if (!bad)
600 break;
601
602 if (bad < 0) {
603 set_bit(BlockedBadBlocks, &rdev->flags);
604 if (!conf->mddev->external &&
605 conf->mddev->flags) {
606 /* It is very unlikely, but we might
607 * still need to write out the
608 * bad block log - better give it
609 * a chance*/
610 md_check_recovery(conf->mddev);
611 }
majianpeng18507532012-07-03 12:11:54 +1000612 /*
613 * Because md_wait_for_blocked_rdev
614 * will dec nr_pending, we must
615 * increment it first.
616 */
617 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000618 md_wait_for_blocked_rdev(rdev, conf->mddev);
619 } else {
620 /* Acknowledged bad block - skip the write */
621 rdev_dec_pending(rdev, conf->mddev);
622 rdev = NULL;
623 }
624 }
625
Dan Williams91c00922007-01-02 13:52:30 -0700626 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100627 if (s->syncing || s->expanding || s->expanded
628 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700629 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
630
Dan Williams2b7497f2008-06-28 08:31:52 +1000631 set_bit(STRIPE_IO_STARTED, &sh->state);
632
Dan Williams91c00922007-01-02 13:52:30 -0700633 bi->bi_bdev = rdev->bdev;
634 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700635 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700636 bi->bi_rw, i);
637 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000638 if (use_new_offset(conf, sh))
639 bi->bi_sector = (sh->sector
640 + rdev->new_data_offset);
641 else
642 bi->bi_sector = (sh->sector
643 + rdev->data_offset);
Dan Williams91c00922007-01-02 13:52:30 -0700644 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700645 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700646 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
647 bi->bi_io_vec[0].bv_offset = 0;
648 bi->bi_size = STRIPE_SIZE;
649 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100650 if (rrdev)
651 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700652 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100653 }
654 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100655 if (s->syncing || s->expanding || s->expanded
656 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100657 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
658
659 set_bit(STRIPE_IO_STARTED, &sh->state);
660
661 rbi->bi_bdev = rrdev->bdev;
662 pr_debug("%s: for %llu schedule op %ld on "
663 "replacement disc %d\n",
664 __func__, (unsigned long long)sh->sector,
665 rbi->bi_rw, i);
666 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000667 if (use_new_offset(conf, sh))
668 rbi->bi_sector = (sh->sector
669 + rrdev->new_data_offset);
670 else
671 rbi->bi_sector = (sh->sector
672 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100673 rbi->bi_flags = 1 << BIO_UPTODATE;
674 rbi->bi_idx = 0;
675 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
676 rbi->bi_io_vec[0].bv_offset = 0;
677 rbi->bi_size = STRIPE_SIZE;
678 rbi->bi_next = NULL;
679 generic_make_request(rbi);
680 }
681 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000682 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700683 set_bit(STRIPE_DEGRADED, &sh->state);
684 pr_debug("skip op %ld on disc %d for sector %llu\n",
685 bi->bi_rw, i, (unsigned long long)sh->sector);
686 clear_bit(R5_LOCKED, &sh->dev[i].flags);
687 set_bit(STRIPE_HANDLE, &sh->state);
688 }
689 }
690}
691
692static struct dma_async_tx_descriptor *
693async_copy_data(int frombio, struct bio *bio, struct page *page,
694 sector_t sector, struct dma_async_tx_descriptor *tx)
695{
696 struct bio_vec *bvl;
697 struct page *bio_page;
698 int i;
699 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700700 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700701 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700702
703 if (bio->bi_sector >= sector)
704 page_offset = (signed)(bio->bi_sector - sector) * 512;
705 else
706 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700707
Dan Williams0403e382009-09-08 17:42:50 -0700708 if (frombio)
709 flags |= ASYNC_TX_FENCE;
710 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
711
Dan Williams91c00922007-01-02 13:52:30 -0700712 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000713 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700714 int clen;
715 int b_offset = 0;
716
717 if (page_offset < 0) {
718 b_offset = -page_offset;
719 page_offset += b_offset;
720 len -= b_offset;
721 }
722
723 if (len > 0 && page_offset + len > STRIPE_SIZE)
724 clen = STRIPE_SIZE - page_offset;
725 else
726 clen = len;
727
728 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000729 b_offset += bvl->bv_offset;
730 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700731 if (frombio)
732 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700733 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700734 else
735 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700736 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700737 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700738 /* chain the operations */
739 submit.depend_tx = tx;
740
Dan Williams91c00922007-01-02 13:52:30 -0700741 if (clen < len) /* hit end of page */
742 break;
743 page_offset += len;
744 }
745
746 return tx;
747}
748
749static void ops_complete_biofill(void *stripe_head_ref)
750{
751 struct stripe_head *sh = stripe_head_ref;
752 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100753 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700754 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700755
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700756 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700757 (unsigned long long)sh->sector);
758
759 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000760 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700761 for (i = sh->disks; i--; ) {
762 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700763
764 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700765 /* and check if we need to reply to a read request,
766 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000767 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700768 */
769 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700770 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700771
Dan Williams91c00922007-01-02 13:52:30 -0700772 BUG_ON(!dev->read);
773 rbi = dev->read;
774 dev->read = NULL;
775 while (rbi && rbi->bi_sector <
776 dev->sector + STRIPE_SECTORS) {
777 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200778 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700779 rbi->bi_next = return_bi;
780 return_bi = rbi;
781 }
Dan Williams91c00922007-01-02 13:52:30 -0700782 rbi = rbi2;
783 }
784 }
785 }
Dan Williams83de75c2008-06-28 08:31:58 +1000786 spin_unlock_irq(&conf->device_lock);
787 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700788
789 return_io(return_bi);
790
Dan Williamse4d84902007-09-24 10:06:13 -0700791 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700792 release_stripe(sh);
793}
794
795static void ops_run_biofill(struct stripe_head *sh)
796{
797 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100798 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700799 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700800 int i;
801
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700802 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700803 (unsigned long long)sh->sector);
804
805 for (i = sh->disks; i--; ) {
806 struct r5dev *dev = &sh->dev[i];
807 if (test_bit(R5_Wantfill, &dev->flags)) {
808 struct bio *rbi;
809 spin_lock_irq(&conf->device_lock);
810 dev->read = rbi = dev->toread;
811 dev->toread = NULL;
812 spin_unlock_irq(&conf->device_lock);
813 while (rbi && rbi->bi_sector <
814 dev->sector + STRIPE_SECTORS) {
815 tx = async_copy_data(0, rbi, dev->page,
816 dev->sector, tx);
817 rbi = r5_next_bio(rbi, dev->sector);
818 }
819 }
820 }
821
822 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700823 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
824 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700825}
826
Dan Williams4e7d2c02009-08-29 19:13:11 -0700827static void mark_target_uptodate(struct stripe_head *sh, int target)
828{
829 struct r5dev *tgt;
830
831 if (target < 0)
832 return;
833
834 tgt = &sh->dev[target];
835 set_bit(R5_UPTODATE, &tgt->flags);
836 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
837 clear_bit(R5_Wantcompute, &tgt->flags);
838}
839
Dan Williamsac6b53b2009-07-14 13:40:19 -0700840static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700841{
842 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700843
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700844 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700845 (unsigned long long)sh->sector);
846
Dan Williamsac6b53b2009-07-14 13:40:19 -0700847 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700848 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700849 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700850
Dan Williamsecc65c92008-06-28 08:31:57 +1000851 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
852 if (sh->check_state == check_state_compute_run)
853 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700854 set_bit(STRIPE_HANDLE, &sh->state);
855 release_stripe(sh);
856}
857
Dan Williamsd6f38f32009-07-14 11:50:52 -0700858/* return a pointer to the address conversion region of the scribble buffer */
859static addr_conv_t *to_addr_conv(struct stripe_head *sh,
860 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700861{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700862 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
863}
864
865static struct dma_async_tx_descriptor *
866ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
867{
Dan Williams91c00922007-01-02 13:52:30 -0700868 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700869 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700870 int target = sh->ops.target;
871 struct r5dev *tgt = &sh->dev[target];
872 struct page *xor_dest = tgt->page;
873 int count = 0;
874 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700875 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700876 int i;
877
878 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700879 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700880 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
881
882 for (i = disks; i--; )
883 if (i != target)
884 xor_srcs[count++] = sh->dev[i].page;
885
886 atomic_inc(&sh->count);
887
Dan Williams0403e382009-09-08 17:42:50 -0700888 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700889 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700890 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700891 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700892 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700893 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700894
Dan Williams91c00922007-01-02 13:52:30 -0700895 return tx;
896}
897
Dan Williamsac6b53b2009-07-14 13:40:19 -0700898/* set_syndrome_sources - populate source buffers for gen_syndrome
899 * @srcs - (struct page *) array of size sh->disks
900 * @sh - stripe_head to parse
901 *
902 * Populates srcs in proper layout order for the stripe and returns the
903 * 'count' of sources to be used in a call to async_gen_syndrome. The P
904 * destination buffer is recorded in srcs[count] and the Q destination
905 * is recorded in srcs[count+1]].
906 */
907static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
908{
909 int disks = sh->disks;
910 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
911 int d0_idx = raid6_d0(sh);
912 int count;
913 int i;
914
915 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100916 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700917
918 count = 0;
919 i = d0_idx;
920 do {
921 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
922
923 srcs[slot] = sh->dev[i].page;
924 i = raid6_next_disk(i, disks);
925 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700926
NeilBrowne4424fe2009-10-16 16:27:34 +1100927 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700928}
929
930static struct dma_async_tx_descriptor *
931ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
932{
933 int disks = sh->disks;
934 struct page **blocks = percpu->scribble;
935 int target;
936 int qd_idx = sh->qd_idx;
937 struct dma_async_tx_descriptor *tx;
938 struct async_submit_ctl submit;
939 struct r5dev *tgt;
940 struct page *dest;
941 int i;
942 int count;
943
944 if (sh->ops.target < 0)
945 target = sh->ops.target2;
946 else if (sh->ops.target2 < 0)
947 target = sh->ops.target;
948 else
949 /* we should only have one valid target */
950 BUG();
951 BUG_ON(target < 0);
952 pr_debug("%s: stripe %llu block: %d\n",
953 __func__, (unsigned long long)sh->sector, target);
954
955 tgt = &sh->dev[target];
956 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
957 dest = tgt->page;
958
959 atomic_inc(&sh->count);
960
961 if (target == qd_idx) {
962 count = set_syndrome_sources(blocks, sh);
963 blocks[count] = NULL; /* regenerating p is not necessary */
964 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700965 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
966 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700967 to_addr_conv(sh, percpu));
968 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
969 } else {
970 /* Compute any data- or p-drive using XOR */
971 count = 0;
972 for (i = disks; i-- ; ) {
973 if (i == target || i == qd_idx)
974 continue;
975 blocks[count++] = sh->dev[i].page;
976 }
977
Dan Williams0403e382009-09-08 17:42:50 -0700978 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
979 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700980 to_addr_conv(sh, percpu));
981 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
982 }
983
984 return tx;
985}
986
987static struct dma_async_tx_descriptor *
988ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
989{
990 int i, count, disks = sh->disks;
991 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
992 int d0_idx = raid6_d0(sh);
993 int faila = -1, failb = -1;
994 int target = sh->ops.target;
995 int target2 = sh->ops.target2;
996 struct r5dev *tgt = &sh->dev[target];
997 struct r5dev *tgt2 = &sh->dev[target2];
998 struct dma_async_tx_descriptor *tx;
999 struct page **blocks = percpu->scribble;
1000 struct async_submit_ctl submit;
1001
1002 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1003 __func__, (unsigned long long)sh->sector, target, target2);
1004 BUG_ON(target < 0 || target2 < 0);
1005 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1006 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1007
Dan Williams6c910a72009-09-16 12:24:54 -07001008 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009 * slot number conversion for 'faila' and 'failb'
1010 */
1011 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001012 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001013 count = 0;
1014 i = d0_idx;
1015 do {
1016 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1017
1018 blocks[slot] = sh->dev[i].page;
1019
1020 if (i == target)
1021 faila = slot;
1022 if (i == target2)
1023 failb = slot;
1024 i = raid6_next_disk(i, disks);
1025 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001026
1027 BUG_ON(faila == failb);
1028 if (failb < faila)
1029 swap(faila, failb);
1030 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1031 __func__, (unsigned long long)sh->sector, faila, failb);
1032
1033 atomic_inc(&sh->count);
1034
1035 if (failb == syndrome_disks+1) {
1036 /* Q disk is one of the missing disks */
1037 if (faila == syndrome_disks) {
1038 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001039 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1040 ops_complete_compute, sh,
1041 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001042 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001043 STRIPE_SIZE, &submit);
1044 } else {
1045 struct page *dest;
1046 int data_target;
1047 int qd_idx = sh->qd_idx;
1048
1049 /* Missing D+Q: recompute D from P, then recompute Q */
1050 if (target == qd_idx)
1051 data_target = target2;
1052 else
1053 data_target = target;
1054
1055 count = 0;
1056 for (i = disks; i-- ; ) {
1057 if (i == data_target || i == qd_idx)
1058 continue;
1059 blocks[count++] = sh->dev[i].page;
1060 }
1061 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001062 init_async_submit(&submit,
1063 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1064 NULL, NULL, NULL,
1065 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001066 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1067 &submit);
1068
1069 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001070 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1071 ops_complete_compute, sh,
1072 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001073 return async_gen_syndrome(blocks, 0, count+2,
1074 STRIPE_SIZE, &submit);
1075 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001076 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001077 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1078 ops_complete_compute, sh,
1079 to_addr_conv(sh, percpu));
1080 if (failb == syndrome_disks) {
1081 /* We're missing D+P. */
1082 return async_raid6_datap_recov(syndrome_disks+2,
1083 STRIPE_SIZE, faila,
1084 blocks, &submit);
1085 } else {
1086 /* We're missing D+D. */
1087 return async_raid6_2data_recov(syndrome_disks+2,
1088 STRIPE_SIZE, faila, failb,
1089 blocks, &submit);
1090 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091 }
1092}
1093
1094
Dan Williams91c00922007-01-02 13:52:30 -07001095static void ops_complete_prexor(void *stripe_head_ref)
1096{
1097 struct stripe_head *sh = stripe_head_ref;
1098
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001099 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001100 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001101}
1102
1103static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001104ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1105 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001106{
Dan Williams91c00922007-01-02 13:52:30 -07001107 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001108 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001109 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001110 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001111
1112 /* existing parity data subtracted */
1113 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1114
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001115 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001116 (unsigned long long)sh->sector);
1117
1118 for (i = disks; i--; ) {
1119 struct r5dev *dev = &sh->dev[i];
1120 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001121 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001122 xor_srcs[count++] = dev->page;
1123 }
1124
Dan Williams0403e382009-09-08 17:42:50 -07001125 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001126 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001127 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001128
1129 return tx;
1130}
1131
1132static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001133ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001134{
1135 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001136 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001137
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001138 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001139 (unsigned long long)sh->sector);
1140
1141 for (i = disks; i--; ) {
1142 struct r5dev *dev = &sh->dev[i];
1143 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001144
Dan Williamsd8ee0722008-06-28 08:32:06 +10001145 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001146 struct bio *wbi;
1147
NeilBrowncbe47ec2011-07-26 11:20:35 +10001148 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001149 chosen = dev->towrite;
1150 dev->towrite = NULL;
1151 BUG_ON(dev->written);
1152 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001153 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001154
1155 while (wbi && wbi->bi_sector <
1156 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001157 if (wbi->bi_rw & REQ_FUA)
1158 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001159 if (wbi->bi_rw & REQ_SYNC)
1160 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001161 tx = async_copy_data(1, wbi, dev->page,
1162 dev->sector, tx);
1163 wbi = r5_next_bio(wbi, dev->sector);
1164 }
1165 }
1166 }
1167
1168 return tx;
1169}
1170
Dan Williamsac6b53b2009-07-14 13:40:19 -07001171static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001172{
1173 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001174 int disks = sh->disks;
1175 int pd_idx = sh->pd_idx;
1176 int qd_idx = sh->qd_idx;
1177 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001178 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001179
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001180 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001181 (unsigned long long)sh->sector);
1182
Shaohua Libc0934f2012-05-22 13:55:05 +10001183 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001184 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001185 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1186 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001187
Dan Williams91c00922007-01-02 13:52:30 -07001188 for (i = disks; i--; ) {
1189 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001190
Tejun Heoe9c74692010-09-03 11:56:18 +02001191 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001192 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001193 if (fua)
1194 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001195 if (sync)
1196 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001197 }
Dan Williams91c00922007-01-02 13:52:30 -07001198 }
1199
Dan Williamsd8ee0722008-06-28 08:32:06 +10001200 if (sh->reconstruct_state == reconstruct_state_drain_run)
1201 sh->reconstruct_state = reconstruct_state_drain_result;
1202 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1203 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1204 else {
1205 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1206 sh->reconstruct_state = reconstruct_state_result;
1207 }
Dan Williams91c00922007-01-02 13:52:30 -07001208
1209 set_bit(STRIPE_HANDLE, &sh->state);
1210 release_stripe(sh);
1211}
1212
1213static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001214ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1215 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001216{
Dan Williams91c00922007-01-02 13:52:30 -07001217 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001218 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001219 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001220 int count = 0, pd_idx = sh->pd_idx, i;
1221 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001222 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001223 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001224
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001225 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001226 (unsigned long long)sh->sector);
1227
1228 /* check if prexor is active which means only process blocks
1229 * that are part of a read-modify-write (written)
1230 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001231 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1232 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001233 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1234 for (i = disks; i--; ) {
1235 struct r5dev *dev = &sh->dev[i];
1236 if (dev->written)
1237 xor_srcs[count++] = dev->page;
1238 }
1239 } else {
1240 xor_dest = sh->dev[pd_idx].page;
1241 for (i = disks; i--; ) {
1242 struct r5dev *dev = &sh->dev[i];
1243 if (i != pd_idx)
1244 xor_srcs[count++] = dev->page;
1245 }
1246 }
1247
Dan Williams91c00922007-01-02 13:52:30 -07001248 /* 1/ if we prexor'd then the dest is reused as a source
1249 * 2/ if we did not prexor then we are redoing the parity
1250 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1251 * for the synchronous xor case
1252 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001253 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001254 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1255
1256 atomic_inc(&sh->count);
1257
Dan Williamsac6b53b2009-07-14 13:40:19 -07001258 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001259 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001260 if (unlikely(count == 1))
1261 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1262 else
1263 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001264}
1265
Dan Williamsac6b53b2009-07-14 13:40:19 -07001266static void
1267ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1268 struct dma_async_tx_descriptor *tx)
1269{
1270 struct async_submit_ctl submit;
1271 struct page **blocks = percpu->scribble;
1272 int count;
1273
1274 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1275
1276 count = set_syndrome_sources(blocks, sh);
1277
1278 atomic_inc(&sh->count);
1279
1280 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1281 sh, to_addr_conv(sh, percpu));
1282 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001283}
1284
1285static void ops_complete_check(void *stripe_head_ref)
1286{
1287 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001288
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001289 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001290 (unsigned long long)sh->sector);
1291
Dan Williamsecc65c92008-06-28 08:31:57 +10001292 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001293 set_bit(STRIPE_HANDLE, &sh->state);
1294 release_stripe(sh);
1295}
1296
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001298{
Dan Williams91c00922007-01-02 13:52:30 -07001299 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001300 int pd_idx = sh->pd_idx;
1301 int qd_idx = sh->qd_idx;
1302 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001303 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001304 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001305 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001306 int count;
1307 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001308
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001309 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001310 (unsigned long long)sh->sector);
1311
Dan Williamsac6b53b2009-07-14 13:40:19 -07001312 count = 0;
1313 xor_dest = sh->dev[pd_idx].page;
1314 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001315 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316 if (i == pd_idx || i == qd_idx)
1317 continue;
1318 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001319 }
1320
Dan Williamsd6f38f32009-07-14 11:50:52 -07001321 init_async_submit(&submit, 0, NULL, NULL, NULL,
1322 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001323 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001324 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001325
Dan Williams91c00922007-01-02 13:52:30 -07001326 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001327 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1328 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001329}
1330
Dan Williamsac6b53b2009-07-14 13:40:19 -07001331static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1332{
1333 struct page **srcs = percpu->scribble;
1334 struct async_submit_ctl submit;
1335 int count;
1336
1337 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1338 (unsigned long long)sh->sector, checkp);
1339
1340 count = set_syndrome_sources(srcs, sh);
1341 if (!checkp)
1342 srcs[count] = NULL;
1343
1344 atomic_inc(&sh->count);
1345 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1346 sh, to_addr_conv(sh, percpu));
1347 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1348 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1349}
1350
Dan Williams417b8d42009-10-16 16:25:22 +11001351static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001352{
1353 int overlap_clear = 0, i, disks = sh->disks;
1354 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001355 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 struct raid5_percpu *percpu;
1358 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001359
Dan Williamsd6f38f32009-07-14 11:50:52 -07001360 cpu = get_cpu();
1361 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001362 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001363 ops_run_biofill(sh);
1364 overlap_clear++;
1365 }
1366
Dan Williams7b3a8712008-06-28 08:32:09 +10001367 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001368 if (level < 6)
1369 tx = ops_run_compute5(sh, percpu);
1370 else {
1371 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1372 tx = ops_run_compute6_1(sh, percpu);
1373 else
1374 tx = ops_run_compute6_2(sh, percpu);
1375 }
1376 /* terminate the chain if reconstruct is not set to be run */
1377 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001378 async_tx_ack(tx);
1379 }
Dan Williams91c00922007-01-02 13:52:30 -07001380
Dan Williams600aa102008-06-28 08:32:05 +10001381 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001382 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001383
Dan Williams600aa102008-06-28 08:32:05 +10001384 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001385 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001386 overlap_clear++;
1387 }
1388
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1390 if (level < 6)
1391 ops_run_reconstruct5(sh, percpu, tx);
1392 else
1393 ops_run_reconstruct6(sh, percpu, tx);
1394 }
Dan Williams91c00922007-01-02 13:52:30 -07001395
Dan Williamsac6b53b2009-07-14 13:40:19 -07001396 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1397 if (sh->check_state == check_state_run)
1398 ops_run_check_p(sh, percpu);
1399 else if (sh->check_state == check_state_run_q)
1400 ops_run_check_pq(sh, percpu, 0);
1401 else if (sh->check_state == check_state_run_pq)
1402 ops_run_check_pq(sh, percpu, 1);
1403 else
1404 BUG();
1405 }
Dan Williams91c00922007-01-02 13:52:30 -07001406
Dan Williams91c00922007-01-02 13:52:30 -07001407 if (overlap_clear)
1408 for (i = disks; i--; ) {
1409 struct r5dev *dev = &sh->dev[i];
1410 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1411 wake_up(&sh->raid_conf->wait_for_overlap);
1412 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001413 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001414}
1415
Dan Williams417b8d42009-10-16 16:25:22 +11001416#ifdef CONFIG_MULTICORE_RAID456
1417static void async_run_ops(void *param, async_cookie_t cookie)
1418{
1419 struct stripe_head *sh = param;
1420 unsigned long ops_request = sh->ops.request;
1421
1422 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1423 wake_up(&sh->ops.wait_for_ops);
1424
1425 __raid_run_ops(sh, ops_request);
1426 release_stripe(sh);
1427}
1428
1429static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1430{
1431 /* since handle_stripe can be called outside of raid5d context
1432 * we need to ensure sh->ops.request is de-staged before another
1433 * request arrives
1434 */
1435 wait_event(sh->ops.wait_for_ops,
1436 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1437 sh->ops.request = ops_request;
1438
1439 atomic_inc(&sh->count);
1440 async_schedule(async_run_ops, sh);
1441}
1442#else
1443#define raid_run_ops __raid_run_ops
1444#endif
1445
NeilBrownd1688a62011-10-11 16:49:52 +11001446static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001449 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001450 if (!sh)
1451 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001452
NeilBrown3f294f42005-11-08 21:39:25 -08001453 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001454 #ifdef CONFIG_MULTICORE_RAID456
1455 init_waitqueue_head(&sh->ops.wait_for_ops);
1456 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001457
NeilBrowne4e11e32010-06-16 16:45:16 +10001458 if (grow_buffers(sh)) {
1459 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001460 kmem_cache_free(conf->slab_cache, sh);
1461 return 0;
1462 }
1463 /* we just created an active stripe so... */
1464 atomic_set(&sh->count, 1);
1465 atomic_inc(&conf->active_stripes);
1466 INIT_LIST_HEAD(&sh->lru);
1467 release_stripe(sh);
1468 return 1;
1469}
1470
NeilBrownd1688a62011-10-11 16:49:52 +11001471static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001472{
Christoph Lametere18b8902006-12-06 20:33:20 -08001473 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001474 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
NeilBrownf4be6b42010-06-01 19:37:25 +10001476 if (conf->mddev->gendisk)
1477 sprintf(conf->cache_name[0],
1478 "raid%d-%s", conf->level, mdname(conf->mddev));
1479 else
1480 sprintf(conf->cache_name[0],
1481 "raid%d-%p", conf->level, conf->mddev);
1482 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1483
NeilBrownad01c9e2006-03-27 01:18:07 -08001484 conf->active_name = 0;
1485 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001487 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (!sc)
1489 return 1;
1490 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001491 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001492 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001493 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
1496}
NeilBrown29269552006-03-27 01:18:10 -08001497
Dan Williamsd6f38f32009-07-14 11:50:52 -07001498/**
1499 * scribble_len - return the required size of the scribble region
1500 * @num - total number of disks in the array
1501 *
1502 * The size must be enough to contain:
1503 * 1/ a struct page pointer for each device in the array +2
1504 * 2/ room to convert each entry in (1) to its corresponding dma
1505 * (dma_map_page()) or page (page_address()) address.
1506 *
1507 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1508 * calculate over all devices (not just the data blocks), using zeros in place
1509 * of the P and Q blocks.
1510 */
1511static size_t scribble_len(int num)
1512{
1513 size_t len;
1514
1515 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1516
1517 return len;
1518}
1519
NeilBrownd1688a62011-10-11 16:49:52 +11001520static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001521{
1522 /* Make all the stripes able to hold 'newsize' devices.
1523 * New slots in each stripe get 'page' set to a new page.
1524 *
1525 * This happens in stages:
1526 * 1/ create a new kmem_cache and allocate the required number of
1527 * stripe_heads.
1528 * 2/ gather all the old stripe_heads and tranfer the pages across
1529 * to the new stripe_heads. This will have the side effect of
1530 * freezing the array as once all stripe_heads have been collected,
1531 * no IO will be possible. Old stripe heads are freed once their
1532 * pages have been transferred over, and the old kmem_cache is
1533 * freed when all stripes are done.
1534 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1535 * we simple return a failre status - no need to clean anything up.
1536 * 4/ allocate new pages for the new slots in the new stripe_heads.
1537 * If this fails, we don't bother trying the shrink the
1538 * stripe_heads down again, we just leave them as they are.
1539 * As each stripe_head is processed the new one is released into
1540 * active service.
1541 *
1542 * Once step2 is started, we cannot afford to wait for a write,
1543 * so we use GFP_NOIO allocations.
1544 */
1545 struct stripe_head *osh, *nsh;
1546 LIST_HEAD(newstripes);
1547 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001548 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001549 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001550 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001551 int i;
1552
1553 if (newsize <= conf->pool_size)
1554 return 0; /* never bother to shrink */
1555
Dan Williamsb5470dc2008-06-27 21:44:04 -07001556 err = md_allow_write(conf->mddev);
1557 if (err)
1558 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001559
NeilBrownad01c9e2006-03-27 01:18:07 -08001560 /* Step 1 */
1561 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1562 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001563 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001564 if (!sc)
1565 return -ENOMEM;
1566
1567 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001568 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001569 if (!nsh)
1570 break;
1571
NeilBrownad01c9e2006-03-27 01:18:07 -08001572 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001573 #ifdef CONFIG_MULTICORE_RAID456
1574 init_waitqueue_head(&nsh->ops.wait_for_ops);
1575 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001576
1577 list_add(&nsh->lru, &newstripes);
1578 }
1579 if (i) {
1580 /* didn't get enough, give up */
1581 while (!list_empty(&newstripes)) {
1582 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1583 list_del(&nsh->lru);
1584 kmem_cache_free(sc, nsh);
1585 }
1586 kmem_cache_destroy(sc);
1587 return -ENOMEM;
1588 }
1589 /* Step 2 - Must use GFP_NOIO now.
1590 * OK, we have enough stripes, start collecting inactive
1591 * stripes and copying them over
1592 */
1593 list_for_each_entry(nsh, &newstripes, lru) {
1594 spin_lock_irq(&conf->device_lock);
1595 wait_event_lock_irq(conf->wait_for_stripe,
1596 !list_empty(&conf->inactive_list),
1597 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001598 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001599 osh = get_free_stripe(conf);
1600 spin_unlock_irq(&conf->device_lock);
1601 atomic_set(&nsh->count, 1);
1602 for(i=0; i<conf->pool_size; i++)
1603 nsh->dev[i].page = osh->dev[i].page;
1604 for( ; i<newsize; i++)
1605 nsh->dev[i].page = NULL;
1606 kmem_cache_free(conf->slab_cache, osh);
1607 }
1608 kmem_cache_destroy(conf->slab_cache);
1609
1610 /* Step 3.
1611 * At this point, we are holding all the stripes so the array
1612 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001613 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001614 */
1615 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1616 if (ndisks) {
1617 for (i=0; i<conf->raid_disks; i++)
1618 ndisks[i] = conf->disks[i];
1619 kfree(conf->disks);
1620 conf->disks = ndisks;
1621 } else
1622 err = -ENOMEM;
1623
Dan Williamsd6f38f32009-07-14 11:50:52 -07001624 get_online_cpus();
1625 conf->scribble_len = scribble_len(newsize);
1626 for_each_present_cpu(cpu) {
1627 struct raid5_percpu *percpu;
1628 void *scribble;
1629
1630 percpu = per_cpu_ptr(conf->percpu, cpu);
1631 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1632
1633 if (scribble) {
1634 kfree(percpu->scribble);
1635 percpu->scribble = scribble;
1636 } else {
1637 err = -ENOMEM;
1638 break;
1639 }
1640 }
1641 put_online_cpus();
1642
NeilBrownad01c9e2006-03-27 01:18:07 -08001643 /* Step 4, return new stripes to service */
1644 while(!list_empty(&newstripes)) {
1645 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1646 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001647
NeilBrownad01c9e2006-03-27 01:18:07 -08001648 for (i=conf->raid_disks; i < newsize; i++)
1649 if (nsh->dev[i].page == NULL) {
1650 struct page *p = alloc_page(GFP_NOIO);
1651 nsh->dev[i].page = p;
1652 if (!p)
1653 err = -ENOMEM;
1654 }
1655 release_stripe(nsh);
1656 }
1657 /* critical section pass, GFP_NOIO no longer needed */
1658
1659 conf->slab_cache = sc;
1660 conf->active_name = 1-conf->active_name;
1661 conf->pool_size = newsize;
1662 return err;
1663}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
NeilBrownd1688a62011-10-11 16:49:52 +11001665static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct stripe_head *sh;
1668
NeilBrown3f294f42005-11-08 21:39:25 -08001669 spin_lock_irq(&conf->device_lock);
1670 sh = get_free_stripe(conf);
1671 spin_unlock_irq(&conf->device_lock);
1672 if (!sh)
1673 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001674 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001675 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001676 kmem_cache_free(conf->slab_cache, sh);
1677 atomic_dec(&conf->active_stripes);
1678 return 1;
1679}
1680
NeilBrownd1688a62011-10-11 16:49:52 +11001681static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001682{
1683 while (drop_one_stripe(conf))
1684 ;
1685
NeilBrown29fc7e32006-02-03 03:03:41 -08001686 if (conf->slab_cache)
1687 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 conf->slab_cache = NULL;
1689}
1690
NeilBrown6712ecf2007-09-27 12:47:43 +02001691static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
NeilBrown99c0fb52009-03-31 14:39:38 +11001693 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001694 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001695 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001697 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001698 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001699 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 for (i=0 ; i<disks; i++)
1702 if (bi == &sh->dev[i].req)
1703 break;
1704
Dan Williams45b42332007-07-09 11:56:43 -07001705 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1706 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 uptodate);
1708 if (i == disks) {
1709 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
NeilBrown14a75d32011-12-23 10:17:52 +11001712 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001713 /* If replacement finished while this request was outstanding,
1714 * 'replacement' might be NULL already.
1715 * In that case it moved down to 'rdev'.
1716 * rdev is not removed until all requests are finished.
1717 */
NeilBrown14a75d32011-12-23 10:17:52 +11001718 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001719 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001720 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
NeilBrown05616be2012-05-21 09:27:00 +10001722 if (use_new_offset(conf, sh))
1723 s = sh->sector + rdev->new_data_offset;
1724 else
1725 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001728 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001729 /* Note that this cannot happen on a
1730 * replacement device. We just fail those on
1731 * any error
1732 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001733 printk_ratelimited(
1734 KERN_INFO
1735 "md/raid:%s: read error corrected"
1736 " (%lu sectors at %llu on %s)\n",
1737 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001738 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001739 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001740 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001741 clear_bit(R5_ReadError, &sh->dev[i].flags);
1742 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1743 }
NeilBrown14a75d32011-12-23 10:17:52 +11001744 if (atomic_read(&rdev->read_errors))
1745 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001747 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001748 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001749 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001752 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001753 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1754 printk_ratelimited(
1755 KERN_WARNING
1756 "md/raid:%s: read error on replacement device "
1757 "(sector %llu on %s).\n",
1758 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001759 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001760 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001761 else if (conf->mddev->degraded >= conf->max_degraded) {
1762 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001763 printk_ratelimited(
1764 KERN_WARNING
1765 "md/raid:%s: read error not correctable "
1766 "(sector %llu on %s).\n",
1767 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001768 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001769 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001770 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001771 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001772 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001773 printk_ratelimited(
1774 KERN_WARNING
1775 "md/raid:%s: read error NOT corrected!! "
1776 "(sector %llu on %s).\n",
1777 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001778 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001779 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001780 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001781 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001782 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001783 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001784 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001785 else
1786 retry = 1;
1787 if (retry)
1788 set_bit(R5_ReadError, &sh->dev[i].flags);
1789 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001790 clear_bit(R5_ReadError, &sh->dev[i].flags);
1791 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001792 if (!(set_bad
1793 && test_bit(In_sync, &rdev->flags)
1794 && rdev_set_badblocks(
1795 rdev, sh->sector, STRIPE_SECTORS, 0)))
1796 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
NeilBrown14a75d32011-12-23 10:17:52 +11001799 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1801 set_bit(STRIPE_HANDLE, &sh->state);
1802 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
NeilBrownd710e132008-10-13 11:55:12 +11001805static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
NeilBrown99c0fb52009-03-31 14:39:38 +11001807 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001808 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001809 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001810 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001812 sector_t first_bad;
1813 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001814 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
NeilBrown977df362011-12-23 10:17:53 +11001816 for (i = 0 ; i < disks; i++) {
1817 if (bi == &sh->dev[i].req) {
1818 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 break;
NeilBrown977df362011-12-23 10:17:53 +11001820 }
1821 if (bi == &sh->dev[i].rreq) {
1822 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001823 if (rdev)
1824 replacement = 1;
1825 else
1826 /* rdev was removed and 'replacement'
1827 * replaced it. rdev is not removed
1828 * until all requests are finished.
1829 */
1830 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001831 break;
1832 }
1833 }
Dan Williams45b42332007-07-09 11:56:43 -07001834 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1836 uptodate);
1837 if (i == disks) {
1838 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001839 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
NeilBrown977df362011-12-23 10:17:53 +11001842 if (replacement) {
1843 if (!uptodate)
1844 md_error(conf->mddev, rdev);
1845 else if (is_badblock(rdev, sh->sector,
1846 STRIPE_SECTORS,
1847 &first_bad, &bad_sectors))
1848 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1849 } else {
1850 if (!uptodate) {
1851 set_bit(WriteErrorSeen, &rdev->flags);
1852 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001853 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1854 set_bit(MD_RECOVERY_NEEDED,
1855 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001856 } else if (is_badblock(rdev, sh->sector,
1857 STRIPE_SECTORS,
1858 &first_bad, &bad_sectors))
1859 set_bit(R5_MadeGood, &sh->dev[i].flags);
1860 }
1861 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
NeilBrown977df362011-12-23 10:17:53 +11001863 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1864 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001866 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
NeilBrown784052e2009-03-31 15:19:07 +11001869static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
NeilBrown784052e2009-03-31 15:19:07 +11001871static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct r5dev *dev = &sh->dev[i];
1874
1875 bio_init(&dev->req);
1876 dev->req.bi_io_vec = &dev->vec;
1877 dev->req.bi_vcnt++;
1878 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001880 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
NeilBrown977df362011-12-23 10:17:53 +11001882 bio_init(&dev->rreq);
1883 dev->rreq.bi_io_vec = &dev->rvec;
1884 dev->rreq.bi_vcnt++;
1885 dev->rreq.bi_max_vecs++;
1886 dev->rreq.bi_private = sh;
1887 dev->rvec.bv_page = dev->page;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001890 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
NeilBrownfd01b882011-10-11 16:47:53 +11001893static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001896 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001897 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001898 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
NeilBrown908f4fb2011-12-23 10:17:50 +11001900 spin_lock_irqsave(&conf->device_lock, flags);
1901 clear_bit(In_sync, &rdev->flags);
1902 mddev->degraded = calc_degraded(conf);
1903 spin_unlock_irqrestore(&conf->device_lock, flags);
1904 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1905
NeilBrownde393cd2011-07-28 11:31:48 +10001906 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001907 set_bit(Faulty, &rdev->flags);
1908 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1909 printk(KERN_ALERT
1910 "md/raid:%s: Disk failure on %s, disabling device.\n"
1911 "md/raid:%s: Operation continuing on %d devices.\n",
1912 mdname(mddev),
1913 bdevname(rdev->bdev, b),
1914 mdname(mddev),
1915 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918/*
1919 * Input: a 'big' sector number,
1920 * Output: index of the data and parity disk, and the sector # in them.
1921 */
NeilBrownd1688a62011-10-11 16:49:52 +11001922static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001923 int previous, int *dd_idx,
1924 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001926 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001927 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001929 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001930 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001932 int algorithm = previous ? conf->prev_algo
1933 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001934 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1935 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001936 int raid_disks = previous ? conf->previous_raid_disks
1937 : conf->raid_disks;
1938 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 /* First compute the information on this sector */
1941
1942 /*
1943 * Compute the chunk number and the sector offset inside the chunk
1944 */
1945 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1946 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /*
1949 * Compute the stripe number
1950 */
NeilBrown35f2a592010-04-20 14:13:34 +10001951 stripe = chunk_number;
1952 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001953 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /*
1955 * Select the parity disk based on the user selected algorithm.
1956 */
NeilBrown84789552011-07-27 11:00:36 +10001957 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001958 switch(conf->level) {
1959 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001960 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001961 break;
1962 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001963 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001965 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001966 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 (*dd_idx)++;
1968 break;
1969 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001970 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001971 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 (*dd_idx)++;
1973 break;
1974 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001975 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001976 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001979 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001980 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001982 case ALGORITHM_PARITY_0:
1983 pd_idx = 0;
1984 (*dd_idx)++;
1985 break;
1986 case ALGORITHM_PARITY_N:
1987 pd_idx = data_disks;
1988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001990 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001991 }
1992 break;
1993 case 6:
1994
NeilBrowne183eae2009-03-31 15:20:22 +11001995 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001996 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001997 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001998 qd_idx = pd_idx + 1;
1999 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002000 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002001 qd_idx = 0;
2002 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002003 (*dd_idx) += 2; /* D D P Q D */
2004 break;
2005 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002006 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002007 qd_idx = pd_idx + 1;
2008 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002010 qd_idx = 0;
2011 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002012 (*dd_idx) += 2; /* D D P Q D */
2013 break;
2014 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002015 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002016 qd_idx = (pd_idx + 1) % raid_disks;
2017 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002018 break;
2019 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002020 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002021 qd_idx = (pd_idx + 1) % raid_disks;
2022 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002024
2025 case ALGORITHM_PARITY_0:
2026 pd_idx = 0;
2027 qd_idx = 1;
2028 (*dd_idx) += 2;
2029 break;
2030 case ALGORITHM_PARITY_N:
2031 pd_idx = data_disks;
2032 qd_idx = data_disks + 1;
2033 break;
2034
2035 case ALGORITHM_ROTATING_ZERO_RESTART:
2036 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2037 * of blocks for computing Q is different.
2038 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002040 qd_idx = pd_idx + 1;
2041 if (pd_idx == raid_disks-1) {
2042 (*dd_idx)++; /* Q D D D P */
2043 qd_idx = 0;
2044 } else if (*dd_idx >= pd_idx)
2045 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002046 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 break;
2048
2049 case ALGORITHM_ROTATING_N_RESTART:
2050 /* Same a left_asymmetric, by first stripe is
2051 * D D D P Q rather than
2052 * Q D D D P
2053 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002054 stripe2 += 1;
2055 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002056 qd_idx = pd_idx + 1;
2057 if (pd_idx == raid_disks-1) {
2058 (*dd_idx)++; /* Q D D D P */
2059 qd_idx = 0;
2060 } else if (*dd_idx >= pd_idx)
2061 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002062 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002063 break;
2064
2065 case ALGORITHM_ROTATING_N_CONTINUE:
2066 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002067 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002068 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2069 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002070 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002071 break;
2072
2073 case ALGORITHM_LEFT_ASYMMETRIC_6:
2074 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002075 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002076 if (*dd_idx >= pd_idx)
2077 (*dd_idx)++;
2078 qd_idx = raid_disks - 1;
2079 break;
2080
2081 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002082 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002083 if (*dd_idx >= pd_idx)
2084 (*dd_idx)++;
2085 qd_idx = raid_disks - 1;
2086 break;
2087
2088 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002089 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002090 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2091 qd_idx = raid_disks - 1;
2092 break;
2093
2094 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002095 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2097 qd_idx = raid_disks - 1;
2098 break;
2099
2100 case ALGORITHM_PARITY_0_6:
2101 pd_idx = 0;
2102 (*dd_idx)++;
2103 qd_idx = raid_disks - 1;
2104 break;
2105
NeilBrown16a53ec2006-06-26 00:27:38 -07002106 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002107 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002108 }
2109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111
NeilBrown911d4ee2009-03-31 14:39:38 +11002112 if (sh) {
2113 sh->pd_idx = pd_idx;
2114 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002115 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 /*
2118 * Finally, compute the new sector number
2119 */
2120 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2121 return new_sector;
2122}
2123
2124
NeilBrown784052e2009-03-31 15:19:07 +11002125static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
NeilBrownd1688a62011-10-11 16:49:52 +11002127 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002128 int raid_disks = sh->disks;
2129 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002131 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2132 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002133 int algorithm = previous ? conf->prev_algo
2134 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 sector_t stripe;
2136 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002137 sector_t chunk_number;
2138 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002140 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
NeilBrown16a53ec2006-06-26 00:27:38 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2144 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
NeilBrown16a53ec2006-06-26 00:27:38 -07002146 if (i == sh->pd_idx)
2147 return 0;
2148 switch(conf->level) {
2149 case 4: break;
2150 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002151 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 case ALGORITHM_LEFT_ASYMMETRIC:
2153 case ALGORITHM_RIGHT_ASYMMETRIC:
2154 if (i > sh->pd_idx)
2155 i--;
2156 break;
2157 case ALGORITHM_LEFT_SYMMETRIC:
2158 case ALGORITHM_RIGHT_SYMMETRIC:
2159 if (i < sh->pd_idx)
2160 i += raid_disks;
2161 i -= (sh->pd_idx + 1);
2162 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002163 case ALGORITHM_PARITY_0:
2164 i -= 1;
2165 break;
2166 case ALGORITHM_PARITY_N:
2167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002169 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002170 }
2171 break;
2172 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002173 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002174 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002175 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002176 case ALGORITHM_LEFT_ASYMMETRIC:
2177 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002178 case ALGORITHM_ROTATING_ZERO_RESTART:
2179 case ALGORITHM_ROTATING_N_RESTART:
2180 if (sh->pd_idx == raid_disks-1)
2181 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002182 else if (i > sh->pd_idx)
2183 i -= 2; /* D D P Q D */
2184 break;
2185 case ALGORITHM_LEFT_SYMMETRIC:
2186 case ALGORITHM_RIGHT_SYMMETRIC:
2187 if (sh->pd_idx == raid_disks-1)
2188 i--; /* Q D D D P */
2189 else {
2190 /* D D P Q D */
2191 if (i < sh->pd_idx)
2192 i += raid_disks;
2193 i -= (sh->pd_idx + 2);
2194 }
2195 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002196 case ALGORITHM_PARITY_0:
2197 i -= 2;
2198 break;
2199 case ALGORITHM_PARITY_N:
2200 break;
2201 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002202 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002203 if (sh->pd_idx == 0)
2204 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002205 else {
2206 /* D D Q P D */
2207 if (i < sh->pd_idx)
2208 i += raid_disks;
2209 i -= (sh->pd_idx + 1);
2210 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002211 break;
2212 case ALGORITHM_LEFT_ASYMMETRIC_6:
2213 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2214 if (i > sh->pd_idx)
2215 i--;
2216 break;
2217 case ALGORITHM_LEFT_SYMMETRIC_6:
2218 case ALGORITHM_RIGHT_SYMMETRIC_6:
2219 if (i < sh->pd_idx)
2220 i += data_disks + 1;
2221 i -= (sh->pd_idx + 1);
2222 break;
2223 case ALGORITHM_PARITY_0_6:
2224 i -= 1;
2225 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002226 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002227 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002228 }
2229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
2231
2232 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002233 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
NeilBrown112bf892009-03-31 14:39:38 +11002235 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002236 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002237 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2238 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002239 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2240 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 0;
2242 }
2243 return r_sector;
2244}
2245
2246
Dan Williams600aa102008-06-28 08:32:05 +10002247static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002248schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002249 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002250{
2251 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002252 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002253 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002254
Dan Williamse33129d2007-01-02 13:52:30 -07002255 if (rcw) {
2256 /* if we are not expanding this is a proper write request, and
2257 * there will be bios with new data to be drained into the
2258 * stripe cache
2259 */
2260 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002261 sh->reconstruct_state = reconstruct_state_drain_run;
2262 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2263 } else
2264 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002265
Dan Williamsac6b53b2009-07-14 13:40:19 -07002266 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002267
2268 for (i = disks; i--; ) {
2269 struct r5dev *dev = &sh->dev[i];
2270
2271 if (dev->towrite) {
2272 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002273 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002274 if (!expand)
2275 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002276 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002277 }
2278 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002279 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002280 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002281 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002282 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002283 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002284 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2285 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2286
Dan Williamsd8ee0722008-06-28 08:32:06 +10002287 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002288 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2289 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002290 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002291
2292 for (i = disks; i--; ) {
2293 struct r5dev *dev = &sh->dev[i];
2294 if (i == pd_idx)
2295 continue;
2296
Dan Williamse33129d2007-01-02 13:52:30 -07002297 if (dev->towrite &&
2298 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002299 test_bit(R5_Wantcompute, &dev->flags))) {
2300 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002301 set_bit(R5_LOCKED, &dev->flags);
2302 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002303 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002304 }
2305 }
2306 }
2307
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002308 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002309 * are in flight
2310 */
2311 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2312 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002313 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002314
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002315 if (level == 6) {
2316 int qd_idx = sh->qd_idx;
2317 struct r5dev *dev = &sh->dev[qd_idx];
2318
2319 set_bit(R5_LOCKED, &dev->flags);
2320 clear_bit(R5_UPTODATE, &dev->flags);
2321 s->locked++;
2322 }
2323
Dan Williams600aa102008-06-28 08:32:05 +10002324 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002325 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002326 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002327}
NeilBrown16a53ec2006-06-26 00:27:38 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329/*
2330 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002331 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * The bi_next chain must be in order.
2333 */
2334static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2335{
2336 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002337 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002338 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
NeilBrowncbe47ec2011-07-26 11:20:35 +10002340 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 (unsigned long long)bi->bi_sector,
2342 (unsigned long long)sh->sector);
2343
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002346 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002348 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2349 firstwrite = 1;
2350 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 bip = &sh->dev[dd_idx].toread;
2352 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2353 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2354 goto overlap;
2355 bip = & (*bip)->bi_next;
2356 }
2357 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2358 goto overlap;
2359
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002360 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (*bip)
2362 bi->bi_next = *bip;
2363 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002364 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (forwrite) {
2367 /* check if page is covered */
2368 sector_t sector = sh->dev[dd_idx].sector;
2369 for (bi=sh->dev[dd_idx].towrite;
2370 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2371 bi && bi->bi_sector <= sector;
2372 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2373 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2374 sector = bi->bi_sector + (bi->bi_size>>9);
2375 }
2376 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2377 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2378 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002379 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002380
2381 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2382 (unsigned long long)(*bip)->bi_sector,
2383 (unsigned long long)sh->sector, dd_idx);
2384
2385 if (conf->mddev->bitmap && firstwrite) {
2386 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2387 STRIPE_SECTORS, 0);
2388 sh->bm_seq = conf->seq_flush+1;
2389 set_bit(STRIPE_BIT_DELAY, &sh->state);
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 1;
2392
2393 overlap:
2394 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2395 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return 0;
2397}
2398
NeilBrownd1688a62011-10-11 16:49:52 +11002399static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002400
NeilBrownd1688a62011-10-11 16:49:52 +11002401static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002402 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002403{
NeilBrown784052e2009-03-31 15:19:07 +11002404 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002405 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002406 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002407 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002408 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002409
NeilBrown112bf892009-03-31 14:39:38 +11002410 raid5_compute_sector(conf,
2411 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002412 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002413 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002414 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002415}
2416
Dan Williamsa4456852007-07-09 11:56:43 -07002417static void
NeilBrownd1688a62011-10-11 16:49:52 +11002418handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002419 struct stripe_head_state *s, int disks,
2420 struct bio **return_bi)
2421{
2422 int i;
2423 for (i = disks; i--; ) {
2424 struct bio *bi;
2425 int bitmap_end = 0;
2426
2427 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002428 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002429 rcu_read_lock();
2430 rdev = rcu_dereference(conf->disks[i].rdev);
2431 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002432 atomic_inc(&rdev->nr_pending);
2433 else
2434 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002435 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002436 if (rdev) {
2437 if (!rdev_set_badblocks(
2438 rdev,
2439 sh->sector,
2440 STRIPE_SECTORS, 0))
2441 md_error(conf->mddev, rdev);
2442 rdev_dec_pending(rdev, conf->mddev);
2443 }
Dan Williamsa4456852007-07-09 11:56:43 -07002444 }
2445 spin_lock_irq(&conf->device_lock);
2446 /* fail all writes first */
2447 bi = sh->dev[i].towrite;
2448 sh->dev[i].towrite = NULL;
2449 if (bi) {
2450 s->to_write--;
2451 bitmap_end = 1;
2452 }
2453
2454 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2455 wake_up(&conf->wait_for_overlap);
2456
2457 while (bi && bi->bi_sector <
2458 sh->dev[i].sector + STRIPE_SECTORS) {
2459 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2460 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002461 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002462 md_write_end(conf->mddev);
2463 bi->bi_next = *return_bi;
2464 *return_bi = bi;
2465 }
2466 bi = nextbi;
2467 }
2468 /* and fail all 'written' */
2469 bi = sh->dev[i].written;
2470 sh->dev[i].written = NULL;
2471 if (bi) bitmap_end = 1;
2472 while (bi && bi->bi_sector <
2473 sh->dev[i].sector + STRIPE_SECTORS) {
2474 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2475 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002476 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002477 md_write_end(conf->mddev);
2478 bi->bi_next = *return_bi;
2479 *return_bi = bi;
2480 }
2481 bi = bi2;
2482 }
2483
Dan Williamsb5e98d62007-01-02 13:52:31 -07002484 /* fail any reads if this device is non-operational and
2485 * the data has not reached the cache yet.
2486 */
2487 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2488 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2489 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002490 bi = sh->dev[i].toread;
2491 sh->dev[i].toread = NULL;
2492 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2493 wake_up(&conf->wait_for_overlap);
2494 if (bi) s->to_read--;
2495 while (bi && bi->bi_sector <
2496 sh->dev[i].sector + STRIPE_SECTORS) {
2497 struct bio *nextbi =
2498 r5_next_bio(bi, sh->dev[i].sector);
2499 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002500 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002501 bi->bi_next = *return_bi;
2502 *return_bi = bi;
2503 }
2504 bi = nextbi;
2505 }
2506 }
2507 spin_unlock_irq(&conf->device_lock);
2508 if (bitmap_end)
2509 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2510 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002511 /* If we were in the middle of a write the parity block might
2512 * still be locked - so just clear all R5_LOCKED flags
2513 */
2514 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002515 }
2516
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002517 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2518 if (atomic_dec_and_test(&conf->pending_full_writes))
2519 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002520}
2521
NeilBrown7f0da592011-07-28 11:39:22 +10002522static void
NeilBrownd1688a62011-10-11 16:49:52 +11002523handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002524 struct stripe_head_state *s)
2525{
2526 int abort = 0;
2527 int i;
2528
NeilBrown7f0da592011-07-28 11:39:22 +10002529 clear_bit(STRIPE_SYNCING, &sh->state);
2530 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002531 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002532 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002533 * Don't even need to abort as that is handled elsewhere
2534 * if needed, and not always wanted e.g. if there is a known
2535 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002536 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002537 * non-sync devices, or abort the recovery
2538 */
NeilBrown18b98372012-04-01 23:48:38 +10002539 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2540 /* During recovery devices cannot be removed, so
2541 * locking and refcounting of rdevs is not needed
2542 */
2543 for (i = 0; i < conf->raid_disks; i++) {
2544 struct md_rdev *rdev = conf->disks[i].rdev;
2545 if (rdev
2546 && !test_bit(Faulty, &rdev->flags)
2547 && !test_bit(In_sync, &rdev->flags)
2548 && !rdev_set_badblocks(rdev, sh->sector,
2549 STRIPE_SECTORS, 0))
2550 abort = 1;
2551 rdev = conf->disks[i].replacement;
2552 if (rdev
2553 && !test_bit(Faulty, &rdev->flags)
2554 && !test_bit(In_sync, &rdev->flags)
2555 && !rdev_set_badblocks(rdev, sh->sector,
2556 STRIPE_SECTORS, 0))
2557 abort = 1;
2558 }
2559 if (abort)
2560 conf->recovery_disabled =
2561 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002562 }
NeilBrown18b98372012-04-01 23:48:38 +10002563 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002564}
2565
NeilBrown9a3e1102011-12-23 10:17:53 +11002566static int want_replace(struct stripe_head *sh, int disk_idx)
2567{
2568 struct md_rdev *rdev;
2569 int rv = 0;
2570 /* Doing recovery so rcu locking not required */
2571 rdev = sh->raid_conf->disks[disk_idx].replacement;
2572 if (rdev
2573 && !test_bit(Faulty, &rdev->flags)
2574 && !test_bit(In_sync, &rdev->flags)
2575 && (rdev->recovery_offset <= sh->sector
2576 || rdev->mddev->recovery_cp <= sh->sector))
2577 rv = 1;
2578
2579 return rv;
2580}
2581
NeilBrown93b3dbc2011-07-27 11:00:36 +10002582/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002583 * to be read or computed to satisfy a request.
2584 *
2585 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002586 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002587 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002588static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2589 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002590{
2591 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002592 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2593 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002594
Dan Williamsf38e1212007-01-02 13:52:30 -07002595 /* is the data in this block needed, and can we get it? */
2596 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002597 !test_bit(R5_UPTODATE, &dev->flags) &&
2598 (dev->toread ||
2599 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2600 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002601 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002602 (s->failed >= 1 && fdev[0]->toread) ||
2603 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002604 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2605 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2606 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002607 /* we would like to get this block, possibly by computing it,
2608 * otherwise read it if the backing disk is insync
2609 */
2610 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2611 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2612 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002613 (s->failed && (disk_idx == s->failed_num[0] ||
2614 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002615 /* have disk failed, and we're requested to fetch it;
2616 * do compute it
2617 */
2618 pr_debug("Computing stripe %llu block %d\n",
2619 (unsigned long long)sh->sector, disk_idx);
2620 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2621 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2622 set_bit(R5_Wantcompute, &dev->flags);
2623 sh->ops.target = disk_idx;
2624 sh->ops.target2 = -1; /* no 2nd target */
2625 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002626 /* Careful: from this point on 'uptodate' is in the eye
2627 * of raid_run_ops which services 'compute' operations
2628 * before writes. R5_Wantcompute flags a block that will
2629 * be R5_UPTODATE by the time it is needed for a
2630 * subsequent operation.
2631 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002632 s->uptodate++;
2633 return 1;
2634 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2635 /* Computing 2-failure is *very* expensive; only
2636 * do it if failed >= 2
2637 */
2638 int other;
2639 for (other = disks; other--; ) {
2640 if (other == disk_idx)
2641 continue;
2642 if (!test_bit(R5_UPTODATE,
2643 &sh->dev[other].flags))
2644 break;
2645 }
2646 BUG_ON(other < 0);
2647 pr_debug("Computing stripe %llu blocks %d,%d\n",
2648 (unsigned long long)sh->sector,
2649 disk_idx, other);
2650 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2651 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2652 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2653 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2654 sh->ops.target = disk_idx;
2655 sh->ops.target2 = other;
2656 s->uptodate += 2;
2657 s->req_compute = 1;
2658 return 1;
2659 } else if (test_bit(R5_Insync, &dev->flags)) {
2660 set_bit(R5_LOCKED, &dev->flags);
2661 set_bit(R5_Wantread, &dev->flags);
2662 s->locked++;
2663 pr_debug("Reading block %d (sync=%d)\n",
2664 disk_idx, s->syncing);
2665 }
2666 }
2667
2668 return 0;
2669}
2670
2671/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002672 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002673 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002674static void handle_stripe_fill(struct stripe_head *sh,
2675 struct stripe_head_state *s,
2676 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002677{
2678 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002679
2680 /* look for blocks to read/compute, skip this if a compute
2681 * is already in flight, or if the stripe contents are in the
2682 * midst of changing due to a write
2683 */
2684 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2685 !sh->reconstruct_state)
2686 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002687 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002688 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002689 set_bit(STRIPE_HANDLE, &sh->state);
2690}
2691
2692
Dan Williams1fe797e2008-06-28 09:16:30 +10002693/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002694 * any written block on an uptodate or failed drive can be returned.
2695 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2696 * never LOCKED, so we don't need to test 'failed' directly.
2697 */
NeilBrownd1688a62011-10-11 16:49:52 +11002698static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002699 struct stripe_head *sh, int disks, struct bio **return_bi)
2700{
2701 int i;
2702 struct r5dev *dev;
2703
2704 for (i = disks; i--; )
2705 if (sh->dev[i].written) {
2706 dev = &sh->dev[i];
2707 if (!test_bit(R5_LOCKED, &dev->flags) &&
2708 test_bit(R5_UPTODATE, &dev->flags)) {
2709 /* We can return any write requests */
2710 struct bio *wbi, *wbi2;
2711 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002712 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002713 spin_lock_irq(&conf->device_lock);
2714 wbi = dev->written;
2715 dev->written = NULL;
2716 while (wbi && wbi->bi_sector <
2717 dev->sector + STRIPE_SECTORS) {
2718 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002719 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002720 md_write_end(conf->mddev);
2721 wbi->bi_next = *return_bi;
2722 *return_bi = wbi;
2723 }
2724 wbi = wbi2;
2725 }
2726 if (dev->towrite == NULL)
2727 bitmap_end = 1;
2728 spin_unlock_irq(&conf->device_lock);
2729 if (bitmap_end)
2730 bitmap_endwrite(conf->mddev->bitmap,
2731 sh->sector,
2732 STRIPE_SECTORS,
2733 !test_bit(STRIPE_DEGRADED, &sh->state),
2734 0);
2735 }
2736 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002737
2738 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2739 if (atomic_dec_and_test(&conf->pending_full_writes))
2740 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002741}
2742
NeilBrownd1688a62011-10-11 16:49:52 +11002743static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002744 struct stripe_head *sh,
2745 struct stripe_head_state *s,
2746 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002747{
2748 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002749 if (conf->max_degraded == 2) {
2750 /* RAID6 requires 'rcw' in current implementation
2751 * Calculate the real rcw later - for now fake it
2752 * look like rcw is cheaper
2753 */
2754 rcw = 1; rmw = 2;
2755 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002756 /* would I have to read this buffer for read_modify_write */
2757 struct r5dev *dev = &sh->dev[i];
2758 if ((dev->towrite || i == sh->pd_idx) &&
2759 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002760 !(test_bit(R5_UPTODATE, &dev->flags) ||
2761 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002762 if (test_bit(R5_Insync, &dev->flags))
2763 rmw++;
2764 else
2765 rmw += 2*disks; /* cannot read it */
2766 }
2767 /* Would I have to read this buffer for reconstruct_write */
2768 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2769 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002770 !(test_bit(R5_UPTODATE, &dev->flags) ||
2771 test_bit(R5_Wantcompute, &dev->flags))) {
2772 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002773 else
2774 rcw += 2*disks;
2775 }
2776 }
Dan Williams45b42332007-07-09 11:56:43 -07002777 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002778 (unsigned long long)sh->sector, rmw, rcw);
2779 set_bit(STRIPE_HANDLE, &sh->state);
2780 if (rmw < rcw && rmw > 0)
2781 /* prefer read-modify-write, but need to get some data */
2782 for (i = disks; i--; ) {
2783 struct r5dev *dev = &sh->dev[i];
2784 if ((dev->towrite || i == sh->pd_idx) &&
2785 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002786 !(test_bit(R5_UPTODATE, &dev->flags) ||
2787 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002788 test_bit(R5_Insync, &dev->flags)) {
2789 if (
2790 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002791 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002792 "%d for r-m-w\n", i);
2793 set_bit(R5_LOCKED, &dev->flags);
2794 set_bit(R5_Wantread, &dev->flags);
2795 s->locked++;
2796 } else {
2797 set_bit(STRIPE_DELAYED, &sh->state);
2798 set_bit(STRIPE_HANDLE, &sh->state);
2799 }
2800 }
2801 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002802 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002803 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002804 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002805 for (i = disks; i--; ) {
2806 struct r5dev *dev = &sh->dev[i];
2807 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002808 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002809 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002810 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002811 test_bit(R5_Wantcompute, &dev->flags))) {
2812 rcw++;
2813 if (!test_bit(R5_Insync, &dev->flags))
2814 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002815 if (
2816 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002817 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002818 "%d for Reconstruct\n", i);
2819 set_bit(R5_LOCKED, &dev->flags);
2820 set_bit(R5_Wantread, &dev->flags);
2821 s->locked++;
2822 } else {
2823 set_bit(STRIPE_DELAYED, &sh->state);
2824 set_bit(STRIPE_HANDLE, &sh->state);
2825 }
2826 }
2827 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002828 }
Dan Williamsa4456852007-07-09 11:56:43 -07002829 /* now if nothing is locked, and if we have enough data,
2830 * we can start a write request
2831 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002832 /* since handle_stripe can be called at any time we need to handle the
2833 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002834 * subsequent call wants to start a write request. raid_run_ops only
2835 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002836 * simultaneously. If this is not the case then new writes need to be
2837 * held off until the compute completes.
2838 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002839 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2840 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2841 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002842 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002843}
2844
NeilBrownd1688a62011-10-11 16:49:52 +11002845static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002846 struct stripe_head_state *s, int disks)
2847{
Dan Williamsecc65c92008-06-28 08:31:57 +10002848 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002849
Dan Williamsbd2ab672008-04-10 21:29:27 -07002850 set_bit(STRIPE_HANDLE, &sh->state);
2851
Dan Williamsecc65c92008-06-28 08:31:57 +10002852 switch (sh->check_state) {
2853 case check_state_idle:
2854 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002855 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002856 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002857 sh->check_state = check_state_run;
2858 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002859 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002860 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002861 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002862 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002863 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002864 /* fall through */
2865 case check_state_compute_result:
2866 sh->check_state = check_state_idle;
2867 if (!dev)
2868 dev = &sh->dev[sh->pd_idx];
2869
2870 /* check that a write has not made the stripe insync */
2871 if (test_bit(STRIPE_INSYNC, &sh->state))
2872 break;
2873
2874 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002875 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2876 BUG_ON(s->uptodate != disks);
2877
2878 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002879 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002880 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002881
Dan Williamsa4456852007-07-09 11:56:43 -07002882 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002883 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002884 break;
2885 case check_state_run:
2886 break; /* we will be called again upon completion */
2887 case check_state_check_result:
2888 sh->check_state = check_state_idle;
2889
2890 /* if a failure occurred during the check operation, leave
2891 * STRIPE_INSYNC not set and let the stripe be handled again
2892 */
2893 if (s->failed)
2894 break;
2895
2896 /* handle a successful check operation, if parity is correct
2897 * we are done. Otherwise update the mismatch count and repair
2898 * parity if !MD_RECOVERY_CHECK
2899 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002900 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002901 /* parity is correct (on disc,
2902 * not in buffer any more)
2903 */
2904 set_bit(STRIPE_INSYNC, &sh->state);
2905 else {
2906 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2907 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2908 /* don't try to repair!! */
2909 set_bit(STRIPE_INSYNC, &sh->state);
2910 else {
2911 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002912 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002913 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2914 set_bit(R5_Wantcompute,
2915 &sh->dev[sh->pd_idx].flags);
2916 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002917 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002918 s->uptodate++;
2919 }
2920 }
2921 break;
2922 case check_state_compute_run:
2923 break;
2924 default:
2925 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2926 __func__, sh->check_state,
2927 (unsigned long long) sh->sector);
2928 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002929 }
2930}
2931
2932
NeilBrownd1688a62011-10-11 16:49:52 +11002933static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002934 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002935 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002936{
Dan Williamsa4456852007-07-09 11:56:43 -07002937 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002938 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002939 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002940
2941 set_bit(STRIPE_HANDLE, &sh->state);
2942
2943 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002944
Dan Williamsa4456852007-07-09 11:56:43 -07002945 /* Want to check and possibly repair P and Q.
2946 * However there could be one 'failed' device, in which
2947 * case we can only check one of them, possibly using the
2948 * other to generate missing data
2949 */
2950
Dan Williamsd82dfee2009-07-14 13:40:57 -07002951 switch (sh->check_state) {
2952 case check_state_idle:
2953 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002954 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002955 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002956 * makes sense to check P (If anything else were failed,
2957 * we would have used P to recreate it).
2958 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002959 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002960 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002961 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002962 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002963 * anything, so it makes sense to check it
2964 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002965 if (sh->check_state == check_state_run)
2966 sh->check_state = check_state_run_pq;
2967 else
2968 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002969 }
Dan Williams36d1c642009-07-14 11:48:22 -07002970
Dan Williamsd82dfee2009-07-14 13:40:57 -07002971 /* discard potentially stale zero_sum_result */
2972 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002973
Dan Williamsd82dfee2009-07-14 13:40:57 -07002974 if (sh->check_state == check_state_run) {
2975 /* async_xor_zero_sum destroys the contents of P */
2976 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2977 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002978 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002979 if (sh->check_state >= check_state_run &&
2980 sh->check_state <= check_state_run_pq) {
2981 /* async_syndrome_zero_sum preserves P and Q, so
2982 * no need to mark them !uptodate here
2983 */
2984 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2985 break;
2986 }
Dan Williams36d1c642009-07-14 11:48:22 -07002987
Dan Williamsd82dfee2009-07-14 13:40:57 -07002988 /* we have 2-disk failure */
2989 BUG_ON(s->failed != 2);
2990 /* fall through */
2991 case check_state_compute_result:
2992 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002993
Dan Williamsd82dfee2009-07-14 13:40:57 -07002994 /* check that a write has not made the stripe insync */
2995 if (test_bit(STRIPE_INSYNC, &sh->state))
2996 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002997
2998 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002999 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003000 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003001 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003002 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003003 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003004 s->locked++;
3005 set_bit(R5_LOCKED, &dev->flags);
3006 set_bit(R5_Wantwrite, &dev->flags);
3007 }
3008 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003009 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003010 s->locked++;
3011 set_bit(R5_LOCKED, &dev->flags);
3012 set_bit(R5_Wantwrite, &dev->flags);
3013 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003015 dev = &sh->dev[pd_idx];
3016 s->locked++;
3017 set_bit(R5_LOCKED, &dev->flags);
3018 set_bit(R5_Wantwrite, &dev->flags);
3019 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003020 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003021 dev = &sh->dev[qd_idx];
3022 s->locked++;
3023 set_bit(R5_LOCKED, &dev->flags);
3024 set_bit(R5_Wantwrite, &dev->flags);
3025 }
3026 clear_bit(STRIPE_DEGRADED, &sh->state);
3027
3028 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003029 break;
3030 case check_state_run:
3031 case check_state_run_q:
3032 case check_state_run_pq:
3033 break; /* we will be called again upon completion */
3034 case check_state_check_result:
3035 sh->check_state = check_state_idle;
3036
3037 /* handle a successful check operation, if parity is correct
3038 * we are done. Otherwise update the mismatch count and repair
3039 * parity if !MD_RECOVERY_CHECK
3040 */
3041 if (sh->ops.zero_sum_result == 0) {
3042 /* both parities are correct */
3043 if (!s->failed)
3044 set_bit(STRIPE_INSYNC, &sh->state);
3045 else {
3046 /* in contrast to the raid5 case we can validate
3047 * parity, but still have a failure to write
3048 * back
3049 */
3050 sh->check_state = check_state_compute_result;
3051 /* Returning at this point means that we may go
3052 * off and bring p and/or q uptodate again so
3053 * we make sure to check zero_sum_result again
3054 * to verify if p or q need writeback
3055 */
3056 }
3057 } else {
3058 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3059 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3060 /* don't try to repair!! */
3061 set_bit(STRIPE_INSYNC, &sh->state);
3062 else {
3063 int *target = &sh->ops.target;
3064
3065 sh->ops.target = -1;
3066 sh->ops.target2 = -1;
3067 sh->check_state = check_state_compute_run;
3068 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3069 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3070 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3071 set_bit(R5_Wantcompute,
3072 &sh->dev[pd_idx].flags);
3073 *target = pd_idx;
3074 target = &sh->ops.target2;
3075 s->uptodate++;
3076 }
3077 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3078 set_bit(R5_Wantcompute,
3079 &sh->dev[qd_idx].flags);
3080 *target = qd_idx;
3081 s->uptodate++;
3082 }
3083 }
3084 }
3085 break;
3086 case check_state_compute_run:
3087 break;
3088 default:
3089 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3090 __func__, sh->check_state,
3091 (unsigned long long) sh->sector);
3092 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003093 }
3094}
3095
NeilBrownd1688a62011-10-11 16:49:52 +11003096static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003097{
3098 int i;
3099
3100 /* We have read all the blocks in this stripe and now we need to
3101 * copy some of them into a target stripe for expand.
3102 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003103 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003104 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3105 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003106 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003107 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003108 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003109 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003110
NeilBrown784052e2009-03-31 15:19:07 +11003111 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003112 sector_t s = raid5_compute_sector(conf, bn, 0,
3113 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003114 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003115 if (sh2 == NULL)
3116 /* so far only the early blocks of this stripe
3117 * have been requested. When later blocks
3118 * get requested, we will try again
3119 */
3120 continue;
3121 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3122 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3123 /* must have already done this block */
3124 release_stripe(sh2);
3125 continue;
3126 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003127
3128 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003129 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003130 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003131 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003132 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003133
Dan Williamsa4456852007-07-09 11:56:43 -07003134 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3135 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3136 for (j = 0; j < conf->raid_disks; j++)
3137 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003138 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003139 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3140 break;
3141 if (j == conf->raid_disks) {
3142 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3143 set_bit(STRIPE_HANDLE, &sh2->state);
3144 }
3145 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003146
Dan Williamsa4456852007-07-09 11:56:43 -07003147 }
NeilBrowna2e08552007-09-11 15:23:36 -07003148 /* done submitting copies, wait for them to complete */
3149 if (tx) {
3150 async_tx_ack(tx);
3151 dma_wait_for_async_tx(tx);
3152 }
Dan Williamsa4456852007-07-09 11:56:43 -07003153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155/*
3156 * handle_stripe - do things to a stripe.
3157 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003158 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3159 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003161 * return some read requests which now have data
3162 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 * schedule a read on some buffers
3164 * schedule a write of some buffers
3165 * return confirmation of parity correctness
3166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 */
Dan Williamsa4456852007-07-09 11:56:43 -07003168
NeilBrownacfe7262011-07-27 11:00:36 +10003169static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003170{
NeilBrownd1688a62011-10-11 16:49:52 +11003171 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003172 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003173 struct r5dev *dev;
3174 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003175 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003176
NeilBrownacfe7262011-07-27 11:00:36 +10003177 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003178
NeilBrownacfe7262011-07-27 11:00:36 +10003179 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3180 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3181 s->failed_num[0] = -1;
3182 s->failed_num[1] = -1;
3183
3184 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003185 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003186 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003187 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003188 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003189 sector_t first_bad;
3190 int bad_sectors;
3191 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003192
NeilBrown16a53ec2006-06-26 00:27:38 -07003193 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003194
Dan Williams45b42332007-07-09 11:56:43 -07003195 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003196 i, dev->flags,
3197 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003198 /* maybe we can reply to a read
3199 *
3200 * new wantfill requests are only permitted while
3201 * ops_complete_biofill is guaranteed to be inactive
3202 */
3203 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3204 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3205 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003206
3207 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003208 if (test_bit(R5_LOCKED, &dev->flags))
3209 s->locked++;
3210 if (test_bit(R5_UPTODATE, &dev->flags))
3211 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003212 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003213 s->compute++;
3214 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003215 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003216
NeilBrownacfe7262011-07-27 11:00:36 +10003217 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003218 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003219 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003220 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003221 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003222 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003223 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003224 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003225 }
Dan Williamsa4456852007-07-09 11:56:43 -07003226 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003227 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003228 /* Prefer to use the replacement for reads, but only
3229 * if it is recovered enough and has no bad blocks.
3230 */
3231 rdev = rcu_dereference(conf->disks[i].replacement);
3232 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3233 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3234 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3235 &first_bad, &bad_sectors))
3236 set_bit(R5_ReadRepl, &dev->flags);
3237 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003238 if (rdev)
3239 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003240 rdev = rcu_dereference(conf->disks[i].rdev);
3241 clear_bit(R5_ReadRepl, &dev->flags);
3242 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003243 if (rdev && test_bit(Faulty, &rdev->flags))
3244 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003245 if (rdev) {
3246 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3247 &first_bad, &bad_sectors);
3248 if (s->blocked_rdev == NULL
3249 && (test_bit(Blocked, &rdev->flags)
3250 || is_bad < 0)) {
3251 if (is_bad < 0)
3252 set_bit(BlockedBadBlocks,
3253 &rdev->flags);
3254 s->blocked_rdev = rdev;
3255 atomic_inc(&rdev->nr_pending);
3256 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003257 }
NeilBrown415e72d2010-06-17 17:25:21 +10003258 clear_bit(R5_Insync, &dev->flags);
3259 if (!rdev)
3260 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003261 else if (is_bad) {
3262 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003263 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3264 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003265 /* treat as in-sync, but with a read error
3266 * which we can now try to correct
3267 */
3268 set_bit(R5_Insync, &dev->flags);
3269 set_bit(R5_ReadError, &dev->flags);
3270 }
3271 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003272 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003273 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003274 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003275 set_bit(R5_Insync, &dev->flags);
3276 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3277 test_bit(R5_Expanded, &dev->flags))
3278 /* If we've reshaped into here, we assume it is Insync.
3279 * We will shortly update recovery_offset to make
3280 * it official.
3281 */
3282 set_bit(R5_Insync, &dev->flags);
3283
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003284 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003285 /* This flag does not apply to '.replacement'
3286 * only to .rdev, so make sure to check that*/
3287 struct md_rdev *rdev2 = rcu_dereference(
3288 conf->disks[i].rdev);
3289 if (rdev2 == rdev)
3290 clear_bit(R5_Insync, &dev->flags);
3291 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003292 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003293 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003294 } else
3295 clear_bit(R5_WriteError, &dev->flags);
3296 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003297 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003298 /* This flag does not apply to '.replacement'
3299 * only to .rdev, so make sure to check that*/
3300 struct md_rdev *rdev2 = rcu_dereference(
3301 conf->disks[i].rdev);
3302 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003303 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003304 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003305 } else
3306 clear_bit(R5_MadeGood, &dev->flags);
3307 }
NeilBrown977df362011-12-23 10:17:53 +11003308 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3309 struct md_rdev *rdev2 = rcu_dereference(
3310 conf->disks[i].replacement);
3311 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3312 s->handle_bad_blocks = 1;
3313 atomic_inc(&rdev2->nr_pending);
3314 } else
3315 clear_bit(R5_MadeGoodRepl, &dev->flags);
3316 }
NeilBrown415e72d2010-06-17 17:25:21 +10003317 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003318 /* The ReadError flag will just be confusing now */
3319 clear_bit(R5_ReadError, &dev->flags);
3320 clear_bit(R5_ReWrite, &dev->flags);
3321 }
NeilBrown415e72d2010-06-17 17:25:21 +10003322 if (test_bit(R5_ReadError, &dev->flags))
3323 clear_bit(R5_Insync, &dev->flags);
3324 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003325 if (s->failed < 2)
3326 s->failed_num[s->failed] = i;
3327 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003328 if (rdev && !test_bit(Faulty, &rdev->flags))
3329 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003330 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003331 }
NeilBrownc4c16632011-07-26 11:34:20 +10003332 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003333 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3334 /* If there is a failed device being replaced,
3335 * we must be recovering.
3336 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003337 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003338 * else we can only be replacing
3339 * sync and recovery both need to read all devices, and so
3340 * use the same flag.
3341 */
3342 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003343 sh->sector >= conf->mddev->recovery_cp ||
3344 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003345 s->syncing = 1;
3346 else
3347 s->replacing = 1;
3348 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003349 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003350}
NeilBrownf4168852007-02-28 20:11:53 -08003351
NeilBrowncc940152011-07-26 11:35:35 +10003352static void handle_stripe(struct stripe_head *sh)
3353{
3354 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003355 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003356 int i;
NeilBrown84789552011-07-27 11:00:36 +10003357 int prexor;
3358 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003359 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003360
3361 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003362 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003363 /* already being handled, ensure it gets handled
3364 * again when current action finishes */
3365 set_bit(STRIPE_HANDLE, &sh->state);
3366 return;
3367 }
3368
3369 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3370 set_bit(STRIPE_SYNCING, &sh->state);
3371 clear_bit(STRIPE_INSYNC, &sh->state);
3372 }
3373 clear_bit(STRIPE_DELAYED, &sh->state);
3374
3375 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3376 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3377 (unsigned long long)sh->sector, sh->state,
3378 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3379 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003380
NeilBrownacfe7262011-07-27 11:00:36 +10003381 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003382
NeilBrownbc2607f2011-07-28 11:39:22 +10003383 if (s.handle_bad_blocks) {
3384 set_bit(STRIPE_HANDLE, &sh->state);
3385 goto finish;
3386 }
3387
NeilBrown474af965fe2011-07-27 11:00:36 +10003388 if (unlikely(s.blocked_rdev)) {
3389 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003390 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003391 set_bit(STRIPE_HANDLE, &sh->state);
3392 goto finish;
3393 }
3394 /* There is nothing for the blocked_rdev to block */
3395 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3396 s.blocked_rdev = NULL;
3397 }
3398
3399 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3400 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3401 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3402 }
3403
3404 pr_debug("locked=%d uptodate=%d to_read=%d"
3405 " to_write=%d failed=%d failed_num=%d,%d\n",
3406 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3407 s.failed_num[0], s.failed_num[1]);
3408 /* check if the array has lost more than max_degraded devices and,
3409 * if so, some requests might need to be failed.
3410 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003411 if (s.failed > conf->max_degraded) {
3412 sh->check_state = 0;
3413 sh->reconstruct_state = 0;
3414 if (s.to_read+s.to_write+s.written)
3415 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003416 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003417 handle_failed_sync(conf, sh, &s);
3418 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003419
3420 /*
3421 * might be able to return some write requests if the parity blocks
3422 * are safe, or on a failed drive
3423 */
3424 pdev = &sh->dev[sh->pd_idx];
3425 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3426 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3427 qdev = &sh->dev[sh->qd_idx];
3428 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3429 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3430 || conf->level < 6;
3431
3432 if (s.written &&
3433 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3434 && !test_bit(R5_LOCKED, &pdev->flags)
3435 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3436 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3437 && !test_bit(R5_LOCKED, &qdev->flags)
3438 && test_bit(R5_UPTODATE, &qdev->flags)))))
3439 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3440
3441 /* Now we might consider reading some blocks, either to check/generate
3442 * parity, or to satisfy requests
3443 * or to load a block that is being partially written.
3444 */
3445 if (s.to_read || s.non_overwrite
3446 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003447 || (s.syncing && (s.uptodate + s.compute < disks))
3448 || s.replacing
3449 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003450 handle_stripe_fill(sh, &s, disks);
3451
NeilBrown84789552011-07-27 11:00:36 +10003452 /* Now we check to see if any write operations have recently
3453 * completed
3454 */
3455 prexor = 0;
3456 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3457 prexor = 1;
3458 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3459 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3460 sh->reconstruct_state = reconstruct_state_idle;
3461
3462 /* All the 'written' buffers and the parity block are ready to
3463 * be written back to disk
3464 */
3465 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3466 BUG_ON(sh->qd_idx >= 0 &&
3467 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3468 for (i = disks; i--; ) {
3469 struct r5dev *dev = &sh->dev[i];
3470 if (test_bit(R5_LOCKED, &dev->flags) &&
3471 (i == sh->pd_idx || i == sh->qd_idx ||
3472 dev->written)) {
3473 pr_debug("Writing block %d\n", i);
3474 set_bit(R5_Wantwrite, &dev->flags);
3475 if (prexor)
3476 continue;
3477 if (!test_bit(R5_Insync, &dev->flags) ||
3478 ((i == sh->pd_idx || i == sh->qd_idx) &&
3479 s.failed == 0))
3480 set_bit(STRIPE_INSYNC, &sh->state);
3481 }
3482 }
3483 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3484 s.dec_preread_active = 1;
3485 }
3486
3487 /* Now to consider new write requests and what else, if anything
3488 * should be read. We do not handle new writes when:
3489 * 1/ A 'write' operation (copy+xor) is already in flight.
3490 * 2/ A 'check' operation is in flight, as it may clobber the parity
3491 * block.
3492 */
3493 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3494 handle_stripe_dirtying(conf, sh, &s, disks);
3495
3496 /* maybe we need to check and possibly fix the parity for this stripe
3497 * Any reads will already have been scheduled, so we just see if enough
3498 * data is available. The parity check is held off while parity
3499 * dependent operations are in flight.
3500 */
3501 if (sh->check_state ||
3502 (s.syncing && s.locked == 0 &&
3503 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3504 !test_bit(STRIPE_INSYNC, &sh->state))) {
3505 if (conf->level == 6)
3506 handle_parity_checks6(conf, sh, &s, disks);
3507 else
3508 handle_parity_checks5(conf, sh, &s, disks);
3509 }
NeilBrownc5a31002011-07-27 11:00:36 +10003510
NeilBrown9a3e1102011-12-23 10:17:53 +11003511 if (s.replacing && s.locked == 0
3512 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3513 /* Write out to replacement devices where possible */
3514 for (i = 0; i < conf->raid_disks; i++)
3515 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3516 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3517 set_bit(R5_WantReplace, &sh->dev[i].flags);
3518 set_bit(R5_LOCKED, &sh->dev[i].flags);
3519 s.locked++;
3520 }
3521 set_bit(STRIPE_INSYNC, &sh->state);
3522 }
3523 if ((s.syncing || s.replacing) && s.locked == 0 &&
3524 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003525 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3526 clear_bit(STRIPE_SYNCING, &sh->state);
3527 }
3528
3529 /* If the failed drives are just a ReadError, then we might need
3530 * to progress the repair/check process
3531 */
3532 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3533 for (i = 0; i < s.failed; i++) {
3534 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3535 if (test_bit(R5_ReadError, &dev->flags)
3536 && !test_bit(R5_LOCKED, &dev->flags)
3537 && test_bit(R5_UPTODATE, &dev->flags)
3538 ) {
3539 if (!test_bit(R5_ReWrite, &dev->flags)) {
3540 set_bit(R5_Wantwrite, &dev->flags);
3541 set_bit(R5_ReWrite, &dev->flags);
3542 set_bit(R5_LOCKED, &dev->flags);
3543 s.locked++;
3544 } else {
3545 /* let's read it back */
3546 set_bit(R5_Wantread, &dev->flags);
3547 set_bit(R5_LOCKED, &dev->flags);
3548 s.locked++;
3549 }
3550 }
3551 }
3552
3553
NeilBrown3687c062011-07-27 11:00:36 +10003554 /* Finish reconstruct operations initiated by the expansion process */
3555 if (sh->reconstruct_state == reconstruct_state_result) {
3556 struct stripe_head *sh_src
3557 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3558 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3559 /* sh cannot be written until sh_src has been read.
3560 * so arrange for sh to be delayed a little
3561 */
3562 set_bit(STRIPE_DELAYED, &sh->state);
3563 set_bit(STRIPE_HANDLE, &sh->state);
3564 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3565 &sh_src->state))
3566 atomic_inc(&conf->preread_active_stripes);
3567 release_stripe(sh_src);
3568 goto finish;
3569 }
3570 if (sh_src)
3571 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003572
NeilBrown3687c062011-07-27 11:00:36 +10003573 sh->reconstruct_state = reconstruct_state_idle;
3574 clear_bit(STRIPE_EXPANDING, &sh->state);
3575 for (i = conf->raid_disks; i--; ) {
3576 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3577 set_bit(R5_LOCKED, &sh->dev[i].flags);
3578 s.locked++;
3579 }
3580 }
3581
3582 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3583 !sh->reconstruct_state) {
3584 /* Need to write out all blocks after computing parity */
3585 sh->disks = conf->raid_disks;
3586 stripe_set_idx(sh->sector, conf, 0, sh);
3587 schedule_reconstruction(sh, &s, 1, 1);
3588 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3589 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3590 atomic_dec(&conf->reshape_stripes);
3591 wake_up(&conf->wait_for_overlap);
3592 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3593 }
3594
3595 if (s.expanding && s.locked == 0 &&
3596 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3597 handle_stripe_expansion(conf, sh);
3598
3599finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003600 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003601 if (unlikely(s.blocked_rdev)) {
3602 if (conf->mddev->external)
3603 md_wait_for_blocked_rdev(s.blocked_rdev,
3604 conf->mddev);
3605 else
3606 /* Internal metadata will immediately
3607 * be written by raid5d, so we don't
3608 * need to wait here.
3609 */
3610 rdev_dec_pending(s.blocked_rdev,
3611 conf->mddev);
3612 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003613
NeilBrownbc2607f2011-07-28 11:39:22 +10003614 if (s.handle_bad_blocks)
3615 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003616 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003617 struct r5dev *dev = &sh->dev[i];
3618 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3619 /* We own a safe reference to the rdev */
3620 rdev = conf->disks[i].rdev;
3621 if (!rdev_set_badblocks(rdev, sh->sector,
3622 STRIPE_SECTORS, 0))
3623 md_error(conf->mddev, rdev);
3624 rdev_dec_pending(rdev, conf->mddev);
3625 }
NeilBrownb84db562011-07-28 11:39:23 +10003626 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3627 rdev = conf->disks[i].rdev;
3628 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003629 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003630 rdev_dec_pending(rdev, conf->mddev);
3631 }
NeilBrown977df362011-12-23 10:17:53 +11003632 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3633 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003634 if (!rdev)
3635 /* rdev have been moved down */
3636 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003637 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003638 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003639 rdev_dec_pending(rdev, conf->mddev);
3640 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003641 }
3642
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003643 if (s.ops_request)
3644 raid_run_ops(sh, s.ops_request);
3645
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003646 ops_run_io(sh, &s);
3647
NeilBrownc5709ef2011-07-26 11:35:20 +10003648 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003649 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003650 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003651 * have actually been submitted.
3652 */
3653 atomic_dec(&conf->preread_active_stripes);
3654 if (atomic_read(&conf->preread_active_stripes) <
3655 IO_THRESHOLD)
3656 md_wakeup_thread(conf->mddev->thread);
3657 }
3658
NeilBrownc5709ef2011-07-26 11:35:20 +10003659 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003660
Dan Williams257a4b42011-11-08 16:22:06 +11003661 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003662}
3663
NeilBrownd1688a62011-10-11 16:49:52 +11003664static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3667 while (!list_empty(&conf->delayed_list)) {
3668 struct list_head *l = conf->delayed_list.next;
3669 struct stripe_head *sh;
3670 sh = list_entry(l, struct stripe_head, lru);
3671 list_del_init(l);
3672 clear_bit(STRIPE_DELAYED, &sh->state);
3673 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3674 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003675 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
NeilBrown482c0832011-04-18 18:25:42 +10003677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
NeilBrownd1688a62011-10-11 16:49:52 +11003680static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003681{
3682 /* device_lock is held */
3683 struct list_head head;
3684 list_add(&head, &conf->bitmap_list);
3685 list_del_init(&conf->bitmap_list);
3686 while (!list_empty(&head)) {
3687 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3688 list_del_init(&sh->lru);
3689 atomic_inc(&sh->count);
3690 __release_stripe(conf, sh);
3691 }
3692}
3693
NeilBrownfd01b882011-10-11 16:47:53 +11003694int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003695{
NeilBrownd1688a62011-10-11 16:49:52 +11003696 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003697
3698 /* No difference between reads and writes. Just check
3699 * how busy the stripe_cache is
3700 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003701
NeilBrownf022b2f2006-10-03 01:15:56 -07003702 if (conf->inactive_blocked)
3703 return 1;
3704 if (conf->quiesce)
3705 return 1;
3706 if (list_empty_careful(&conf->inactive_list))
3707 return 1;
3708
3709 return 0;
3710}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003711EXPORT_SYMBOL_GPL(md_raid5_congested);
3712
3713static int raid5_congested(void *data, int bits)
3714{
NeilBrownfd01b882011-10-11 16:47:53 +11003715 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003716
3717 return mddev_congested(mddev, bits) ||
3718 md_raid5_congested(mddev, bits);
3719}
NeilBrownf022b2f2006-10-03 01:15:56 -07003720
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003721/* We want read requests to align with chunks where possible,
3722 * but write requests don't need to.
3723 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003724static int raid5_mergeable_bvec(struct request_queue *q,
3725 struct bvec_merge_data *bvm,
3726 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003727{
NeilBrownfd01b882011-10-11 16:47:53 +11003728 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003729 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003730 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003731 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003732 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003733
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003734 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003735 return biovec->bv_len; /* always allow writes to be mergeable */
3736
Andre Noll664e7c42009-06-18 08:45:27 +10003737 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3738 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003739 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3740 if (max < 0) max = 0;
3741 if (max <= biovec->bv_len && bio_sectors == 0)
3742 return biovec->bv_len;
3743 else
3744 return max;
3745}
3746
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003747
NeilBrownfd01b882011-10-11 16:47:53 +11003748static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003749{
3750 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003751 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003752 unsigned int bio_sectors = bio->bi_size >> 9;
3753
Andre Noll664e7c42009-06-18 08:45:27 +10003754 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3755 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003756 return chunk_sectors >=
3757 ((sector & (chunk_sectors - 1)) + bio_sectors);
3758}
3759
3760/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003761 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3762 * later sampled by raid5d.
3763 */
NeilBrownd1688a62011-10-11 16:49:52 +11003764static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003765{
3766 unsigned long flags;
3767
3768 spin_lock_irqsave(&conf->device_lock, flags);
3769
3770 bi->bi_next = conf->retry_read_aligned_list;
3771 conf->retry_read_aligned_list = bi;
3772
3773 spin_unlock_irqrestore(&conf->device_lock, flags);
3774 md_wakeup_thread(conf->mddev->thread);
3775}
3776
3777
NeilBrownd1688a62011-10-11 16:49:52 +11003778static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003779{
3780 struct bio *bi;
3781
3782 bi = conf->retry_read_aligned;
3783 if (bi) {
3784 conf->retry_read_aligned = NULL;
3785 return bi;
3786 }
3787 bi = conf->retry_read_aligned_list;
3788 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003789 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003790 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003791 /*
3792 * this sets the active strip count to 1 and the processed
3793 * strip count to zero (upper 8 bits)
3794 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003795 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003796 }
3797
3798 return bi;
3799}
3800
3801
3802/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003803 * The "raid5_align_endio" should check if the read succeeded and if it
3804 * did, call bio_endio on the original bio (having bio_put the new bio
3805 * first).
3806 * If the read failed..
3807 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003808static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003809{
3810 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003811 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003812 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003813 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003814 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003815
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003816 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003817
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003818 rdev = (void*)raid_bi->bi_next;
3819 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003820 mddev = rdev->mddev;
3821 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003822
3823 rdev_dec_pending(rdev, conf->mddev);
3824
3825 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003826 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003827 if (atomic_dec_and_test(&conf->active_aligned_reads))
3828 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003829 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003830 }
3831
3832
Dan Williams45b42332007-07-09 11:56:43 -07003833 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003834
3835 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003836}
3837
Neil Brown387bb172007-02-08 14:20:29 -08003838static int bio_fits_rdev(struct bio *bi)
3839{
Jens Axboe165125e2007-07-24 09:28:11 +02003840 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003841
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003842 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003843 return 0;
3844 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003845 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003846 return 0;
3847
3848 if (q->merge_bvec_fn)
3849 /* it's too hard to apply the merge_bvec_fn at this stage,
3850 * just just give up
3851 */
3852 return 0;
3853
3854 return 1;
3855}
3856
3857
NeilBrownfd01b882011-10-11 16:47:53 +11003858static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003859{
NeilBrownd1688a62011-10-11 16:49:52 +11003860 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003861 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003862 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003863 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003864 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003865
3866 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003867 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003868 return 0;
3869 }
3870 /*
NeilBrowna167f662010-10-26 18:31:13 +11003871 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003872 */
NeilBrowna167f662010-10-26 18:31:13 +11003873 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003874 if (!align_bi)
3875 return 0;
3876 /*
3877 * set bi_end_io to a new function, and set bi_private to the
3878 * original bio.
3879 */
3880 align_bi->bi_end_io = raid5_align_endio;
3881 align_bi->bi_private = raid_bio;
3882 /*
3883 * compute position
3884 */
NeilBrown112bf892009-03-31 14:39:38 +11003885 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3886 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003887 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003888
NeilBrown671488c2011-12-23 10:17:52 +11003889 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003890 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003891 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3892 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3893 rdev->recovery_offset < end_sector) {
3894 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3895 if (rdev &&
3896 (test_bit(Faulty, &rdev->flags) ||
3897 !(test_bit(In_sync, &rdev->flags) ||
3898 rdev->recovery_offset >= end_sector)))
3899 rdev = NULL;
3900 }
3901 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003902 sector_t first_bad;
3903 int bad_sectors;
3904
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003905 atomic_inc(&rdev->nr_pending);
3906 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907 raid_bio->bi_next = (void*)rdev;
3908 align_bi->bi_bdev = rdev->bdev;
3909 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003910
NeilBrown31c176e2011-07-28 11:39:22 +10003911 if (!bio_fits_rdev(align_bi) ||
3912 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3913 &first_bad, &bad_sectors)) {
3914 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003915 bio_put(align_bi);
3916 rdev_dec_pending(rdev, mddev);
3917 return 0;
3918 }
3919
majianpeng6c0544e2012-06-12 08:31:10 +08003920 /* No reshape active, so we can trust rdev->data_offset */
3921 align_bi->bi_sector += rdev->data_offset;
3922
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003923 spin_lock_irq(&conf->device_lock);
3924 wait_event_lock_irq(conf->wait_for_stripe,
3925 conf->quiesce == 0,
3926 conf->device_lock, /* nothing */);
3927 atomic_inc(&conf->active_aligned_reads);
3928 spin_unlock_irq(&conf->device_lock);
3929
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003930 generic_make_request(align_bi);
3931 return 1;
3932 } else {
3933 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003934 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003935 return 0;
3936 }
3937}
3938
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003939/* __get_priority_stripe - get the next stripe to process
3940 *
3941 * Full stripe writes are allowed to pass preread active stripes up until
3942 * the bypass_threshold is exceeded. In general the bypass_count
3943 * increments when the handle_list is handled before the hold_list; however, it
3944 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3945 * stripe with in flight i/o. The bypass_count will be reset when the
3946 * head of the hold_list has changed, i.e. the head was promoted to the
3947 * handle_list.
3948 */
NeilBrownd1688a62011-10-11 16:49:52 +11003949static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003950{
3951 struct stripe_head *sh;
3952
3953 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3954 __func__,
3955 list_empty(&conf->handle_list) ? "empty" : "busy",
3956 list_empty(&conf->hold_list) ? "empty" : "busy",
3957 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3958
3959 if (!list_empty(&conf->handle_list)) {
3960 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3961
3962 if (list_empty(&conf->hold_list))
3963 conf->bypass_count = 0;
3964 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3965 if (conf->hold_list.next == conf->last_hold)
3966 conf->bypass_count++;
3967 else {
3968 conf->last_hold = conf->hold_list.next;
3969 conf->bypass_count -= conf->bypass_threshold;
3970 if (conf->bypass_count < 0)
3971 conf->bypass_count = 0;
3972 }
3973 }
3974 } else if (!list_empty(&conf->hold_list) &&
3975 ((conf->bypass_threshold &&
3976 conf->bypass_count > conf->bypass_threshold) ||
3977 atomic_read(&conf->pending_full_writes) == 0)) {
3978 sh = list_entry(conf->hold_list.next,
3979 typeof(*sh), lru);
3980 conf->bypass_count -= conf->bypass_threshold;
3981 if (conf->bypass_count < 0)
3982 conf->bypass_count = 0;
3983 } else
3984 return NULL;
3985
3986 list_del_init(&sh->lru);
3987 atomic_inc(&sh->count);
3988 BUG_ON(atomic_read(&sh->count) != 1);
3989 return sh;
3990}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003991
Shaohua Li8811b592012-08-02 08:33:00 +10003992struct raid5_plug_cb {
3993 struct blk_plug_cb cb;
3994 struct list_head list;
3995};
3996
3997static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
3998{
3999 struct raid5_plug_cb *cb = container_of(
4000 blk_cb, struct raid5_plug_cb, cb);
4001 struct stripe_head *sh;
4002 struct mddev *mddev = cb->cb.data;
4003 struct r5conf *conf = mddev->private;
4004
4005 if (cb->list.next && !list_empty(&cb->list)) {
4006 spin_lock_irq(&conf->device_lock);
4007 while (!list_empty(&cb->list)) {
4008 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4009 list_del_init(&sh->lru);
4010 /*
4011 * avoid race release_stripe_plug() sees
4012 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4013 * is still in our list
4014 */
4015 smp_mb__before_clear_bit();
4016 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4017 __release_stripe(conf, sh);
4018 }
4019 spin_unlock_irq(&conf->device_lock);
4020 }
4021 kfree(cb);
4022}
4023
4024static void release_stripe_plug(struct mddev *mddev,
4025 struct stripe_head *sh)
4026{
4027 struct blk_plug_cb *blk_cb = blk_check_plugged(
4028 raid5_unplug, mddev,
4029 sizeof(struct raid5_plug_cb));
4030 struct raid5_plug_cb *cb;
4031
4032 if (!blk_cb) {
4033 release_stripe(sh);
4034 return;
4035 }
4036
4037 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4038
4039 if (cb->list.next == NULL)
4040 INIT_LIST_HEAD(&cb->list);
4041
4042 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4043 list_add_tail(&sh->lru, &cb->list);
4044 else
4045 release_stripe(sh);
4046}
4047
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004048static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
NeilBrownd1688a62011-10-11 16:49:52 +11004050 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004051 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 sector_t new_sector;
4053 sector_t logical_sector, last_sector;
4054 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004055 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004056 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Tejun Heoe9c74692010-09-03 11:56:18 +02004058 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4059 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004060 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004061 }
4062
NeilBrown3d310eb2005-06-21 17:17:26 -07004063 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004064
NeilBrown802ba062006-12-13 00:34:13 -08004065 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004066 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004067 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004068 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4071 last_sector = bi->bi_sector + (bi->bi_size>>9);
4072 bi->bi_next = NULL;
4073 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4076 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004077 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004078
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004079 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004080 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004081 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004082 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004083 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004084 * 64bit on a 32bit platform, and so it might be
4085 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004086 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004087 * the lock is dropped, so once we get a reference
4088 * to the stripe that we think it is, we will have
4089 * to check again.
4090 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004091 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004092 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004093 ? logical_sector < conf->reshape_progress
4094 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004095 previous = 1;
4096 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004097 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004098 ? logical_sector < conf->reshape_safe
4099 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004100 spin_unlock_irq(&conf->device_lock);
4101 schedule();
4102 goto retry;
4103 }
4104 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004105 spin_unlock_irq(&conf->device_lock);
4106 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004107
NeilBrown112bf892009-03-31 14:39:38 +11004108 new_sector = raid5_compute_sector(conf, logical_sector,
4109 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004110 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004111 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 (unsigned long long)new_sector,
4113 (unsigned long long)logical_sector);
4114
NeilBrownb5663ba2009-03-31 14:39:38 +11004115 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004116 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004118 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004119 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004120 * stripe, so we must do the range check again.
4121 * Expansion could still move past after this
4122 * test, but as we are holding a reference to
4123 * 'sh', we know that if that happens,
4124 * STRIPE_EXPANDING will get set and the expansion
4125 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004126 */
4127 int must_retry = 0;
4128 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004129 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004130 ? logical_sector >= conf->reshape_progress
4131 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004132 /* mismatch, need to try again */
4133 must_retry = 1;
4134 spin_unlock_irq(&conf->device_lock);
4135 if (must_retry) {
4136 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004137 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004138 goto retry;
4139 }
4140 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004141
Namhyung Kimffd96e32011-07-18 17:38:51 +10004142 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004143 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004144 logical_sector < mddev->suspend_hi) {
4145 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004146 /* As the suspend_* range is controlled by
4147 * userspace, we want an interruptible
4148 * wait.
4149 */
4150 flush_signals(current);
4151 prepare_to_wait(&conf->wait_for_overlap,
4152 &w, TASK_INTERRUPTIBLE);
4153 if (logical_sector >= mddev->suspend_lo &&
4154 logical_sector < mddev->suspend_hi)
4155 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004156 goto retry;
4157 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004158
4159 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004160 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004161 /* Stripe is busy expanding or
4162 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 * and wait a while
4164 */
NeilBrown482c0832011-04-18 18:25:42 +10004165 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 release_stripe(sh);
4167 schedule();
4168 goto retry;
4169 }
4170 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004171 set_bit(STRIPE_HANDLE, &sh->state);
4172 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004173 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004174 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4175 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004176 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 } else {
4178 /* cannot get stripe for read-ahead, just give-up */
4179 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4180 finish_wait(&conf->wait_for_overlap, &w);
4181 break;
4182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004186 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004187 spin_unlock_irq(&conf->device_lock);
4188 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
NeilBrown16a53ec2006-06-26 00:27:38 -07004190 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004192
Neil Brown0e13fe232008-06-28 08:31:20 +10004193 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
NeilBrownfd01b882011-10-11 16:47:53 +11004197static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004198
NeilBrownfd01b882011-10-11 16:47:53 +11004199static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
NeilBrown52c03292006-06-26 00:27:43 -07004201 /* reshaping is quite different to recovery/resync so it is
4202 * handled quite separately ... here.
4203 *
4204 * On each call to sync_request, we gather one chunk worth of
4205 * destination stripes and flag them as expanding.
4206 * Then we find all the source stripes and request reads.
4207 * As the reads complete, handle_stripe will copy the data
4208 * into the destination stripe and release that stripe.
4209 */
NeilBrownd1688a62011-10-11 16:49:52 +11004210 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004212 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004213 int raid_disks = conf->previous_raid_disks;
4214 int data_disks = raid_disks - conf->max_degraded;
4215 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004216 int i;
4217 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004218 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004219 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004220 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004221 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004222
NeilBrownfef9c612009-03-31 15:16:46 +11004223 if (sector_nr == 0) {
4224 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004225 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004226 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4227 sector_nr = raid5_size(mddev, 0, 0)
4228 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004229 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004230 conf->reshape_progress > 0)
4231 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004232 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004233 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004234 mddev->curr_resync_completed = sector_nr;
4235 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004236 *skipped = 1;
4237 return sector_nr;
4238 }
NeilBrown52c03292006-06-26 00:27:43 -07004239 }
4240
NeilBrown7a661382009-03-31 15:21:40 +11004241 /* We need to process a full chunk at a time.
4242 * If old and new chunk sizes differ, we need to process the
4243 * largest of these
4244 */
Andre Noll664e7c42009-06-18 08:45:27 +10004245 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4246 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004247 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004248 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004249
NeilBrownb5254dd2012-05-21 09:27:01 +10004250 /* We update the metadata at least every 10 seconds, or when
4251 * the data about to be copied would over-write the source of
4252 * the data at the front of the range. i.e. one new_stripe
4253 * along from reshape_progress new_maps to after where
4254 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004255 */
NeilBrownfef9c612009-03-31 15:16:46 +11004256 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004257 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004258 readpos = conf->reshape_progress;
4259 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004260 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004261 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004262 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004263 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004264 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004265 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004266 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004267 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004268 readpos -= min_t(sector_t, reshape_sectors, readpos);
4269 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004270 }
NeilBrown52c03292006-06-26 00:27:43 -07004271
NeilBrownb5254dd2012-05-21 09:27:01 +10004272 /* Having calculated the 'writepos' possibly use it
4273 * to set 'stripe_addr' which is where we will write to.
4274 */
4275 if (mddev->reshape_backwards) {
4276 BUG_ON(conf->reshape_progress == 0);
4277 stripe_addr = writepos;
4278 BUG_ON((mddev->dev_sectors &
4279 ~((sector_t)reshape_sectors - 1))
4280 - reshape_sectors - stripe_addr
4281 != sector_nr);
4282 } else {
4283 BUG_ON(writepos != sector_nr + reshape_sectors);
4284 stripe_addr = sector_nr;
4285 }
4286
NeilBrownc8f517c2009-03-31 15:28:40 +11004287 /* 'writepos' is the most advanced device address we might write.
4288 * 'readpos' is the least advanced device address we might read.
4289 * 'safepos' is the least address recorded in the metadata as having
4290 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004291 * If there is a min_offset_diff, these are adjusted either by
4292 * increasing the safepos/readpos if diff is negative, or
4293 * increasing writepos if diff is positive.
4294 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004295 * ensure safety in the face of a crash - that must be done by userspace
4296 * making a backup of the data. So in that case there is no particular
4297 * rush to update metadata.
4298 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4299 * update the metadata to advance 'safepos' to match 'readpos' so that
4300 * we can be safe in the event of a crash.
4301 * So we insist on updating metadata if safepos is behind writepos and
4302 * readpos is beyond writepos.
4303 * In any case, update the metadata every 10 seconds.
4304 * Maybe that number should be configurable, but I'm not sure it is
4305 * worth it.... maybe it could be a multiple of safemode_delay???
4306 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004307 if (conf->min_offset_diff < 0) {
4308 safepos += -conf->min_offset_diff;
4309 readpos += -conf->min_offset_diff;
4310 } else
4311 writepos += conf->min_offset_diff;
4312
NeilBrown2c810cd2012-05-21 09:27:00 +10004313 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004314 ? (safepos > writepos && readpos < writepos)
4315 : (safepos < writepos && readpos > writepos)) ||
4316 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004317 /* Cannot proceed until we've updated the superblock... */
4318 wait_event(conf->wait_for_overlap,
4319 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004320 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004321 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004322 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004323 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004324 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004325 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004326 kthread_should_stop());
4327 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004328 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004329 spin_unlock_irq(&conf->device_lock);
4330 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004331 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004332 }
4333
NeilBrownab69ae12009-03-31 15:26:47 +11004334 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004335 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004336 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004337 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004338 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004339 set_bit(STRIPE_EXPANDING, &sh->state);
4340 atomic_inc(&conf->reshape_stripes);
4341 /* If any of this stripe is beyond the end of the old
4342 * array, then we need to zero those blocks
4343 */
4344 for (j=sh->disks; j--;) {
4345 sector_t s;
4346 if (j == sh->pd_idx)
4347 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004348 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004349 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004350 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004351 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004352 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004353 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004354 continue;
4355 }
4356 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4357 set_bit(R5_Expanded, &sh->dev[j].flags);
4358 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4359 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004360 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004361 set_bit(STRIPE_EXPAND_READY, &sh->state);
4362 set_bit(STRIPE_HANDLE, &sh->state);
4363 }
NeilBrownab69ae12009-03-31 15:26:47 +11004364 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004365 }
4366 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004367 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004368 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004369 else
NeilBrown7a661382009-03-31 15:21:40 +11004370 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004371 spin_unlock_irq(&conf->device_lock);
4372 /* Ok, those stripe are ready. We can start scheduling
4373 * reads on the source stripes.
4374 * The source stripes are determined by mapping the first and last
4375 * block on the destination stripes.
4376 */
NeilBrown52c03292006-06-26 00:27:43 -07004377 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004378 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004379 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004380 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004381 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004382 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004383 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004384 if (last_sector >= mddev->dev_sectors)
4385 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004386 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004387 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004388 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4389 set_bit(STRIPE_HANDLE, &sh->state);
4390 release_stripe(sh);
4391 first_sector += STRIPE_SECTORS;
4392 }
NeilBrownab69ae12009-03-31 15:26:47 +11004393 /* Now that the sources are clearly marked, we can release
4394 * the destination stripes
4395 */
4396 while (!list_empty(&stripes)) {
4397 sh = list_entry(stripes.next, struct stripe_head, lru);
4398 list_del_init(&sh->lru);
4399 release_stripe(sh);
4400 }
NeilBrownc6207272008-02-06 01:39:52 -08004401 /* If this takes us to the resync_max point where we have to pause,
4402 * then we need to write out the superblock.
4403 */
NeilBrown7a661382009-03-31 15:21:40 +11004404 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004405 if ((sector_nr - mddev->curr_resync_completed) * 2
4406 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004407 /* Cannot proceed until we've updated the superblock... */
4408 wait_event(conf->wait_for_overlap,
4409 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004410 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004411 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004412 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004413 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4414 md_wakeup_thread(mddev->thread);
4415 wait_event(mddev->sb_wait,
4416 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4417 || kthread_should_stop());
4418 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004419 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004420 spin_unlock_irq(&conf->device_lock);
4421 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004422 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004423 }
NeilBrown7a661382009-03-31 15:21:40 +11004424 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004425}
4426
4427/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004428static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004429{
NeilBrownd1688a62011-10-11 16:49:52 +11004430 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004431 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004432 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004433 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004434 int still_degraded = 0;
4435 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
NeilBrown72626682005-09-09 16:23:54 -07004437 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004439
NeilBrown29269552006-03-27 01:18:10 -08004440 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4441 end_reshape(conf);
4442 return 0;
4443 }
NeilBrown72626682005-09-09 16:23:54 -07004444
4445 if (mddev->curr_resync < max_sector) /* aborted */
4446 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4447 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004448 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004449 conf->fullsync = 0;
4450 bitmap_close_sync(mddev->bitmap);
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return 0;
4453 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004454
NeilBrown64bd6602009-08-03 10:59:58 +10004455 /* Allow raid5_quiesce to complete */
4456 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4457
NeilBrown52c03292006-06-26 00:27:43 -07004458 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4459 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004460
NeilBrownc6207272008-02-06 01:39:52 -08004461 /* No need to check resync_max as we never do more than one
4462 * stripe, and as resync_max will always be on a chunk boundary,
4463 * if the check in md_do_sync didn't fire, there is no chance
4464 * of overstepping resync_max here
4465 */
4466
NeilBrown16a53ec2006-06-26 00:27:38 -07004467 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 * to resync, then assert that we are finished, because there is
4469 * nothing we can do.
4470 */
NeilBrown3285edf2006-06-26 00:27:55 -07004471 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004472 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004473 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004474 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return rv;
4476 }
NeilBrown72626682005-09-09 16:23:54 -07004477 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004478 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004479 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4480 /* we can skip this block, and probably more */
4481 sync_blocks /= STRIPE_SECTORS;
4482 *skipped = 1;
4483 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
NeilBrownb47490c2008-02-06 01:39:50 -08004486 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4487
NeilBrowna8c906c2009-06-09 14:39:59 +10004488 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004490 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004492 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004494 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004496 /* Need to check if array will still be degraded after recovery/resync
4497 * We don't need to check the 'failed' flag as when that gets set,
4498 * recovery aborts.
4499 */
NeilBrownf001a702009-06-09 14:30:31 +10004500 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004501 if (conf->disks[i].rdev == NULL)
4502 still_degraded = 1;
4503
4504 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4505
NeilBrown83206d62011-07-26 11:19:49 +10004506 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
NeilBrown14425772009-10-16 15:55:25 +11004508 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 release_stripe(sh);
4510
4511 return STRIPE_SECTORS;
4512}
4513
NeilBrownd1688a62011-10-11 16:49:52 +11004514static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004515{
4516 /* We may not be able to submit a whole bio at once as there
4517 * may not be enough stripe_heads available.
4518 * We cannot pre-allocate enough stripe_heads as we may need
4519 * more than exist in the cache (if we allow ever large chunks).
4520 * So we do one stripe head at a time and record in
4521 * ->bi_hw_segments how many have been done.
4522 *
4523 * We *know* that this entire raid_bio is in one chunk, so
4524 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4525 */
4526 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004527 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004528 sector_t sector, logical_sector, last_sector;
4529 int scnt = 0;
4530 int remaining;
4531 int handled = 0;
4532
4533 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004534 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004535 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004536 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4537
4538 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004539 logical_sector += STRIPE_SECTORS,
4540 sector += STRIPE_SECTORS,
4541 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004542
Jens Axboe960e7392008-08-15 10:41:18 +02004543 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004544 /* already done this stripe */
4545 continue;
4546
NeilBrowna8c906c2009-06-09 14:39:59 +10004547 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004548
4549 if (!sh) {
4550 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004551 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004552 conf->retry_read_aligned = raid_bio;
4553 return handled;
4554 }
4555
Neil Brown387bb172007-02-08 14:20:29 -08004556 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4557 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004558 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004559 conf->retry_read_aligned = raid_bio;
4560 return handled;
4561 }
4562
Dan Williams36d1c642009-07-14 11:48:22 -07004563 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004564 release_stripe(sh);
4565 handled++;
4566 }
4567 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004568 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004569 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004570 if (remaining == 0)
4571 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004572 if (atomic_dec_and_test(&conf->active_aligned_reads))
4573 wake_up(&conf->wait_for_stripe);
4574 return handled;
4575}
4576
Shaohua Li46a06402012-08-02 08:33:15 +10004577#define MAX_STRIPE_BATCH 8
4578static int handle_active_stripes(struct r5conf *conf)
4579{
4580 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4581 int i, batch_size = 0;
4582
4583 while (batch_size < MAX_STRIPE_BATCH &&
4584 (sh = __get_priority_stripe(conf)) != NULL)
4585 batch[batch_size++] = sh;
4586
4587 if (batch_size == 0)
4588 return batch_size;
4589 spin_unlock_irq(&conf->device_lock);
4590
4591 for (i = 0; i < batch_size; i++)
4592 handle_stripe(batch[i]);
4593
4594 cond_resched();
4595
4596 spin_lock_irq(&conf->device_lock);
4597 for (i = 0; i < batch_size; i++)
4598 __release_stripe(conf, batch[i]);
4599 return batch_size;
4600}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602/*
4603 * This is our raid5 kernel thread.
4604 *
4605 * We scan the hash table for stripes which can be handled now.
4606 * During the scan, completed stripes are saved for us by the interrupt
4607 * handler, so that they will not have to wait for our next wakeup.
4608 */
NeilBrownfd01b882011-10-11 16:47:53 +11004609static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
NeilBrownd1688a62011-10-11 16:49:52 +11004611 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004613 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Dan Williams45b42332007-07-09 11:56:43 -07004615 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004619 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 handled = 0;
4621 spin_lock_irq(&conf->device_lock);
4622 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004623 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004624 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
NeilBrown0021b7b2012-07-31 09:08:14 +02004626 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004627 !list_empty(&conf->bitmap_list)) {
4628 /* Now is a good time to flush some bitmap updates */
4629 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004630 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004631 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004632 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004633 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004634 activate_bit_delay(conf);
4635 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004636 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004637
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004638 while ((bio = remove_bio_from_retry(conf))) {
4639 int ok;
4640 spin_unlock_irq(&conf->device_lock);
4641 ok = retry_aligned_read(conf, bio);
4642 spin_lock_irq(&conf->device_lock);
4643 if (!ok)
4644 break;
4645 handled++;
4646 }
4647
Shaohua Li46a06402012-08-02 08:33:15 +10004648 batch_size = handle_active_stripes(conf);
4649 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004651 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Shaohua Li46a06402012-08-02 08:33:15 +10004653 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4654 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004655 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004656 spin_lock_irq(&conf->device_lock);
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Dan Williams45b42332007-07-09 11:56:43 -07004659 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 spin_unlock_irq(&conf->device_lock);
4662
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004663 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004664 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Dan Williams45b42332007-07-09 11:56:43 -07004666 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
NeilBrown3f294f42005-11-08 21:39:25 -08004669static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004670raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004671{
NeilBrownd1688a62011-10-11 16:49:52 +11004672 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004673 if (conf)
4674 return sprintf(page, "%d\n", conf->max_nr_stripes);
4675 else
4676 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004677}
4678
NeilBrownc41d4ac2010-06-01 19:37:24 +10004679int
NeilBrownfd01b882011-10-11 16:47:53 +11004680raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004681{
NeilBrownd1688a62011-10-11 16:49:52 +11004682 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004683 int err;
4684
4685 if (size <= 16 || size > 32768)
4686 return -EINVAL;
4687 while (size < conf->max_nr_stripes) {
4688 if (drop_one_stripe(conf))
4689 conf->max_nr_stripes--;
4690 else
4691 break;
4692 }
4693 err = md_allow_write(mddev);
4694 if (err)
4695 return err;
4696 while (size > conf->max_nr_stripes) {
4697 if (grow_one_stripe(conf))
4698 conf->max_nr_stripes++;
4699 else break;
4700 }
4701 return 0;
4702}
4703EXPORT_SYMBOL(raid5_set_cache_size);
4704
NeilBrown3f294f42005-11-08 21:39:25 -08004705static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004706raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004707{
NeilBrownd1688a62011-10-11 16:49:52 +11004708 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004709 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004710 int err;
4711
NeilBrown3f294f42005-11-08 21:39:25 -08004712 if (len >= PAGE_SIZE)
4713 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004714 if (!conf)
4715 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004716
Dan Williams4ef197d82008-04-28 02:15:54 -07004717 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004718 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004719 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004720 if (err)
4721 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004722 return len;
4723}
NeilBrown007583c2005-11-08 21:39:30 -08004724
NeilBrown96de1e62005-11-08 21:39:39 -08004725static struct md_sysfs_entry
4726raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4727 raid5_show_stripe_cache_size,
4728 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004729
4730static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004731raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004732{
NeilBrownd1688a62011-10-11 16:49:52 +11004733 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004734 if (conf)
4735 return sprintf(page, "%d\n", conf->bypass_threshold);
4736 else
4737 return 0;
4738}
4739
4740static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004741raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004742{
NeilBrownd1688a62011-10-11 16:49:52 +11004743 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004744 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004745 if (len >= PAGE_SIZE)
4746 return -EINVAL;
4747 if (!conf)
4748 return -ENODEV;
4749
Dan Williams4ef197d82008-04-28 02:15:54 -07004750 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004751 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004752 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004753 return -EINVAL;
4754 conf->bypass_threshold = new;
4755 return len;
4756}
4757
4758static struct md_sysfs_entry
4759raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4760 S_IRUGO | S_IWUSR,
4761 raid5_show_preread_threshold,
4762 raid5_store_preread_threshold);
4763
4764static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004765stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004766{
NeilBrownd1688a62011-10-11 16:49:52 +11004767 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004768 if (conf)
4769 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4770 else
4771 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004772}
4773
NeilBrown96de1e62005-11-08 21:39:39 -08004774static struct md_sysfs_entry
4775raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004776
NeilBrown007583c2005-11-08 21:39:30 -08004777static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004778 &raid5_stripecache_size.attr,
4779 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004780 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004781 NULL,
4782};
NeilBrown007583c2005-11-08 21:39:30 -08004783static struct attribute_group raid5_attrs_group = {
4784 .name = NULL,
4785 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004786};
4787
Dan Williams80c3a6c2009-03-17 18:10:40 -07004788static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004789raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004790{
NeilBrownd1688a62011-10-11 16:49:52 +11004791 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004792
4793 if (!sectors)
4794 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004795 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004796 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004797 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004798
Andre Noll9d8f0362009-06-18 08:45:01 +10004799 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004800 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004801 return sectors * (raid_disks - conf->max_degraded);
4802}
4803
NeilBrownd1688a62011-10-11 16:49:52 +11004804static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004805{
4806 struct raid5_percpu *percpu;
4807 unsigned long cpu;
4808
4809 if (!conf->percpu)
4810 return;
4811
4812 get_online_cpus();
4813 for_each_possible_cpu(cpu) {
4814 percpu = per_cpu_ptr(conf->percpu, cpu);
4815 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004816 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004817 }
4818#ifdef CONFIG_HOTPLUG_CPU
4819 unregister_cpu_notifier(&conf->cpu_notify);
4820#endif
4821 put_online_cpus();
4822
4823 free_percpu(conf->percpu);
4824}
4825
NeilBrownd1688a62011-10-11 16:49:52 +11004826static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004827{
4828 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004829 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004830 kfree(conf->disks);
4831 kfree(conf->stripe_hashtbl);
4832 kfree(conf);
4833}
4834
Dan Williams36d1c642009-07-14 11:48:22 -07004835#ifdef CONFIG_HOTPLUG_CPU
4836static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4837 void *hcpu)
4838{
NeilBrownd1688a62011-10-11 16:49:52 +11004839 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004840 long cpu = (long)hcpu;
4841 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4842
4843 switch (action) {
4844 case CPU_UP_PREPARE:
4845 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004846 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004847 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004848 if (!percpu->scribble)
4849 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4850
4851 if (!percpu->scribble ||
4852 (conf->level == 6 && !percpu->spare_page)) {
4853 safe_put_page(percpu->spare_page);
4854 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004855 pr_err("%s: failed memory allocation for cpu%ld\n",
4856 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004857 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004858 }
4859 break;
4860 case CPU_DEAD:
4861 case CPU_DEAD_FROZEN:
4862 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004863 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004864 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004865 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004866 break;
4867 default:
4868 break;
4869 }
4870 return NOTIFY_OK;
4871}
4872#endif
4873
NeilBrownd1688a62011-10-11 16:49:52 +11004874static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004875{
4876 unsigned long cpu;
4877 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004878 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004879 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004880 int err;
4881
Dan Williams36d1c642009-07-14 11:48:22 -07004882 allcpus = alloc_percpu(struct raid5_percpu);
4883 if (!allcpus)
4884 return -ENOMEM;
4885 conf->percpu = allcpus;
4886
4887 get_online_cpus();
4888 err = 0;
4889 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004890 if (conf->level == 6) {
4891 spare_page = alloc_page(GFP_KERNEL);
4892 if (!spare_page) {
4893 err = -ENOMEM;
4894 break;
4895 }
4896 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4897 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004898 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004899 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004900 err = -ENOMEM;
4901 break;
4902 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004903 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004904 }
4905#ifdef CONFIG_HOTPLUG_CPU
4906 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4907 conf->cpu_notify.priority = 0;
4908 if (err == 0)
4909 err = register_cpu_notifier(&conf->cpu_notify);
4910#endif
4911 put_online_cpus();
4912
4913 return err;
4914}
4915
NeilBrownd1688a62011-10-11 16:49:52 +11004916static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
NeilBrownd1688a62011-10-11 16:49:52 +11004918 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004919 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004920 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004922 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
NeilBrown91adb562009-03-31 14:39:39 +11004924 if (mddev->new_level != 5
4925 && mddev->new_level != 4
4926 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004927 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004928 mdname(mddev), mddev->new_level);
4929 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
NeilBrown91adb562009-03-31 14:39:39 +11004931 if ((mddev->new_level == 5
4932 && !algorithm_valid_raid5(mddev->new_layout)) ||
4933 (mddev->new_level == 6
4934 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004935 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004936 mdname(mddev), mddev->new_layout);
4937 return ERR_PTR(-EIO);
4938 }
4939 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004940 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004941 mdname(mddev), mddev->raid_disks);
4942 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
Andre Noll664e7c42009-06-18 08:45:27 +10004945 if (!mddev->new_chunk_sectors ||
4946 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4947 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004948 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4949 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004950 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004951 }
4952
NeilBrownd1688a62011-10-11 16:49:52 +11004953 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004954 if (conf == NULL)
4955 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004956 spin_lock_init(&conf->device_lock);
4957 init_waitqueue_head(&conf->wait_for_stripe);
4958 init_waitqueue_head(&conf->wait_for_overlap);
4959 INIT_LIST_HEAD(&conf->handle_list);
4960 INIT_LIST_HEAD(&conf->hold_list);
4961 INIT_LIST_HEAD(&conf->delayed_list);
4962 INIT_LIST_HEAD(&conf->bitmap_list);
4963 INIT_LIST_HEAD(&conf->inactive_list);
4964 atomic_set(&conf->active_stripes, 0);
4965 atomic_set(&conf->preread_active_stripes, 0);
4966 atomic_set(&conf->active_aligned_reads, 0);
4967 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004968 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004969
4970 conf->raid_disks = mddev->raid_disks;
4971 if (mddev->reshape_position == MaxSector)
4972 conf->previous_raid_disks = mddev->raid_disks;
4973 else
4974 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004975 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4976 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004977
NeilBrown5e5e3e72009-10-16 16:35:30 +11004978 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004979 GFP_KERNEL);
4980 if (!conf->disks)
4981 goto abort;
4982
4983 conf->mddev = mddev;
4984
4985 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4986 goto abort;
4987
Dan Williams36d1c642009-07-14 11:48:22 -07004988 conf->level = mddev->new_level;
4989 if (raid5_alloc_percpu(conf) != 0)
4990 goto abort;
4991
NeilBrown0c55e022010-05-03 14:09:02 +10004992 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004993
NeilBrowndafb20f2012-03-19 12:46:39 +11004994 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004995 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004996 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004997 || raid_disk < 0)
4998 continue;
4999 disk = conf->disks + raid_disk;
5000
NeilBrown17045f52011-12-23 10:17:53 +11005001 if (test_bit(Replacement, &rdev->flags)) {
5002 if (disk->replacement)
5003 goto abort;
5004 disk->replacement = rdev;
5005 } else {
5006 if (disk->rdev)
5007 goto abort;
5008 disk->rdev = rdev;
5009 }
NeilBrown91adb562009-03-31 14:39:39 +11005010
5011 if (test_bit(In_sync, &rdev->flags)) {
5012 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005013 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5014 " disk %d\n",
5015 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005016 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005017 /* Cannot rely on bitmap to complete recovery */
5018 conf->fullsync = 1;
5019 }
5020
Andre Noll09c9e5f2009-06-18 08:45:55 +10005021 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005022 conf->level = mddev->new_level;
5023 if (conf->level == 6)
5024 conf->max_degraded = 2;
5025 else
5026 conf->max_degraded = 1;
5027 conf->algorithm = mddev->new_layout;
5028 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005029 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005030 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005031 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005032 conf->prev_algo = mddev->layout;
5033 }
NeilBrown91adb562009-03-31 14:39:39 +11005034
5035 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005036 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005037 if (grow_stripes(conf, conf->max_nr_stripes)) {
5038 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005039 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5040 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005041 goto abort;
5042 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005043 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5044 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005045
NeilBrown02326052012-07-03 15:56:52 +10005046 sprintf(pers_name, "raid%d", mddev->new_level);
5047 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005048 if (!conf->thread) {
5049 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005050 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005051 mdname(mddev));
5052 goto abort;
5053 }
5054
5055 return conf;
5056
5057 abort:
5058 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005059 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005060 return ERR_PTR(-EIO);
5061 } else
5062 return ERR_PTR(-ENOMEM);
5063}
5064
NeilBrownc148ffd2009-11-13 17:47:00 +11005065
5066static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5067{
5068 switch (algo) {
5069 case ALGORITHM_PARITY_0:
5070 if (raid_disk < max_degraded)
5071 return 1;
5072 break;
5073 case ALGORITHM_PARITY_N:
5074 if (raid_disk >= raid_disks - max_degraded)
5075 return 1;
5076 break;
5077 case ALGORITHM_PARITY_0_6:
5078 if (raid_disk == 0 ||
5079 raid_disk == raid_disks - 1)
5080 return 1;
5081 break;
5082 case ALGORITHM_LEFT_ASYMMETRIC_6:
5083 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5084 case ALGORITHM_LEFT_SYMMETRIC_6:
5085 case ALGORITHM_RIGHT_SYMMETRIC_6:
5086 if (raid_disk == raid_disks - 1)
5087 return 1;
5088 }
5089 return 0;
5090}
5091
NeilBrownfd01b882011-10-11 16:47:53 +11005092static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005093{
NeilBrownd1688a62011-10-11 16:49:52 +11005094 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005095 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005096 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005097 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005098 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005099 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005100 long long min_offset_diff = 0;
5101 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005102
Andre Noll8c6ac8682009-06-18 08:48:06 +10005103 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005104 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005105 " -- starting background reconstruction\n",
5106 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005107
5108 rdev_for_each(rdev, mddev) {
5109 long long diff;
5110 if (rdev->raid_disk < 0)
5111 continue;
5112 diff = (rdev->new_data_offset - rdev->data_offset);
5113 if (first) {
5114 min_offset_diff = diff;
5115 first = 0;
5116 } else if (mddev->reshape_backwards &&
5117 diff < min_offset_diff)
5118 min_offset_diff = diff;
5119 else if (!mddev->reshape_backwards &&
5120 diff > min_offset_diff)
5121 min_offset_diff = diff;
5122 }
5123
NeilBrownf6705572006-03-27 01:18:11 -08005124 if (mddev->reshape_position != MaxSector) {
5125 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005126 * Difficulties arise if the stripe we would write to
5127 * next is at or after the stripe we would read from next.
5128 * For a reshape that changes the number of devices, this
5129 * is only possible for a very short time, and mdadm makes
5130 * sure that time appears to have past before assembling
5131 * the array. So we fail if that time hasn't passed.
5132 * For a reshape that keeps the number of devices the same
5133 * mdadm must be monitoring the reshape can keeping the
5134 * critical areas read-only and backed up. It will start
5135 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005136 */
5137 sector_t here_new, here_old;
5138 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005139 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005140
NeilBrown88ce4932009-03-31 15:24:23 +11005141 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005142 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005143 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005144 mdname(mddev));
5145 return -EINVAL;
5146 }
NeilBrownf6705572006-03-27 01:18:11 -08005147 old_disks = mddev->raid_disks - mddev->delta_disks;
5148 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005149 * further up in new geometry must map after here in old
5150 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005151 */
5152 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005153 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005154 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005155 printk(KERN_ERR "md/raid:%s: reshape_position not "
5156 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005157 return -EINVAL;
5158 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005159 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005160 /* here_new is the stripe we will write to */
5161 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005162 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005163 (old_disks-max_degraded));
5164 /* here_old is the first stripe that we might need to read
5165 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005166 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005167 if ((here_new * mddev->new_chunk_sectors !=
5168 here_old * mddev->chunk_sectors)) {
5169 printk(KERN_ERR "md/raid:%s: reshape position is"
5170 " confused - aborting\n", mdname(mddev));
5171 return -EINVAL;
5172 }
NeilBrown67ac6012009-08-13 10:06:24 +10005173 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005174 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005175 * and taking constant backups.
5176 * mdadm always starts a situation like this in
5177 * readonly mode so it can take control before
5178 * allowing any writes. So just check for that.
5179 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005180 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5181 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5182 /* not really in-place - so OK */;
5183 else if (mddev->ro == 0) {
5184 printk(KERN_ERR "md/raid:%s: in-place reshape "
5185 "must be started in read-only mode "
5186 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005187 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005188 return -EINVAL;
5189 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005190 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005191 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005192 here_old * mddev->chunk_sectors)
5193 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005194 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005195 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005196 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5197 "auto-recovery - aborting.\n",
5198 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005199 return -EINVAL;
5200 }
NeilBrown0c55e022010-05-03 14:09:02 +10005201 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5202 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005203 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005204 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005205 BUG_ON(mddev->level != mddev->new_level);
5206 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005207 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005208 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005209 }
5210
NeilBrown245f46c2009-03-31 14:39:39 +11005211 if (mddev->private == NULL)
5212 conf = setup_conf(mddev);
5213 else
5214 conf = mddev->private;
5215
NeilBrown91adb562009-03-31 14:39:39 +11005216 if (IS_ERR(conf))
5217 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005218
NeilBrownb5254dd2012-05-21 09:27:01 +10005219 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005220 mddev->thread = conf->thread;
5221 conf->thread = NULL;
5222 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
NeilBrown17045f52011-12-23 10:17:53 +11005224 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5225 i++) {
5226 rdev = conf->disks[i].rdev;
5227 if (!rdev && conf->disks[i].replacement) {
5228 /* The replacement is all we have yet */
5229 rdev = conf->disks[i].replacement;
5230 conf->disks[i].replacement = NULL;
5231 clear_bit(Replacement, &rdev->flags);
5232 conf->disks[i].rdev = rdev;
5233 }
5234 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005235 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005236 if (conf->disks[i].replacement &&
5237 conf->reshape_progress != MaxSector) {
5238 /* replacements and reshape simply do not mix. */
5239 printk(KERN_ERR "md: cannot handle concurrent "
5240 "replacement and reshape.\n");
5241 goto abort;
5242 }
NeilBrown2f115882010-06-17 17:41:03 +10005243 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005244 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005245 continue;
5246 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005247 /* This disc is not fully in-sync. However if it
5248 * just stored parity (beyond the recovery_offset),
5249 * when we don't need to be concerned about the
5250 * array being dirty.
5251 * When reshape goes 'backwards', we never have
5252 * partially completed devices, so we only need
5253 * to worry about reshape going forwards.
5254 */
5255 /* Hack because v0.91 doesn't store recovery_offset properly. */
5256 if (mddev->major_version == 0 &&
5257 mddev->minor_version > 90)
5258 rdev->recovery_offset = reshape_offset;
5259
NeilBrownc148ffd2009-11-13 17:47:00 +11005260 if (rdev->recovery_offset < reshape_offset) {
5261 /* We need to check old and new layout */
5262 if (!only_parity(rdev->raid_disk,
5263 conf->algorithm,
5264 conf->raid_disks,
5265 conf->max_degraded))
5266 continue;
5267 }
5268 if (!only_parity(rdev->raid_disk,
5269 conf->prev_algo,
5270 conf->previous_raid_disks,
5271 conf->max_degraded))
5272 continue;
5273 dirty_parity_disks++;
5274 }
NeilBrown91adb562009-03-31 14:39:39 +11005275
NeilBrown17045f52011-12-23 10:17:53 +11005276 /*
5277 * 0 for a fully functional array, 1 or 2 for a degraded array.
5278 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005279 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
NeilBrown674806d2010-06-16 17:17:53 +10005281 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005282 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005284 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 goto abort;
5286 }
5287
NeilBrown91adb562009-03-31 14:39:39 +11005288 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005289 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005290 mddev->resync_max_sectors = mddev->dev_sectors;
5291
NeilBrownc148ffd2009-11-13 17:47:00 +11005292 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005294 if (mddev->ok_start_degraded)
5295 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005296 "md/raid:%s: starting dirty degraded array"
5297 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005298 mdname(mddev));
5299 else {
5300 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005301 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005302 mdname(mddev));
5303 goto abort;
5304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
5306
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005308 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5309 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005310 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5311 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 else
NeilBrown0c55e022010-05-03 14:09:02 +10005313 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5314 " out of %d devices, algorithm %d\n",
5315 mdname(mddev), conf->level,
5316 mddev->raid_disks - mddev->degraded,
5317 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318
5319 print_raid5_conf(conf);
5320
NeilBrownfef9c612009-03-31 15:16:46 +11005321 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005322 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005323 atomic_set(&conf->reshape_stripes, 0);
5324 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5325 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5326 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5327 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5328 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005329 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005330 }
5331
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005334 if (mddev->to_remove == &raid5_attrs_group)
5335 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005336 else if (mddev->kobj.sd &&
5337 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005338 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005339 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005340 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005341 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5342
5343 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005344 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005345 /* read-ahead size must cover two whole stripes, which
5346 * is 2 * (datadisks) * chunksize where 'n' is the
5347 * number of raid devices
5348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5350 int stripe = data_disks *
5351 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5352 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5353 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005354
5355 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005356
5357 mddev->queue->backing_dev_info.congested_data = mddev;
5358 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005359
5360 chunk_size = mddev->chunk_sectors << 9;
5361 blk_queue_io_min(mddev->queue, chunk_size);
5362 blk_queue_io_opt(mddev->queue, chunk_size *
5363 (conf->raid_disks - conf->max_degraded));
5364
NeilBrown05616be2012-05-21 09:27:00 +10005365 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005366 disk_stack_limits(mddev->gendisk, rdev->bdev,
5367 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005368 disk_stack_limits(mddev->gendisk, rdev->bdev,
5369 rdev->new_data_offset << 9);
5370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 return 0;
5374abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005375 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005376 print_raid5_conf(conf);
5377 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005379 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return -EIO;
5381}
5382
NeilBrownfd01b882011-10-11 16:47:53 +11005383static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
NeilBrownd1688a62011-10-11 16:49:52 +11005385 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
NeilBrown01f96c02011-09-21 15:30:20 +10005387 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005388 if (mddev->queue)
5389 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005390 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005391 mddev->private = NULL;
5392 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 return 0;
5394}
5395
NeilBrownfd01b882011-10-11 16:47:53 +11005396static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
NeilBrownd1688a62011-10-11 16:49:52 +11005398 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 int i;
5400
Andre Noll9d8f0362009-06-18 08:45:01 +10005401 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5402 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005403 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 for (i = 0; i < conf->raid_disks; i++)
5405 seq_printf (seq, "%s",
5406 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005407 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
NeilBrownd1688a62011-10-11 16:49:52 +11005411static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
5413 int i;
5414 struct disk_info *tmp;
5415
NeilBrown0c55e022010-05-03 14:09:02 +10005416 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 if (!conf) {
5418 printk("(conf==NULL)\n");
5419 return;
5420 }
NeilBrown0c55e022010-05-03 14:09:02 +10005421 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5422 conf->raid_disks,
5423 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 for (i = 0; i < conf->raid_disks; i++) {
5426 char b[BDEVNAME_SIZE];
5427 tmp = conf->disks + i;
5428 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005429 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5430 i, !test_bit(Faulty, &tmp->rdev->flags),
5431 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433}
5434
NeilBrownfd01b882011-10-11 16:47:53 +11005435static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005438 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005440 int count = 0;
5441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 for (i = 0; i < conf->raid_disks; i++) {
5444 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005445 if (tmp->replacement
5446 && tmp->replacement->recovery_offset == MaxSector
5447 && !test_bit(Faulty, &tmp->replacement->flags)
5448 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5449 /* Replacement has just become active. */
5450 if (!tmp->rdev
5451 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5452 count++;
5453 if (tmp->rdev) {
5454 /* Replaced device not technically faulty,
5455 * but we need to be sure it gets removed
5456 * and never re-added.
5457 */
5458 set_bit(Faulty, &tmp->rdev->flags);
5459 sysfs_notify_dirent_safe(
5460 tmp->rdev->sysfs_state);
5461 }
5462 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5463 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005464 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005465 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005466 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005467 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005468 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 }
5470 }
NeilBrown6b965622010-08-18 11:56:59 +10005471 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005472 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005473 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005475 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
NeilBrownb8321b62011-12-23 10:17:51 +11005478static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
NeilBrownd1688a62011-10-11 16:49:52 +11005480 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005482 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005483 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 struct disk_info *p = conf->disks + number;
5485
5486 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005487 if (rdev == p->rdev)
5488 rdevp = &p->rdev;
5489 else if (rdev == p->replacement)
5490 rdevp = &p->replacement;
5491 else
5492 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005493
NeilBrown657e3e42011-12-23 10:17:52 +11005494 if (number >= conf->raid_disks &&
5495 conf->reshape_progress == MaxSector)
5496 clear_bit(In_sync, &rdev->flags);
5497
5498 if (test_bit(In_sync, &rdev->flags) ||
5499 atomic_read(&rdev->nr_pending)) {
5500 err = -EBUSY;
5501 goto abort;
5502 }
5503 /* Only remove non-faulty devices if recovery
5504 * isn't possible.
5505 */
5506 if (!test_bit(Faulty, &rdev->flags) &&
5507 mddev->recovery_disabled != conf->recovery_disabled &&
5508 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005509 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005510 number < conf->raid_disks) {
5511 err = -EBUSY;
5512 goto abort;
5513 }
5514 *rdevp = NULL;
5515 synchronize_rcu();
5516 if (atomic_read(&rdev->nr_pending)) {
5517 /* lost the race, try later */
5518 err = -EBUSY;
5519 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005520 } else if (p->replacement) {
5521 /* We must have just cleared 'rdev' */
5522 p->rdev = p->replacement;
5523 clear_bit(Replacement, &p->replacement->flags);
5524 smp_mb(); /* Make sure other CPUs may see both as identical
5525 * but will never see neither - if they are careful
5526 */
5527 p->replacement = NULL;
5528 clear_bit(WantReplacement, &rdev->flags);
5529 } else
5530 /* We might have just removed the Replacement as faulty-
5531 * clear the bit just in case
5532 */
5533 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534abort:
5535
5536 print_raid5_conf(conf);
5537 return err;
5538}
5539
NeilBrownfd01b882011-10-11 16:47:53 +11005540static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
NeilBrownd1688a62011-10-11 16:49:52 +11005542 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005543 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 int disk;
5545 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005546 int first = 0;
5547 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
NeilBrown7f0da592011-07-28 11:39:22 +10005549 if (mddev->recovery_disabled == conf->recovery_disabled)
5550 return -EBUSY;
5551
NeilBrowndc10c642012-03-19 12:46:37 +11005552 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Neil Brown6c2fce22008-06-28 08:31:31 +10005556 if (rdev->raid_disk >= 0)
5557 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005560 * find the disk ... but prefer rdev->saved_raid_disk
5561 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005563 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005564 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005565 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005566 first = rdev->saved_raid_disk;
5567
5568 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005569 p = conf->disks + disk;
5570 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005571 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005573 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005574 if (rdev->saved_raid_disk != disk)
5575 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005576 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005579 }
5580 for (disk = first; disk <= last; disk++) {
5581 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005582 if (test_bit(WantReplacement, &p->rdev->flags) &&
5583 p->replacement == NULL) {
5584 clear_bit(In_sync, &rdev->flags);
5585 set_bit(Replacement, &rdev->flags);
5586 rdev->raid_disk = disk;
5587 err = 0;
5588 conf->fullsync = 1;
5589 rcu_assign_pointer(p->replacement, rdev);
5590 break;
5591 }
5592 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005593out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596}
5597
NeilBrownfd01b882011-10-11 16:47:53 +11005598static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599{
5600 /* no resync is happening, and there is enough space
5601 * on all devices, so we can resize.
5602 * We need to make sure resync covers any new space.
5603 * If the array is shrinking we should possibly wait until
5604 * any io in the removed space completes, but it hardly seems
5605 * worth it.
5606 */
NeilBrowna4a61252012-05-22 13:55:27 +10005607 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005608 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005609 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5610 if (mddev->external_size &&
5611 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005612 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005613 if (mddev->bitmap) {
5614 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5615 if (ret)
5616 return ret;
5617 }
5618 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005619 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005620 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005621 if (sectors > mddev->dev_sectors &&
5622 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005623 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5625 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005626 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005627 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 return 0;
5629}
5630
NeilBrownfd01b882011-10-11 16:47:53 +11005631static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005632{
5633 /* Can only proceed if there are plenty of stripe_heads.
5634 * We need a minimum of one full stripe,, and for sensible progress
5635 * it is best to have about 4 times that.
5636 * If we require 4 times, then the default 256 4K stripe_heads will
5637 * allow for chunk sizes up to 256K, which is probably OK.
5638 * If the chunk size is greater, user-space should request more
5639 * stripe_heads first.
5640 */
NeilBrownd1688a62011-10-11 16:49:52 +11005641 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005642 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5643 > conf->max_nr_stripes ||
5644 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5645 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005646 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5647 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005648 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5649 / STRIPE_SIZE)*4);
5650 return 0;
5651 }
5652 return 1;
5653}
5654
NeilBrownfd01b882011-10-11 16:47:53 +11005655static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005656{
NeilBrownd1688a62011-10-11 16:49:52 +11005657 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005658
NeilBrown88ce4932009-03-31 15:24:23 +11005659 if (mddev->delta_disks == 0 &&
5660 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005661 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005662 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005663 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005664 return -EINVAL;
5665 if (mddev->delta_disks < 0) {
5666 /* We might be able to shrink, but the devices must
5667 * be made bigger first.
5668 * For raid6, 4 is the minimum size.
5669 * Otherwise 2 is the minimum
5670 */
5671 int min = 2;
5672 if (mddev->level == 6)
5673 min = 4;
5674 if (mddev->raid_disks + mddev->delta_disks < min)
5675 return -EINVAL;
5676 }
NeilBrown29269552006-03-27 01:18:10 -08005677
NeilBrown01ee22b2009-06-18 08:47:20 +10005678 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005679 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005680
NeilBrownec32a2b2009-03-31 15:17:38 +11005681 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005682}
5683
NeilBrownfd01b882011-10-11 16:47:53 +11005684static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005685{
NeilBrownd1688a62011-10-11 16:49:52 +11005686 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005687 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005688 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005689 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005690
NeilBrownf4168852007-02-28 20:11:53 -08005691 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005692 return -EBUSY;
5693
NeilBrown01ee22b2009-06-18 08:47:20 +10005694 if (!check_stripe_cache(mddev))
5695 return -ENOSPC;
5696
NeilBrown30b67642012-05-22 13:55:28 +10005697 if (has_failed(conf))
5698 return -EINVAL;
5699
NeilBrownc6563a82012-05-21 09:27:00 +10005700 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005701 if (!test_bit(In_sync, &rdev->flags)
5702 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005703 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005704 }
NeilBrown63c70c42006-03-27 01:18:13 -08005705
NeilBrownf4168852007-02-28 20:11:53 -08005706 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005707 /* Not enough devices even to make a degraded array
5708 * of that size
5709 */
5710 return -EINVAL;
5711
NeilBrownec32a2b2009-03-31 15:17:38 +11005712 /* Refuse to reduce size of the array. Any reductions in
5713 * array size must be through explicit setting of array_size
5714 * attribute.
5715 */
5716 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5717 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005718 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005719 "before number of disks\n", mdname(mddev));
5720 return -EINVAL;
5721 }
5722
NeilBrownf6705572006-03-27 01:18:11 -08005723 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005724 spin_lock_irq(&conf->device_lock);
5725 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005726 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005727 conf->prev_chunk_sectors = conf->chunk_sectors;
5728 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005729 conf->prev_algo = conf->algorithm;
5730 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005731 conf->generation++;
5732 /* Code that selects data_offset needs to see the generation update
5733 * if reshape_progress has been set - so a memory barrier needed.
5734 */
5735 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005736 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005737 conf->reshape_progress = raid5_size(mddev, 0, 0);
5738 else
5739 conf->reshape_progress = 0;
5740 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005741 spin_unlock_irq(&conf->device_lock);
5742
5743 /* Add some new drives, as many as will fit.
5744 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005745 * Don't add devices if we are reducing the number of
5746 * devices in the array. This is because it is not possible
5747 * to correctly record the "partially reconstructed" state of
5748 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005749 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005750 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005751 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005752 if (rdev->raid_disk < 0 &&
5753 !test_bit(Faulty, &rdev->flags)) {
5754 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005755 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005756 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005757 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005758 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005759 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005760
5761 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005762 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005763 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005764 } else if (rdev->raid_disk >= conf->previous_raid_disks
5765 && !test_bit(Faulty, &rdev->flags)) {
5766 /* This is a spare that was manually added */
5767 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005768 }
NeilBrown29269552006-03-27 01:18:10 -08005769
NeilBrown87a8dec2011-01-31 11:57:43 +11005770 /* When a reshape changes the number of devices,
5771 * ->degraded is measured against the larger of the
5772 * pre and post number of devices.
5773 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005774 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005775 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005776 spin_unlock_irqrestore(&conf->device_lock, flags);
5777 }
NeilBrown63c70c42006-03-27 01:18:13 -08005778 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005779 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005780 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005781
NeilBrown29269552006-03-27 01:18:10 -08005782 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5783 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5784 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5785 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5786 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005787 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005788 if (!mddev->sync_thread) {
5789 mddev->recovery = 0;
5790 spin_lock_irq(&conf->device_lock);
5791 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005792 rdev_for_each(rdev, mddev)
5793 rdev->new_data_offset = rdev->data_offset;
5794 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005795 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005796 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005797 spin_unlock_irq(&conf->device_lock);
5798 return -EAGAIN;
5799 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005800 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005801 md_wakeup_thread(mddev->sync_thread);
5802 md_new_event(mddev);
5803 return 0;
5804}
NeilBrown29269552006-03-27 01:18:10 -08005805
NeilBrownec32a2b2009-03-31 15:17:38 +11005806/* This is called from the reshape thread and should make any
5807 * changes needed in 'conf'
5808 */
NeilBrownd1688a62011-10-11 16:49:52 +11005809static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005810{
NeilBrown29269552006-03-27 01:18:10 -08005811
NeilBrownf6705572006-03-27 01:18:11 -08005812 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005813 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005814
NeilBrownf6705572006-03-27 01:18:11 -08005815 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005816 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005817 rdev_for_each(rdev, conf->mddev)
5818 rdev->data_offset = rdev->new_data_offset;
5819 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005820 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005821 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005822 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005823
5824 /* read-ahead size must cover two whole stripes, which is
5825 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5826 */
NeilBrown4a5add42010-06-01 19:37:28 +10005827 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005828 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005829 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005830 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005831 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5832 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5833 }
NeilBrown29269552006-03-27 01:18:10 -08005834 }
NeilBrown29269552006-03-27 01:18:10 -08005835}
5836
NeilBrownec32a2b2009-03-31 15:17:38 +11005837/* This is called from the raid5d thread with mddev_lock held.
5838 * It makes config changes to the device.
5839 */
NeilBrownfd01b882011-10-11 16:47:53 +11005840static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005841{
NeilBrownd1688a62011-10-11 16:49:52 +11005842 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005843
5844 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5845
NeilBrownec32a2b2009-03-31 15:17:38 +11005846 if (mddev->delta_disks > 0) {
5847 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5848 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005849 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005850 } else {
5851 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005852 spin_lock_irq(&conf->device_lock);
5853 mddev->degraded = calc_degraded(conf);
5854 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005855 for (d = conf->raid_disks ;
5856 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005857 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005858 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005859 if (rdev)
5860 clear_bit(In_sync, &rdev->flags);
5861 rdev = conf->disks[d].replacement;
5862 if (rdev)
5863 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005864 }
NeilBrowncea9c222009-03-31 15:15:05 +11005865 }
NeilBrown88ce4932009-03-31 15:24:23 +11005866 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005867 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005868 mddev->reshape_position = MaxSector;
5869 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005870 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005871 }
5872}
5873
NeilBrownfd01b882011-10-11 16:47:53 +11005874static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005875{
NeilBrownd1688a62011-10-11 16:49:52 +11005876 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005877
5878 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005879 case 2: /* resume for a suspend */
5880 wake_up(&conf->wait_for_overlap);
5881 break;
5882
NeilBrown72626682005-09-09 16:23:54 -07005883 case 1: /* stop all writes */
5884 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005885 /* '2' tells resync/reshape to pause so that all
5886 * active stripes can drain
5887 */
5888 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005889 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005890 atomic_read(&conf->active_stripes) == 0 &&
5891 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005892 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005893 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005894 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005895 /* allow reshape to continue */
5896 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005897 break;
5898
5899 case 0: /* re-enable writes */
5900 spin_lock_irq(&conf->device_lock);
5901 conf->quiesce = 0;
5902 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005903 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005904 spin_unlock_irq(&conf->device_lock);
5905 break;
5906 }
NeilBrown72626682005-09-09 16:23:54 -07005907}
NeilBrownb15c2e52006-01-06 00:20:16 -08005908
NeilBrownd562b0c2009-03-31 14:39:39 +11005909
NeilBrownfd01b882011-10-11 16:47:53 +11005910static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005911{
NeilBrowne373ab12011-10-11 16:48:59 +11005912 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005913 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005914
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005915 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005916 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005917 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5918 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005919 return ERR_PTR(-EINVAL);
5920 }
5921
NeilBrowne373ab12011-10-11 16:48:59 +11005922 sectors = raid0_conf->strip_zone[0].zone_end;
5923 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005924 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005925 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005926 mddev->new_layout = ALGORITHM_PARITY_N;
5927 mddev->new_chunk_sectors = mddev->chunk_sectors;
5928 mddev->raid_disks += 1;
5929 mddev->delta_disks = 1;
5930 /* make sure it will be not marked as dirty */
5931 mddev->recovery_cp = MaxSector;
5932
5933 return setup_conf(mddev);
5934}
5935
5936
NeilBrownfd01b882011-10-11 16:47:53 +11005937static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005938{
5939 int chunksect;
5940
5941 if (mddev->raid_disks != 2 ||
5942 mddev->degraded > 1)
5943 return ERR_PTR(-EINVAL);
5944
5945 /* Should check if there are write-behind devices? */
5946
5947 chunksect = 64*2; /* 64K by default */
5948
5949 /* The array must be an exact multiple of chunksize */
5950 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5951 chunksect >>= 1;
5952
5953 if ((chunksect<<9) < STRIPE_SIZE)
5954 /* array size does not allow a suitable chunk size */
5955 return ERR_PTR(-EINVAL);
5956
5957 mddev->new_level = 5;
5958 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005959 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005960
5961 return setup_conf(mddev);
5962}
5963
NeilBrownfd01b882011-10-11 16:47:53 +11005964static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005965{
5966 int new_layout;
5967
5968 switch (mddev->layout) {
5969 case ALGORITHM_LEFT_ASYMMETRIC_6:
5970 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5971 break;
5972 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5973 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5974 break;
5975 case ALGORITHM_LEFT_SYMMETRIC_6:
5976 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5977 break;
5978 case ALGORITHM_RIGHT_SYMMETRIC_6:
5979 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5980 break;
5981 case ALGORITHM_PARITY_0_6:
5982 new_layout = ALGORITHM_PARITY_0;
5983 break;
5984 case ALGORITHM_PARITY_N:
5985 new_layout = ALGORITHM_PARITY_N;
5986 break;
5987 default:
5988 return ERR_PTR(-EINVAL);
5989 }
5990 mddev->new_level = 5;
5991 mddev->new_layout = new_layout;
5992 mddev->delta_disks = -1;
5993 mddev->raid_disks -= 1;
5994 return setup_conf(mddev);
5995}
5996
NeilBrownd562b0c2009-03-31 14:39:39 +11005997
NeilBrownfd01b882011-10-11 16:47:53 +11005998static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005999{
NeilBrown88ce4932009-03-31 15:24:23 +11006000 /* For a 2-drive array, the layout and chunk size can be changed
6001 * immediately as not restriping is needed.
6002 * For larger arrays we record the new value - after validation
6003 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006004 */
NeilBrownd1688a62011-10-11 16:49:52 +11006005 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006006 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006007
NeilBrown597a7112009-06-18 08:47:42 +10006008 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006009 return -EINVAL;
6010 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006011 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006012 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006013 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006014 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006015 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006016 /* not factor of array size */
6017 return -EINVAL;
6018 }
6019
6020 /* They look valid */
6021
NeilBrown88ce4932009-03-31 15:24:23 +11006022 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006023 /* can make the change immediately */
6024 if (mddev->new_layout >= 0) {
6025 conf->algorithm = mddev->new_layout;
6026 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006027 }
6028 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006029 conf->chunk_sectors = new_chunk ;
6030 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006031 }
6032 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6033 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006034 }
NeilBrown50ac1682009-06-18 08:47:55 +10006035 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006036}
6037
NeilBrownfd01b882011-10-11 16:47:53 +11006038static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006039{
NeilBrown597a7112009-06-18 08:47:42 +10006040 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006041
NeilBrown597a7112009-06-18 08:47:42 +10006042 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006043 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006044 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006045 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006046 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006047 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006048 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006049 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006050 /* not factor of array size */
6051 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006052 }
NeilBrown88ce4932009-03-31 15:24:23 +11006053
6054 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006055 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006056}
6057
NeilBrownfd01b882011-10-11 16:47:53 +11006058static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006059{
6060 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006061 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006062 * raid1 - if there are two drives. We need to know the chunk size
6063 * raid4 - trivial - just use a raid4 layout.
6064 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006065 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006066 if (mddev->level == 0)
6067 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006068 if (mddev->level == 1)
6069 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006070 if (mddev->level == 4) {
6071 mddev->new_layout = ALGORITHM_PARITY_N;
6072 mddev->new_level = 5;
6073 return setup_conf(mddev);
6074 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006075 if (mddev->level == 6)
6076 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006077
6078 return ERR_PTR(-EINVAL);
6079}
6080
NeilBrownfd01b882011-10-11 16:47:53 +11006081static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006082{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006083 /* raid4 can take over:
6084 * raid0 - if there is only one strip zone
6085 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006086 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006087 if (mddev->level == 0)
6088 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006089 if (mddev->level == 5 &&
6090 mddev->layout == ALGORITHM_PARITY_N) {
6091 mddev->new_layout = 0;
6092 mddev->new_level = 4;
6093 return setup_conf(mddev);
6094 }
6095 return ERR_PTR(-EINVAL);
6096}
NeilBrownd562b0c2009-03-31 14:39:39 +11006097
NeilBrown84fc4b52011-10-11 16:49:58 +11006098static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006099
NeilBrownfd01b882011-10-11 16:47:53 +11006100static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006101{
6102 /* Currently can only take over a raid5. We map the
6103 * personality to an equivalent raid6 personality
6104 * with the Q block at the end.
6105 */
6106 int new_layout;
6107
6108 if (mddev->pers != &raid5_personality)
6109 return ERR_PTR(-EINVAL);
6110 if (mddev->degraded > 1)
6111 return ERR_PTR(-EINVAL);
6112 if (mddev->raid_disks > 253)
6113 return ERR_PTR(-EINVAL);
6114 if (mddev->raid_disks < 3)
6115 return ERR_PTR(-EINVAL);
6116
6117 switch (mddev->layout) {
6118 case ALGORITHM_LEFT_ASYMMETRIC:
6119 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6120 break;
6121 case ALGORITHM_RIGHT_ASYMMETRIC:
6122 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6123 break;
6124 case ALGORITHM_LEFT_SYMMETRIC:
6125 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6126 break;
6127 case ALGORITHM_RIGHT_SYMMETRIC:
6128 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6129 break;
6130 case ALGORITHM_PARITY_0:
6131 new_layout = ALGORITHM_PARITY_0_6;
6132 break;
6133 case ALGORITHM_PARITY_N:
6134 new_layout = ALGORITHM_PARITY_N;
6135 break;
6136 default:
6137 return ERR_PTR(-EINVAL);
6138 }
6139 mddev->new_level = 6;
6140 mddev->new_layout = new_layout;
6141 mddev->delta_disks = 1;
6142 mddev->raid_disks += 1;
6143 return setup_conf(mddev);
6144}
6145
6146
NeilBrown84fc4b52011-10-11 16:49:58 +11006147static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006148{
6149 .name = "raid6",
6150 .level = 6,
6151 .owner = THIS_MODULE,
6152 .make_request = make_request,
6153 .run = run,
6154 .stop = stop,
6155 .status = status,
6156 .error_handler = error,
6157 .hot_add_disk = raid5_add_disk,
6158 .hot_remove_disk= raid5_remove_disk,
6159 .spare_active = raid5_spare_active,
6160 .sync_request = sync_request,
6161 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006162 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006163 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006164 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006165 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006166 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006167 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006168};
NeilBrown84fc4b52011-10-11 16:49:58 +11006169static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170{
6171 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006172 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 .owner = THIS_MODULE,
6174 .make_request = make_request,
6175 .run = run,
6176 .stop = stop,
6177 .status = status,
6178 .error_handler = error,
6179 .hot_add_disk = raid5_add_disk,
6180 .hot_remove_disk= raid5_remove_disk,
6181 .spare_active = raid5_spare_active,
6182 .sync_request = sync_request,
6183 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006184 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006185 .check_reshape = raid5_check_reshape,
6186 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006187 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006188 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006189 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190};
6191
NeilBrown84fc4b52011-10-11 16:49:58 +11006192static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
NeilBrown2604b702006-01-06 00:20:36 -08006194 .name = "raid4",
6195 .level = 4,
6196 .owner = THIS_MODULE,
6197 .make_request = make_request,
6198 .run = run,
6199 .stop = stop,
6200 .status = status,
6201 .error_handler = error,
6202 .hot_add_disk = raid5_add_disk,
6203 .hot_remove_disk= raid5_remove_disk,
6204 .spare_active = raid5_spare_active,
6205 .sync_request = sync_request,
6206 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006207 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006208 .check_reshape = raid5_check_reshape,
6209 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006210 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006211 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006212 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006213};
6214
6215static int __init raid5_init(void)
6216{
NeilBrown16a53ec2006-06-26 00:27:38 -07006217 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006218 register_md_personality(&raid5_personality);
6219 register_md_personality(&raid4_personality);
6220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221}
6222
NeilBrown2604b702006-01-06 00:20:36 -08006223static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224{
NeilBrown16a53ec2006-06-26 00:27:38 -07006225 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006226 unregister_md_personality(&raid5_personality);
6227 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
6230module_init(raid5_init);
6231module_exit(raid5_exit);
6232MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006233MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006235MODULE_ALIAS("md-raid5");
6236MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006237MODULE_ALIAS("md-level-5");
6238MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006239MODULE_ALIAS("md-personality-8"); /* RAID6 */
6240MODULE_ALIAS("md-raid6");
6241MODULE_ALIAS("md-level-6");
6242
6243/* This used to be two separate modules, they were: */
6244MODULE_ALIAS("raid5");
6245MODULE_ALIAS("raid6");