blob: bde9da2baa392d76a1ea885a136790487ccf962f [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)
474 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 if (!test_bit(STRIPE_HANDLE, &sh->state))
477 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700478 if (list_empty(&sh->lru) &&
479 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700480 BUG();
481 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
484 } while (sh == NULL);
485
486 if (sh)
487 atomic_inc(&sh->count);
488
489 spin_unlock_irq(&conf->device_lock);
490 return sh;
491}
492
NeilBrown05616be2012-05-21 09:27:00 +1000493/* Determine if 'data_offset' or 'new_data_offset' should be used
494 * in this stripe_head.
495 */
496static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
497{
498 sector_t progress = conf->reshape_progress;
499 /* Need a memory barrier to make sure we see the value
500 * of conf->generation, or ->data_offset that was set before
501 * reshape_progress was updated.
502 */
503 smp_rmb();
504 if (progress == MaxSector)
505 return 0;
506 if (sh->generation == conf->generation - 1)
507 return 0;
508 /* We are in a reshape, and this is a new-generation stripe,
509 * so use new_data_offset.
510 */
511 return 1;
512}
513
NeilBrown6712ecf2007-09-27 12:47:43 +0200514static void
515raid5_end_read_request(struct bio *bi, int error);
516static void
517raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700518
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000519static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700520{
NeilBrownd1688a62011-10-11 16:49:52 +1100521 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700522 int i, disks = sh->disks;
523
524 might_sleep();
525
526 for (i = disks; i--; ) {
527 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100528 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100529 struct bio *bi, *rbi;
530 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200531 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
532 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
533 rw = WRITE_FUA;
534 else
535 rw = WRITE;
536 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700537 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100538 else if (test_and_clear_bit(R5_WantReplace,
539 &sh->dev[i].flags)) {
540 rw = WRITE;
541 replace_only = 1;
542 } else
Dan Williams91c00922007-01-02 13:52:30 -0700543 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000544 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
545 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700546
547 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100548 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700549
550 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100551 rbi->bi_rw = rw;
552 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700553 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100554 rbi->bi_end_io = raid5_end_write_request;
555 } else
Dan Williams91c00922007-01-02 13:52:30 -0700556 bi->bi_end_io = raid5_end_read_request;
557
558 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100559 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100560 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
561 rdev = rcu_dereference(conf->disks[i].rdev);
562 if (!rdev) {
563 rdev = rrdev;
564 rrdev = NULL;
565 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100566 if (rw & WRITE) {
567 if (replace_only)
568 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100569 if (rdev == rrdev)
570 /* We raced and saw duplicates */
571 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100572 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100573 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100574 rdev = rrdev;
575 rrdev = NULL;
576 }
NeilBrown977df362011-12-23 10:17:53 +1100577
Dan Williams91c00922007-01-02 13:52:30 -0700578 if (rdev && test_bit(Faulty, &rdev->flags))
579 rdev = NULL;
580 if (rdev)
581 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100582 if (rrdev && test_bit(Faulty, &rrdev->flags))
583 rrdev = NULL;
584 if (rrdev)
585 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700586 rcu_read_unlock();
587
NeilBrown73e92e52011-07-28 11:39:22 +1000588 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100589 * need to check for writes. We never accept write errors
590 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000591 */
592 while ((rw & WRITE) && rdev &&
593 test_bit(WriteErrorSeen, &rdev->flags)) {
594 sector_t first_bad;
595 int bad_sectors;
596 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
597 &first_bad, &bad_sectors);
598 if (!bad)
599 break;
600
601 if (bad < 0) {
602 set_bit(BlockedBadBlocks, &rdev->flags);
603 if (!conf->mddev->external &&
604 conf->mddev->flags) {
605 /* It is very unlikely, but we might
606 * still need to write out the
607 * bad block log - better give it
608 * a chance*/
609 md_check_recovery(conf->mddev);
610 }
majianpeng18507532012-07-03 12:11:54 +1000611 /*
612 * Because md_wait_for_blocked_rdev
613 * will dec nr_pending, we must
614 * increment it first.
615 */
616 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000617 md_wait_for_blocked_rdev(rdev, conf->mddev);
618 } else {
619 /* Acknowledged bad block - skip the write */
620 rdev_dec_pending(rdev, conf->mddev);
621 rdev = NULL;
622 }
623 }
624
Dan Williams91c00922007-01-02 13:52:30 -0700625 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100626 if (s->syncing || s->expanding || s->expanded
627 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700628 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
629
Dan Williams2b7497f2008-06-28 08:31:52 +1000630 set_bit(STRIPE_IO_STARTED, &sh->state);
631
Dan Williams91c00922007-01-02 13:52:30 -0700632 bi->bi_bdev = rdev->bdev;
633 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700634 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700635 bi->bi_rw, i);
636 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000637 if (use_new_offset(conf, sh))
638 bi->bi_sector = (sh->sector
639 + rdev->new_data_offset);
640 else
641 bi->bi_sector = (sh->sector
642 + rdev->data_offset);
Dan Williams91c00922007-01-02 13:52:30 -0700643 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700644 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700645 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
646 bi->bi_io_vec[0].bv_offset = 0;
647 bi->bi_size = STRIPE_SIZE;
648 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100649 if (rrdev)
650 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700651 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100652 }
653 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100654 if (s->syncing || s->expanding || s->expanded
655 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100656 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
657
658 set_bit(STRIPE_IO_STARTED, &sh->state);
659
660 rbi->bi_bdev = rrdev->bdev;
661 pr_debug("%s: for %llu schedule op %ld on "
662 "replacement disc %d\n",
663 __func__, (unsigned long long)sh->sector,
664 rbi->bi_rw, i);
665 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000666 if (use_new_offset(conf, sh))
667 rbi->bi_sector = (sh->sector
668 + rrdev->new_data_offset);
669 else
670 rbi->bi_sector = (sh->sector
671 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100672 rbi->bi_flags = 1 << BIO_UPTODATE;
673 rbi->bi_idx = 0;
674 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
675 rbi->bi_io_vec[0].bv_offset = 0;
676 rbi->bi_size = STRIPE_SIZE;
677 rbi->bi_next = NULL;
678 generic_make_request(rbi);
679 }
680 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000681 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700682 set_bit(STRIPE_DEGRADED, &sh->state);
683 pr_debug("skip op %ld on disc %d for sector %llu\n",
684 bi->bi_rw, i, (unsigned long long)sh->sector);
685 clear_bit(R5_LOCKED, &sh->dev[i].flags);
686 set_bit(STRIPE_HANDLE, &sh->state);
687 }
688 }
689}
690
691static struct dma_async_tx_descriptor *
692async_copy_data(int frombio, struct bio *bio, struct page *page,
693 sector_t sector, struct dma_async_tx_descriptor *tx)
694{
695 struct bio_vec *bvl;
696 struct page *bio_page;
697 int i;
698 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700699 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700700 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700701
702 if (bio->bi_sector >= sector)
703 page_offset = (signed)(bio->bi_sector - sector) * 512;
704 else
705 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700706
Dan Williams0403e382009-09-08 17:42:50 -0700707 if (frombio)
708 flags |= ASYNC_TX_FENCE;
709 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
710
Dan Williams91c00922007-01-02 13:52:30 -0700711 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000712 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700713 int clen;
714 int b_offset = 0;
715
716 if (page_offset < 0) {
717 b_offset = -page_offset;
718 page_offset += b_offset;
719 len -= b_offset;
720 }
721
722 if (len > 0 && page_offset + len > STRIPE_SIZE)
723 clen = STRIPE_SIZE - page_offset;
724 else
725 clen = len;
726
727 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000728 b_offset += bvl->bv_offset;
729 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700730 if (frombio)
731 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700732 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700733 else
734 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700735 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700736 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700737 /* chain the operations */
738 submit.depend_tx = tx;
739
Dan Williams91c00922007-01-02 13:52:30 -0700740 if (clen < len) /* hit end of page */
741 break;
742 page_offset += len;
743 }
744
745 return tx;
746}
747
748static void ops_complete_biofill(void *stripe_head_ref)
749{
750 struct stripe_head *sh = stripe_head_ref;
751 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100752 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700753 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700754
Harvey Harrisone46b2722008-04-28 02:15:50 -0700755 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700756 (unsigned long long)sh->sector);
757
758 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000759 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700760 for (i = sh->disks; i--; ) {
761 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700762
763 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700764 /* and check if we need to reply to a read request,
765 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000766 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700767 */
768 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700769 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700770
Dan Williams91c00922007-01-02 13:52:30 -0700771 BUG_ON(!dev->read);
772 rbi = dev->read;
773 dev->read = NULL;
774 while (rbi && rbi->bi_sector <
775 dev->sector + STRIPE_SECTORS) {
776 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200777 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700778 rbi->bi_next = return_bi;
779 return_bi = rbi;
780 }
Dan Williams91c00922007-01-02 13:52:30 -0700781 rbi = rbi2;
782 }
783 }
784 }
Dan Williams83de75c2008-06-28 08:31:58 +1000785 spin_unlock_irq(&conf->device_lock);
786 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700787
788 return_io(return_bi);
789
Dan Williamse4d84902007-09-24 10:06:13 -0700790 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700791 release_stripe(sh);
792}
793
794static void ops_run_biofill(struct stripe_head *sh)
795{
796 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100797 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700798 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700799 int i;
800
Harvey Harrisone46b2722008-04-28 02:15:50 -0700801 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700802 (unsigned long long)sh->sector);
803
804 for (i = sh->disks; i--; ) {
805 struct r5dev *dev = &sh->dev[i];
806 if (test_bit(R5_Wantfill, &dev->flags)) {
807 struct bio *rbi;
808 spin_lock_irq(&conf->device_lock);
809 dev->read = rbi = dev->toread;
810 dev->toread = NULL;
811 spin_unlock_irq(&conf->device_lock);
812 while (rbi && rbi->bi_sector <
813 dev->sector + STRIPE_SECTORS) {
814 tx = async_copy_data(0, rbi, dev->page,
815 dev->sector, tx);
816 rbi = r5_next_bio(rbi, dev->sector);
817 }
818 }
819 }
820
821 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700822 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
823 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700824}
825
Dan Williams4e7d2c02009-08-29 19:13:11 -0700826static void mark_target_uptodate(struct stripe_head *sh, int target)
827{
828 struct r5dev *tgt;
829
830 if (target < 0)
831 return;
832
833 tgt = &sh->dev[target];
834 set_bit(R5_UPTODATE, &tgt->flags);
835 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
836 clear_bit(R5_Wantcompute, &tgt->flags);
837}
838
Dan Williamsac6b53b2009-07-14 13:40:19 -0700839static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700840{
841 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700842
Harvey Harrisone46b2722008-04-28 02:15:50 -0700843 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700844 (unsigned long long)sh->sector);
845
Dan Williamsac6b53b2009-07-14 13:40:19 -0700846 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700847 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700848 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700849
Dan Williamsecc65c92008-06-28 08:31:57 +1000850 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
851 if (sh->check_state == check_state_compute_run)
852 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700853 set_bit(STRIPE_HANDLE, &sh->state);
854 release_stripe(sh);
855}
856
Dan Williamsd6f38f32009-07-14 11:50:52 -0700857/* return a pointer to the address conversion region of the scribble buffer */
858static addr_conv_t *to_addr_conv(struct stripe_head *sh,
859 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700860{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700861 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
862}
863
864static struct dma_async_tx_descriptor *
865ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
866{
Dan Williams91c00922007-01-02 13:52:30 -0700867 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700868 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700869 int target = sh->ops.target;
870 struct r5dev *tgt = &sh->dev[target];
871 struct page *xor_dest = tgt->page;
872 int count = 0;
873 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700874 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700875 int i;
876
877 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700878 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700879 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
880
881 for (i = disks; i--; )
882 if (i != target)
883 xor_srcs[count++] = sh->dev[i].page;
884
885 atomic_inc(&sh->count);
886
Dan Williams0403e382009-09-08 17:42:50 -0700887 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700888 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700889 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700890 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700891 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700892 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700893
Dan Williams91c00922007-01-02 13:52:30 -0700894 return tx;
895}
896
Dan Williamsac6b53b2009-07-14 13:40:19 -0700897/* set_syndrome_sources - populate source buffers for gen_syndrome
898 * @srcs - (struct page *) array of size sh->disks
899 * @sh - stripe_head to parse
900 *
901 * Populates srcs in proper layout order for the stripe and returns the
902 * 'count' of sources to be used in a call to async_gen_syndrome. The P
903 * destination buffer is recorded in srcs[count] and the Q destination
904 * is recorded in srcs[count+1]].
905 */
906static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
907{
908 int disks = sh->disks;
909 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
910 int d0_idx = raid6_d0(sh);
911 int count;
912 int i;
913
914 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100915 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700916
917 count = 0;
918 i = d0_idx;
919 do {
920 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
921
922 srcs[slot] = sh->dev[i].page;
923 i = raid6_next_disk(i, disks);
924 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700925
NeilBrowne4424fe2009-10-16 16:27:34 +1100926 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700927}
928
929static struct dma_async_tx_descriptor *
930ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
931{
932 int disks = sh->disks;
933 struct page **blocks = percpu->scribble;
934 int target;
935 int qd_idx = sh->qd_idx;
936 struct dma_async_tx_descriptor *tx;
937 struct async_submit_ctl submit;
938 struct r5dev *tgt;
939 struct page *dest;
940 int i;
941 int count;
942
943 if (sh->ops.target < 0)
944 target = sh->ops.target2;
945 else if (sh->ops.target2 < 0)
946 target = sh->ops.target;
947 else
948 /* we should only have one valid target */
949 BUG();
950 BUG_ON(target < 0);
951 pr_debug("%s: stripe %llu block: %d\n",
952 __func__, (unsigned long long)sh->sector, target);
953
954 tgt = &sh->dev[target];
955 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
956 dest = tgt->page;
957
958 atomic_inc(&sh->count);
959
960 if (target == qd_idx) {
961 count = set_syndrome_sources(blocks, sh);
962 blocks[count] = NULL; /* regenerating p is not necessary */
963 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700964 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
965 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700966 to_addr_conv(sh, percpu));
967 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
968 } else {
969 /* Compute any data- or p-drive using XOR */
970 count = 0;
971 for (i = disks; i-- ; ) {
972 if (i == target || i == qd_idx)
973 continue;
974 blocks[count++] = sh->dev[i].page;
975 }
976
Dan Williams0403e382009-09-08 17:42:50 -0700977 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
978 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 to_addr_conv(sh, percpu));
980 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
981 }
982
983 return tx;
984}
985
986static struct dma_async_tx_descriptor *
987ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
988{
989 int i, count, disks = sh->disks;
990 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
991 int d0_idx = raid6_d0(sh);
992 int faila = -1, failb = -1;
993 int target = sh->ops.target;
994 int target2 = sh->ops.target2;
995 struct r5dev *tgt = &sh->dev[target];
996 struct r5dev *tgt2 = &sh->dev[target2];
997 struct dma_async_tx_descriptor *tx;
998 struct page **blocks = percpu->scribble;
999 struct async_submit_ctl submit;
1000
1001 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1002 __func__, (unsigned long long)sh->sector, target, target2);
1003 BUG_ON(target < 0 || target2 < 0);
1004 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1005 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1006
Dan Williams6c910a72009-09-16 12:24:54 -07001007 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001008 * slot number conversion for 'faila' and 'failb'
1009 */
1010 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001011 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001012 count = 0;
1013 i = d0_idx;
1014 do {
1015 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1016
1017 blocks[slot] = sh->dev[i].page;
1018
1019 if (i == target)
1020 faila = slot;
1021 if (i == target2)
1022 failb = slot;
1023 i = raid6_next_disk(i, disks);
1024 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001025
1026 BUG_ON(faila == failb);
1027 if (failb < faila)
1028 swap(faila, failb);
1029 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1030 __func__, (unsigned long long)sh->sector, faila, failb);
1031
1032 atomic_inc(&sh->count);
1033
1034 if (failb == syndrome_disks+1) {
1035 /* Q disk is one of the missing disks */
1036 if (faila == syndrome_disks) {
1037 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001038 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1039 ops_complete_compute, sh,
1040 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001041 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042 STRIPE_SIZE, &submit);
1043 } else {
1044 struct page *dest;
1045 int data_target;
1046 int qd_idx = sh->qd_idx;
1047
1048 /* Missing D+Q: recompute D from P, then recompute Q */
1049 if (target == qd_idx)
1050 data_target = target2;
1051 else
1052 data_target = target;
1053
1054 count = 0;
1055 for (i = disks; i-- ; ) {
1056 if (i == data_target || i == qd_idx)
1057 continue;
1058 blocks[count++] = sh->dev[i].page;
1059 }
1060 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001061 init_async_submit(&submit,
1062 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1063 NULL, NULL, NULL,
1064 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001065 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1066 &submit);
1067
1068 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001069 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1070 ops_complete_compute, sh,
1071 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001072 return async_gen_syndrome(blocks, 0, count+2,
1073 STRIPE_SIZE, &submit);
1074 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001075 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001076 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1077 ops_complete_compute, sh,
1078 to_addr_conv(sh, percpu));
1079 if (failb == syndrome_disks) {
1080 /* We're missing D+P. */
1081 return async_raid6_datap_recov(syndrome_disks+2,
1082 STRIPE_SIZE, faila,
1083 blocks, &submit);
1084 } else {
1085 /* We're missing D+D. */
1086 return async_raid6_2data_recov(syndrome_disks+2,
1087 STRIPE_SIZE, faila, failb,
1088 blocks, &submit);
1089 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 }
1091}
1092
1093
Dan Williams91c00922007-01-02 13:52:30 -07001094static void ops_complete_prexor(void *stripe_head_ref)
1095{
1096 struct stripe_head *sh = stripe_head_ref;
1097
Harvey Harrisone46b2722008-04-28 02:15:50 -07001098 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001099 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001100}
1101
1102static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001103ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1104 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001105{
Dan Williams91c00922007-01-02 13:52:30 -07001106 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001107 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001108 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001109 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001110
1111 /* existing parity data subtracted */
1112 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1113
Harvey Harrisone46b2722008-04-28 02:15:50 -07001114 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001115 (unsigned long long)sh->sector);
1116
1117 for (i = disks; i--; ) {
1118 struct r5dev *dev = &sh->dev[i];
1119 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001120 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001121 xor_srcs[count++] = dev->page;
1122 }
1123
Dan Williams0403e382009-09-08 17:42:50 -07001124 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001125 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001126 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001127
1128 return tx;
1129}
1130
1131static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001132ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001133{
1134 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001135 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001136
Harvey Harrisone46b2722008-04-28 02:15:50 -07001137 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001138 (unsigned long long)sh->sector);
1139
1140 for (i = disks; i--; ) {
1141 struct r5dev *dev = &sh->dev[i];
1142 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001143
Dan Williamsd8ee0722008-06-28 08:32:06 +10001144 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001145 struct bio *wbi;
1146
NeilBrowncbe47ec2011-07-26 11:20:35 +10001147 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001148 chosen = dev->towrite;
1149 dev->towrite = NULL;
1150 BUG_ON(dev->written);
1151 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001152 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001153
1154 while (wbi && wbi->bi_sector <
1155 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001156 if (wbi->bi_rw & REQ_FUA)
1157 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001158 if (wbi->bi_rw & REQ_SYNC)
1159 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001160 tx = async_copy_data(1, wbi, dev->page,
1161 dev->sector, tx);
1162 wbi = r5_next_bio(wbi, dev->sector);
1163 }
1164 }
1165 }
1166
1167 return tx;
1168}
1169
Dan Williamsac6b53b2009-07-14 13:40:19 -07001170static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001171{
1172 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001173 int disks = sh->disks;
1174 int pd_idx = sh->pd_idx;
1175 int qd_idx = sh->qd_idx;
1176 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001177 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001178
Harvey Harrisone46b2722008-04-28 02:15:50 -07001179 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001180 (unsigned long long)sh->sector);
1181
Shaohua Libc0934f2012-05-22 13:55:05 +10001182 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001183 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001184 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1185 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001186
Dan Williams91c00922007-01-02 13:52:30 -07001187 for (i = disks; i--; ) {
1188 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001189
Tejun Heoe9c74692010-09-03 11:56:18 +02001190 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001191 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001192 if (fua)
1193 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001194 if (sync)
1195 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001196 }
Dan Williams91c00922007-01-02 13:52:30 -07001197 }
1198
Dan Williamsd8ee0722008-06-28 08:32:06 +10001199 if (sh->reconstruct_state == reconstruct_state_drain_run)
1200 sh->reconstruct_state = reconstruct_state_drain_result;
1201 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1202 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1203 else {
1204 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1205 sh->reconstruct_state = reconstruct_state_result;
1206 }
Dan Williams91c00922007-01-02 13:52:30 -07001207
1208 set_bit(STRIPE_HANDLE, &sh->state);
1209 release_stripe(sh);
1210}
1211
1212static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001213ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1214 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001215{
Dan Williams91c00922007-01-02 13:52:30 -07001216 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001217 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001218 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001219 int count = 0, pd_idx = sh->pd_idx, i;
1220 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001221 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001222 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001223
Harvey Harrisone46b2722008-04-28 02:15:50 -07001224 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001225 (unsigned long long)sh->sector);
1226
1227 /* check if prexor is active which means only process blocks
1228 * that are part of a read-modify-write (written)
1229 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001230 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1231 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001232 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1233 for (i = disks; i--; ) {
1234 struct r5dev *dev = &sh->dev[i];
1235 if (dev->written)
1236 xor_srcs[count++] = dev->page;
1237 }
1238 } else {
1239 xor_dest = sh->dev[pd_idx].page;
1240 for (i = disks; i--; ) {
1241 struct r5dev *dev = &sh->dev[i];
1242 if (i != pd_idx)
1243 xor_srcs[count++] = dev->page;
1244 }
1245 }
1246
Dan Williams91c00922007-01-02 13:52:30 -07001247 /* 1/ if we prexor'd then the dest is reused as a source
1248 * 2/ if we did not prexor then we are redoing the parity
1249 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1250 * for the synchronous xor case
1251 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001252 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001253 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1254
1255 atomic_inc(&sh->count);
1256
Dan Williamsac6b53b2009-07-14 13:40:19 -07001257 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001258 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001259 if (unlikely(count == 1))
1260 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1261 else
1262 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001263}
1264
Dan Williamsac6b53b2009-07-14 13:40:19 -07001265static void
1266ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1267 struct dma_async_tx_descriptor *tx)
1268{
1269 struct async_submit_ctl submit;
1270 struct page **blocks = percpu->scribble;
1271 int count;
1272
1273 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1274
1275 count = set_syndrome_sources(blocks, sh);
1276
1277 atomic_inc(&sh->count);
1278
1279 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1280 sh, to_addr_conv(sh, percpu));
1281 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001282}
1283
1284static void ops_complete_check(void *stripe_head_ref)
1285{
1286 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001287
Harvey Harrisone46b2722008-04-28 02:15:50 -07001288 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001289 (unsigned long long)sh->sector);
1290
Dan Williamsecc65c92008-06-28 08:31:57 +10001291 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001292 set_bit(STRIPE_HANDLE, &sh->state);
1293 release_stripe(sh);
1294}
1295
Dan Williamsac6b53b2009-07-14 13:40:19 -07001296static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001297{
Dan Williams91c00922007-01-02 13:52:30 -07001298 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001299 int pd_idx = sh->pd_idx;
1300 int qd_idx = sh->qd_idx;
1301 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001302 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001303 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001304 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001305 int count;
1306 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001307
Harvey Harrisone46b2722008-04-28 02:15:50 -07001308 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001309 (unsigned long long)sh->sector);
1310
Dan Williamsac6b53b2009-07-14 13:40:19 -07001311 count = 0;
1312 xor_dest = sh->dev[pd_idx].page;
1313 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001314 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001315 if (i == pd_idx || i == qd_idx)
1316 continue;
1317 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001318 }
1319
Dan Williamsd6f38f32009-07-14 11:50:52 -07001320 init_async_submit(&submit, 0, NULL, NULL, NULL,
1321 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001322 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001323 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001324
Dan Williams91c00922007-01-02 13:52:30 -07001325 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001326 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1327 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001328}
1329
Dan Williamsac6b53b2009-07-14 13:40:19 -07001330static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1331{
1332 struct page **srcs = percpu->scribble;
1333 struct async_submit_ctl submit;
1334 int count;
1335
1336 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1337 (unsigned long long)sh->sector, checkp);
1338
1339 count = set_syndrome_sources(srcs, sh);
1340 if (!checkp)
1341 srcs[count] = NULL;
1342
1343 atomic_inc(&sh->count);
1344 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1345 sh, to_addr_conv(sh, percpu));
1346 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1347 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1348}
1349
Dan Williams417b8d42009-10-16 16:25:22 +11001350static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001351{
1352 int overlap_clear = 0, i, disks = sh->disks;
1353 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001354 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001355 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001356 struct raid5_percpu *percpu;
1357 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001358
Dan Williamsd6f38f32009-07-14 11:50:52 -07001359 cpu = get_cpu();
1360 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001361 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001362 ops_run_biofill(sh);
1363 overlap_clear++;
1364 }
1365
Dan Williams7b3a8712008-06-28 08:32:09 +10001366 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001367 if (level < 6)
1368 tx = ops_run_compute5(sh, percpu);
1369 else {
1370 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1371 tx = ops_run_compute6_1(sh, percpu);
1372 else
1373 tx = ops_run_compute6_2(sh, percpu);
1374 }
1375 /* terminate the chain if reconstruct is not set to be run */
1376 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001377 async_tx_ack(tx);
1378 }
Dan Williams91c00922007-01-02 13:52:30 -07001379
Dan Williams600aa102008-06-28 08:32:05 +10001380 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001381 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001382
Dan Williams600aa102008-06-28 08:32:05 +10001383 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001384 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001385 overlap_clear++;
1386 }
1387
Dan Williamsac6b53b2009-07-14 13:40:19 -07001388 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1389 if (level < 6)
1390 ops_run_reconstruct5(sh, percpu, tx);
1391 else
1392 ops_run_reconstruct6(sh, percpu, tx);
1393 }
Dan Williams91c00922007-01-02 13:52:30 -07001394
Dan Williamsac6b53b2009-07-14 13:40:19 -07001395 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1396 if (sh->check_state == check_state_run)
1397 ops_run_check_p(sh, percpu);
1398 else if (sh->check_state == check_state_run_q)
1399 ops_run_check_pq(sh, percpu, 0);
1400 else if (sh->check_state == check_state_run_pq)
1401 ops_run_check_pq(sh, percpu, 1);
1402 else
1403 BUG();
1404 }
Dan Williams91c00922007-01-02 13:52:30 -07001405
Dan Williams91c00922007-01-02 13:52:30 -07001406 if (overlap_clear)
1407 for (i = disks; i--; ) {
1408 struct r5dev *dev = &sh->dev[i];
1409 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1410 wake_up(&sh->raid_conf->wait_for_overlap);
1411 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001412 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001413}
1414
Dan Williams417b8d42009-10-16 16:25:22 +11001415#ifdef CONFIG_MULTICORE_RAID456
1416static void async_run_ops(void *param, async_cookie_t cookie)
1417{
1418 struct stripe_head *sh = param;
1419 unsigned long ops_request = sh->ops.request;
1420
1421 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1422 wake_up(&sh->ops.wait_for_ops);
1423
1424 __raid_run_ops(sh, ops_request);
1425 release_stripe(sh);
1426}
1427
1428static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1429{
1430 /* since handle_stripe can be called outside of raid5d context
1431 * we need to ensure sh->ops.request is de-staged before another
1432 * request arrives
1433 */
1434 wait_event(sh->ops.wait_for_ops,
1435 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1436 sh->ops.request = ops_request;
1437
1438 atomic_inc(&sh->count);
1439 async_schedule(async_run_ops, sh);
1440}
1441#else
1442#define raid_run_ops __raid_run_ops
1443#endif
1444
NeilBrownd1688a62011-10-11 16:49:52 +11001445static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001448 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001449 if (!sh)
1450 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001451
NeilBrown3f294f42005-11-08 21:39:25 -08001452 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001453 #ifdef CONFIG_MULTICORE_RAID456
1454 init_waitqueue_head(&sh->ops.wait_for_ops);
1455 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001456
NeilBrowne4e11e32010-06-16 16:45:16 +10001457 if (grow_buffers(sh)) {
1458 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001459 kmem_cache_free(conf->slab_cache, sh);
1460 return 0;
1461 }
1462 /* we just created an active stripe so... */
1463 atomic_set(&sh->count, 1);
1464 atomic_inc(&conf->active_stripes);
1465 INIT_LIST_HEAD(&sh->lru);
1466 release_stripe(sh);
1467 return 1;
1468}
1469
NeilBrownd1688a62011-10-11 16:49:52 +11001470static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001471{
Christoph Lametere18b8902006-12-06 20:33:20 -08001472 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001473 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
NeilBrownf4be6b42010-06-01 19:37:25 +10001475 if (conf->mddev->gendisk)
1476 sprintf(conf->cache_name[0],
1477 "raid%d-%s", conf->level, mdname(conf->mddev));
1478 else
1479 sprintf(conf->cache_name[0],
1480 "raid%d-%p", conf->level, conf->mddev);
1481 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1482
NeilBrownad01c9e2006-03-27 01:18:07 -08001483 conf->active_name = 0;
1484 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001486 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (!sc)
1488 return 1;
1489 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001490 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001491 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001492 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return 0;
1495}
NeilBrown29269552006-03-27 01:18:10 -08001496
Dan Williamsd6f38f32009-07-14 11:50:52 -07001497/**
1498 * scribble_len - return the required size of the scribble region
1499 * @num - total number of disks in the array
1500 *
1501 * The size must be enough to contain:
1502 * 1/ a struct page pointer for each device in the array +2
1503 * 2/ room to convert each entry in (1) to its corresponding dma
1504 * (dma_map_page()) or page (page_address()) address.
1505 *
1506 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1507 * calculate over all devices (not just the data blocks), using zeros in place
1508 * of the P and Q blocks.
1509 */
1510static size_t scribble_len(int num)
1511{
1512 size_t len;
1513
1514 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1515
1516 return len;
1517}
1518
NeilBrownd1688a62011-10-11 16:49:52 +11001519static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001520{
1521 /* Make all the stripes able to hold 'newsize' devices.
1522 * New slots in each stripe get 'page' set to a new page.
1523 *
1524 * This happens in stages:
1525 * 1/ create a new kmem_cache and allocate the required number of
1526 * stripe_heads.
1527 * 2/ gather all the old stripe_heads and tranfer the pages across
1528 * to the new stripe_heads. This will have the side effect of
1529 * freezing the array as once all stripe_heads have been collected,
1530 * no IO will be possible. Old stripe heads are freed once their
1531 * pages have been transferred over, and the old kmem_cache is
1532 * freed when all stripes are done.
1533 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1534 * we simple return a failre status - no need to clean anything up.
1535 * 4/ allocate new pages for the new slots in the new stripe_heads.
1536 * If this fails, we don't bother trying the shrink the
1537 * stripe_heads down again, we just leave them as they are.
1538 * As each stripe_head is processed the new one is released into
1539 * active service.
1540 *
1541 * Once step2 is started, we cannot afford to wait for a write,
1542 * so we use GFP_NOIO allocations.
1543 */
1544 struct stripe_head *osh, *nsh;
1545 LIST_HEAD(newstripes);
1546 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001547 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001548 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001549 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001550 int i;
1551
1552 if (newsize <= conf->pool_size)
1553 return 0; /* never bother to shrink */
1554
Dan Williamsb5470dc2008-06-27 21:44:04 -07001555 err = md_allow_write(conf->mddev);
1556 if (err)
1557 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001558
NeilBrownad01c9e2006-03-27 01:18:07 -08001559 /* Step 1 */
1560 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1561 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001562 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001563 if (!sc)
1564 return -ENOMEM;
1565
1566 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001567 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001568 if (!nsh)
1569 break;
1570
NeilBrownad01c9e2006-03-27 01:18:07 -08001571 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001572 #ifdef CONFIG_MULTICORE_RAID456
1573 init_waitqueue_head(&nsh->ops.wait_for_ops);
1574 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001575
1576 list_add(&nsh->lru, &newstripes);
1577 }
1578 if (i) {
1579 /* didn't get enough, give up */
1580 while (!list_empty(&newstripes)) {
1581 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1582 list_del(&nsh->lru);
1583 kmem_cache_free(sc, nsh);
1584 }
1585 kmem_cache_destroy(sc);
1586 return -ENOMEM;
1587 }
1588 /* Step 2 - Must use GFP_NOIO now.
1589 * OK, we have enough stripes, start collecting inactive
1590 * stripes and copying them over
1591 */
1592 list_for_each_entry(nsh, &newstripes, lru) {
1593 spin_lock_irq(&conf->device_lock);
1594 wait_event_lock_irq(conf->wait_for_stripe,
1595 !list_empty(&conf->inactive_list),
1596 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001597 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001598 osh = get_free_stripe(conf);
1599 spin_unlock_irq(&conf->device_lock);
1600 atomic_set(&nsh->count, 1);
1601 for(i=0; i<conf->pool_size; i++)
1602 nsh->dev[i].page = osh->dev[i].page;
1603 for( ; i<newsize; i++)
1604 nsh->dev[i].page = NULL;
1605 kmem_cache_free(conf->slab_cache, osh);
1606 }
1607 kmem_cache_destroy(conf->slab_cache);
1608
1609 /* Step 3.
1610 * At this point, we are holding all the stripes so the array
1611 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001612 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001613 */
1614 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1615 if (ndisks) {
1616 for (i=0; i<conf->raid_disks; i++)
1617 ndisks[i] = conf->disks[i];
1618 kfree(conf->disks);
1619 conf->disks = ndisks;
1620 } else
1621 err = -ENOMEM;
1622
Dan Williamsd6f38f32009-07-14 11:50:52 -07001623 get_online_cpus();
1624 conf->scribble_len = scribble_len(newsize);
1625 for_each_present_cpu(cpu) {
1626 struct raid5_percpu *percpu;
1627 void *scribble;
1628
1629 percpu = per_cpu_ptr(conf->percpu, cpu);
1630 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1631
1632 if (scribble) {
1633 kfree(percpu->scribble);
1634 percpu->scribble = scribble;
1635 } else {
1636 err = -ENOMEM;
1637 break;
1638 }
1639 }
1640 put_online_cpus();
1641
NeilBrownad01c9e2006-03-27 01:18:07 -08001642 /* Step 4, return new stripes to service */
1643 while(!list_empty(&newstripes)) {
1644 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1645 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001646
NeilBrownad01c9e2006-03-27 01:18:07 -08001647 for (i=conf->raid_disks; i < newsize; i++)
1648 if (nsh->dev[i].page == NULL) {
1649 struct page *p = alloc_page(GFP_NOIO);
1650 nsh->dev[i].page = p;
1651 if (!p)
1652 err = -ENOMEM;
1653 }
1654 release_stripe(nsh);
1655 }
1656 /* critical section pass, GFP_NOIO no longer needed */
1657
1658 conf->slab_cache = sc;
1659 conf->active_name = 1-conf->active_name;
1660 conf->pool_size = newsize;
1661 return err;
1662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
NeilBrownd1688a62011-10-11 16:49:52 +11001664static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct stripe_head *sh;
1667
NeilBrown3f294f42005-11-08 21:39:25 -08001668 spin_lock_irq(&conf->device_lock);
1669 sh = get_free_stripe(conf);
1670 spin_unlock_irq(&conf->device_lock);
1671 if (!sh)
1672 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001673 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001674 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001675 kmem_cache_free(conf->slab_cache, sh);
1676 atomic_dec(&conf->active_stripes);
1677 return 1;
1678}
1679
NeilBrownd1688a62011-10-11 16:49:52 +11001680static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001681{
1682 while (drop_one_stripe(conf))
1683 ;
1684
NeilBrown29fc7e32006-02-03 03:03:41 -08001685 if (conf->slab_cache)
1686 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 conf->slab_cache = NULL;
1688}
1689
NeilBrown6712ecf2007-09-27 12:47:43 +02001690static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
NeilBrown99c0fb52009-03-31 14:39:38 +11001692 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001693 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001694 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001696 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001697 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001698 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 for (i=0 ; i<disks; i++)
1701 if (bi == &sh->dev[i].req)
1702 break;
1703
Dan Williams45b42332007-07-09 11:56:43 -07001704 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1705 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 uptodate);
1707 if (i == disks) {
1708 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
NeilBrown14a75d32011-12-23 10:17:52 +11001711 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001712 /* If replacement finished while this request was outstanding,
1713 * 'replacement' might be NULL already.
1714 * In that case it moved down to 'rdev'.
1715 * rdev is not removed until all requests are finished.
1716 */
NeilBrown14a75d32011-12-23 10:17:52 +11001717 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001718 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001719 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
NeilBrown05616be2012-05-21 09:27:00 +10001721 if (use_new_offset(conf, sh))
1722 s = sh->sector + rdev->new_data_offset;
1723 else
1724 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001727 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001728 /* Note that this cannot happen on a
1729 * replacement device. We just fail those on
1730 * any error
1731 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001732 printk_ratelimited(
1733 KERN_INFO
1734 "md/raid:%s: read error corrected"
1735 " (%lu sectors at %llu on %s)\n",
1736 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001737 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001738 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001739 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001740 clear_bit(R5_ReadError, &sh->dev[i].flags);
1741 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1742 }
NeilBrown14a75d32011-12-23 10:17:52 +11001743 if (atomic_read(&rdev->read_errors))
1744 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001746 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001747 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001748 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001751 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001752 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1753 printk_ratelimited(
1754 KERN_WARNING
1755 "md/raid:%s: read error on replacement device "
1756 "(sector %llu on %s).\n",
1757 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001758 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001759 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001760 else if (conf->mddev->degraded >= conf->max_degraded) {
1761 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001762 printk_ratelimited(
1763 KERN_WARNING
1764 "md/raid:%s: read error not correctable "
1765 "(sector %llu on %s).\n",
1766 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001767 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001768 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001769 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001770 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001771 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001772 printk_ratelimited(
1773 KERN_WARNING
1774 "md/raid:%s: read error NOT corrected!! "
1775 "(sector %llu on %s).\n",
1776 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001777 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001778 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001779 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001780 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001781 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001782 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001783 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001784 else
1785 retry = 1;
1786 if (retry)
1787 set_bit(R5_ReadError, &sh->dev[i].flags);
1788 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001789 clear_bit(R5_ReadError, &sh->dev[i].flags);
1790 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001791 if (!(set_bad
1792 && test_bit(In_sync, &rdev->flags)
1793 && rdev_set_badblocks(
1794 rdev, sh->sector, STRIPE_SECTORS, 0)))
1795 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
NeilBrown14a75d32011-12-23 10:17:52 +11001798 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1800 set_bit(STRIPE_HANDLE, &sh->state);
1801 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
NeilBrownd710e132008-10-13 11:55:12 +11001804static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
NeilBrown99c0fb52009-03-31 14:39:38 +11001806 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001807 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001808 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001809 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001811 sector_t first_bad;
1812 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001813 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
NeilBrown977df362011-12-23 10:17:53 +11001815 for (i = 0 ; i < disks; i++) {
1816 if (bi == &sh->dev[i].req) {
1817 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
NeilBrown977df362011-12-23 10:17:53 +11001819 }
1820 if (bi == &sh->dev[i].rreq) {
1821 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001822 if (rdev)
1823 replacement = 1;
1824 else
1825 /* rdev was removed and 'replacement'
1826 * replaced it. rdev is not removed
1827 * until all requests are finished.
1828 */
1829 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001830 break;
1831 }
1832 }
Dan Williams45b42332007-07-09 11:56:43 -07001833 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1835 uptodate);
1836 if (i == disks) {
1837 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001838 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840
NeilBrown977df362011-12-23 10:17:53 +11001841 if (replacement) {
1842 if (!uptodate)
1843 md_error(conf->mddev, rdev);
1844 else if (is_badblock(rdev, sh->sector,
1845 STRIPE_SECTORS,
1846 &first_bad, &bad_sectors))
1847 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1848 } else {
1849 if (!uptodate) {
1850 set_bit(WriteErrorSeen, &rdev->flags);
1851 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001852 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1853 set_bit(MD_RECOVERY_NEEDED,
1854 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001855 } else if (is_badblock(rdev, sh->sector,
1856 STRIPE_SECTORS,
1857 &first_bad, &bad_sectors))
1858 set_bit(R5_MadeGood, &sh->dev[i].flags);
1859 }
1860 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
NeilBrown977df362011-12-23 10:17:53 +11001862 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1863 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001865 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
NeilBrown784052e2009-03-31 15:19:07 +11001868static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
NeilBrown784052e2009-03-31 15:19:07 +11001870static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct r5dev *dev = &sh->dev[i];
1873
1874 bio_init(&dev->req);
1875 dev->req.bi_io_vec = &dev->vec;
1876 dev->req.bi_vcnt++;
1877 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001879 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
NeilBrown977df362011-12-23 10:17:53 +11001881 bio_init(&dev->rreq);
1882 dev->rreq.bi_io_vec = &dev->rvec;
1883 dev->rreq.bi_vcnt++;
1884 dev->rreq.bi_max_vecs++;
1885 dev->rreq.bi_private = sh;
1886 dev->rvec.bv_page = dev->page;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001889 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
NeilBrownfd01b882011-10-11 16:47:53 +11001892static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001895 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001896 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001897 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
NeilBrown908f4fb2011-12-23 10:17:50 +11001899 spin_lock_irqsave(&conf->device_lock, flags);
1900 clear_bit(In_sync, &rdev->flags);
1901 mddev->degraded = calc_degraded(conf);
1902 spin_unlock_irqrestore(&conf->device_lock, flags);
1903 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1904
NeilBrownde393cd2011-07-28 11:31:48 +10001905 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001906 set_bit(Faulty, &rdev->flags);
1907 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1908 printk(KERN_ALERT
1909 "md/raid:%s: Disk failure on %s, disabling device.\n"
1910 "md/raid:%s: Operation continuing on %d devices.\n",
1911 mdname(mddev),
1912 bdevname(rdev->bdev, b),
1913 mdname(mddev),
1914 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917/*
1918 * Input: a 'big' sector number,
1919 * Output: index of the data and parity disk, and the sector # in them.
1920 */
NeilBrownd1688a62011-10-11 16:49:52 +11001921static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001922 int previous, int *dd_idx,
1923 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001925 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001926 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001928 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001929 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001931 int algorithm = previous ? conf->prev_algo
1932 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001933 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1934 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001935 int raid_disks = previous ? conf->previous_raid_disks
1936 : conf->raid_disks;
1937 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* First compute the information on this sector */
1940
1941 /*
1942 * Compute the chunk number and the sector offset inside the chunk
1943 */
1944 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1945 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /*
1948 * Compute the stripe number
1949 */
NeilBrown35f2a592010-04-20 14:13:34 +10001950 stripe = chunk_number;
1951 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001952 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /*
1954 * Select the parity disk based on the user selected algorithm.
1955 */
NeilBrown84789552011-07-27 11:00:36 +10001956 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001957 switch(conf->level) {
1958 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001959 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001960 break;
1961 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001962 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001964 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001965 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 (*dd_idx)++;
1967 break;
1968 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001969 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001970 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 (*dd_idx)++;
1972 break;
1973 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001974 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001975 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001978 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001979 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001981 case ALGORITHM_PARITY_0:
1982 pd_idx = 0;
1983 (*dd_idx)++;
1984 break;
1985 case ALGORITHM_PARITY_N:
1986 pd_idx = data_disks;
1987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001989 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001990 }
1991 break;
1992 case 6:
1993
NeilBrowne183eae2009-03-31 15:20:22 +11001994 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001995 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001996 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001997 qd_idx = pd_idx + 1;
1998 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001999 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002000 qd_idx = 0;
2001 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002002 (*dd_idx) += 2; /* D D P Q D */
2003 break;
2004 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002006 qd_idx = pd_idx + 1;
2007 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002008 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002009 qd_idx = 0;
2010 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002011 (*dd_idx) += 2; /* D D P Q D */
2012 break;
2013 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002014 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002015 qd_idx = (pd_idx + 1) % raid_disks;
2016 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 break;
2018 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002019 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002020 qd_idx = (pd_idx + 1) % raid_disks;
2021 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002022 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002023
2024 case ALGORITHM_PARITY_0:
2025 pd_idx = 0;
2026 qd_idx = 1;
2027 (*dd_idx) += 2;
2028 break;
2029 case ALGORITHM_PARITY_N:
2030 pd_idx = data_disks;
2031 qd_idx = data_disks + 1;
2032 break;
2033
2034 case ALGORITHM_ROTATING_ZERO_RESTART:
2035 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2036 * of blocks for computing Q is different.
2037 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002038 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002039 qd_idx = pd_idx + 1;
2040 if (pd_idx == raid_disks-1) {
2041 (*dd_idx)++; /* Q D D D P */
2042 qd_idx = 0;
2043 } else if (*dd_idx >= pd_idx)
2044 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002045 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 break;
2047
2048 case ALGORITHM_ROTATING_N_RESTART:
2049 /* Same a left_asymmetric, by first stripe is
2050 * D D D P Q rather than
2051 * Q D D D P
2052 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002053 stripe2 += 1;
2054 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002055 qd_idx = pd_idx + 1;
2056 if (pd_idx == raid_disks-1) {
2057 (*dd_idx)++; /* Q D D D P */
2058 qd_idx = 0;
2059 } else if (*dd_idx >= pd_idx)
2060 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002061 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002062 break;
2063
2064 case ALGORITHM_ROTATING_N_CONTINUE:
2065 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002066 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002067 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2068 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002069 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002070 break;
2071
2072 case ALGORITHM_LEFT_ASYMMETRIC_6:
2073 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002074 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002075 if (*dd_idx >= pd_idx)
2076 (*dd_idx)++;
2077 qd_idx = raid_disks - 1;
2078 break;
2079
2080 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002081 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002082 if (*dd_idx >= pd_idx)
2083 (*dd_idx)++;
2084 qd_idx = raid_disks - 1;
2085 break;
2086
2087 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002088 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002089 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2090 qd_idx = raid_disks - 1;
2091 break;
2092
2093 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002094 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002095 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2096 qd_idx = raid_disks - 1;
2097 break;
2098
2099 case ALGORITHM_PARITY_0_6:
2100 pd_idx = 0;
2101 (*dd_idx)++;
2102 qd_idx = raid_disks - 1;
2103 break;
2104
NeilBrown16a53ec2006-06-26 00:27:38 -07002105 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002106 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002107 }
2108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110
NeilBrown911d4ee2009-03-31 14:39:38 +11002111 if (sh) {
2112 sh->pd_idx = pd_idx;
2113 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002114 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /*
2117 * Finally, compute the new sector number
2118 */
2119 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2120 return new_sector;
2121}
2122
2123
NeilBrown784052e2009-03-31 15:19:07 +11002124static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
NeilBrownd1688a62011-10-11 16:49:52 +11002126 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002127 int raid_disks = sh->disks;
2128 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002130 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2131 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002132 int algorithm = previous ? conf->prev_algo
2133 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 sector_t stripe;
2135 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002136 sector_t chunk_number;
2137 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002139 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
NeilBrown16a53ec2006-06-26 00:27:38 -07002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2143 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
NeilBrown16a53ec2006-06-26 00:27:38 -07002145 if (i == sh->pd_idx)
2146 return 0;
2147 switch(conf->level) {
2148 case 4: break;
2149 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002150 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 case ALGORITHM_LEFT_ASYMMETRIC:
2152 case ALGORITHM_RIGHT_ASYMMETRIC:
2153 if (i > sh->pd_idx)
2154 i--;
2155 break;
2156 case ALGORITHM_LEFT_SYMMETRIC:
2157 case ALGORITHM_RIGHT_SYMMETRIC:
2158 if (i < sh->pd_idx)
2159 i += raid_disks;
2160 i -= (sh->pd_idx + 1);
2161 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002162 case ALGORITHM_PARITY_0:
2163 i -= 1;
2164 break;
2165 case ALGORITHM_PARITY_N:
2166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002168 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002169 }
2170 break;
2171 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002172 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002174 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002175 case ALGORITHM_LEFT_ASYMMETRIC:
2176 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002177 case ALGORITHM_ROTATING_ZERO_RESTART:
2178 case ALGORITHM_ROTATING_N_RESTART:
2179 if (sh->pd_idx == raid_disks-1)
2180 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002181 else if (i > sh->pd_idx)
2182 i -= 2; /* D D P Q D */
2183 break;
2184 case ALGORITHM_LEFT_SYMMETRIC:
2185 case ALGORITHM_RIGHT_SYMMETRIC:
2186 if (sh->pd_idx == raid_disks-1)
2187 i--; /* Q D D D P */
2188 else {
2189 /* D D P Q D */
2190 if (i < sh->pd_idx)
2191 i += raid_disks;
2192 i -= (sh->pd_idx + 2);
2193 }
2194 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002195 case ALGORITHM_PARITY_0:
2196 i -= 2;
2197 break;
2198 case ALGORITHM_PARITY_N:
2199 break;
2200 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002201 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002202 if (sh->pd_idx == 0)
2203 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002204 else {
2205 /* D D Q P D */
2206 if (i < sh->pd_idx)
2207 i += raid_disks;
2208 i -= (sh->pd_idx + 1);
2209 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002210 break;
2211 case ALGORITHM_LEFT_ASYMMETRIC_6:
2212 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2213 if (i > sh->pd_idx)
2214 i--;
2215 break;
2216 case ALGORITHM_LEFT_SYMMETRIC_6:
2217 case ALGORITHM_RIGHT_SYMMETRIC_6:
2218 if (i < sh->pd_idx)
2219 i += data_disks + 1;
2220 i -= (sh->pd_idx + 1);
2221 break;
2222 case ALGORITHM_PARITY_0_6:
2223 i -= 1;
2224 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002225 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002226 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002227 }
2228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230
2231 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002232 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
NeilBrown112bf892009-03-31 14:39:38 +11002234 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002235 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002236 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2237 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002238 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2239 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
2241 }
2242 return r_sector;
2243}
2244
2245
Dan Williams600aa102008-06-28 08:32:05 +10002246static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002247schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002248 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002249{
2250 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002251 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002252 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002253
Dan Williamse33129d2007-01-02 13:52:30 -07002254 if (rcw) {
2255 /* if we are not expanding this is a proper write request, and
2256 * there will be bios with new data to be drained into the
2257 * stripe cache
2258 */
2259 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002260 sh->reconstruct_state = reconstruct_state_drain_run;
2261 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2262 } else
2263 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002264
Dan Williamsac6b53b2009-07-14 13:40:19 -07002265 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002266
2267 for (i = disks; i--; ) {
2268 struct r5dev *dev = &sh->dev[i];
2269
2270 if (dev->towrite) {
2271 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002272 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002273 if (!expand)
2274 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002275 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002276 }
2277 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002278 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002279 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002280 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002281 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002282 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002283 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2284 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2285
Dan Williamsd8ee0722008-06-28 08:32:06 +10002286 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002287 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2288 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002289 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002290
2291 for (i = disks; i--; ) {
2292 struct r5dev *dev = &sh->dev[i];
2293 if (i == pd_idx)
2294 continue;
2295
Dan Williamse33129d2007-01-02 13:52:30 -07002296 if (dev->towrite &&
2297 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002298 test_bit(R5_Wantcompute, &dev->flags))) {
2299 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002300 set_bit(R5_LOCKED, &dev->flags);
2301 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002302 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002303 }
2304 }
2305 }
2306
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002307 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002308 * are in flight
2309 */
2310 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2311 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002312 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002313
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002314 if (level == 6) {
2315 int qd_idx = sh->qd_idx;
2316 struct r5dev *dev = &sh->dev[qd_idx];
2317
2318 set_bit(R5_LOCKED, &dev->flags);
2319 clear_bit(R5_UPTODATE, &dev->flags);
2320 s->locked++;
2321 }
2322
Dan Williams600aa102008-06-28 08:32:05 +10002323 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002324 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002325 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002326}
NeilBrown16a53ec2006-06-26 00:27:38 -07002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328/*
2329 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002330 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * The bi_next chain must be in order.
2332 */
2333static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2334{
2335 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002336 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002337 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
NeilBrowncbe47ec2011-07-26 11:20:35 +10002339 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 (unsigned long long)bi->bi_sector,
2341 (unsigned long long)sh->sector);
2342
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002345 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002347 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2348 firstwrite = 1;
2349 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 bip = &sh->dev[dd_idx].toread;
2351 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2352 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2353 goto overlap;
2354 bip = & (*bip)->bi_next;
2355 }
2356 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2357 goto overlap;
2358
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002359 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (*bip)
2361 bi->bi_next = *bip;
2362 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002363 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (forwrite) {
2366 /* check if page is covered */
2367 sector_t sector = sh->dev[dd_idx].sector;
2368 for (bi=sh->dev[dd_idx].towrite;
2369 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2370 bi && bi->bi_sector <= sector;
2371 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2372 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2373 sector = bi->bi_sector + (bi->bi_size>>9);
2374 }
2375 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2376 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2377 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002378 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002379
2380 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2381 (unsigned long long)(*bip)->bi_sector,
2382 (unsigned long long)sh->sector, dd_idx);
2383
2384 if (conf->mddev->bitmap && firstwrite) {
2385 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2386 STRIPE_SECTORS, 0);
2387 sh->bm_seq = conf->seq_flush+1;
2388 set_bit(STRIPE_BIT_DELAY, &sh->state);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return 1;
2391
2392 overlap:
2393 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2394 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return 0;
2396}
2397
NeilBrownd1688a62011-10-11 16:49:52 +11002398static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002399
NeilBrownd1688a62011-10-11 16:49:52 +11002400static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002401 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002402{
NeilBrown784052e2009-03-31 15:19:07 +11002403 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002404 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002405 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002406 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002407 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002408
NeilBrown112bf892009-03-31 14:39:38 +11002409 raid5_compute_sector(conf,
2410 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002411 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002412 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002413 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002414}
2415
Dan Williamsa4456852007-07-09 11:56:43 -07002416static void
NeilBrownd1688a62011-10-11 16:49:52 +11002417handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002418 struct stripe_head_state *s, int disks,
2419 struct bio **return_bi)
2420{
2421 int i;
2422 for (i = disks; i--; ) {
2423 struct bio *bi;
2424 int bitmap_end = 0;
2425
2426 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002427 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002428 rcu_read_lock();
2429 rdev = rcu_dereference(conf->disks[i].rdev);
2430 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002431 atomic_inc(&rdev->nr_pending);
2432 else
2433 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002434 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002435 if (rdev) {
2436 if (!rdev_set_badblocks(
2437 rdev,
2438 sh->sector,
2439 STRIPE_SECTORS, 0))
2440 md_error(conf->mddev, rdev);
2441 rdev_dec_pending(rdev, conf->mddev);
2442 }
Dan Williamsa4456852007-07-09 11:56:43 -07002443 }
2444 spin_lock_irq(&conf->device_lock);
2445 /* fail all writes first */
2446 bi = sh->dev[i].towrite;
2447 sh->dev[i].towrite = NULL;
2448 if (bi) {
2449 s->to_write--;
2450 bitmap_end = 1;
2451 }
2452
2453 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2454 wake_up(&conf->wait_for_overlap);
2455
2456 while (bi && bi->bi_sector <
2457 sh->dev[i].sector + STRIPE_SECTORS) {
2458 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2459 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002460 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002461 md_write_end(conf->mddev);
2462 bi->bi_next = *return_bi;
2463 *return_bi = bi;
2464 }
2465 bi = nextbi;
2466 }
2467 /* and fail all 'written' */
2468 bi = sh->dev[i].written;
2469 sh->dev[i].written = NULL;
2470 if (bi) bitmap_end = 1;
2471 while (bi && bi->bi_sector <
2472 sh->dev[i].sector + STRIPE_SECTORS) {
2473 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2474 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002475 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002476 md_write_end(conf->mddev);
2477 bi->bi_next = *return_bi;
2478 *return_bi = bi;
2479 }
2480 bi = bi2;
2481 }
2482
Dan Williamsb5e98d62007-01-02 13:52:31 -07002483 /* fail any reads if this device is non-operational and
2484 * the data has not reached the cache yet.
2485 */
2486 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2487 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2488 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002489 bi = sh->dev[i].toread;
2490 sh->dev[i].toread = NULL;
2491 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2492 wake_up(&conf->wait_for_overlap);
2493 if (bi) s->to_read--;
2494 while (bi && bi->bi_sector <
2495 sh->dev[i].sector + STRIPE_SECTORS) {
2496 struct bio *nextbi =
2497 r5_next_bio(bi, sh->dev[i].sector);
2498 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002499 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002500 bi->bi_next = *return_bi;
2501 *return_bi = bi;
2502 }
2503 bi = nextbi;
2504 }
2505 }
2506 spin_unlock_irq(&conf->device_lock);
2507 if (bitmap_end)
2508 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2509 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002510 /* If we were in the middle of a write the parity block might
2511 * still be locked - so just clear all R5_LOCKED flags
2512 */
2513 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002514 }
2515
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002516 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2517 if (atomic_dec_and_test(&conf->pending_full_writes))
2518 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002519}
2520
NeilBrown7f0da592011-07-28 11:39:22 +10002521static void
NeilBrownd1688a62011-10-11 16:49:52 +11002522handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002523 struct stripe_head_state *s)
2524{
2525 int abort = 0;
2526 int i;
2527
NeilBrown7f0da592011-07-28 11:39:22 +10002528 clear_bit(STRIPE_SYNCING, &sh->state);
2529 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002530 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002531 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002532 * Don't even need to abort as that is handled elsewhere
2533 * if needed, and not always wanted e.g. if there is a known
2534 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002535 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002536 * non-sync devices, or abort the recovery
2537 */
NeilBrown18b98372012-04-01 23:48:38 +10002538 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2539 /* During recovery devices cannot be removed, so
2540 * locking and refcounting of rdevs is not needed
2541 */
2542 for (i = 0; i < conf->raid_disks; i++) {
2543 struct md_rdev *rdev = conf->disks[i].rdev;
2544 if (rdev
2545 && !test_bit(Faulty, &rdev->flags)
2546 && !test_bit(In_sync, &rdev->flags)
2547 && !rdev_set_badblocks(rdev, sh->sector,
2548 STRIPE_SECTORS, 0))
2549 abort = 1;
2550 rdev = conf->disks[i].replacement;
2551 if (rdev
2552 && !test_bit(Faulty, &rdev->flags)
2553 && !test_bit(In_sync, &rdev->flags)
2554 && !rdev_set_badblocks(rdev, sh->sector,
2555 STRIPE_SECTORS, 0))
2556 abort = 1;
2557 }
2558 if (abort)
2559 conf->recovery_disabled =
2560 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002561 }
NeilBrown18b98372012-04-01 23:48:38 +10002562 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002563}
2564
NeilBrown9a3e1102011-12-23 10:17:53 +11002565static int want_replace(struct stripe_head *sh, int disk_idx)
2566{
2567 struct md_rdev *rdev;
2568 int rv = 0;
2569 /* Doing recovery so rcu locking not required */
2570 rdev = sh->raid_conf->disks[disk_idx].replacement;
2571 if (rdev
2572 && !test_bit(Faulty, &rdev->flags)
2573 && !test_bit(In_sync, &rdev->flags)
2574 && (rdev->recovery_offset <= sh->sector
2575 || rdev->mddev->recovery_cp <= sh->sector))
2576 rv = 1;
2577
2578 return rv;
2579}
2580
NeilBrown93b3dbc2011-07-27 11:00:36 +10002581/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002582 * to be read or computed to satisfy a request.
2583 *
2584 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002585 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002586 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002587static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2588 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002589{
2590 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002591 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2592 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002593
Dan Williamsf38e1212007-01-02 13:52:30 -07002594 /* is the data in this block needed, and can we get it? */
2595 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002596 !test_bit(R5_UPTODATE, &dev->flags) &&
2597 (dev->toread ||
2598 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2599 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002600 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002601 (s->failed >= 1 && fdev[0]->toread) ||
2602 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002603 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2604 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2605 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002606 /* we would like to get this block, possibly by computing it,
2607 * otherwise read it if the backing disk is insync
2608 */
2609 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2610 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2611 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002612 (s->failed && (disk_idx == s->failed_num[0] ||
2613 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002614 /* have disk failed, and we're requested to fetch it;
2615 * do compute it
2616 */
2617 pr_debug("Computing stripe %llu block %d\n",
2618 (unsigned long long)sh->sector, disk_idx);
2619 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2620 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2621 set_bit(R5_Wantcompute, &dev->flags);
2622 sh->ops.target = disk_idx;
2623 sh->ops.target2 = -1; /* no 2nd target */
2624 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002625 /* Careful: from this point on 'uptodate' is in the eye
2626 * of raid_run_ops which services 'compute' operations
2627 * before writes. R5_Wantcompute flags a block that will
2628 * be R5_UPTODATE by the time it is needed for a
2629 * subsequent operation.
2630 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002631 s->uptodate++;
2632 return 1;
2633 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2634 /* Computing 2-failure is *very* expensive; only
2635 * do it if failed >= 2
2636 */
2637 int other;
2638 for (other = disks; other--; ) {
2639 if (other == disk_idx)
2640 continue;
2641 if (!test_bit(R5_UPTODATE,
2642 &sh->dev[other].flags))
2643 break;
2644 }
2645 BUG_ON(other < 0);
2646 pr_debug("Computing stripe %llu blocks %d,%d\n",
2647 (unsigned long long)sh->sector,
2648 disk_idx, other);
2649 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2650 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2651 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2652 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2653 sh->ops.target = disk_idx;
2654 sh->ops.target2 = other;
2655 s->uptodate += 2;
2656 s->req_compute = 1;
2657 return 1;
2658 } else if (test_bit(R5_Insync, &dev->flags)) {
2659 set_bit(R5_LOCKED, &dev->flags);
2660 set_bit(R5_Wantread, &dev->flags);
2661 s->locked++;
2662 pr_debug("Reading block %d (sync=%d)\n",
2663 disk_idx, s->syncing);
2664 }
2665 }
2666
2667 return 0;
2668}
2669
2670/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002671 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002672 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002673static void handle_stripe_fill(struct stripe_head *sh,
2674 struct stripe_head_state *s,
2675 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002676{
2677 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002678
2679 /* look for blocks to read/compute, skip this if a compute
2680 * is already in flight, or if the stripe contents are in the
2681 * midst of changing due to a write
2682 */
2683 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2684 !sh->reconstruct_state)
2685 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002686 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002687 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002688 set_bit(STRIPE_HANDLE, &sh->state);
2689}
2690
2691
Dan Williams1fe797e2008-06-28 09:16:30 +10002692/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002693 * any written block on an uptodate or failed drive can be returned.
2694 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2695 * never LOCKED, so we don't need to test 'failed' directly.
2696 */
NeilBrownd1688a62011-10-11 16:49:52 +11002697static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002698 struct stripe_head *sh, int disks, struct bio **return_bi)
2699{
2700 int i;
2701 struct r5dev *dev;
2702
2703 for (i = disks; i--; )
2704 if (sh->dev[i].written) {
2705 dev = &sh->dev[i];
2706 if (!test_bit(R5_LOCKED, &dev->flags) &&
2707 test_bit(R5_UPTODATE, &dev->flags)) {
2708 /* We can return any write requests */
2709 struct bio *wbi, *wbi2;
2710 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002711 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002712 spin_lock_irq(&conf->device_lock);
2713 wbi = dev->written;
2714 dev->written = NULL;
2715 while (wbi && wbi->bi_sector <
2716 dev->sector + STRIPE_SECTORS) {
2717 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002718 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002719 md_write_end(conf->mddev);
2720 wbi->bi_next = *return_bi;
2721 *return_bi = wbi;
2722 }
2723 wbi = wbi2;
2724 }
2725 if (dev->towrite == NULL)
2726 bitmap_end = 1;
2727 spin_unlock_irq(&conf->device_lock);
2728 if (bitmap_end)
2729 bitmap_endwrite(conf->mddev->bitmap,
2730 sh->sector,
2731 STRIPE_SECTORS,
2732 !test_bit(STRIPE_DEGRADED, &sh->state),
2733 0);
2734 }
2735 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002736
2737 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2738 if (atomic_dec_and_test(&conf->pending_full_writes))
2739 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002740}
2741
NeilBrownd1688a62011-10-11 16:49:52 +11002742static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002743 struct stripe_head *sh,
2744 struct stripe_head_state *s,
2745 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002746{
2747 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002748 if (conf->max_degraded == 2) {
2749 /* RAID6 requires 'rcw' in current implementation
2750 * Calculate the real rcw later - for now fake it
2751 * look like rcw is cheaper
2752 */
2753 rcw = 1; rmw = 2;
2754 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002755 /* would I have to read this buffer for read_modify_write */
2756 struct r5dev *dev = &sh->dev[i];
2757 if ((dev->towrite || i == sh->pd_idx) &&
2758 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002759 !(test_bit(R5_UPTODATE, &dev->flags) ||
2760 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002761 if (test_bit(R5_Insync, &dev->flags))
2762 rmw++;
2763 else
2764 rmw += 2*disks; /* cannot read it */
2765 }
2766 /* Would I have to read this buffer for reconstruct_write */
2767 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2768 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002769 !(test_bit(R5_UPTODATE, &dev->flags) ||
2770 test_bit(R5_Wantcompute, &dev->flags))) {
2771 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002772 else
2773 rcw += 2*disks;
2774 }
2775 }
Dan Williams45b42332007-07-09 11:56:43 -07002776 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002777 (unsigned long long)sh->sector, rmw, rcw);
2778 set_bit(STRIPE_HANDLE, &sh->state);
2779 if (rmw < rcw && rmw > 0)
2780 /* prefer read-modify-write, but need to get some data */
2781 for (i = disks; i--; ) {
2782 struct r5dev *dev = &sh->dev[i];
2783 if ((dev->towrite || i == sh->pd_idx) &&
2784 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002785 !(test_bit(R5_UPTODATE, &dev->flags) ||
2786 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002787 test_bit(R5_Insync, &dev->flags)) {
2788 if (
2789 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002790 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002791 "%d for r-m-w\n", i);
2792 set_bit(R5_LOCKED, &dev->flags);
2793 set_bit(R5_Wantread, &dev->flags);
2794 s->locked++;
2795 } else {
2796 set_bit(STRIPE_DELAYED, &sh->state);
2797 set_bit(STRIPE_HANDLE, &sh->state);
2798 }
2799 }
2800 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002801 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002802 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002803 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002804 for (i = disks; i--; ) {
2805 struct r5dev *dev = &sh->dev[i];
2806 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002807 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002808 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002809 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002810 test_bit(R5_Wantcompute, &dev->flags))) {
2811 rcw++;
2812 if (!test_bit(R5_Insync, &dev->flags))
2813 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002814 if (
2815 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002816 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002817 "%d for Reconstruct\n", i);
2818 set_bit(R5_LOCKED, &dev->flags);
2819 set_bit(R5_Wantread, &dev->flags);
2820 s->locked++;
2821 } else {
2822 set_bit(STRIPE_DELAYED, &sh->state);
2823 set_bit(STRIPE_HANDLE, &sh->state);
2824 }
2825 }
2826 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002827 }
Dan Williamsa4456852007-07-09 11:56:43 -07002828 /* now if nothing is locked, and if we have enough data,
2829 * we can start a write request
2830 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002831 /* since handle_stripe can be called at any time we need to handle the
2832 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002833 * subsequent call wants to start a write request. raid_run_ops only
2834 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002835 * simultaneously. If this is not the case then new writes need to be
2836 * held off until the compute completes.
2837 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002838 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2839 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2840 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002841 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002842}
2843
NeilBrownd1688a62011-10-11 16:49:52 +11002844static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002845 struct stripe_head_state *s, int disks)
2846{
Dan Williamsecc65c92008-06-28 08:31:57 +10002847 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002848
Dan Williamsbd2ab672008-04-10 21:29:27 -07002849 set_bit(STRIPE_HANDLE, &sh->state);
2850
Dan Williamsecc65c92008-06-28 08:31:57 +10002851 switch (sh->check_state) {
2852 case check_state_idle:
2853 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002854 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002855 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002856 sh->check_state = check_state_run;
2857 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002858 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002859 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002860 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002861 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002862 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002863 /* fall through */
2864 case check_state_compute_result:
2865 sh->check_state = check_state_idle;
2866 if (!dev)
2867 dev = &sh->dev[sh->pd_idx];
2868
2869 /* check that a write has not made the stripe insync */
2870 if (test_bit(STRIPE_INSYNC, &sh->state))
2871 break;
2872
2873 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002874 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2875 BUG_ON(s->uptodate != disks);
2876
2877 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002878 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002879 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002880
Dan Williamsa4456852007-07-09 11:56:43 -07002881 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002882 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002883 break;
2884 case check_state_run:
2885 break; /* we will be called again upon completion */
2886 case check_state_check_result:
2887 sh->check_state = check_state_idle;
2888
2889 /* if a failure occurred during the check operation, leave
2890 * STRIPE_INSYNC not set and let the stripe be handled again
2891 */
2892 if (s->failed)
2893 break;
2894
2895 /* handle a successful check operation, if parity is correct
2896 * we are done. Otherwise update the mismatch count and repair
2897 * parity if !MD_RECOVERY_CHECK
2898 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002899 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002900 /* parity is correct (on disc,
2901 * not in buffer any more)
2902 */
2903 set_bit(STRIPE_INSYNC, &sh->state);
2904 else {
2905 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2906 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2907 /* don't try to repair!! */
2908 set_bit(STRIPE_INSYNC, &sh->state);
2909 else {
2910 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002911 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002912 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2913 set_bit(R5_Wantcompute,
2914 &sh->dev[sh->pd_idx].flags);
2915 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002916 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002917 s->uptodate++;
2918 }
2919 }
2920 break;
2921 case check_state_compute_run:
2922 break;
2923 default:
2924 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2925 __func__, sh->check_state,
2926 (unsigned long long) sh->sector);
2927 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002928 }
2929}
2930
2931
NeilBrownd1688a62011-10-11 16:49:52 +11002932static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002933 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002934 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002935{
Dan Williamsa4456852007-07-09 11:56:43 -07002936 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002937 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002938 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002939
2940 set_bit(STRIPE_HANDLE, &sh->state);
2941
2942 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002943
Dan Williamsa4456852007-07-09 11:56:43 -07002944 /* Want to check and possibly repair P and Q.
2945 * However there could be one 'failed' device, in which
2946 * case we can only check one of them, possibly using the
2947 * other to generate missing data
2948 */
2949
Dan Williamsd82dfee2009-07-14 13:40:57 -07002950 switch (sh->check_state) {
2951 case check_state_idle:
2952 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002953 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002954 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002955 * makes sense to check P (If anything else were failed,
2956 * we would have used P to recreate it).
2957 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002958 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002959 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002960 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002961 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002962 * anything, so it makes sense to check it
2963 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002964 if (sh->check_state == check_state_run)
2965 sh->check_state = check_state_run_pq;
2966 else
2967 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002968 }
Dan Williams36d1c642009-07-14 11:48:22 -07002969
Dan Williamsd82dfee2009-07-14 13:40:57 -07002970 /* discard potentially stale zero_sum_result */
2971 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002972
Dan Williamsd82dfee2009-07-14 13:40:57 -07002973 if (sh->check_state == check_state_run) {
2974 /* async_xor_zero_sum destroys the contents of P */
2975 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2976 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002977 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002978 if (sh->check_state >= check_state_run &&
2979 sh->check_state <= check_state_run_pq) {
2980 /* async_syndrome_zero_sum preserves P and Q, so
2981 * no need to mark them !uptodate here
2982 */
2983 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2984 break;
2985 }
Dan Williams36d1c642009-07-14 11:48:22 -07002986
Dan Williamsd82dfee2009-07-14 13:40:57 -07002987 /* we have 2-disk failure */
2988 BUG_ON(s->failed != 2);
2989 /* fall through */
2990 case check_state_compute_result:
2991 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002992
Dan Williamsd82dfee2009-07-14 13:40:57 -07002993 /* check that a write has not made the stripe insync */
2994 if (test_bit(STRIPE_INSYNC, &sh->state))
2995 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002996
2997 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002998 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002999 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003000 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003001 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003002 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003003 s->locked++;
3004 set_bit(R5_LOCKED, &dev->flags);
3005 set_bit(R5_Wantwrite, &dev->flags);
3006 }
3007 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003008 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003009 s->locked++;
3010 set_bit(R5_LOCKED, &dev->flags);
3011 set_bit(R5_Wantwrite, &dev->flags);
3012 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003013 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003014 dev = &sh->dev[pd_idx];
3015 s->locked++;
3016 set_bit(R5_LOCKED, &dev->flags);
3017 set_bit(R5_Wantwrite, &dev->flags);
3018 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003019 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003020 dev = &sh->dev[qd_idx];
3021 s->locked++;
3022 set_bit(R5_LOCKED, &dev->flags);
3023 set_bit(R5_Wantwrite, &dev->flags);
3024 }
3025 clear_bit(STRIPE_DEGRADED, &sh->state);
3026
3027 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003028 break;
3029 case check_state_run:
3030 case check_state_run_q:
3031 case check_state_run_pq:
3032 break; /* we will be called again upon completion */
3033 case check_state_check_result:
3034 sh->check_state = check_state_idle;
3035
3036 /* handle a successful check operation, if parity is correct
3037 * we are done. Otherwise update the mismatch count and repair
3038 * parity if !MD_RECOVERY_CHECK
3039 */
3040 if (sh->ops.zero_sum_result == 0) {
3041 /* both parities are correct */
3042 if (!s->failed)
3043 set_bit(STRIPE_INSYNC, &sh->state);
3044 else {
3045 /* in contrast to the raid5 case we can validate
3046 * parity, but still have a failure to write
3047 * back
3048 */
3049 sh->check_state = check_state_compute_result;
3050 /* Returning at this point means that we may go
3051 * off and bring p and/or q uptodate again so
3052 * we make sure to check zero_sum_result again
3053 * to verify if p or q need writeback
3054 */
3055 }
3056 } else {
3057 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3058 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3059 /* don't try to repair!! */
3060 set_bit(STRIPE_INSYNC, &sh->state);
3061 else {
3062 int *target = &sh->ops.target;
3063
3064 sh->ops.target = -1;
3065 sh->ops.target2 = -1;
3066 sh->check_state = check_state_compute_run;
3067 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3068 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3069 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3070 set_bit(R5_Wantcompute,
3071 &sh->dev[pd_idx].flags);
3072 *target = pd_idx;
3073 target = &sh->ops.target2;
3074 s->uptodate++;
3075 }
3076 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3077 set_bit(R5_Wantcompute,
3078 &sh->dev[qd_idx].flags);
3079 *target = qd_idx;
3080 s->uptodate++;
3081 }
3082 }
3083 }
3084 break;
3085 case check_state_compute_run:
3086 break;
3087 default:
3088 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3089 __func__, sh->check_state,
3090 (unsigned long long) sh->sector);
3091 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003092 }
3093}
3094
NeilBrownd1688a62011-10-11 16:49:52 +11003095static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003096{
3097 int i;
3098
3099 /* We have read all the blocks in this stripe and now we need to
3100 * copy some of them into a target stripe for expand.
3101 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003102 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003103 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3104 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003105 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003106 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003107 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003108 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003109
NeilBrown784052e2009-03-31 15:19:07 +11003110 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003111 sector_t s = raid5_compute_sector(conf, bn, 0,
3112 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003113 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003114 if (sh2 == NULL)
3115 /* so far only the early blocks of this stripe
3116 * have been requested. When later blocks
3117 * get requested, we will try again
3118 */
3119 continue;
3120 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3121 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3122 /* must have already done this block */
3123 release_stripe(sh2);
3124 continue;
3125 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003126
3127 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003128 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003129 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003130 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003131 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003132
Dan Williamsa4456852007-07-09 11:56:43 -07003133 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3134 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3135 for (j = 0; j < conf->raid_disks; j++)
3136 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003137 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003138 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3139 break;
3140 if (j == conf->raid_disks) {
3141 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3142 set_bit(STRIPE_HANDLE, &sh2->state);
3143 }
3144 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003145
Dan Williamsa4456852007-07-09 11:56:43 -07003146 }
NeilBrowna2e08552007-09-11 15:23:36 -07003147 /* done submitting copies, wait for them to complete */
3148 if (tx) {
3149 async_tx_ack(tx);
3150 dma_wait_for_async_tx(tx);
3151 }
Dan Williamsa4456852007-07-09 11:56:43 -07003152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154/*
3155 * handle_stripe - do things to a stripe.
3156 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003157 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3158 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003160 * return some read requests which now have data
3161 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 * schedule a read on some buffers
3163 * schedule a write of some buffers
3164 * return confirmation of parity correctness
3165 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 */
Dan Williamsa4456852007-07-09 11:56:43 -07003167
NeilBrownacfe7262011-07-27 11:00:36 +10003168static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003169{
NeilBrownd1688a62011-10-11 16:49:52 +11003170 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003171 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003172 struct r5dev *dev;
3173 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003174 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003175
NeilBrownacfe7262011-07-27 11:00:36 +10003176 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003177
NeilBrownacfe7262011-07-27 11:00:36 +10003178 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3179 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3180 s->failed_num[0] = -1;
3181 s->failed_num[1] = -1;
3182
3183 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003184 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10003185 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003186 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003187 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003188 sector_t first_bad;
3189 int bad_sectors;
3190 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003191
NeilBrown16a53ec2006-06-26 00:27:38 -07003192 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003193
Dan Williams45b42332007-07-09 11:56:43 -07003194 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003195 i, dev->flags,
3196 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003197 /* maybe we can reply to a read
3198 *
3199 * new wantfill requests are only permitted while
3200 * ops_complete_biofill is guaranteed to be inactive
3201 */
3202 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3203 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3204 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003205
3206 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003207 if (test_bit(R5_LOCKED, &dev->flags))
3208 s->locked++;
3209 if (test_bit(R5_UPTODATE, &dev->flags))
3210 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003211 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003212 s->compute++;
3213 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003214 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003215
NeilBrownacfe7262011-07-27 11:00:36 +10003216 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003217 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003218 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003219 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003220 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003221 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003222 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003223 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003224 }
Dan Williamsa4456852007-07-09 11:56:43 -07003225 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003226 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003227 /* Prefer to use the replacement for reads, but only
3228 * if it is recovered enough and has no bad blocks.
3229 */
3230 rdev = rcu_dereference(conf->disks[i].replacement);
3231 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3232 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3233 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3234 &first_bad, &bad_sectors))
3235 set_bit(R5_ReadRepl, &dev->flags);
3236 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003237 if (rdev)
3238 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003239 rdev = rcu_dereference(conf->disks[i].rdev);
3240 clear_bit(R5_ReadRepl, &dev->flags);
3241 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003242 if (rdev && test_bit(Faulty, &rdev->flags))
3243 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003244 if (rdev) {
3245 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3246 &first_bad, &bad_sectors);
3247 if (s->blocked_rdev == NULL
3248 && (test_bit(Blocked, &rdev->flags)
3249 || is_bad < 0)) {
3250 if (is_bad < 0)
3251 set_bit(BlockedBadBlocks,
3252 &rdev->flags);
3253 s->blocked_rdev = rdev;
3254 atomic_inc(&rdev->nr_pending);
3255 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003256 }
NeilBrown415e72d2010-06-17 17:25:21 +10003257 clear_bit(R5_Insync, &dev->flags);
3258 if (!rdev)
3259 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003260 else if (is_bad) {
3261 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003262 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3263 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003264 /* treat as in-sync, but with a read error
3265 * which we can now try to correct
3266 */
3267 set_bit(R5_Insync, &dev->flags);
3268 set_bit(R5_ReadError, &dev->flags);
3269 }
3270 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003271 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003272 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003273 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003274 set_bit(R5_Insync, &dev->flags);
3275 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3276 test_bit(R5_Expanded, &dev->flags))
3277 /* If we've reshaped into here, we assume it is Insync.
3278 * We will shortly update recovery_offset to make
3279 * it official.
3280 */
3281 set_bit(R5_Insync, &dev->flags);
3282
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003283 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003284 /* This flag does not apply to '.replacement'
3285 * only to .rdev, so make sure to check that*/
3286 struct md_rdev *rdev2 = rcu_dereference(
3287 conf->disks[i].rdev);
3288 if (rdev2 == rdev)
3289 clear_bit(R5_Insync, &dev->flags);
3290 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003291 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003292 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003293 } else
3294 clear_bit(R5_WriteError, &dev->flags);
3295 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003296 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003297 /* This flag does not apply to '.replacement'
3298 * only to .rdev, so make sure to check that*/
3299 struct md_rdev *rdev2 = rcu_dereference(
3300 conf->disks[i].rdev);
3301 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003302 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003303 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003304 } else
3305 clear_bit(R5_MadeGood, &dev->flags);
3306 }
NeilBrown977df362011-12-23 10:17:53 +11003307 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3308 struct md_rdev *rdev2 = rcu_dereference(
3309 conf->disks[i].replacement);
3310 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3311 s->handle_bad_blocks = 1;
3312 atomic_inc(&rdev2->nr_pending);
3313 } else
3314 clear_bit(R5_MadeGoodRepl, &dev->flags);
3315 }
NeilBrown415e72d2010-06-17 17:25:21 +10003316 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003317 /* The ReadError flag will just be confusing now */
3318 clear_bit(R5_ReadError, &dev->flags);
3319 clear_bit(R5_ReWrite, &dev->flags);
3320 }
NeilBrown415e72d2010-06-17 17:25:21 +10003321 if (test_bit(R5_ReadError, &dev->flags))
3322 clear_bit(R5_Insync, &dev->flags);
3323 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003324 if (s->failed < 2)
3325 s->failed_num[s->failed] = i;
3326 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003327 if (rdev && !test_bit(Faulty, &rdev->flags))
3328 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003329 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003330 }
NeilBrownc4c16632011-07-26 11:34:20 +10003331 spin_unlock_irq(&conf->device_lock);
NeilBrown9a3e1102011-12-23 10:17:53 +11003332 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3333 /* If there is a failed device being replaced,
3334 * we must be recovering.
3335 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003336 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003337 * else we can only be replacing
3338 * sync and recovery both need to read all devices, and so
3339 * use the same flag.
3340 */
3341 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003342 sh->sector >= conf->mddev->recovery_cp ||
3343 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003344 s->syncing = 1;
3345 else
3346 s->replacing = 1;
3347 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003348 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003349}
NeilBrownf4168852007-02-28 20:11:53 -08003350
NeilBrowncc940152011-07-26 11:35:35 +10003351static void handle_stripe(struct stripe_head *sh)
3352{
3353 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003354 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003355 int i;
NeilBrown84789552011-07-27 11:00:36 +10003356 int prexor;
3357 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003358 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003359
3360 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003361 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003362 /* already being handled, ensure it gets handled
3363 * again when current action finishes */
3364 set_bit(STRIPE_HANDLE, &sh->state);
3365 return;
3366 }
3367
3368 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3369 set_bit(STRIPE_SYNCING, &sh->state);
3370 clear_bit(STRIPE_INSYNC, &sh->state);
3371 }
3372 clear_bit(STRIPE_DELAYED, &sh->state);
3373
3374 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3375 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3376 (unsigned long long)sh->sector, sh->state,
3377 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3378 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003379
NeilBrownacfe7262011-07-27 11:00:36 +10003380 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003381
NeilBrownbc2607f2011-07-28 11:39:22 +10003382 if (s.handle_bad_blocks) {
3383 set_bit(STRIPE_HANDLE, &sh->state);
3384 goto finish;
3385 }
3386
NeilBrown474af965fe2011-07-27 11:00:36 +10003387 if (unlikely(s.blocked_rdev)) {
3388 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003389 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003390 set_bit(STRIPE_HANDLE, &sh->state);
3391 goto finish;
3392 }
3393 /* There is nothing for the blocked_rdev to block */
3394 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3395 s.blocked_rdev = NULL;
3396 }
3397
3398 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3399 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3400 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3401 }
3402
3403 pr_debug("locked=%d uptodate=%d to_read=%d"
3404 " to_write=%d failed=%d failed_num=%d,%d\n",
3405 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3406 s.failed_num[0], s.failed_num[1]);
3407 /* check if the array has lost more than max_degraded devices and,
3408 * if so, some requests might need to be failed.
3409 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003410 if (s.failed > conf->max_degraded) {
3411 sh->check_state = 0;
3412 sh->reconstruct_state = 0;
3413 if (s.to_read+s.to_write+s.written)
3414 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003415 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003416 handle_failed_sync(conf, sh, &s);
3417 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003418
3419 /*
3420 * might be able to return some write requests if the parity blocks
3421 * are safe, or on a failed drive
3422 */
3423 pdev = &sh->dev[sh->pd_idx];
3424 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3425 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3426 qdev = &sh->dev[sh->qd_idx];
3427 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3428 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3429 || conf->level < 6;
3430
3431 if (s.written &&
3432 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3433 && !test_bit(R5_LOCKED, &pdev->flags)
3434 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3435 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3436 && !test_bit(R5_LOCKED, &qdev->flags)
3437 && test_bit(R5_UPTODATE, &qdev->flags)))))
3438 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3439
3440 /* Now we might consider reading some blocks, either to check/generate
3441 * parity, or to satisfy requests
3442 * or to load a block that is being partially written.
3443 */
3444 if (s.to_read || s.non_overwrite
3445 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003446 || (s.syncing && (s.uptodate + s.compute < disks))
3447 || s.replacing
3448 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003449 handle_stripe_fill(sh, &s, disks);
3450
NeilBrown84789552011-07-27 11:00:36 +10003451 /* Now we check to see if any write operations have recently
3452 * completed
3453 */
3454 prexor = 0;
3455 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3456 prexor = 1;
3457 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3458 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3459 sh->reconstruct_state = reconstruct_state_idle;
3460
3461 /* All the 'written' buffers and the parity block are ready to
3462 * be written back to disk
3463 */
3464 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3465 BUG_ON(sh->qd_idx >= 0 &&
3466 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3467 for (i = disks; i--; ) {
3468 struct r5dev *dev = &sh->dev[i];
3469 if (test_bit(R5_LOCKED, &dev->flags) &&
3470 (i == sh->pd_idx || i == sh->qd_idx ||
3471 dev->written)) {
3472 pr_debug("Writing block %d\n", i);
3473 set_bit(R5_Wantwrite, &dev->flags);
3474 if (prexor)
3475 continue;
3476 if (!test_bit(R5_Insync, &dev->flags) ||
3477 ((i == sh->pd_idx || i == sh->qd_idx) &&
3478 s.failed == 0))
3479 set_bit(STRIPE_INSYNC, &sh->state);
3480 }
3481 }
3482 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3483 s.dec_preread_active = 1;
3484 }
3485
3486 /* Now to consider new write requests and what else, if anything
3487 * should be read. We do not handle new writes when:
3488 * 1/ A 'write' operation (copy+xor) is already in flight.
3489 * 2/ A 'check' operation is in flight, as it may clobber the parity
3490 * block.
3491 */
3492 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3493 handle_stripe_dirtying(conf, sh, &s, disks);
3494
3495 /* maybe we need to check and possibly fix the parity for this stripe
3496 * Any reads will already have been scheduled, so we just see if enough
3497 * data is available. The parity check is held off while parity
3498 * dependent operations are in flight.
3499 */
3500 if (sh->check_state ||
3501 (s.syncing && s.locked == 0 &&
3502 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3503 !test_bit(STRIPE_INSYNC, &sh->state))) {
3504 if (conf->level == 6)
3505 handle_parity_checks6(conf, sh, &s, disks);
3506 else
3507 handle_parity_checks5(conf, sh, &s, disks);
3508 }
NeilBrownc5a31002011-07-27 11:00:36 +10003509
NeilBrown9a3e1102011-12-23 10:17:53 +11003510 if (s.replacing && s.locked == 0
3511 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3512 /* Write out to replacement devices where possible */
3513 for (i = 0; i < conf->raid_disks; i++)
3514 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3515 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3516 set_bit(R5_WantReplace, &sh->dev[i].flags);
3517 set_bit(R5_LOCKED, &sh->dev[i].flags);
3518 s.locked++;
3519 }
3520 set_bit(STRIPE_INSYNC, &sh->state);
3521 }
3522 if ((s.syncing || s.replacing) && s.locked == 0 &&
3523 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003524 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3525 clear_bit(STRIPE_SYNCING, &sh->state);
3526 }
3527
3528 /* If the failed drives are just a ReadError, then we might need
3529 * to progress the repair/check process
3530 */
3531 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3532 for (i = 0; i < s.failed; i++) {
3533 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3534 if (test_bit(R5_ReadError, &dev->flags)
3535 && !test_bit(R5_LOCKED, &dev->flags)
3536 && test_bit(R5_UPTODATE, &dev->flags)
3537 ) {
3538 if (!test_bit(R5_ReWrite, &dev->flags)) {
3539 set_bit(R5_Wantwrite, &dev->flags);
3540 set_bit(R5_ReWrite, &dev->flags);
3541 set_bit(R5_LOCKED, &dev->flags);
3542 s.locked++;
3543 } else {
3544 /* let's read it back */
3545 set_bit(R5_Wantread, &dev->flags);
3546 set_bit(R5_LOCKED, &dev->flags);
3547 s.locked++;
3548 }
3549 }
3550 }
3551
3552
NeilBrown3687c062011-07-27 11:00:36 +10003553 /* Finish reconstruct operations initiated by the expansion process */
3554 if (sh->reconstruct_state == reconstruct_state_result) {
3555 struct stripe_head *sh_src
3556 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3557 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3558 /* sh cannot be written until sh_src has been read.
3559 * so arrange for sh to be delayed a little
3560 */
3561 set_bit(STRIPE_DELAYED, &sh->state);
3562 set_bit(STRIPE_HANDLE, &sh->state);
3563 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3564 &sh_src->state))
3565 atomic_inc(&conf->preread_active_stripes);
3566 release_stripe(sh_src);
3567 goto finish;
3568 }
3569 if (sh_src)
3570 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003571
NeilBrown3687c062011-07-27 11:00:36 +10003572 sh->reconstruct_state = reconstruct_state_idle;
3573 clear_bit(STRIPE_EXPANDING, &sh->state);
3574 for (i = conf->raid_disks; i--; ) {
3575 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3576 set_bit(R5_LOCKED, &sh->dev[i].flags);
3577 s.locked++;
3578 }
3579 }
3580
3581 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3582 !sh->reconstruct_state) {
3583 /* Need to write out all blocks after computing parity */
3584 sh->disks = conf->raid_disks;
3585 stripe_set_idx(sh->sector, conf, 0, sh);
3586 schedule_reconstruction(sh, &s, 1, 1);
3587 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3588 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3589 atomic_dec(&conf->reshape_stripes);
3590 wake_up(&conf->wait_for_overlap);
3591 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3592 }
3593
3594 if (s.expanding && s.locked == 0 &&
3595 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3596 handle_stripe_expansion(conf, sh);
3597
3598finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003599 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003600 if (unlikely(s.blocked_rdev)) {
3601 if (conf->mddev->external)
3602 md_wait_for_blocked_rdev(s.blocked_rdev,
3603 conf->mddev);
3604 else
3605 /* Internal metadata will immediately
3606 * be written by raid5d, so we don't
3607 * need to wait here.
3608 */
3609 rdev_dec_pending(s.blocked_rdev,
3610 conf->mddev);
3611 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003612
NeilBrownbc2607f2011-07-28 11:39:22 +10003613 if (s.handle_bad_blocks)
3614 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003615 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003616 struct r5dev *dev = &sh->dev[i];
3617 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3618 /* We own a safe reference to the rdev */
3619 rdev = conf->disks[i].rdev;
3620 if (!rdev_set_badblocks(rdev, sh->sector,
3621 STRIPE_SECTORS, 0))
3622 md_error(conf->mddev, rdev);
3623 rdev_dec_pending(rdev, conf->mddev);
3624 }
NeilBrownb84db562011-07-28 11:39:23 +10003625 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3626 rdev = conf->disks[i].rdev;
3627 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003628 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003629 rdev_dec_pending(rdev, conf->mddev);
3630 }
NeilBrown977df362011-12-23 10:17:53 +11003631 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3632 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003633 if (!rdev)
3634 /* rdev have been moved down */
3635 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003636 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003637 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003638 rdev_dec_pending(rdev, conf->mddev);
3639 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003640 }
3641
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003642 if (s.ops_request)
3643 raid_run_ops(sh, s.ops_request);
3644
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003645 ops_run_io(sh, &s);
3646
NeilBrownc5709ef2011-07-26 11:35:20 +10003647 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003648 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003649 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003650 * have actually been submitted.
3651 */
3652 atomic_dec(&conf->preread_active_stripes);
3653 if (atomic_read(&conf->preread_active_stripes) <
3654 IO_THRESHOLD)
3655 md_wakeup_thread(conf->mddev->thread);
3656 }
3657
NeilBrownc5709ef2011-07-26 11:35:20 +10003658 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003659
Dan Williams257a4b42011-11-08 16:22:06 +11003660 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003661}
3662
NeilBrownd1688a62011-10-11 16:49:52 +11003663static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3666 while (!list_empty(&conf->delayed_list)) {
3667 struct list_head *l = conf->delayed_list.next;
3668 struct stripe_head *sh;
3669 sh = list_entry(l, struct stripe_head, lru);
3670 list_del_init(l);
3671 clear_bit(STRIPE_DELAYED, &sh->state);
3672 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3673 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003674 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
NeilBrown482c0832011-04-18 18:25:42 +10003676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677}
3678
NeilBrownd1688a62011-10-11 16:49:52 +11003679static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003680{
3681 /* device_lock is held */
3682 struct list_head head;
3683 list_add(&head, &conf->bitmap_list);
3684 list_del_init(&conf->bitmap_list);
3685 while (!list_empty(&head)) {
3686 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3687 list_del_init(&sh->lru);
3688 atomic_inc(&sh->count);
3689 __release_stripe(conf, sh);
3690 }
3691}
3692
NeilBrownfd01b882011-10-11 16:47:53 +11003693int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003694{
NeilBrownd1688a62011-10-11 16:49:52 +11003695 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003696
3697 /* No difference between reads and writes. Just check
3698 * how busy the stripe_cache is
3699 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003700
NeilBrownf022b2f2006-10-03 01:15:56 -07003701 if (conf->inactive_blocked)
3702 return 1;
3703 if (conf->quiesce)
3704 return 1;
3705 if (list_empty_careful(&conf->inactive_list))
3706 return 1;
3707
3708 return 0;
3709}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003710EXPORT_SYMBOL_GPL(md_raid5_congested);
3711
3712static int raid5_congested(void *data, int bits)
3713{
NeilBrownfd01b882011-10-11 16:47:53 +11003714 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003715
3716 return mddev_congested(mddev, bits) ||
3717 md_raid5_congested(mddev, bits);
3718}
NeilBrownf022b2f2006-10-03 01:15:56 -07003719
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003720/* We want read requests to align with chunks where possible,
3721 * but write requests don't need to.
3722 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003723static int raid5_mergeable_bvec(struct request_queue *q,
3724 struct bvec_merge_data *bvm,
3725 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003726{
NeilBrownfd01b882011-10-11 16:47:53 +11003727 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003728 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003729 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003730 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003731 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003732
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003733 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003734 return biovec->bv_len; /* always allow writes to be mergeable */
3735
Andre Noll664e7c42009-06-18 08:45:27 +10003736 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3737 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003738 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3739 if (max < 0) max = 0;
3740 if (max <= biovec->bv_len && bio_sectors == 0)
3741 return biovec->bv_len;
3742 else
3743 return max;
3744}
3745
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003746
NeilBrownfd01b882011-10-11 16:47:53 +11003747static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003748{
3749 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003750 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003751 unsigned int bio_sectors = bio->bi_size >> 9;
3752
Andre Noll664e7c42009-06-18 08:45:27 +10003753 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3754 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003755 return chunk_sectors >=
3756 ((sector & (chunk_sectors - 1)) + bio_sectors);
3757}
3758
3759/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3761 * later sampled by raid5d.
3762 */
NeilBrownd1688a62011-10-11 16:49:52 +11003763static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003764{
3765 unsigned long flags;
3766
3767 spin_lock_irqsave(&conf->device_lock, flags);
3768
3769 bi->bi_next = conf->retry_read_aligned_list;
3770 conf->retry_read_aligned_list = bi;
3771
3772 spin_unlock_irqrestore(&conf->device_lock, flags);
3773 md_wakeup_thread(conf->mddev->thread);
3774}
3775
3776
NeilBrownd1688a62011-10-11 16:49:52 +11003777static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003778{
3779 struct bio *bi;
3780
3781 bi = conf->retry_read_aligned;
3782 if (bi) {
3783 conf->retry_read_aligned = NULL;
3784 return bi;
3785 }
3786 bi = conf->retry_read_aligned_list;
3787 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003788 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003789 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003790 /*
3791 * this sets the active strip count to 1 and the processed
3792 * strip count to zero (upper 8 bits)
3793 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003794 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003795 }
3796
3797 return bi;
3798}
3799
3800
3801/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003802 * The "raid5_align_endio" should check if the read succeeded and if it
3803 * did, call bio_endio on the original bio (having bio_put the new bio
3804 * first).
3805 * If the read failed..
3806 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003807static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003808{
3809 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003810 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003811 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003812 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003813 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003814
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003815 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003816
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003817 rdev = (void*)raid_bi->bi_next;
3818 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003819 mddev = rdev->mddev;
3820 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003821
3822 rdev_dec_pending(rdev, conf->mddev);
3823
3824 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003825 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003826 if (atomic_dec_and_test(&conf->active_aligned_reads))
3827 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003828 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003829 }
3830
3831
Dan Williams45b42332007-07-09 11:56:43 -07003832 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003833
3834 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003835}
3836
Neil Brown387bb172007-02-08 14:20:29 -08003837static int bio_fits_rdev(struct bio *bi)
3838{
Jens Axboe165125e2007-07-24 09:28:11 +02003839 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003840
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003841 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003842 return 0;
3843 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003844 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003845 return 0;
3846
3847 if (q->merge_bvec_fn)
3848 /* it's too hard to apply the merge_bvec_fn at this stage,
3849 * just just give up
3850 */
3851 return 0;
3852
3853 return 1;
3854}
3855
3856
NeilBrownfd01b882011-10-11 16:47:53 +11003857static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003858{
NeilBrownd1688a62011-10-11 16:49:52 +11003859 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003860 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003861 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003862 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003863 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003864
3865 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003866 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003867 return 0;
3868 }
3869 /*
NeilBrowna167f662010-10-26 18:31:13 +11003870 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003871 */
NeilBrowna167f662010-10-26 18:31:13 +11003872 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003873 if (!align_bi)
3874 return 0;
3875 /*
3876 * set bi_end_io to a new function, and set bi_private to the
3877 * original bio.
3878 */
3879 align_bi->bi_end_io = raid5_align_endio;
3880 align_bi->bi_private = raid_bio;
3881 /*
3882 * compute position
3883 */
NeilBrown112bf892009-03-31 14:39:38 +11003884 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3885 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003886 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003887
NeilBrown671488c2011-12-23 10:17:52 +11003888 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003889 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003890 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3891 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3892 rdev->recovery_offset < end_sector) {
3893 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3894 if (rdev &&
3895 (test_bit(Faulty, &rdev->flags) ||
3896 !(test_bit(In_sync, &rdev->flags) ||
3897 rdev->recovery_offset >= end_sector)))
3898 rdev = NULL;
3899 }
3900 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003901 sector_t first_bad;
3902 int bad_sectors;
3903
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003904 atomic_inc(&rdev->nr_pending);
3905 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003906 raid_bio->bi_next = (void*)rdev;
3907 align_bi->bi_bdev = rdev->bdev;
3908 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003909
NeilBrown31c176e2011-07-28 11:39:22 +10003910 if (!bio_fits_rdev(align_bi) ||
3911 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3912 &first_bad, &bad_sectors)) {
3913 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003914 bio_put(align_bi);
3915 rdev_dec_pending(rdev, mddev);
3916 return 0;
3917 }
3918
majianpeng6c0544e2012-06-12 08:31:10 +08003919 /* No reshape active, so we can trust rdev->data_offset */
3920 align_bi->bi_sector += rdev->data_offset;
3921
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003922 spin_lock_irq(&conf->device_lock);
3923 wait_event_lock_irq(conf->wait_for_stripe,
3924 conf->quiesce == 0,
3925 conf->device_lock, /* nothing */);
3926 atomic_inc(&conf->active_aligned_reads);
3927 spin_unlock_irq(&conf->device_lock);
3928
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003929 generic_make_request(align_bi);
3930 return 1;
3931 } else {
3932 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003933 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934 return 0;
3935 }
3936}
3937
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003938/* __get_priority_stripe - get the next stripe to process
3939 *
3940 * Full stripe writes are allowed to pass preread active stripes up until
3941 * the bypass_threshold is exceeded. In general the bypass_count
3942 * increments when the handle_list is handled before the hold_list; however, it
3943 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3944 * stripe with in flight i/o. The bypass_count will be reset when the
3945 * head of the hold_list has changed, i.e. the head was promoted to the
3946 * handle_list.
3947 */
NeilBrownd1688a62011-10-11 16:49:52 +11003948static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003949{
3950 struct stripe_head *sh;
3951
3952 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3953 __func__,
3954 list_empty(&conf->handle_list) ? "empty" : "busy",
3955 list_empty(&conf->hold_list) ? "empty" : "busy",
3956 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3957
3958 if (!list_empty(&conf->handle_list)) {
3959 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3960
3961 if (list_empty(&conf->hold_list))
3962 conf->bypass_count = 0;
3963 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3964 if (conf->hold_list.next == conf->last_hold)
3965 conf->bypass_count++;
3966 else {
3967 conf->last_hold = conf->hold_list.next;
3968 conf->bypass_count -= conf->bypass_threshold;
3969 if (conf->bypass_count < 0)
3970 conf->bypass_count = 0;
3971 }
3972 }
3973 } else if (!list_empty(&conf->hold_list) &&
3974 ((conf->bypass_threshold &&
3975 conf->bypass_count > conf->bypass_threshold) ||
3976 atomic_read(&conf->pending_full_writes) == 0)) {
3977 sh = list_entry(conf->hold_list.next,
3978 typeof(*sh), lru);
3979 conf->bypass_count -= conf->bypass_threshold;
3980 if (conf->bypass_count < 0)
3981 conf->bypass_count = 0;
3982 } else
3983 return NULL;
3984
3985 list_del_init(&sh->lru);
3986 atomic_inc(&sh->count);
3987 BUG_ON(atomic_read(&sh->count) != 1);
3988 return sh;
3989}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003990
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003991static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
NeilBrownd1688a62011-10-11 16:49:52 +11003993 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003994 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 sector_t new_sector;
3996 sector_t logical_sector, last_sector;
3997 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003998 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003999 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Tejun Heoe9c74692010-09-03 11:56:18 +02004001 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4002 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004003 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004004 }
4005
NeilBrown3d310eb2005-06-21 17:17:26 -07004006 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004007
NeilBrown802ba062006-12-13 00:34:13 -08004008 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004009 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004010 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004011 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4014 last_sector = bi->bi_sector + (bi->bi_size>>9);
4015 bi->bi_next = NULL;
4016 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4019 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004020 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004021
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004022 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004023 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004024 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004025 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004026 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004027 * 64bit on a 32bit platform, and so it might be
4028 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004029 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004030 * the lock is dropped, so once we get a reference
4031 * to the stripe that we think it is, we will have
4032 * to check again.
4033 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004034 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004035 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004036 ? logical_sector < conf->reshape_progress
4037 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004038 previous = 1;
4039 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004040 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004041 ? logical_sector < conf->reshape_safe
4042 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004043 spin_unlock_irq(&conf->device_lock);
4044 schedule();
4045 goto retry;
4046 }
4047 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004048 spin_unlock_irq(&conf->device_lock);
4049 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004050
NeilBrown112bf892009-03-31 14:39:38 +11004051 new_sector = raid5_compute_sector(conf, logical_sector,
4052 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004053 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004054 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 (unsigned long long)new_sector,
4056 (unsigned long long)logical_sector);
4057
NeilBrownb5663ba2009-03-31 14:39:38 +11004058 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004059 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004061 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004062 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004063 * stripe, so we must do the range check again.
4064 * Expansion could still move past after this
4065 * test, but as we are holding a reference to
4066 * 'sh', we know that if that happens,
4067 * STRIPE_EXPANDING will get set and the expansion
4068 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004069 */
4070 int must_retry = 0;
4071 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004072 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004073 ? logical_sector >= conf->reshape_progress
4074 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004075 /* mismatch, need to try again */
4076 must_retry = 1;
4077 spin_unlock_irq(&conf->device_lock);
4078 if (must_retry) {
4079 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004080 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004081 goto retry;
4082 }
4083 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004084
Namhyung Kimffd96e32011-07-18 17:38:51 +10004085 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004086 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004087 logical_sector < mddev->suspend_hi) {
4088 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004089 /* As the suspend_* range is controlled by
4090 * userspace, we want an interruptible
4091 * wait.
4092 */
4093 flush_signals(current);
4094 prepare_to_wait(&conf->wait_for_overlap,
4095 &w, TASK_INTERRUPTIBLE);
4096 if (logical_sector >= mddev->suspend_lo &&
4097 logical_sector < mddev->suspend_hi)
4098 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004099 goto retry;
4100 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004101
4102 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004103 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004104 /* Stripe is busy expanding or
4105 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 * and wait a while
4107 */
NeilBrown482c0832011-04-18 18:25:42 +10004108 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 release_stripe(sh);
4110 schedule();
4111 goto retry;
4112 }
4113 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004114 set_bit(STRIPE_HANDLE, &sh->state);
4115 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004116 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004117 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4118 atomic_inc(&conf->preread_active_stripes);
NeilBrownb357f042012-07-03 17:45:31 +10004119 mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 } else {
4122 /* cannot get stripe for read-ahead, just give-up */
4123 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4124 finish_wait(&conf->wait_for_overlap, &w);
4125 break;
4126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004130 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004131 spin_unlock_irq(&conf->device_lock);
4132 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
NeilBrown16a53ec2006-06-26 00:27:38 -07004134 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004136
Neil Brown0e13fe232008-06-28 08:31:20 +10004137 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
NeilBrownfd01b882011-10-11 16:47:53 +11004141static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004142
NeilBrownfd01b882011-10-11 16:47:53 +11004143static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
NeilBrown52c03292006-06-26 00:27:43 -07004145 /* reshaping is quite different to recovery/resync so it is
4146 * handled quite separately ... here.
4147 *
4148 * On each call to sync_request, we gather one chunk worth of
4149 * destination stripes and flag them as expanding.
4150 * Then we find all the source stripes and request reads.
4151 * As the reads complete, handle_stripe will copy the data
4152 * into the destination stripe and release that stripe.
4153 */
NeilBrownd1688a62011-10-11 16:49:52 +11004154 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004156 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004157 int raid_disks = conf->previous_raid_disks;
4158 int data_disks = raid_disks - conf->max_degraded;
4159 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004160 int i;
4161 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004162 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004163 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004164 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004165 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004166
NeilBrownfef9c612009-03-31 15:16:46 +11004167 if (sector_nr == 0) {
4168 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004169 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004170 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4171 sector_nr = raid5_size(mddev, 0, 0)
4172 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004173 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004174 conf->reshape_progress > 0)
4175 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004176 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004177 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004178 mddev->curr_resync_completed = sector_nr;
4179 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004180 *skipped = 1;
4181 return sector_nr;
4182 }
NeilBrown52c03292006-06-26 00:27:43 -07004183 }
4184
NeilBrown7a661382009-03-31 15:21:40 +11004185 /* We need to process a full chunk at a time.
4186 * If old and new chunk sizes differ, we need to process the
4187 * largest of these
4188 */
Andre Noll664e7c42009-06-18 08:45:27 +10004189 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4190 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004191 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004192 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004193
NeilBrownb5254dd2012-05-21 09:27:01 +10004194 /* We update the metadata at least every 10 seconds, or when
4195 * the data about to be copied would over-write the source of
4196 * the data at the front of the range. i.e. one new_stripe
4197 * along from reshape_progress new_maps to after where
4198 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004199 */
NeilBrownfef9c612009-03-31 15:16:46 +11004200 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004201 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004202 readpos = conf->reshape_progress;
4203 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004204 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004205 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004206 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004207 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004208 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004209 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004210 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004211 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004212 readpos -= min_t(sector_t, reshape_sectors, readpos);
4213 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004214 }
NeilBrown52c03292006-06-26 00:27:43 -07004215
NeilBrownb5254dd2012-05-21 09:27:01 +10004216 /* Having calculated the 'writepos' possibly use it
4217 * to set 'stripe_addr' which is where we will write to.
4218 */
4219 if (mddev->reshape_backwards) {
4220 BUG_ON(conf->reshape_progress == 0);
4221 stripe_addr = writepos;
4222 BUG_ON((mddev->dev_sectors &
4223 ~((sector_t)reshape_sectors - 1))
4224 - reshape_sectors - stripe_addr
4225 != sector_nr);
4226 } else {
4227 BUG_ON(writepos != sector_nr + reshape_sectors);
4228 stripe_addr = sector_nr;
4229 }
4230
NeilBrownc8f517c2009-03-31 15:28:40 +11004231 /* 'writepos' is the most advanced device address we might write.
4232 * 'readpos' is the least advanced device address we might read.
4233 * 'safepos' is the least address recorded in the metadata as having
4234 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004235 * If there is a min_offset_diff, these are adjusted either by
4236 * increasing the safepos/readpos if diff is negative, or
4237 * increasing writepos if diff is positive.
4238 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004239 * ensure safety in the face of a crash - that must be done by userspace
4240 * making a backup of the data. So in that case there is no particular
4241 * rush to update metadata.
4242 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4243 * update the metadata to advance 'safepos' to match 'readpos' so that
4244 * we can be safe in the event of a crash.
4245 * So we insist on updating metadata if safepos is behind writepos and
4246 * readpos is beyond writepos.
4247 * In any case, update the metadata every 10 seconds.
4248 * Maybe that number should be configurable, but I'm not sure it is
4249 * worth it.... maybe it could be a multiple of safemode_delay???
4250 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004251 if (conf->min_offset_diff < 0) {
4252 safepos += -conf->min_offset_diff;
4253 readpos += -conf->min_offset_diff;
4254 } else
4255 writepos += conf->min_offset_diff;
4256
NeilBrown2c810cd2012-05-21 09:27:00 +10004257 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004258 ? (safepos > writepos && readpos < writepos)
4259 : (safepos < writepos && readpos > writepos)) ||
4260 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004261 /* Cannot proceed until we've updated the superblock... */
4262 wait_event(conf->wait_for_overlap,
4263 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004264 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004265 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004266 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004267 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004268 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004269 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004270 kthread_should_stop());
4271 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004272 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004273 spin_unlock_irq(&conf->device_lock);
4274 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004275 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004276 }
4277
NeilBrownab69ae12009-03-31 15:26:47 +11004278 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004279 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004280 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004281 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004282 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004283 set_bit(STRIPE_EXPANDING, &sh->state);
4284 atomic_inc(&conf->reshape_stripes);
4285 /* If any of this stripe is beyond the end of the old
4286 * array, then we need to zero those blocks
4287 */
4288 for (j=sh->disks; j--;) {
4289 sector_t s;
4290 if (j == sh->pd_idx)
4291 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004292 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004293 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004294 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004295 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004296 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004297 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004298 continue;
4299 }
4300 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4301 set_bit(R5_Expanded, &sh->dev[j].flags);
4302 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4303 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004304 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004305 set_bit(STRIPE_EXPAND_READY, &sh->state);
4306 set_bit(STRIPE_HANDLE, &sh->state);
4307 }
NeilBrownab69ae12009-03-31 15:26:47 +11004308 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004309 }
4310 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004311 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004312 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004313 else
NeilBrown7a661382009-03-31 15:21:40 +11004314 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004315 spin_unlock_irq(&conf->device_lock);
4316 /* Ok, those stripe are ready. We can start scheduling
4317 * reads on the source stripes.
4318 * The source stripes are determined by mapping the first and last
4319 * block on the destination stripes.
4320 */
NeilBrown52c03292006-06-26 00:27:43 -07004321 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004322 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004323 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004324 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004325 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004326 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004327 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004328 if (last_sector >= mddev->dev_sectors)
4329 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004330 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004331 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004332 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4333 set_bit(STRIPE_HANDLE, &sh->state);
4334 release_stripe(sh);
4335 first_sector += STRIPE_SECTORS;
4336 }
NeilBrownab69ae12009-03-31 15:26:47 +11004337 /* Now that the sources are clearly marked, we can release
4338 * the destination stripes
4339 */
4340 while (!list_empty(&stripes)) {
4341 sh = list_entry(stripes.next, struct stripe_head, lru);
4342 list_del_init(&sh->lru);
4343 release_stripe(sh);
4344 }
NeilBrownc6207272008-02-06 01:39:52 -08004345 /* If this takes us to the resync_max point where we have to pause,
4346 * then we need to write out the superblock.
4347 */
NeilBrown7a661382009-03-31 15:21:40 +11004348 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004349 if ((sector_nr - mddev->curr_resync_completed) * 2
4350 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004351 /* Cannot proceed until we've updated the superblock... */
4352 wait_event(conf->wait_for_overlap,
4353 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004354 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004355 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004356 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004357 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4358 md_wakeup_thread(mddev->thread);
4359 wait_event(mddev->sb_wait,
4360 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4361 || kthread_should_stop());
4362 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004363 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004364 spin_unlock_irq(&conf->device_lock);
4365 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004366 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004367 }
NeilBrown7a661382009-03-31 15:21:40 +11004368 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004369}
4370
4371/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004372static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004373{
NeilBrownd1688a62011-10-11 16:49:52 +11004374 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004375 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004376 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004377 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004378 int still_degraded = 0;
4379 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
NeilBrown72626682005-09-09 16:23:54 -07004381 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004383
NeilBrown29269552006-03-27 01:18:10 -08004384 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4385 end_reshape(conf);
4386 return 0;
4387 }
NeilBrown72626682005-09-09 16:23:54 -07004388
4389 if (mddev->curr_resync < max_sector) /* aborted */
4390 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4391 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004392 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004393 conf->fullsync = 0;
4394 bitmap_close_sync(mddev->bitmap);
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return 0;
4397 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004398
NeilBrown64bd6602009-08-03 10:59:58 +10004399 /* Allow raid5_quiesce to complete */
4400 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4401
NeilBrown52c03292006-06-26 00:27:43 -07004402 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4403 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004404
NeilBrownc6207272008-02-06 01:39:52 -08004405 /* No need to check resync_max as we never do more than one
4406 * stripe, and as resync_max will always be on a chunk boundary,
4407 * if the check in md_do_sync didn't fire, there is no chance
4408 * of overstepping resync_max here
4409 */
4410
NeilBrown16a53ec2006-06-26 00:27:38 -07004411 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 * to resync, then assert that we are finished, because there is
4413 * nothing we can do.
4414 */
NeilBrown3285edf2006-06-26 00:27:55 -07004415 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004416 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004417 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004418 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return rv;
4420 }
NeilBrown72626682005-09-09 16:23:54 -07004421 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004422 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004423 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4424 /* we can skip this block, and probably more */
4425 sync_blocks /= STRIPE_SECTORS;
4426 *skipped = 1;
4427 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
NeilBrownb47490c2008-02-06 01:39:50 -08004430 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4431
NeilBrowna8c906c2009-06-09 14:39:59 +10004432 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004434 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004436 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004438 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004440 /* Need to check if array will still be degraded after recovery/resync
4441 * We don't need to check the 'failed' flag as when that gets set,
4442 * recovery aborts.
4443 */
NeilBrownf001a702009-06-09 14:30:31 +10004444 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004445 if (conf->disks[i].rdev == NULL)
4446 still_degraded = 1;
4447
4448 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4449
NeilBrown83206d62011-07-26 11:19:49 +10004450 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
NeilBrown14425772009-10-16 15:55:25 +11004452 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 release_stripe(sh);
4454
4455 return STRIPE_SECTORS;
4456}
4457
NeilBrownd1688a62011-10-11 16:49:52 +11004458static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004459{
4460 /* We may not be able to submit a whole bio at once as there
4461 * may not be enough stripe_heads available.
4462 * We cannot pre-allocate enough stripe_heads as we may need
4463 * more than exist in the cache (if we allow ever large chunks).
4464 * So we do one stripe head at a time and record in
4465 * ->bi_hw_segments how many have been done.
4466 *
4467 * We *know* that this entire raid_bio is in one chunk, so
4468 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4469 */
4470 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004471 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004472 sector_t sector, logical_sector, last_sector;
4473 int scnt = 0;
4474 int remaining;
4475 int handled = 0;
4476
4477 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004478 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004479 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004480 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4481
4482 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004483 logical_sector += STRIPE_SECTORS,
4484 sector += STRIPE_SECTORS,
4485 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004486
Jens Axboe960e7392008-08-15 10:41:18 +02004487 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004488 /* already done this stripe */
4489 continue;
4490
NeilBrowna8c906c2009-06-09 14:39:59 +10004491 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004492
4493 if (!sh) {
4494 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004495 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004496 conf->retry_read_aligned = raid_bio;
4497 return handled;
4498 }
4499
Neil Brown387bb172007-02-08 14:20:29 -08004500 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4501 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004502 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004503 conf->retry_read_aligned = raid_bio;
4504 return handled;
4505 }
4506
Dan Williams36d1c642009-07-14 11:48:22 -07004507 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004508 release_stripe(sh);
4509 handled++;
4510 }
4511 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004512 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004513 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004514 if (remaining == 0)
4515 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004516 if (atomic_dec_and_test(&conf->active_aligned_reads))
4517 wake_up(&conf->wait_for_stripe);
4518 return handled;
4519}
4520
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522/*
4523 * This is our raid5 kernel thread.
4524 *
4525 * We scan the hash table for stripes which can be handled now.
4526 * During the scan, completed stripes are saved for us by the interrupt
4527 * handler, so that they will not have to wait for our next wakeup.
4528 */
NeilBrownfd01b882011-10-11 16:47:53 +11004529static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530{
4531 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004532 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004534 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Dan Williams45b42332007-07-09 11:56:43 -07004536 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004540 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 handled = 0;
4542 spin_lock_irq(&conf->device_lock);
4543 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004544 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
NeilBrown0021b7b2012-07-31 09:08:14 +02004546 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004547 !list_empty(&conf->bitmap_list)) {
4548 /* Now is a good time to flush some bitmap updates */
4549 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004550 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004551 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004552 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004553 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004554 activate_bit_delay(conf);
4555 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004556 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004557
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004558 while ((bio = remove_bio_from_retry(conf))) {
4559 int ok;
4560 spin_unlock_irq(&conf->device_lock);
4561 ok = retry_aligned_read(conf, bio);
4562 spin_lock_irq(&conf->device_lock);
4563 if (!ok)
4564 break;
4565 handled++;
4566 }
4567
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004568 sh = __get_priority_stripe(conf);
4569
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004570 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 spin_unlock_irq(&conf->device_lock);
4573
4574 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004575 handle_stripe(sh);
4576 release_stripe(sh);
4577 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
NeilBrownde393cd2011-07-28 11:31:48 +10004579 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4580 md_check_recovery(mddev);
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 spin_lock_irq(&conf->device_lock);
4583 }
Dan Williams45b42332007-07-09 11:56:43 -07004584 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 spin_unlock_irq(&conf->device_lock);
4587
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004588 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004589 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Dan Williams45b42332007-07-09 11:56:43 -07004591 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592}
4593
NeilBrown3f294f42005-11-08 21:39:25 -08004594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004595raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004596{
NeilBrownd1688a62011-10-11 16:49:52 +11004597 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004598 if (conf)
4599 return sprintf(page, "%d\n", conf->max_nr_stripes);
4600 else
4601 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004602}
4603
NeilBrownc41d4ac2010-06-01 19:37:24 +10004604int
NeilBrownfd01b882011-10-11 16:47:53 +11004605raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004606{
NeilBrownd1688a62011-10-11 16:49:52 +11004607 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004608 int err;
4609
4610 if (size <= 16 || size > 32768)
4611 return -EINVAL;
4612 while (size < conf->max_nr_stripes) {
4613 if (drop_one_stripe(conf))
4614 conf->max_nr_stripes--;
4615 else
4616 break;
4617 }
4618 err = md_allow_write(mddev);
4619 if (err)
4620 return err;
4621 while (size > conf->max_nr_stripes) {
4622 if (grow_one_stripe(conf))
4623 conf->max_nr_stripes++;
4624 else break;
4625 }
4626 return 0;
4627}
4628EXPORT_SYMBOL(raid5_set_cache_size);
4629
NeilBrown3f294f42005-11-08 21:39:25 -08004630static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004631raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004632{
NeilBrownd1688a62011-10-11 16:49:52 +11004633 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004634 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004635 int err;
4636
NeilBrown3f294f42005-11-08 21:39:25 -08004637 if (len >= PAGE_SIZE)
4638 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004639 if (!conf)
4640 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004641
Dan Williams4ef197d82008-04-28 02:15:54 -07004642 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004643 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004644 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004645 if (err)
4646 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004647 return len;
4648}
NeilBrown007583c2005-11-08 21:39:30 -08004649
NeilBrown96de1e62005-11-08 21:39:39 -08004650static struct md_sysfs_entry
4651raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4652 raid5_show_stripe_cache_size,
4653 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004654
4655static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004656raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004657{
NeilBrownd1688a62011-10-11 16:49:52 +11004658 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004659 if (conf)
4660 return sprintf(page, "%d\n", conf->bypass_threshold);
4661 else
4662 return 0;
4663}
4664
4665static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004666raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004667{
NeilBrownd1688a62011-10-11 16:49:52 +11004668 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004669 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004670 if (len >= PAGE_SIZE)
4671 return -EINVAL;
4672 if (!conf)
4673 return -ENODEV;
4674
Dan Williams4ef197d82008-04-28 02:15:54 -07004675 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004676 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004677 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004678 return -EINVAL;
4679 conf->bypass_threshold = new;
4680 return len;
4681}
4682
4683static struct md_sysfs_entry
4684raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4685 S_IRUGO | S_IWUSR,
4686 raid5_show_preread_threshold,
4687 raid5_store_preread_threshold);
4688
4689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004690stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004691{
NeilBrownd1688a62011-10-11 16:49:52 +11004692 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004693 if (conf)
4694 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4695 else
4696 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004697}
4698
NeilBrown96de1e62005-11-08 21:39:39 -08004699static struct md_sysfs_entry
4700raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004701
NeilBrown007583c2005-11-08 21:39:30 -08004702static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004703 &raid5_stripecache_size.attr,
4704 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004705 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004706 NULL,
4707};
NeilBrown007583c2005-11-08 21:39:30 -08004708static struct attribute_group raid5_attrs_group = {
4709 .name = NULL,
4710 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004711};
4712
Dan Williams80c3a6c2009-03-17 18:10:40 -07004713static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004714raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004715{
NeilBrownd1688a62011-10-11 16:49:52 +11004716 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004717
4718 if (!sectors)
4719 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004720 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004721 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004722 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004723
Andre Noll9d8f0362009-06-18 08:45:01 +10004724 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004725 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004726 return sectors * (raid_disks - conf->max_degraded);
4727}
4728
NeilBrownd1688a62011-10-11 16:49:52 +11004729static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004730{
4731 struct raid5_percpu *percpu;
4732 unsigned long cpu;
4733
4734 if (!conf->percpu)
4735 return;
4736
4737 get_online_cpus();
4738 for_each_possible_cpu(cpu) {
4739 percpu = per_cpu_ptr(conf->percpu, cpu);
4740 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004741 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004742 }
4743#ifdef CONFIG_HOTPLUG_CPU
4744 unregister_cpu_notifier(&conf->cpu_notify);
4745#endif
4746 put_online_cpus();
4747
4748 free_percpu(conf->percpu);
4749}
4750
NeilBrownd1688a62011-10-11 16:49:52 +11004751static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004752{
4753 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004754 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004755 kfree(conf->disks);
4756 kfree(conf->stripe_hashtbl);
4757 kfree(conf);
4758}
4759
Dan Williams36d1c642009-07-14 11:48:22 -07004760#ifdef CONFIG_HOTPLUG_CPU
4761static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4762 void *hcpu)
4763{
NeilBrownd1688a62011-10-11 16:49:52 +11004764 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004765 long cpu = (long)hcpu;
4766 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4767
4768 switch (action) {
4769 case CPU_UP_PREPARE:
4770 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004771 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004772 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004773 if (!percpu->scribble)
4774 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4775
4776 if (!percpu->scribble ||
4777 (conf->level == 6 && !percpu->spare_page)) {
4778 safe_put_page(percpu->spare_page);
4779 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004780 pr_err("%s: failed memory allocation for cpu%ld\n",
4781 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004782 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004783 }
4784 break;
4785 case CPU_DEAD:
4786 case CPU_DEAD_FROZEN:
4787 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004788 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004789 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004790 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004791 break;
4792 default:
4793 break;
4794 }
4795 return NOTIFY_OK;
4796}
4797#endif
4798
NeilBrownd1688a62011-10-11 16:49:52 +11004799static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004800{
4801 unsigned long cpu;
4802 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004803 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004804 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004805 int err;
4806
Dan Williams36d1c642009-07-14 11:48:22 -07004807 allcpus = alloc_percpu(struct raid5_percpu);
4808 if (!allcpus)
4809 return -ENOMEM;
4810 conf->percpu = allcpus;
4811
4812 get_online_cpus();
4813 err = 0;
4814 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004815 if (conf->level == 6) {
4816 spare_page = alloc_page(GFP_KERNEL);
4817 if (!spare_page) {
4818 err = -ENOMEM;
4819 break;
4820 }
4821 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4822 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004823 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004824 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004825 err = -ENOMEM;
4826 break;
4827 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004828 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004829 }
4830#ifdef CONFIG_HOTPLUG_CPU
4831 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4832 conf->cpu_notify.priority = 0;
4833 if (err == 0)
4834 err = register_cpu_notifier(&conf->cpu_notify);
4835#endif
4836 put_online_cpus();
4837
4838 return err;
4839}
4840
NeilBrownd1688a62011-10-11 16:49:52 +11004841static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
NeilBrownd1688a62011-10-11 16:49:52 +11004843 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004844 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004845 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004847 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
NeilBrown91adb562009-03-31 14:39:39 +11004849 if (mddev->new_level != 5
4850 && mddev->new_level != 4
4851 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004852 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004853 mdname(mddev), mddev->new_level);
4854 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
NeilBrown91adb562009-03-31 14:39:39 +11004856 if ((mddev->new_level == 5
4857 && !algorithm_valid_raid5(mddev->new_layout)) ||
4858 (mddev->new_level == 6
4859 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004860 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004861 mdname(mddev), mddev->new_layout);
4862 return ERR_PTR(-EIO);
4863 }
4864 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004865 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004866 mdname(mddev), mddev->raid_disks);
4867 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Andre Noll664e7c42009-06-18 08:45:27 +10004870 if (!mddev->new_chunk_sectors ||
4871 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4872 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004873 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4874 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004875 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004876 }
4877
NeilBrownd1688a62011-10-11 16:49:52 +11004878 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004879 if (conf == NULL)
4880 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004881 spin_lock_init(&conf->device_lock);
4882 init_waitqueue_head(&conf->wait_for_stripe);
4883 init_waitqueue_head(&conf->wait_for_overlap);
4884 INIT_LIST_HEAD(&conf->handle_list);
4885 INIT_LIST_HEAD(&conf->hold_list);
4886 INIT_LIST_HEAD(&conf->delayed_list);
4887 INIT_LIST_HEAD(&conf->bitmap_list);
4888 INIT_LIST_HEAD(&conf->inactive_list);
4889 atomic_set(&conf->active_stripes, 0);
4890 atomic_set(&conf->preread_active_stripes, 0);
4891 atomic_set(&conf->active_aligned_reads, 0);
4892 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004893 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004894
4895 conf->raid_disks = mddev->raid_disks;
4896 if (mddev->reshape_position == MaxSector)
4897 conf->previous_raid_disks = mddev->raid_disks;
4898 else
4899 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004900 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4901 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004902
NeilBrown5e5e3e72009-10-16 16:35:30 +11004903 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004904 GFP_KERNEL);
4905 if (!conf->disks)
4906 goto abort;
4907
4908 conf->mddev = mddev;
4909
4910 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4911 goto abort;
4912
Dan Williams36d1c642009-07-14 11:48:22 -07004913 conf->level = mddev->new_level;
4914 if (raid5_alloc_percpu(conf) != 0)
4915 goto abort;
4916
NeilBrown0c55e022010-05-03 14:09:02 +10004917 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004918
NeilBrowndafb20f2012-03-19 12:46:39 +11004919 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004920 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004921 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004922 || raid_disk < 0)
4923 continue;
4924 disk = conf->disks + raid_disk;
4925
NeilBrown17045f52011-12-23 10:17:53 +11004926 if (test_bit(Replacement, &rdev->flags)) {
4927 if (disk->replacement)
4928 goto abort;
4929 disk->replacement = rdev;
4930 } else {
4931 if (disk->rdev)
4932 goto abort;
4933 disk->rdev = rdev;
4934 }
NeilBrown91adb562009-03-31 14:39:39 +11004935
4936 if (test_bit(In_sync, &rdev->flags)) {
4937 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004938 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4939 " disk %d\n",
4940 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004941 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004942 /* Cannot rely on bitmap to complete recovery */
4943 conf->fullsync = 1;
4944 }
4945
Andre Noll09c9e5f2009-06-18 08:45:55 +10004946 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004947 conf->level = mddev->new_level;
4948 if (conf->level == 6)
4949 conf->max_degraded = 2;
4950 else
4951 conf->max_degraded = 1;
4952 conf->algorithm = mddev->new_layout;
4953 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004954 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004955 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004956 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004957 conf->prev_algo = mddev->layout;
4958 }
NeilBrown91adb562009-03-31 14:39:39 +11004959
4960 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004961 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004962 if (grow_stripes(conf, conf->max_nr_stripes)) {
4963 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004964 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4965 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004966 goto abort;
4967 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004968 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4969 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004970
NeilBrown02326052012-07-03 15:56:52 +10004971 sprintf(pers_name, "raid%d", mddev->new_level);
4972 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11004973 if (!conf->thread) {
4974 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004975 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004976 mdname(mddev));
4977 goto abort;
4978 }
4979
4980 return conf;
4981
4982 abort:
4983 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004984 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004985 return ERR_PTR(-EIO);
4986 } else
4987 return ERR_PTR(-ENOMEM);
4988}
4989
NeilBrownc148ffd2009-11-13 17:47:00 +11004990
4991static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4992{
4993 switch (algo) {
4994 case ALGORITHM_PARITY_0:
4995 if (raid_disk < max_degraded)
4996 return 1;
4997 break;
4998 case ALGORITHM_PARITY_N:
4999 if (raid_disk >= raid_disks - max_degraded)
5000 return 1;
5001 break;
5002 case ALGORITHM_PARITY_0_6:
5003 if (raid_disk == 0 ||
5004 raid_disk == raid_disks - 1)
5005 return 1;
5006 break;
5007 case ALGORITHM_LEFT_ASYMMETRIC_6:
5008 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5009 case ALGORITHM_LEFT_SYMMETRIC_6:
5010 case ALGORITHM_RIGHT_SYMMETRIC_6:
5011 if (raid_disk == raid_disks - 1)
5012 return 1;
5013 }
5014 return 0;
5015}
5016
NeilBrownfd01b882011-10-11 16:47:53 +11005017static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005018{
NeilBrownd1688a62011-10-11 16:49:52 +11005019 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005020 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005021 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005022 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005023 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005024 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005025 long long min_offset_diff = 0;
5026 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005027
Andre Noll8c6ac862009-06-18 08:48:06 +10005028 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005029 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005030 " -- starting background reconstruction\n",
5031 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005032
5033 rdev_for_each(rdev, mddev) {
5034 long long diff;
5035 if (rdev->raid_disk < 0)
5036 continue;
5037 diff = (rdev->new_data_offset - rdev->data_offset);
5038 if (first) {
5039 min_offset_diff = diff;
5040 first = 0;
5041 } else if (mddev->reshape_backwards &&
5042 diff < min_offset_diff)
5043 min_offset_diff = diff;
5044 else if (!mddev->reshape_backwards &&
5045 diff > min_offset_diff)
5046 min_offset_diff = diff;
5047 }
5048
NeilBrownf6705572006-03-27 01:18:11 -08005049 if (mddev->reshape_position != MaxSector) {
5050 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005051 * Difficulties arise if the stripe we would write to
5052 * next is at or after the stripe we would read from next.
5053 * For a reshape that changes the number of devices, this
5054 * is only possible for a very short time, and mdadm makes
5055 * sure that time appears to have past before assembling
5056 * the array. So we fail if that time hasn't passed.
5057 * For a reshape that keeps the number of devices the same
5058 * mdadm must be monitoring the reshape can keeping the
5059 * critical areas read-only and backed up. It will start
5060 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005061 */
5062 sector_t here_new, here_old;
5063 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005064 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005065
NeilBrown88ce4932009-03-31 15:24:23 +11005066 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005067 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005068 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005069 mdname(mddev));
5070 return -EINVAL;
5071 }
NeilBrownf6705572006-03-27 01:18:11 -08005072 old_disks = mddev->raid_disks - mddev->delta_disks;
5073 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005074 * further up in new geometry must map after here in old
5075 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005076 */
5077 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005078 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005079 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005080 printk(KERN_ERR "md/raid:%s: reshape_position not "
5081 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005082 return -EINVAL;
5083 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005084 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005085 /* here_new is the stripe we will write to */
5086 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005087 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005088 (old_disks-max_degraded));
5089 /* here_old is the first stripe that we might need to read
5090 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005091 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005092 if ((here_new * mddev->new_chunk_sectors !=
5093 here_old * mddev->chunk_sectors)) {
5094 printk(KERN_ERR "md/raid:%s: reshape position is"
5095 " confused - aborting\n", mdname(mddev));
5096 return -EINVAL;
5097 }
NeilBrown67ac6012009-08-13 10:06:24 +10005098 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005099 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005100 * and taking constant backups.
5101 * mdadm always starts a situation like this in
5102 * readonly mode so it can take control before
5103 * allowing any writes. So just check for that.
5104 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005105 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5106 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5107 /* not really in-place - so OK */;
5108 else if (mddev->ro == 0) {
5109 printk(KERN_ERR "md/raid:%s: in-place reshape "
5110 "must be started in read-only mode "
5111 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005112 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005113 return -EINVAL;
5114 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005115 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005116 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005117 here_old * mddev->chunk_sectors)
5118 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005119 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005120 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005121 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5122 "auto-recovery - aborting.\n",
5123 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005124 return -EINVAL;
5125 }
NeilBrown0c55e022010-05-03 14:09:02 +10005126 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5127 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005128 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005129 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005130 BUG_ON(mddev->level != mddev->new_level);
5131 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005132 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005133 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005134 }
5135
NeilBrown245f46c2009-03-31 14:39:39 +11005136 if (mddev->private == NULL)
5137 conf = setup_conf(mddev);
5138 else
5139 conf = mddev->private;
5140
NeilBrown91adb562009-03-31 14:39:39 +11005141 if (IS_ERR(conf))
5142 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005143
NeilBrownb5254dd2012-05-21 09:27:01 +10005144 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005145 mddev->thread = conf->thread;
5146 conf->thread = NULL;
5147 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrown17045f52011-12-23 10:17:53 +11005149 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5150 i++) {
5151 rdev = conf->disks[i].rdev;
5152 if (!rdev && conf->disks[i].replacement) {
5153 /* The replacement is all we have yet */
5154 rdev = conf->disks[i].replacement;
5155 conf->disks[i].replacement = NULL;
5156 clear_bit(Replacement, &rdev->flags);
5157 conf->disks[i].rdev = rdev;
5158 }
5159 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005160 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005161 if (conf->disks[i].replacement &&
5162 conf->reshape_progress != MaxSector) {
5163 /* replacements and reshape simply do not mix. */
5164 printk(KERN_ERR "md: cannot handle concurrent "
5165 "replacement and reshape.\n");
5166 goto abort;
5167 }
NeilBrown2f115882010-06-17 17:41:03 +10005168 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005169 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005170 continue;
5171 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005172 /* This disc is not fully in-sync. However if it
5173 * just stored parity (beyond the recovery_offset),
5174 * when we don't need to be concerned about the
5175 * array being dirty.
5176 * When reshape goes 'backwards', we never have
5177 * partially completed devices, so we only need
5178 * to worry about reshape going forwards.
5179 */
5180 /* Hack because v0.91 doesn't store recovery_offset properly. */
5181 if (mddev->major_version == 0 &&
5182 mddev->minor_version > 90)
5183 rdev->recovery_offset = reshape_offset;
5184
NeilBrownc148ffd2009-11-13 17:47:00 +11005185 if (rdev->recovery_offset < reshape_offset) {
5186 /* We need to check old and new layout */
5187 if (!only_parity(rdev->raid_disk,
5188 conf->algorithm,
5189 conf->raid_disks,
5190 conf->max_degraded))
5191 continue;
5192 }
5193 if (!only_parity(rdev->raid_disk,
5194 conf->prev_algo,
5195 conf->previous_raid_disks,
5196 conf->max_degraded))
5197 continue;
5198 dirty_parity_disks++;
5199 }
NeilBrown91adb562009-03-31 14:39:39 +11005200
NeilBrown17045f52011-12-23 10:17:53 +11005201 /*
5202 * 0 for a fully functional array, 1 or 2 for a degraded array.
5203 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005204 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
NeilBrown674806d2010-06-16 17:17:53 +10005206 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005207 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005209 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 goto abort;
5211 }
5212
NeilBrown91adb562009-03-31 14:39:39 +11005213 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005214 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005215 mddev->resync_max_sectors = mddev->dev_sectors;
5216
NeilBrownc148ffd2009-11-13 17:47:00 +11005217 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005219 if (mddev->ok_start_degraded)
5220 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005221 "md/raid:%s: starting dirty degraded array"
5222 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005223 mdname(mddev));
5224 else {
5225 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005226 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005227 mdname(mddev));
5228 goto abort;
5229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005233 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5234 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005235 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5236 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 else
NeilBrown0c55e022010-05-03 14:09:02 +10005238 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5239 " out of %d devices, algorithm %d\n",
5240 mdname(mddev), conf->level,
5241 mddev->raid_disks - mddev->degraded,
5242 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
5244 print_raid5_conf(conf);
5245
NeilBrownfef9c612009-03-31 15:16:46 +11005246 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005247 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005248 atomic_set(&conf->reshape_stripes, 0);
5249 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5250 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5251 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5252 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5253 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005254 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005255 }
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005259 if (mddev->to_remove == &raid5_attrs_group)
5260 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005261 else if (mddev->kobj.sd &&
5262 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005263 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005264 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005265 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005266 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5267
5268 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005269 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005270 /* read-ahead size must cover two whole stripes, which
5271 * is 2 * (datadisks) * chunksize where 'n' is the
5272 * number of raid devices
5273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5275 int stripe = data_disks *
5276 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5277 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5278 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005279
5280 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005281
5282 mddev->queue->backing_dev_info.congested_data = mddev;
5283 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005284
5285 chunk_size = mddev->chunk_sectors << 9;
5286 blk_queue_io_min(mddev->queue, chunk_size);
5287 blk_queue_io_opt(mddev->queue, chunk_size *
5288 (conf->raid_disks - conf->max_degraded));
5289
NeilBrown05616be2012-05-21 09:27:00 +10005290 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005291 disk_stack_limits(mddev->gendisk, rdev->bdev,
5292 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005293 disk_stack_limits(mddev->gendisk, rdev->bdev,
5294 rdev->new_data_offset << 9);
5295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 }
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 return 0;
5299abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005300 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005301 print_raid5_conf(conf);
5302 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005304 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 return -EIO;
5306}
5307
NeilBrownfd01b882011-10-11 16:47:53 +11005308static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
NeilBrownd1688a62011-10-11 16:49:52 +11005310 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
NeilBrown01f96c02011-09-21 15:30:20 +10005312 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005313 if (mddev->queue)
5314 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005315 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005316 mddev->private = NULL;
5317 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return 0;
5319}
5320
NeilBrownfd01b882011-10-11 16:47:53 +11005321static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
NeilBrownd1688a62011-10-11 16:49:52 +11005323 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 int i;
5325
Andre Noll9d8f0362009-06-18 08:45:01 +10005326 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5327 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005328 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 for (i = 0; i < conf->raid_disks; i++)
5330 seq_printf (seq, "%s",
5331 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005332 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334}
5335
NeilBrownd1688a62011-10-11 16:49:52 +11005336static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
5338 int i;
5339 struct disk_info *tmp;
5340
NeilBrown0c55e022010-05-03 14:09:02 +10005341 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 if (!conf) {
5343 printk("(conf==NULL)\n");
5344 return;
5345 }
NeilBrown0c55e022010-05-03 14:09:02 +10005346 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5347 conf->raid_disks,
5348 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350 for (i = 0; i < conf->raid_disks; i++) {
5351 char b[BDEVNAME_SIZE];
5352 tmp = conf->disks + i;
5353 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005354 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5355 i, !test_bit(Faulty, &tmp->rdev->flags),
5356 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 }
5358}
5359
NeilBrownfd01b882011-10-11 16:47:53 +11005360static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
5362 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005363 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005365 int count = 0;
5366 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
5368 for (i = 0; i < conf->raid_disks; i++) {
5369 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005370 if (tmp->replacement
5371 && tmp->replacement->recovery_offset == MaxSector
5372 && !test_bit(Faulty, &tmp->replacement->flags)
5373 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5374 /* Replacement has just become active. */
5375 if (!tmp->rdev
5376 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5377 count++;
5378 if (tmp->rdev) {
5379 /* Replaced device not technically faulty,
5380 * but we need to be sure it gets removed
5381 * and never re-added.
5382 */
5383 set_bit(Faulty, &tmp->rdev->flags);
5384 sysfs_notify_dirent_safe(
5385 tmp->rdev->sysfs_state);
5386 }
5387 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5388 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005389 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005390 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005391 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005392 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005393 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 }
5395 }
NeilBrown6b965622010-08-18 11:56:59 +10005396 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005397 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005398 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005400 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401}
5402
NeilBrownb8321b62011-12-23 10:17:51 +11005403static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
NeilBrownd1688a62011-10-11 16:49:52 +11005405 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005407 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005408 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 struct disk_info *p = conf->disks + number;
5410
5411 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005412 if (rdev == p->rdev)
5413 rdevp = &p->rdev;
5414 else if (rdev == p->replacement)
5415 rdevp = &p->replacement;
5416 else
5417 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005418
NeilBrown657e3e42011-12-23 10:17:52 +11005419 if (number >= conf->raid_disks &&
5420 conf->reshape_progress == MaxSector)
5421 clear_bit(In_sync, &rdev->flags);
5422
5423 if (test_bit(In_sync, &rdev->flags) ||
5424 atomic_read(&rdev->nr_pending)) {
5425 err = -EBUSY;
5426 goto abort;
5427 }
5428 /* Only remove non-faulty devices if recovery
5429 * isn't possible.
5430 */
5431 if (!test_bit(Faulty, &rdev->flags) &&
5432 mddev->recovery_disabled != conf->recovery_disabled &&
5433 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005434 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005435 number < conf->raid_disks) {
5436 err = -EBUSY;
5437 goto abort;
5438 }
5439 *rdevp = NULL;
5440 synchronize_rcu();
5441 if (atomic_read(&rdev->nr_pending)) {
5442 /* lost the race, try later */
5443 err = -EBUSY;
5444 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005445 } else if (p->replacement) {
5446 /* We must have just cleared 'rdev' */
5447 p->rdev = p->replacement;
5448 clear_bit(Replacement, &p->replacement->flags);
5449 smp_mb(); /* Make sure other CPUs may see both as identical
5450 * but will never see neither - if they are careful
5451 */
5452 p->replacement = NULL;
5453 clear_bit(WantReplacement, &rdev->flags);
5454 } else
5455 /* We might have just removed the Replacement as faulty-
5456 * clear the bit just in case
5457 */
5458 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459abort:
5460
5461 print_raid5_conf(conf);
5462 return err;
5463}
5464
NeilBrownfd01b882011-10-11 16:47:53 +11005465static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466{
NeilBrownd1688a62011-10-11 16:49:52 +11005467 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005468 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 int disk;
5470 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005471 int first = 0;
5472 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
NeilBrown7f0da592011-07-28 11:39:22 +10005474 if (mddev->recovery_disabled == conf->recovery_disabled)
5475 return -EBUSY;
5476
NeilBrowndc10c642012-03-19 12:46:37 +11005477 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Neil Brown6c2fce22008-06-28 08:31:31 +10005481 if (rdev->raid_disk >= 0)
5482 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005485 * find the disk ... but prefer rdev->saved_raid_disk
5486 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005488 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005489 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005490 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005491 first = rdev->saved_raid_disk;
5492
5493 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005494 p = conf->disks + disk;
5495 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005496 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005498 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005499 if (rdev->saved_raid_disk != disk)
5500 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005501 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005504 }
5505 for (disk = first; disk <= last; disk++) {
5506 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005507 if (test_bit(WantReplacement, &p->rdev->flags) &&
5508 p->replacement == NULL) {
5509 clear_bit(In_sync, &rdev->flags);
5510 set_bit(Replacement, &rdev->flags);
5511 rdev->raid_disk = disk;
5512 err = 0;
5513 conf->fullsync = 1;
5514 rcu_assign_pointer(p->replacement, rdev);
5515 break;
5516 }
5517 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005518out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005520 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
NeilBrownfd01b882011-10-11 16:47:53 +11005523static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
5525 /* no resync is happening, and there is enough space
5526 * on all devices, so we can resize.
5527 * We need to make sure resync covers any new space.
5528 * If the array is shrinking we should possibly wait until
5529 * any io in the removed space completes, but it hardly seems
5530 * worth it.
5531 */
NeilBrowna4a61252012-05-22 13:55:27 +10005532 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005533 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005534 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5535 if (mddev->external_size &&
5536 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005537 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005538 if (mddev->bitmap) {
5539 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5540 if (ret)
5541 return ret;
5542 }
5543 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005544 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005545 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005546 if (sectors > mddev->dev_sectors &&
5547 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005548 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5550 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005551 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005552 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 return 0;
5554}
5555
NeilBrownfd01b882011-10-11 16:47:53 +11005556static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005557{
5558 /* Can only proceed if there are plenty of stripe_heads.
5559 * We need a minimum of one full stripe,, and for sensible progress
5560 * it is best to have about 4 times that.
5561 * If we require 4 times, then the default 256 4K stripe_heads will
5562 * allow for chunk sizes up to 256K, which is probably OK.
5563 * If the chunk size is greater, user-space should request more
5564 * stripe_heads first.
5565 */
NeilBrownd1688a62011-10-11 16:49:52 +11005566 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005567 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5568 > conf->max_nr_stripes ||
5569 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5570 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005571 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5572 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005573 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5574 / STRIPE_SIZE)*4);
5575 return 0;
5576 }
5577 return 1;
5578}
5579
NeilBrownfd01b882011-10-11 16:47:53 +11005580static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005581{
NeilBrownd1688a62011-10-11 16:49:52 +11005582 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005583
NeilBrown88ce4932009-03-31 15:24:23 +11005584 if (mddev->delta_disks == 0 &&
5585 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005586 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005587 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005588 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005589 return -EINVAL;
5590 if (mddev->delta_disks < 0) {
5591 /* We might be able to shrink, but the devices must
5592 * be made bigger first.
5593 * For raid6, 4 is the minimum size.
5594 * Otherwise 2 is the minimum
5595 */
5596 int min = 2;
5597 if (mddev->level == 6)
5598 min = 4;
5599 if (mddev->raid_disks + mddev->delta_disks < min)
5600 return -EINVAL;
5601 }
NeilBrown29269552006-03-27 01:18:10 -08005602
NeilBrown01ee22b2009-06-18 08:47:20 +10005603 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005604 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005605
NeilBrownec32a2b2009-03-31 15:17:38 +11005606 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005607}
5608
NeilBrownfd01b882011-10-11 16:47:53 +11005609static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005610{
NeilBrownd1688a62011-10-11 16:49:52 +11005611 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005612 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005613 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005614 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005615
NeilBrownf4168852007-02-28 20:11:53 -08005616 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005617 return -EBUSY;
5618
NeilBrown01ee22b2009-06-18 08:47:20 +10005619 if (!check_stripe_cache(mddev))
5620 return -ENOSPC;
5621
NeilBrown30b67642012-05-22 13:55:28 +10005622 if (has_failed(conf))
5623 return -EINVAL;
5624
NeilBrownc6563a82012-05-21 09:27:00 +10005625 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005626 if (!test_bit(In_sync, &rdev->flags)
5627 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005628 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005629 }
NeilBrown63c70c42006-03-27 01:18:13 -08005630
NeilBrownf4168852007-02-28 20:11:53 -08005631 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005632 /* Not enough devices even to make a degraded array
5633 * of that size
5634 */
5635 return -EINVAL;
5636
NeilBrownec32a2b2009-03-31 15:17:38 +11005637 /* Refuse to reduce size of the array. Any reductions in
5638 * array size must be through explicit setting of array_size
5639 * attribute.
5640 */
5641 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5642 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005643 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005644 "before number of disks\n", mdname(mddev));
5645 return -EINVAL;
5646 }
5647
NeilBrownf6705572006-03-27 01:18:11 -08005648 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005649 spin_lock_irq(&conf->device_lock);
5650 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005651 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005652 conf->prev_chunk_sectors = conf->chunk_sectors;
5653 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005654 conf->prev_algo = conf->algorithm;
5655 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005656 conf->generation++;
5657 /* Code that selects data_offset needs to see the generation update
5658 * if reshape_progress has been set - so a memory barrier needed.
5659 */
5660 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005661 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005662 conf->reshape_progress = raid5_size(mddev, 0, 0);
5663 else
5664 conf->reshape_progress = 0;
5665 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005666 spin_unlock_irq(&conf->device_lock);
5667
5668 /* Add some new drives, as many as will fit.
5669 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005670 * Don't add devices if we are reducing the number of
5671 * devices in the array. This is because it is not possible
5672 * to correctly record the "partially reconstructed" state of
5673 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005674 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005675 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005676 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005677 if (rdev->raid_disk < 0 &&
5678 !test_bit(Faulty, &rdev->flags)) {
5679 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005680 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005681 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005682 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005683 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005684 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005685
5686 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005687 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005688 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005689 } else if (rdev->raid_disk >= conf->previous_raid_disks
5690 && !test_bit(Faulty, &rdev->flags)) {
5691 /* This is a spare that was manually added */
5692 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005693 }
NeilBrown29269552006-03-27 01:18:10 -08005694
NeilBrown87a8dec2011-01-31 11:57:43 +11005695 /* When a reshape changes the number of devices,
5696 * ->degraded is measured against the larger of the
5697 * pre and post number of devices.
5698 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005699 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005700 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005701 spin_unlock_irqrestore(&conf->device_lock, flags);
5702 }
NeilBrown63c70c42006-03-27 01:18:13 -08005703 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005704 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005705 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005706
NeilBrown29269552006-03-27 01:18:10 -08005707 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5708 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5709 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5710 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5711 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005712 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005713 if (!mddev->sync_thread) {
5714 mddev->recovery = 0;
5715 spin_lock_irq(&conf->device_lock);
5716 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005717 rdev_for_each(rdev, mddev)
5718 rdev->new_data_offset = rdev->data_offset;
5719 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005720 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005721 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005722 spin_unlock_irq(&conf->device_lock);
5723 return -EAGAIN;
5724 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005725 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005726 md_wakeup_thread(mddev->sync_thread);
5727 md_new_event(mddev);
5728 return 0;
5729}
NeilBrown29269552006-03-27 01:18:10 -08005730
NeilBrownec32a2b2009-03-31 15:17:38 +11005731/* This is called from the reshape thread and should make any
5732 * changes needed in 'conf'
5733 */
NeilBrownd1688a62011-10-11 16:49:52 +11005734static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005735{
NeilBrown29269552006-03-27 01:18:10 -08005736
NeilBrownf6705572006-03-27 01:18:11 -08005737 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005738 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005739
NeilBrownf6705572006-03-27 01:18:11 -08005740 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005741 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005742 rdev_for_each(rdev, conf->mddev)
5743 rdev->data_offset = rdev->new_data_offset;
5744 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005745 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005746 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005747 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005748
5749 /* read-ahead size must cover two whole stripes, which is
5750 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5751 */
NeilBrown4a5add42010-06-01 19:37:28 +10005752 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005753 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005754 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005755 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005756 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5757 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5758 }
NeilBrown29269552006-03-27 01:18:10 -08005759 }
NeilBrown29269552006-03-27 01:18:10 -08005760}
5761
NeilBrownec32a2b2009-03-31 15:17:38 +11005762/* This is called from the raid5d thread with mddev_lock held.
5763 * It makes config changes to the device.
5764 */
NeilBrownfd01b882011-10-11 16:47:53 +11005765static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005766{
NeilBrownd1688a62011-10-11 16:49:52 +11005767 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005768
5769 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5770
NeilBrownec32a2b2009-03-31 15:17:38 +11005771 if (mddev->delta_disks > 0) {
5772 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5773 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005774 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005775 } else {
5776 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005777 spin_lock_irq(&conf->device_lock);
5778 mddev->degraded = calc_degraded(conf);
5779 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005780 for (d = conf->raid_disks ;
5781 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005782 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005783 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005784 if (rdev)
5785 clear_bit(In_sync, &rdev->flags);
5786 rdev = conf->disks[d].replacement;
5787 if (rdev)
5788 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005789 }
NeilBrowncea9c222009-03-31 15:15:05 +11005790 }
NeilBrown88ce4932009-03-31 15:24:23 +11005791 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005792 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005793 mddev->reshape_position = MaxSector;
5794 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005795 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005796 }
5797}
5798
NeilBrownfd01b882011-10-11 16:47:53 +11005799static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005800{
NeilBrownd1688a62011-10-11 16:49:52 +11005801 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005802
5803 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005804 case 2: /* resume for a suspend */
5805 wake_up(&conf->wait_for_overlap);
5806 break;
5807
NeilBrown72626682005-09-09 16:23:54 -07005808 case 1: /* stop all writes */
5809 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005810 /* '2' tells resync/reshape to pause so that all
5811 * active stripes can drain
5812 */
5813 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005814 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005815 atomic_read(&conf->active_stripes) == 0 &&
5816 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005817 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005818 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005819 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005820 /* allow reshape to continue */
5821 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005822 break;
5823
5824 case 0: /* re-enable writes */
5825 spin_lock_irq(&conf->device_lock);
5826 conf->quiesce = 0;
5827 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005828 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005829 spin_unlock_irq(&conf->device_lock);
5830 break;
5831 }
NeilBrown72626682005-09-09 16:23:54 -07005832}
NeilBrownb15c2e52006-01-06 00:20:16 -08005833
NeilBrownd562b0c2009-03-31 14:39:39 +11005834
NeilBrownfd01b882011-10-11 16:47:53 +11005835static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005836{
NeilBrowne373ab12011-10-11 16:48:59 +11005837 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005838 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005839
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005840 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005841 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005842 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5843 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005844 return ERR_PTR(-EINVAL);
5845 }
5846
NeilBrowne373ab12011-10-11 16:48:59 +11005847 sectors = raid0_conf->strip_zone[0].zone_end;
5848 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005849 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005850 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005851 mddev->new_layout = ALGORITHM_PARITY_N;
5852 mddev->new_chunk_sectors = mddev->chunk_sectors;
5853 mddev->raid_disks += 1;
5854 mddev->delta_disks = 1;
5855 /* make sure it will be not marked as dirty */
5856 mddev->recovery_cp = MaxSector;
5857
5858 return setup_conf(mddev);
5859}
5860
5861
NeilBrownfd01b882011-10-11 16:47:53 +11005862static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005863{
5864 int chunksect;
5865
5866 if (mddev->raid_disks != 2 ||
5867 mddev->degraded > 1)
5868 return ERR_PTR(-EINVAL);
5869
5870 /* Should check if there are write-behind devices? */
5871
5872 chunksect = 64*2; /* 64K by default */
5873
5874 /* The array must be an exact multiple of chunksize */
5875 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5876 chunksect >>= 1;
5877
5878 if ((chunksect<<9) < STRIPE_SIZE)
5879 /* array size does not allow a suitable chunk size */
5880 return ERR_PTR(-EINVAL);
5881
5882 mddev->new_level = 5;
5883 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005884 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005885
5886 return setup_conf(mddev);
5887}
5888
NeilBrownfd01b882011-10-11 16:47:53 +11005889static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005890{
5891 int new_layout;
5892
5893 switch (mddev->layout) {
5894 case ALGORITHM_LEFT_ASYMMETRIC_6:
5895 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5896 break;
5897 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5898 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5899 break;
5900 case ALGORITHM_LEFT_SYMMETRIC_6:
5901 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5902 break;
5903 case ALGORITHM_RIGHT_SYMMETRIC_6:
5904 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5905 break;
5906 case ALGORITHM_PARITY_0_6:
5907 new_layout = ALGORITHM_PARITY_0;
5908 break;
5909 case ALGORITHM_PARITY_N:
5910 new_layout = ALGORITHM_PARITY_N;
5911 break;
5912 default:
5913 return ERR_PTR(-EINVAL);
5914 }
5915 mddev->new_level = 5;
5916 mddev->new_layout = new_layout;
5917 mddev->delta_disks = -1;
5918 mddev->raid_disks -= 1;
5919 return setup_conf(mddev);
5920}
5921
NeilBrownd562b0c2009-03-31 14:39:39 +11005922
NeilBrownfd01b882011-10-11 16:47:53 +11005923static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005924{
NeilBrown88ce4932009-03-31 15:24:23 +11005925 /* For a 2-drive array, the layout and chunk size can be changed
5926 * immediately as not restriping is needed.
5927 * For larger arrays we record the new value - after validation
5928 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005929 */
NeilBrownd1688a62011-10-11 16:49:52 +11005930 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005931 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005932
NeilBrown597a7112009-06-18 08:47:42 +10005933 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005934 return -EINVAL;
5935 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005936 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005937 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005938 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005939 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005940 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005941 /* not factor of array size */
5942 return -EINVAL;
5943 }
5944
5945 /* They look valid */
5946
NeilBrown88ce4932009-03-31 15:24:23 +11005947 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005948 /* can make the change immediately */
5949 if (mddev->new_layout >= 0) {
5950 conf->algorithm = mddev->new_layout;
5951 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005952 }
5953 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005954 conf->chunk_sectors = new_chunk ;
5955 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005956 }
5957 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5958 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005959 }
NeilBrown50ac1682009-06-18 08:47:55 +10005960 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005961}
5962
NeilBrownfd01b882011-10-11 16:47:53 +11005963static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005964{
NeilBrown597a7112009-06-18 08:47:42 +10005965 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005966
NeilBrown597a7112009-06-18 08:47:42 +10005967 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005968 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005969 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005970 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005971 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005972 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005973 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005974 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005975 /* not factor of array size */
5976 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005977 }
NeilBrown88ce4932009-03-31 15:24:23 +11005978
5979 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005980 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005981}
5982
NeilBrownfd01b882011-10-11 16:47:53 +11005983static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005984{
5985 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005986 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005987 * raid1 - if there are two drives. We need to know the chunk size
5988 * raid4 - trivial - just use a raid4 layout.
5989 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005990 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005991 if (mddev->level == 0)
5992 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005993 if (mddev->level == 1)
5994 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005995 if (mddev->level == 4) {
5996 mddev->new_layout = ALGORITHM_PARITY_N;
5997 mddev->new_level = 5;
5998 return setup_conf(mddev);
5999 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006000 if (mddev->level == 6)
6001 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006002
6003 return ERR_PTR(-EINVAL);
6004}
6005
NeilBrownfd01b882011-10-11 16:47:53 +11006006static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006007{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006008 /* raid4 can take over:
6009 * raid0 - if there is only one strip zone
6010 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006011 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006012 if (mddev->level == 0)
6013 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006014 if (mddev->level == 5 &&
6015 mddev->layout == ALGORITHM_PARITY_N) {
6016 mddev->new_layout = 0;
6017 mddev->new_level = 4;
6018 return setup_conf(mddev);
6019 }
6020 return ERR_PTR(-EINVAL);
6021}
NeilBrownd562b0c2009-03-31 14:39:39 +11006022
NeilBrown84fc4b52011-10-11 16:49:58 +11006023static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006024
NeilBrownfd01b882011-10-11 16:47:53 +11006025static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006026{
6027 /* Currently can only take over a raid5. We map the
6028 * personality to an equivalent raid6 personality
6029 * with the Q block at the end.
6030 */
6031 int new_layout;
6032
6033 if (mddev->pers != &raid5_personality)
6034 return ERR_PTR(-EINVAL);
6035 if (mddev->degraded > 1)
6036 return ERR_PTR(-EINVAL);
6037 if (mddev->raid_disks > 253)
6038 return ERR_PTR(-EINVAL);
6039 if (mddev->raid_disks < 3)
6040 return ERR_PTR(-EINVAL);
6041
6042 switch (mddev->layout) {
6043 case ALGORITHM_LEFT_ASYMMETRIC:
6044 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6045 break;
6046 case ALGORITHM_RIGHT_ASYMMETRIC:
6047 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6048 break;
6049 case ALGORITHM_LEFT_SYMMETRIC:
6050 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6051 break;
6052 case ALGORITHM_RIGHT_SYMMETRIC:
6053 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6054 break;
6055 case ALGORITHM_PARITY_0:
6056 new_layout = ALGORITHM_PARITY_0_6;
6057 break;
6058 case ALGORITHM_PARITY_N:
6059 new_layout = ALGORITHM_PARITY_N;
6060 break;
6061 default:
6062 return ERR_PTR(-EINVAL);
6063 }
6064 mddev->new_level = 6;
6065 mddev->new_layout = new_layout;
6066 mddev->delta_disks = 1;
6067 mddev->raid_disks += 1;
6068 return setup_conf(mddev);
6069}
6070
6071
NeilBrown84fc4b52011-10-11 16:49:58 +11006072static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006073{
6074 .name = "raid6",
6075 .level = 6,
6076 .owner = THIS_MODULE,
6077 .make_request = make_request,
6078 .run = run,
6079 .stop = stop,
6080 .status = status,
6081 .error_handler = error,
6082 .hot_add_disk = raid5_add_disk,
6083 .hot_remove_disk= raid5_remove_disk,
6084 .spare_active = raid5_spare_active,
6085 .sync_request = sync_request,
6086 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006087 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006088 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006089 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006090 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006091 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006092 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006093};
NeilBrown84fc4b52011-10-11 16:49:58 +11006094static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095{
6096 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006097 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 .owner = THIS_MODULE,
6099 .make_request = make_request,
6100 .run = run,
6101 .stop = stop,
6102 .status = status,
6103 .error_handler = error,
6104 .hot_add_disk = raid5_add_disk,
6105 .hot_remove_disk= raid5_remove_disk,
6106 .spare_active = raid5_spare_active,
6107 .sync_request = sync_request,
6108 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006109 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006110 .check_reshape = raid5_check_reshape,
6111 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006112 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006113 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006114 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115};
6116
NeilBrown84fc4b52011-10-11 16:49:58 +11006117static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
NeilBrown2604b702006-01-06 00:20:36 -08006119 .name = "raid4",
6120 .level = 4,
6121 .owner = THIS_MODULE,
6122 .make_request = make_request,
6123 .run = run,
6124 .stop = stop,
6125 .status = status,
6126 .error_handler = error,
6127 .hot_add_disk = raid5_add_disk,
6128 .hot_remove_disk= raid5_remove_disk,
6129 .spare_active = raid5_spare_active,
6130 .sync_request = sync_request,
6131 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006132 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006133 .check_reshape = raid5_check_reshape,
6134 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006135 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006136 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006137 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006138};
6139
6140static int __init raid5_init(void)
6141{
NeilBrown16a53ec2006-06-26 00:27:38 -07006142 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006143 register_md_personality(&raid5_personality);
6144 register_md_personality(&raid4_personality);
6145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146}
6147
NeilBrown2604b702006-01-06 00:20:36 -08006148static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149{
NeilBrown16a53ec2006-06-26 00:27:38 -07006150 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006151 unregister_md_personality(&raid5_personality);
6152 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153}
6154
6155module_init(raid5_init);
6156module_exit(raid5_exit);
6157MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006158MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006160MODULE_ALIAS("md-raid5");
6161MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006162MODULE_ALIAS("md-level-5");
6163MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006164MODULE_ALIAS("md-personality-8"); /* RAID6 */
6165MODULE_ALIAS("md-raid6");
6166MODULE_ALIAS("md-level-6");
6167
6168/* This used to be two separate modules, they were: */
6169MODULE_ALIAS("raid5");
6170MODULE_ALIAS("raid6");