blob: 2d2aaf6e98e2608c302cbcc025a5c6a8427699ab [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)) {
NeilBrown482c0832011-04-18 18:25:42 +1000199 if (test_bit(STRIPE_DELAYED, &sh->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown482c0832011-04-18 18:25:42 +1000201 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
202 sh->bm_seq - conf->seq_write > 0)
NeilBrown72626682005-09-09 16:23:54 -0700203 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown482c0832011-04-18 18:25:42 +1000204 else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
NeilBrownd1688a62011-10-11 16:49:52 +1100229 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrownd1688a62011-10-11 16:49:52 +1100245static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100257static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
NeilBrowne4e11e32010-06-16 16:45:16 +1000273static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct page *p;
276 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000277 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
NeilBrowne4e11e32010-06-16 16:45:16 +1000279 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
NeilBrowne4e11e32010-06-16 16:45:16 +1000288static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100305static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
NeilBrownd1688a62011-10-11 16:49:52 +1100310 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000338 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrownd1688a62011-10-11 16:49:52 +1100346static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
NeilBrown674806d2010-06-16 17:17:53 +1000360/*
361 * Need to check if array has failed when deciding whether to:
362 * - start an array
363 * - remove non-faulty devices
364 * - add a spare
365 * - allow a reshape
366 * This determination is simple when no reshape is happening.
367 * However if there is a reshape, we need to carefully check
368 * both the before and after sections.
369 * This is because some failed devices may only affect one
370 * of the two sections, and some non-in_sync devices may
371 * be insync in the section most affected by failed devices.
372 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100373static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000374{
NeilBrown908f4fb2011-12-23 10:17:50 +1100375 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000376 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000377
378 rcu_read_lock();
379 degraded = 0;
380 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100381 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000382 if (!rdev || test_bit(Faulty, &rdev->flags))
383 degraded++;
384 else if (test_bit(In_sync, &rdev->flags))
385 ;
386 else
387 /* not in-sync or faulty.
388 * If the reshape increases the number of devices,
389 * this is being recovered by the reshape, so
390 * this 'previous' section is not in_sync.
391 * If the number of devices is being reduced however,
392 * the device can only be part of the array if
393 * we are reverting a reshape, so this section will
394 * be in-sync.
395 */
396 if (conf->raid_disks >= conf->previous_raid_disks)
397 degraded++;
398 }
399 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100400 if (conf->raid_disks == conf->previous_raid_disks)
401 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000402 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100403 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000404 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100405 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000406 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100407 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000408 else if (test_bit(In_sync, &rdev->flags))
409 ;
410 else
411 /* not in-sync or faulty.
412 * If reshape increases the number of devices, this
413 * section has already been recovered, else it
414 * almost certainly hasn't.
415 */
416 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (degraded2 > degraded)
421 return degraded2;
422 return degraded;
423}
424
425static int has_failed(struct r5conf *conf)
426{
427 int degraded;
428
429 if (conf->mddev->reshape_position == MaxSector)
430 return conf->mddev->degraded > conf->max_degraded;
431
432 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000433 if (degraded > conf->max_degraded)
434 return 1;
435 return 0;
436}
437
NeilBrownb5663ba2009-03-31 14:39:38 +1100438static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100439get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000440 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct stripe_head *sh;
443
Dan Williams45b42332007-07-09 11:56:43 -0700444 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 spin_lock_irq(&conf->device_lock);
447
448 do {
NeilBrown72626682005-09-09 16:23:54 -0700449 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000450 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700451 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100452 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!sh) {
454 if (!conf->inactive_blocked)
455 sh = get_free_stripe(conf);
456 if (noblock && sh == NULL)
457 break;
458 if (!sh) {
459 conf->inactive_blocked = 1;
460 wait_event_lock_irq(conf->wait_for_stripe,
461 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800462 (atomic_read(&conf->active_stripes)
463 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 || !conf->inactive_blocked),
465 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000466 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 conf->inactive_blocked = 0;
468 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100469 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100472 BUG_ON(!list_empty(&sh->lru)
473 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else {
475 if (!test_bit(STRIPE_HANDLE, &sh->state))
476 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700477 if (list_empty(&sh->lru) &&
478 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700479 BUG();
480 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
483 } while (sh == NULL);
484
485 if (sh)
486 atomic_inc(&sh->count);
487
488 spin_unlock_irq(&conf->device_lock);
489 return sh;
490}
491
NeilBrown6712ecf2007-09-27 12:47:43 +0200492static void
493raid5_end_read_request(struct bio *bi, int error);
494static void
495raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700496
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000497static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700498{
NeilBrownd1688a62011-10-11 16:49:52 +1100499 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700500 int i, disks = sh->disks;
501
502 might_sleep();
503
504 for (i = disks; i--; ) {
505 int rw;
506 struct bio *bi;
NeilBrown3cb03002011-10-11 16:45:26 +1100507 struct md_rdev *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200508 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
509 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
510 rw = WRITE_FUA;
511 else
512 rw = WRITE;
513 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700514 rw = READ;
515 else
516 continue;
517
518 bi = &sh->dev[i].req;
519
520 bi->bi_rw = rw;
Namhyung Kimb0629622011-06-14 14:20:19 +1000521 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700522 bi->bi_end_io = raid5_end_write_request;
523 else
524 bi->bi_end_io = raid5_end_read_request;
525
526 rcu_read_lock();
527 rdev = rcu_dereference(conf->disks[i].rdev);
528 if (rdev && test_bit(Faulty, &rdev->flags))
529 rdev = NULL;
530 if (rdev)
531 atomic_inc(&rdev->nr_pending);
532 rcu_read_unlock();
533
NeilBrown73e92e52011-07-28 11:39:22 +1000534 /* We have already checked bad blocks for reads. Now
535 * need to check for writes.
536 */
537 while ((rw & WRITE) && rdev &&
538 test_bit(WriteErrorSeen, &rdev->flags)) {
539 sector_t first_bad;
540 int bad_sectors;
541 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
542 &first_bad, &bad_sectors);
543 if (!bad)
544 break;
545
546 if (bad < 0) {
547 set_bit(BlockedBadBlocks, &rdev->flags);
548 if (!conf->mddev->external &&
549 conf->mddev->flags) {
550 /* It is very unlikely, but we might
551 * still need to write out the
552 * bad block log - better give it
553 * a chance*/
554 md_check_recovery(conf->mddev);
555 }
556 md_wait_for_blocked_rdev(rdev, conf->mddev);
557 } else {
558 /* Acknowledged bad block - skip the write */
559 rdev_dec_pending(rdev, conf->mddev);
560 rdev = NULL;
561 }
562 }
563
Dan Williams91c00922007-01-02 13:52:30 -0700564 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000565 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700566 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
567
Dan Williams2b7497f2008-06-28 08:31:52 +1000568 set_bit(STRIPE_IO_STARTED, &sh->state);
569
Dan Williams91c00922007-01-02 13:52:30 -0700570 bi->bi_bdev = rdev->bdev;
571 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700572 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700573 bi->bi_rw, i);
574 atomic_inc(&sh->count);
575 bi->bi_sector = sh->sector + rdev->data_offset;
576 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700577 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700578 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
579 bi->bi_io_vec[0].bv_offset = 0;
580 bi->bi_size = STRIPE_SIZE;
581 bi->bi_next = NULL;
Dan Williams91c00922007-01-02 13:52:30 -0700582 generic_make_request(bi);
583 } else {
Namhyung Kimb0629622011-06-14 14:20:19 +1000584 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700585 set_bit(STRIPE_DEGRADED, &sh->state);
586 pr_debug("skip op %ld on disc %d for sector %llu\n",
587 bi->bi_rw, i, (unsigned long long)sh->sector);
588 clear_bit(R5_LOCKED, &sh->dev[i].flags);
589 set_bit(STRIPE_HANDLE, &sh->state);
590 }
591 }
592}
593
594static struct dma_async_tx_descriptor *
595async_copy_data(int frombio, struct bio *bio, struct page *page,
596 sector_t sector, struct dma_async_tx_descriptor *tx)
597{
598 struct bio_vec *bvl;
599 struct page *bio_page;
600 int i;
601 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700602 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700603 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700604
605 if (bio->bi_sector >= sector)
606 page_offset = (signed)(bio->bi_sector - sector) * 512;
607 else
608 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700609
Dan Williams0403e382009-09-08 17:42:50 -0700610 if (frombio)
611 flags |= ASYNC_TX_FENCE;
612 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
613
Dan Williams91c00922007-01-02 13:52:30 -0700614 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000615 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700616 int clen;
617 int b_offset = 0;
618
619 if (page_offset < 0) {
620 b_offset = -page_offset;
621 page_offset += b_offset;
622 len -= b_offset;
623 }
624
625 if (len > 0 && page_offset + len > STRIPE_SIZE)
626 clen = STRIPE_SIZE - page_offset;
627 else
628 clen = len;
629
630 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000631 b_offset += bvl->bv_offset;
632 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700633 if (frombio)
634 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700635 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700636 else
637 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700638 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700639 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700640 /* chain the operations */
641 submit.depend_tx = tx;
642
Dan Williams91c00922007-01-02 13:52:30 -0700643 if (clen < len) /* hit end of page */
644 break;
645 page_offset += len;
646 }
647
648 return tx;
649}
650
651static void ops_complete_biofill(void *stripe_head_ref)
652{
653 struct stripe_head *sh = stripe_head_ref;
654 struct bio *return_bi = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100655 struct r5conf *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700656 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700657
Harvey Harrisone46b2722008-04-28 02:15:50 -0700658 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700659 (unsigned long long)sh->sector);
660
661 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000662 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700663 for (i = sh->disks; i--; ) {
664 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700665
666 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700667 /* and check if we need to reply to a read request,
668 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000669 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700670 */
671 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700672 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700673
Dan Williams91c00922007-01-02 13:52:30 -0700674 BUG_ON(!dev->read);
675 rbi = dev->read;
676 dev->read = NULL;
677 while (rbi && rbi->bi_sector <
678 dev->sector + STRIPE_SECTORS) {
679 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200680 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700681 rbi->bi_next = return_bi;
682 return_bi = rbi;
683 }
Dan Williams91c00922007-01-02 13:52:30 -0700684 rbi = rbi2;
685 }
686 }
687 }
Dan Williams83de75c2008-06-28 08:31:58 +1000688 spin_unlock_irq(&conf->device_lock);
689 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700690
691 return_io(return_bi);
692
Dan Williamse4d84902007-09-24 10:06:13 -0700693 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700694 release_stripe(sh);
695}
696
697static void ops_run_biofill(struct stripe_head *sh)
698{
699 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +1100700 struct r5conf *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700701 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700702 int i;
703
Harvey Harrisone46b2722008-04-28 02:15:50 -0700704 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700705 (unsigned long long)sh->sector);
706
707 for (i = sh->disks; i--; ) {
708 struct r5dev *dev = &sh->dev[i];
709 if (test_bit(R5_Wantfill, &dev->flags)) {
710 struct bio *rbi;
711 spin_lock_irq(&conf->device_lock);
712 dev->read = rbi = dev->toread;
713 dev->toread = NULL;
714 spin_unlock_irq(&conf->device_lock);
715 while (rbi && rbi->bi_sector <
716 dev->sector + STRIPE_SECTORS) {
717 tx = async_copy_data(0, rbi, dev->page,
718 dev->sector, tx);
719 rbi = r5_next_bio(rbi, dev->sector);
720 }
721 }
722 }
723
724 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700725 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
726 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700727}
728
Dan Williams4e7d2c02009-08-29 19:13:11 -0700729static void mark_target_uptodate(struct stripe_head *sh, int target)
730{
731 struct r5dev *tgt;
732
733 if (target < 0)
734 return;
735
736 tgt = &sh->dev[target];
737 set_bit(R5_UPTODATE, &tgt->flags);
738 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
739 clear_bit(R5_Wantcompute, &tgt->flags);
740}
741
Dan Williamsac6b53b2009-07-14 13:40:19 -0700742static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700743{
744 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700745
Harvey Harrisone46b2722008-04-28 02:15:50 -0700746 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700747 (unsigned long long)sh->sector);
748
Dan Williamsac6b53b2009-07-14 13:40:19 -0700749 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700750 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700751 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700752
Dan Williamsecc65c92008-06-28 08:31:57 +1000753 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
754 if (sh->check_state == check_state_compute_run)
755 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700756 set_bit(STRIPE_HANDLE, &sh->state);
757 release_stripe(sh);
758}
759
Dan Williamsd6f38f32009-07-14 11:50:52 -0700760/* return a pointer to the address conversion region of the scribble buffer */
761static addr_conv_t *to_addr_conv(struct stripe_head *sh,
762 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700763{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700764 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
765}
766
767static struct dma_async_tx_descriptor *
768ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
769{
Dan Williams91c00922007-01-02 13:52:30 -0700770 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700771 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700772 int target = sh->ops.target;
773 struct r5dev *tgt = &sh->dev[target];
774 struct page *xor_dest = tgt->page;
775 int count = 0;
776 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700777 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700778 int i;
779
780 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700781 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700782 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
783
784 for (i = disks; i--; )
785 if (i != target)
786 xor_srcs[count++] = sh->dev[i].page;
787
788 atomic_inc(&sh->count);
789
Dan Williams0403e382009-09-08 17:42:50 -0700790 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700791 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700792 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700793 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700794 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700795 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700796
Dan Williams91c00922007-01-02 13:52:30 -0700797 return tx;
798}
799
Dan Williamsac6b53b2009-07-14 13:40:19 -0700800/* set_syndrome_sources - populate source buffers for gen_syndrome
801 * @srcs - (struct page *) array of size sh->disks
802 * @sh - stripe_head to parse
803 *
804 * Populates srcs in proper layout order for the stripe and returns the
805 * 'count' of sources to be used in a call to async_gen_syndrome. The P
806 * destination buffer is recorded in srcs[count] and the Q destination
807 * is recorded in srcs[count+1]].
808 */
809static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
810{
811 int disks = sh->disks;
812 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
813 int d0_idx = raid6_d0(sh);
814 int count;
815 int i;
816
817 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100818 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700819
820 count = 0;
821 i = d0_idx;
822 do {
823 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
824
825 srcs[slot] = sh->dev[i].page;
826 i = raid6_next_disk(i, disks);
827 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700828
NeilBrowne4424fe2009-10-16 16:27:34 +1100829 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700830}
831
832static struct dma_async_tx_descriptor *
833ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
834{
835 int disks = sh->disks;
836 struct page **blocks = percpu->scribble;
837 int target;
838 int qd_idx = sh->qd_idx;
839 struct dma_async_tx_descriptor *tx;
840 struct async_submit_ctl submit;
841 struct r5dev *tgt;
842 struct page *dest;
843 int i;
844 int count;
845
846 if (sh->ops.target < 0)
847 target = sh->ops.target2;
848 else if (sh->ops.target2 < 0)
849 target = sh->ops.target;
850 else
851 /* we should only have one valid target */
852 BUG();
853 BUG_ON(target < 0);
854 pr_debug("%s: stripe %llu block: %d\n",
855 __func__, (unsigned long long)sh->sector, target);
856
857 tgt = &sh->dev[target];
858 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
859 dest = tgt->page;
860
861 atomic_inc(&sh->count);
862
863 if (target == qd_idx) {
864 count = set_syndrome_sources(blocks, sh);
865 blocks[count] = NULL; /* regenerating p is not necessary */
866 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700867 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
868 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700869 to_addr_conv(sh, percpu));
870 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
871 } else {
872 /* Compute any data- or p-drive using XOR */
873 count = 0;
874 for (i = disks; i-- ; ) {
875 if (i == target || i == qd_idx)
876 continue;
877 blocks[count++] = sh->dev[i].page;
878 }
879
Dan Williams0403e382009-09-08 17:42:50 -0700880 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
881 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700882 to_addr_conv(sh, percpu));
883 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
884 }
885
886 return tx;
887}
888
889static struct dma_async_tx_descriptor *
890ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
891{
892 int i, count, disks = sh->disks;
893 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
894 int d0_idx = raid6_d0(sh);
895 int faila = -1, failb = -1;
896 int target = sh->ops.target;
897 int target2 = sh->ops.target2;
898 struct r5dev *tgt = &sh->dev[target];
899 struct r5dev *tgt2 = &sh->dev[target2];
900 struct dma_async_tx_descriptor *tx;
901 struct page **blocks = percpu->scribble;
902 struct async_submit_ctl submit;
903
904 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
905 __func__, (unsigned long long)sh->sector, target, target2);
906 BUG_ON(target < 0 || target2 < 0);
907 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
908 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
909
Dan Williams6c910a72009-09-16 12:24:54 -0700910 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700911 * slot number conversion for 'faila' and 'failb'
912 */
913 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100914 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700915 count = 0;
916 i = d0_idx;
917 do {
918 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
919
920 blocks[slot] = sh->dev[i].page;
921
922 if (i == target)
923 faila = slot;
924 if (i == target2)
925 failb = slot;
926 i = raid6_next_disk(i, disks);
927 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700928
929 BUG_ON(faila == failb);
930 if (failb < faila)
931 swap(faila, failb);
932 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
933 __func__, (unsigned long long)sh->sector, faila, failb);
934
935 atomic_inc(&sh->count);
936
937 if (failb == syndrome_disks+1) {
938 /* Q disk is one of the missing disks */
939 if (faila == syndrome_disks) {
940 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700941 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
942 ops_complete_compute, sh,
943 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100944 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700945 STRIPE_SIZE, &submit);
946 } else {
947 struct page *dest;
948 int data_target;
949 int qd_idx = sh->qd_idx;
950
951 /* Missing D+Q: recompute D from P, then recompute Q */
952 if (target == qd_idx)
953 data_target = target2;
954 else
955 data_target = target;
956
957 count = 0;
958 for (i = disks; i-- ; ) {
959 if (i == data_target || i == qd_idx)
960 continue;
961 blocks[count++] = sh->dev[i].page;
962 }
963 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700964 init_async_submit(&submit,
965 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
966 NULL, NULL, NULL,
967 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700968 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
969 &submit);
970
971 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700972 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
973 ops_complete_compute, sh,
974 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700975 return async_gen_syndrome(blocks, 0, count+2,
976 STRIPE_SIZE, &submit);
977 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700978 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700979 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
980 ops_complete_compute, sh,
981 to_addr_conv(sh, percpu));
982 if (failb == syndrome_disks) {
983 /* We're missing D+P. */
984 return async_raid6_datap_recov(syndrome_disks+2,
985 STRIPE_SIZE, faila,
986 blocks, &submit);
987 } else {
988 /* We're missing D+D. */
989 return async_raid6_2data_recov(syndrome_disks+2,
990 STRIPE_SIZE, faila, failb,
991 blocks, &submit);
992 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700993 }
994}
995
996
Dan Williams91c00922007-01-02 13:52:30 -0700997static void ops_complete_prexor(void *stripe_head_ref)
998{
999 struct stripe_head *sh = stripe_head_ref;
1000
Harvey Harrisone46b2722008-04-28 02:15:50 -07001001 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001002 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001003}
1004
1005static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001006ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1007 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001008{
Dan Williams91c00922007-01-02 13:52:30 -07001009 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001010 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001011 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001012 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001013
1014 /* existing parity data subtracted */
1015 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1016
Harvey Harrisone46b2722008-04-28 02:15:50 -07001017 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001018 (unsigned long long)sh->sector);
1019
1020 for (i = disks; i--; ) {
1021 struct r5dev *dev = &sh->dev[i];
1022 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001023 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001024 xor_srcs[count++] = dev->page;
1025 }
1026
Dan Williams0403e382009-09-08 17:42:50 -07001027 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001028 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001029 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001030
1031 return tx;
1032}
1033
1034static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001035ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001036{
1037 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001038 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001039
Harvey Harrisone46b2722008-04-28 02:15:50 -07001040 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001041 (unsigned long long)sh->sector);
1042
1043 for (i = disks; i--; ) {
1044 struct r5dev *dev = &sh->dev[i];
1045 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001046
Dan Williamsd8ee0722008-06-28 08:32:06 +10001047 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001048 struct bio *wbi;
1049
NeilBrowncbe47ec2011-07-26 11:20:35 +10001050 spin_lock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001051 chosen = dev->towrite;
1052 dev->towrite = NULL;
1053 BUG_ON(dev->written);
1054 wbi = dev->written = chosen;
NeilBrowncbe47ec2011-07-26 11:20:35 +10001055 spin_unlock_irq(&sh->raid_conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001056
1057 while (wbi && wbi->bi_sector <
1058 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001059 if (wbi->bi_rw & REQ_FUA)
1060 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001061 tx = async_copy_data(1, wbi, dev->page,
1062 dev->sector, tx);
1063 wbi = r5_next_bio(wbi, dev->sector);
1064 }
1065 }
1066 }
1067
1068 return tx;
1069}
1070
Dan Williamsac6b53b2009-07-14 13:40:19 -07001071static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001072{
1073 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001074 int disks = sh->disks;
1075 int pd_idx = sh->pd_idx;
1076 int qd_idx = sh->qd_idx;
1077 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001078 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001079
Harvey Harrisone46b2722008-04-28 02:15:50 -07001080 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001081 (unsigned long long)sh->sector);
1082
Tejun Heoe9c74692010-09-03 11:56:18 +02001083 for (i = disks; i--; )
1084 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1085
Dan Williams91c00922007-01-02 13:52:30 -07001086 for (i = disks; i--; ) {
1087 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088
Tejun Heoe9c74692010-09-03 11:56:18 +02001089 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001090 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001091 if (fua)
1092 set_bit(R5_WantFUA, &dev->flags);
1093 }
Dan Williams91c00922007-01-02 13:52:30 -07001094 }
1095
Dan Williamsd8ee0722008-06-28 08:32:06 +10001096 if (sh->reconstruct_state == reconstruct_state_drain_run)
1097 sh->reconstruct_state = reconstruct_state_drain_result;
1098 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1099 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1100 else {
1101 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1102 sh->reconstruct_state = reconstruct_state_result;
1103 }
Dan Williams91c00922007-01-02 13:52:30 -07001104
1105 set_bit(STRIPE_HANDLE, &sh->state);
1106 release_stripe(sh);
1107}
1108
1109static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001110ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1111 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001112{
Dan Williams91c00922007-01-02 13:52:30 -07001113 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001114 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001115 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001116 int count = 0, pd_idx = sh->pd_idx, i;
1117 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001118 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001119 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001120
Harvey Harrisone46b2722008-04-28 02:15:50 -07001121 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001122 (unsigned long long)sh->sector);
1123
1124 /* check if prexor is active which means only process blocks
1125 * that are part of a read-modify-write (written)
1126 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001127 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1128 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001129 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1130 for (i = disks; i--; ) {
1131 struct r5dev *dev = &sh->dev[i];
1132 if (dev->written)
1133 xor_srcs[count++] = dev->page;
1134 }
1135 } else {
1136 xor_dest = sh->dev[pd_idx].page;
1137 for (i = disks; i--; ) {
1138 struct r5dev *dev = &sh->dev[i];
1139 if (i != pd_idx)
1140 xor_srcs[count++] = dev->page;
1141 }
1142 }
1143
Dan Williams91c00922007-01-02 13:52:30 -07001144 /* 1/ if we prexor'd then the dest is reused as a source
1145 * 2/ if we did not prexor then we are redoing the parity
1146 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1147 * for the synchronous xor case
1148 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001149 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001150 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1151
1152 atomic_inc(&sh->count);
1153
Dan Williamsac6b53b2009-07-14 13:40:19 -07001154 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001155 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001156 if (unlikely(count == 1))
1157 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1158 else
1159 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001160}
1161
Dan Williamsac6b53b2009-07-14 13:40:19 -07001162static void
1163ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1164 struct dma_async_tx_descriptor *tx)
1165{
1166 struct async_submit_ctl submit;
1167 struct page **blocks = percpu->scribble;
1168 int count;
1169
1170 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1171
1172 count = set_syndrome_sources(blocks, sh);
1173
1174 atomic_inc(&sh->count);
1175
1176 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1177 sh, to_addr_conv(sh, percpu));
1178 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001179}
1180
1181static void ops_complete_check(void *stripe_head_ref)
1182{
1183 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001184
Harvey Harrisone46b2722008-04-28 02:15:50 -07001185 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001186 (unsigned long long)sh->sector);
1187
Dan Williamsecc65c92008-06-28 08:31:57 +10001188 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001189 set_bit(STRIPE_HANDLE, &sh->state);
1190 release_stripe(sh);
1191}
1192
Dan Williamsac6b53b2009-07-14 13:40:19 -07001193static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001194{
Dan Williams91c00922007-01-02 13:52:30 -07001195 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001196 int pd_idx = sh->pd_idx;
1197 int qd_idx = sh->qd_idx;
1198 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001199 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001200 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001201 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001202 int count;
1203 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001204
Harvey Harrisone46b2722008-04-28 02:15:50 -07001205 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001206 (unsigned long long)sh->sector);
1207
Dan Williamsac6b53b2009-07-14 13:40:19 -07001208 count = 0;
1209 xor_dest = sh->dev[pd_idx].page;
1210 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001211 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001212 if (i == pd_idx || i == qd_idx)
1213 continue;
1214 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001215 }
1216
Dan Williamsd6f38f32009-07-14 11:50:52 -07001217 init_async_submit(&submit, 0, NULL, NULL, NULL,
1218 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001219 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001220 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001221
Dan Williams91c00922007-01-02 13:52:30 -07001222 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001223 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1224 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001225}
1226
Dan Williamsac6b53b2009-07-14 13:40:19 -07001227static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1228{
1229 struct page **srcs = percpu->scribble;
1230 struct async_submit_ctl submit;
1231 int count;
1232
1233 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1234 (unsigned long long)sh->sector, checkp);
1235
1236 count = set_syndrome_sources(srcs, sh);
1237 if (!checkp)
1238 srcs[count] = NULL;
1239
1240 atomic_inc(&sh->count);
1241 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1242 sh, to_addr_conv(sh, percpu));
1243 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1244 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1245}
1246
Dan Williams417b8d42009-10-16 16:25:22 +11001247static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001248{
1249 int overlap_clear = 0, i, disks = sh->disks;
1250 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001251 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001252 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001253 struct raid5_percpu *percpu;
1254 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001255
Dan Williamsd6f38f32009-07-14 11:50:52 -07001256 cpu = get_cpu();
1257 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001258 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001259 ops_run_biofill(sh);
1260 overlap_clear++;
1261 }
1262
Dan Williams7b3a8712008-06-28 08:32:09 +10001263 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001264 if (level < 6)
1265 tx = ops_run_compute5(sh, percpu);
1266 else {
1267 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1268 tx = ops_run_compute6_1(sh, percpu);
1269 else
1270 tx = ops_run_compute6_2(sh, percpu);
1271 }
1272 /* terminate the chain if reconstruct is not set to be run */
1273 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001274 async_tx_ack(tx);
1275 }
Dan Williams91c00922007-01-02 13:52:30 -07001276
Dan Williams600aa102008-06-28 08:32:05 +10001277 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001278 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001279
Dan Williams600aa102008-06-28 08:32:05 +10001280 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001281 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001282 overlap_clear++;
1283 }
1284
Dan Williamsac6b53b2009-07-14 13:40:19 -07001285 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1286 if (level < 6)
1287 ops_run_reconstruct5(sh, percpu, tx);
1288 else
1289 ops_run_reconstruct6(sh, percpu, tx);
1290 }
Dan Williams91c00922007-01-02 13:52:30 -07001291
Dan Williamsac6b53b2009-07-14 13:40:19 -07001292 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1293 if (sh->check_state == check_state_run)
1294 ops_run_check_p(sh, percpu);
1295 else if (sh->check_state == check_state_run_q)
1296 ops_run_check_pq(sh, percpu, 0);
1297 else if (sh->check_state == check_state_run_pq)
1298 ops_run_check_pq(sh, percpu, 1);
1299 else
1300 BUG();
1301 }
Dan Williams91c00922007-01-02 13:52:30 -07001302
Dan Williams91c00922007-01-02 13:52:30 -07001303 if (overlap_clear)
1304 for (i = disks; i--; ) {
1305 struct r5dev *dev = &sh->dev[i];
1306 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1307 wake_up(&sh->raid_conf->wait_for_overlap);
1308 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001309 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001310}
1311
Dan Williams417b8d42009-10-16 16:25:22 +11001312#ifdef CONFIG_MULTICORE_RAID456
1313static void async_run_ops(void *param, async_cookie_t cookie)
1314{
1315 struct stripe_head *sh = param;
1316 unsigned long ops_request = sh->ops.request;
1317
1318 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1319 wake_up(&sh->ops.wait_for_ops);
1320
1321 __raid_run_ops(sh, ops_request);
1322 release_stripe(sh);
1323}
1324
1325static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1326{
1327 /* since handle_stripe can be called outside of raid5d context
1328 * we need to ensure sh->ops.request is de-staged before another
1329 * request arrives
1330 */
1331 wait_event(sh->ops.wait_for_ops,
1332 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1333 sh->ops.request = ops_request;
1334
1335 atomic_inc(&sh->count);
1336 async_schedule(async_run_ops, sh);
1337}
1338#else
1339#define raid_run_ops __raid_run_ops
1340#endif
1341
NeilBrownd1688a62011-10-11 16:49:52 +11001342static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001345 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001346 if (!sh)
1347 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001348
NeilBrown3f294f42005-11-08 21:39:25 -08001349 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001350 #ifdef CONFIG_MULTICORE_RAID456
1351 init_waitqueue_head(&sh->ops.wait_for_ops);
1352 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001353
NeilBrowne4e11e32010-06-16 16:45:16 +10001354 if (grow_buffers(sh)) {
1355 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001356 kmem_cache_free(conf->slab_cache, sh);
1357 return 0;
1358 }
1359 /* we just created an active stripe so... */
1360 atomic_set(&sh->count, 1);
1361 atomic_inc(&conf->active_stripes);
1362 INIT_LIST_HEAD(&sh->lru);
1363 release_stripe(sh);
1364 return 1;
1365}
1366
NeilBrownd1688a62011-10-11 16:49:52 +11001367static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001368{
Christoph Lametere18b8902006-12-06 20:33:20 -08001369 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001370 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
NeilBrownf4be6b42010-06-01 19:37:25 +10001372 if (conf->mddev->gendisk)
1373 sprintf(conf->cache_name[0],
1374 "raid%d-%s", conf->level, mdname(conf->mddev));
1375 else
1376 sprintf(conf->cache_name[0],
1377 "raid%d-%p", conf->level, conf->mddev);
1378 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1379
NeilBrownad01c9e2006-03-27 01:18:07 -08001380 conf->active_name = 0;
1381 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001383 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (!sc)
1385 return 1;
1386 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001387 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001388 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001389 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return 0;
1392}
NeilBrown29269552006-03-27 01:18:10 -08001393
Dan Williamsd6f38f32009-07-14 11:50:52 -07001394/**
1395 * scribble_len - return the required size of the scribble region
1396 * @num - total number of disks in the array
1397 *
1398 * The size must be enough to contain:
1399 * 1/ a struct page pointer for each device in the array +2
1400 * 2/ room to convert each entry in (1) to its corresponding dma
1401 * (dma_map_page()) or page (page_address()) address.
1402 *
1403 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1404 * calculate over all devices (not just the data blocks), using zeros in place
1405 * of the P and Q blocks.
1406 */
1407static size_t scribble_len(int num)
1408{
1409 size_t len;
1410
1411 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1412
1413 return len;
1414}
1415
NeilBrownd1688a62011-10-11 16:49:52 +11001416static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001417{
1418 /* Make all the stripes able to hold 'newsize' devices.
1419 * New slots in each stripe get 'page' set to a new page.
1420 *
1421 * This happens in stages:
1422 * 1/ create a new kmem_cache and allocate the required number of
1423 * stripe_heads.
1424 * 2/ gather all the old stripe_heads and tranfer the pages across
1425 * to the new stripe_heads. This will have the side effect of
1426 * freezing the array as once all stripe_heads have been collected,
1427 * no IO will be possible. Old stripe heads are freed once their
1428 * pages have been transferred over, and the old kmem_cache is
1429 * freed when all stripes are done.
1430 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1431 * we simple return a failre status - no need to clean anything up.
1432 * 4/ allocate new pages for the new slots in the new stripe_heads.
1433 * If this fails, we don't bother trying the shrink the
1434 * stripe_heads down again, we just leave them as they are.
1435 * As each stripe_head is processed the new one is released into
1436 * active service.
1437 *
1438 * Once step2 is started, we cannot afford to wait for a write,
1439 * so we use GFP_NOIO allocations.
1440 */
1441 struct stripe_head *osh, *nsh;
1442 LIST_HEAD(newstripes);
1443 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001444 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001445 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001446 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001447 int i;
1448
1449 if (newsize <= conf->pool_size)
1450 return 0; /* never bother to shrink */
1451
Dan Williamsb5470dc2008-06-27 21:44:04 -07001452 err = md_allow_write(conf->mddev);
1453 if (err)
1454 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001455
NeilBrownad01c9e2006-03-27 01:18:07 -08001456 /* Step 1 */
1457 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1458 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001459 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001460 if (!sc)
1461 return -ENOMEM;
1462
1463 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001464 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001465 if (!nsh)
1466 break;
1467
NeilBrownad01c9e2006-03-27 01:18:07 -08001468 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001469 #ifdef CONFIG_MULTICORE_RAID456
1470 init_waitqueue_head(&nsh->ops.wait_for_ops);
1471 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001472
1473 list_add(&nsh->lru, &newstripes);
1474 }
1475 if (i) {
1476 /* didn't get enough, give up */
1477 while (!list_empty(&newstripes)) {
1478 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1479 list_del(&nsh->lru);
1480 kmem_cache_free(sc, nsh);
1481 }
1482 kmem_cache_destroy(sc);
1483 return -ENOMEM;
1484 }
1485 /* Step 2 - Must use GFP_NOIO now.
1486 * OK, we have enough stripes, start collecting inactive
1487 * stripes and copying them over
1488 */
1489 list_for_each_entry(nsh, &newstripes, lru) {
1490 spin_lock_irq(&conf->device_lock);
1491 wait_event_lock_irq(conf->wait_for_stripe,
1492 !list_empty(&conf->inactive_list),
1493 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001494 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001495 osh = get_free_stripe(conf);
1496 spin_unlock_irq(&conf->device_lock);
1497 atomic_set(&nsh->count, 1);
1498 for(i=0; i<conf->pool_size; i++)
1499 nsh->dev[i].page = osh->dev[i].page;
1500 for( ; i<newsize; i++)
1501 nsh->dev[i].page = NULL;
1502 kmem_cache_free(conf->slab_cache, osh);
1503 }
1504 kmem_cache_destroy(conf->slab_cache);
1505
1506 /* Step 3.
1507 * At this point, we are holding all the stripes so the array
1508 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001509 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001510 */
1511 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1512 if (ndisks) {
1513 for (i=0; i<conf->raid_disks; i++)
1514 ndisks[i] = conf->disks[i];
1515 kfree(conf->disks);
1516 conf->disks = ndisks;
1517 } else
1518 err = -ENOMEM;
1519
Dan Williamsd6f38f32009-07-14 11:50:52 -07001520 get_online_cpus();
1521 conf->scribble_len = scribble_len(newsize);
1522 for_each_present_cpu(cpu) {
1523 struct raid5_percpu *percpu;
1524 void *scribble;
1525
1526 percpu = per_cpu_ptr(conf->percpu, cpu);
1527 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1528
1529 if (scribble) {
1530 kfree(percpu->scribble);
1531 percpu->scribble = scribble;
1532 } else {
1533 err = -ENOMEM;
1534 break;
1535 }
1536 }
1537 put_online_cpus();
1538
NeilBrownad01c9e2006-03-27 01:18:07 -08001539 /* Step 4, return new stripes to service */
1540 while(!list_empty(&newstripes)) {
1541 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1542 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001543
NeilBrownad01c9e2006-03-27 01:18:07 -08001544 for (i=conf->raid_disks; i < newsize; i++)
1545 if (nsh->dev[i].page == NULL) {
1546 struct page *p = alloc_page(GFP_NOIO);
1547 nsh->dev[i].page = p;
1548 if (!p)
1549 err = -ENOMEM;
1550 }
1551 release_stripe(nsh);
1552 }
1553 /* critical section pass, GFP_NOIO no longer needed */
1554
1555 conf->slab_cache = sc;
1556 conf->active_name = 1-conf->active_name;
1557 conf->pool_size = newsize;
1558 return err;
1559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
NeilBrownd1688a62011-10-11 16:49:52 +11001561static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 struct stripe_head *sh;
1564
NeilBrown3f294f42005-11-08 21:39:25 -08001565 spin_lock_irq(&conf->device_lock);
1566 sh = get_free_stripe(conf);
1567 spin_unlock_irq(&conf->device_lock);
1568 if (!sh)
1569 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001570 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001571 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001572 kmem_cache_free(conf->slab_cache, sh);
1573 atomic_dec(&conf->active_stripes);
1574 return 1;
1575}
1576
NeilBrownd1688a62011-10-11 16:49:52 +11001577static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001578{
1579 while (drop_one_stripe(conf))
1580 ;
1581
NeilBrown29fc7e32006-02-03 03:03:41 -08001582 if (conf->slab_cache)
1583 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 conf->slab_cache = NULL;
1585}
1586
NeilBrown6712ecf2007-09-27 12:47:43 +02001587static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
NeilBrown99c0fb52009-03-31 14:39:38 +11001589 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001590 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001591 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001593 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11001594 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 for (i=0 ; i<disks; i++)
1598 if (bi == &sh->dev[i].req)
1599 break;
1600
Dan Williams45b42332007-07-09 11:56:43 -07001601 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1602 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 uptodate);
1604 if (i == disks) {
1605 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001606 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001611 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001612 rdev = conf->disks[i].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001613 printk_ratelimited(
1614 KERN_INFO
1615 "md/raid:%s: read error corrected"
1616 " (%lu sectors at %llu on %s)\n",
1617 mdname(conf->mddev), STRIPE_SECTORS,
1618 (unsigned long long)(sh->sector
1619 + rdev->data_offset),
1620 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001621 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001622 clear_bit(R5_ReadError, &sh->dev[i].flags);
1623 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1624 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001625 if (atomic_read(&conf->disks[i].rdev->read_errors))
1626 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001628 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001629 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001630 rdev = conf->disks[i].rdev;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001633 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001634 if (conf->mddev->degraded >= conf->max_degraded)
Christian Dietrich8bda4702011-07-27 11:00:36 +10001635 printk_ratelimited(
1636 KERN_WARNING
1637 "md/raid:%s: read error not correctable "
1638 "(sector %llu on %s).\n",
1639 mdname(conf->mddev),
1640 (unsigned long long)(sh->sector
1641 + rdev->data_offset),
1642 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001643 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001644 /* Oh, no!!! */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001645 printk_ratelimited(
1646 KERN_WARNING
1647 "md/raid:%s: read error NOT corrected!! "
1648 "(sector %llu on %s).\n",
1649 mdname(conf->mddev),
1650 (unsigned long long)(sh->sector
1651 + rdev->data_offset),
1652 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001653 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001654 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001655 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001656 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001657 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001658 else
1659 retry = 1;
1660 if (retry)
1661 set_bit(R5_ReadError, &sh->dev[i].flags);
1662 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001663 clear_bit(R5_ReadError, &sh->dev[i].flags);
1664 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001665 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1670 set_bit(STRIPE_HANDLE, &sh->state);
1671 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
NeilBrownd710e132008-10-13 11:55:12 +11001674static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
NeilBrown99c0fb52009-03-31 14:39:38 +11001676 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001677 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001678 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001680 sector_t first_bad;
1681 int bad_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 for (i=0 ; i<disks; i++)
1684 if (bi == &sh->dev[i].req)
1685 break;
1686
Dan Williams45b42332007-07-09 11:56:43 -07001687 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1689 uptodate);
1690 if (i == disks) {
1691 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001692 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
NeilBrownbc2607f2011-07-28 11:39:22 +10001695 if (!uptodate) {
1696 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1697 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrownb84db562011-07-28 11:39:23 +10001698 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1699 &first_bad, &bad_sectors))
1700 set_bit(R5_MadeGood, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1703
1704 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1705 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001706 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709
NeilBrown784052e2009-03-31 15:19:07 +11001710static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrown784052e2009-03-31 15:19:07 +11001712static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct r5dev *dev = &sh->dev[i];
1715
1716 bio_init(&dev->req);
1717 dev->req.bi_io_vec = &dev->vec;
1718 dev->req.bi_vcnt++;
1719 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001721 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001724 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
NeilBrownfd01b882011-10-11 16:47:53 +11001727static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001730 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001731 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001732 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
NeilBrown908f4fb2011-12-23 10:17:50 +11001734 spin_lock_irqsave(&conf->device_lock, flags);
1735 clear_bit(In_sync, &rdev->flags);
1736 mddev->degraded = calc_degraded(conf);
1737 spin_unlock_irqrestore(&conf->device_lock, flags);
1738 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1739
NeilBrownde393cd2011-07-28 11:31:48 +10001740 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001741 set_bit(Faulty, &rdev->flags);
1742 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1743 printk(KERN_ALERT
1744 "md/raid:%s: Disk failure on %s, disabling device.\n"
1745 "md/raid:%s: Operation continuing on %d devices.\n",
1746 mdname(mddev),
1747 bdevname(rdev->bdev, b),
1748 mdname(mddev),
1749 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752/*
1753 * Input: a 'big' sector number,
1754 * Output: index of the data and parity disk, and the sector # in them.
1755 */
NeilBrownd1688a62011-10-11 16:49:52 +11001756static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001757 int previous, int *dd_idx,
1758 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001760 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001761 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001763 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001764 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001766 int algorithm = previous ? conf->prev_algo
1767 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001768 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1769 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001770 int raid_disks = previous ? conf->previous_raid_disks
1771 : conf->raid_disks;
1772 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /* First compute the information on this sector */
1775
1776 /*
1777 * Compute the chunk number and the sector offset inside the chunk
1778 */
1779 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1780 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /*
1783 * Compute the stripe number
1784 */
NeilBrown35f2a592010-04-20 14:13:34 +10001785 stripe = chunk_number;
1786 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001787 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /*
1789 * Select the parity disk based on the user selected algorithm.
1790 */
NeilBrown84789552011-07-27 11:00:36 +10001791 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001792 switch(conf->level) {
1793 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001794 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001795 break;
1796 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001797 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001799 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001800 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 (*dd_idx)++;
1802 break;
1803 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001804 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001805 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 (*dd_idx)++;
1807 break;
1808 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001809 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001810 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 break;
1812 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001813 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001814 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001816 case ALGORITHM_PARITY_0:
1817 pd_idx = 0;
1818 (*dd_idx)++;
1819 break;
1820 case ALGORITHM_PARITY_N:
1821 pd_idx = data_disks;
1822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001824 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001825 }
1826 break;
1827 case 6:
1828
NeilBrowne183eae2009-03-31 15:20:22 +11001829 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001830 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001831 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001832 qd_idx = pd_idx + 1;
1833 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001834 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001835 qd_idx = 0;
1836 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001837 (*dd_idx) += 2; /* D D P Q D */
1838 break;
1839 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001840 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001841 qd_idx = pd_idx + 1;
1842 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001843 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001844 qd_idx = 0;
1845 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001846 (*dd_idx) += 2; /* D D P Q D */
1847 break;
1848 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001849 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001850 qd_idx = (pd_idx + 1) % raid_disks;
1851 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001852 break;
1853 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001854 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001855 qd_idx = (pd_idx + 1) % raid_disks;
1856 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001857 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001858
1859 case ALGORITHM_PARITY_0:
1860 pd_idx = 0;
1861 qd_idx = 1;
1862 (*dd_idx) += 2;
1863 break;
1864 case ALGORITHM_PARITY_N:
1865 pd_idx = data_disks;
1866 qd_idx = data_disks + 1;
1867 break;
1868
1869 case ALGORITHM_ROTATING_ZERO_RESTART:
1870 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1871 * of blocks for computing Q is different.
1872 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001873 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001874 qd_idx = pd_idx + 1;
1875 if (pd_idx == raid_disks-1) {
1876 (*dd_idx)++; /* Q D D D P */
1877 qd_idx = 0;
1878 } else if (*dd_idx >= pd_idx)
1879 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001880 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001881 break;
1882
1883 case ALGORITHM_ROTATING_N_RESTART:
1884 /* Same a left_asymmetric, by first stripe is
1885 * D D D P Q rather than
1886 * Q D D D P
1887 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001888 stripe2 += 1;
1889 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001890 qd_idx = pd_idx + 1;
1891 if (pd_idx == raid_disks-1) {
1892 (*dd_idx)++; /* Q D D D P */
1893 qd_idx = 0;
1894 } else if (*dd_idx >= pd_idx)
1895 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001896 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001897 break;
1898
1899 case ALGORITHM_ROTATING_N_CONTINUE:
1900 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001901 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001902 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1903 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001904 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001905 break;
1906
1907 case ALGORITHM_LEFT_ASYMMETRIC_6:
1908 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001909 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001910 if (*dd_idx >= pd_idx)
1911 (*dd_idx)++;
1912 qd_idx = raid_disks - 1;
1913 break;
1914
1915 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001916 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001917 if (*dd_idx >= pd_idx)
1918 (*dd_idx)++;
1919 qd_idx = raid_disks - 1;
1920 break;
1921
1922 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001923 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001924 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1925 qd_idx = raid_disks - 1;
1926 break;
1927
1928 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001929 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001930 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1931 qd_idx = raid_disks - 1;
1932 break;
1933
1934 case ALGORITHM_PARITY_0_6:
1935 pd_idx = 0;
1936 (*dd_idx)++;
1937 qd_idx = raid_disks - 1;
1938 break;
1939
NeilBrown16a53ec2006-06-26 00:27:38 -07001940 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001941 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001942 }
1943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
NeilBrown911d4ee2009-03-31 14:39:38 +11001946 if (sh) {
1947 sh->pd_idx = pd_idx;
1948 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001949 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Finally, compute the new sector number
1953 */
1954 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1955 return new_sector;
1956}
1957
1958
NeilBrown784052e2009-03-31 15:19:07 +11001959static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
NeilBrownd1688a62011-10-11 16:49:52 +11001961 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001962 int raid_disks = sh->disks;
1963 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001965 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1966 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001967 int algorithm = previous ? conf->prev_algo
1968 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sector_t stripe;
1970 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001971 sector_t chunk_number;
1972 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001974 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
NeilBrown16a53ec2006-06-26 00:27:38 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1978 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
NeilBrown16a53ec2006-06-26 00:27:38 -07001980 if (i == sh->pd_idx)
1981 return 0;
1982 switch(conf->level) {
1983 case 4: break;
1984 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001985 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 case ALGORITHM_LEFT_ASYMMETRIC:
1987 case ALGORITHM_RIGHT_ASYMMETRIC:
1988 if (i > sh->pd_idx)
1989 i--;
1990 break;
1991 case ALGORITHM_LEFT_SYMMETRIC:
1992 case ALGORITHM_RIGHT_SYMMETRIC:
1993 if (i < sh->pd_idx)
1994 i += raid_disks;
1995 i -= (sh->pd_idx + 1);
1996 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001997 case ALGORITHM_PARITY_0:
1998 i -= 1;
1999 break;
2000 case ALGORITHM_PARITY_N:
2001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002003 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002004 }
2005 break;
2006 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002007 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002008 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002009 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002010 case ALGORITHM_LEFT_ASYMMETRIC:
2011 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002012 case ALGORITHM_ROTATING_ZERO_RESTART:
2013 case ALGORITHM_ROTATING_N_RESTART:
2014 if (sh->pd_idx == raid_disks-1)
2015 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002016 else if (i > sh->pd_idx)
2017 i -= 2; /* D D P Q D */
2018 break;
2019 case ALGORITHM_LEFT_SYMMETRIC:
2020 case ALGORITHM_RIGHT_SYMMETRIC:
2021 if (sh->pd_idx == raid_disks-1)
2022 i--; /* Q D D D P */
2023 else {
2024 /* D D P Q D */
2025 if (i < sh->pd_idx)
2026 i += raid_disks;
2027 i -= (sh->pd_idx + 2);
2028 }
2029 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002030 case ALGORITHM_PARITY_0:
2031 i -= 2;
2032 break;
2033 case ALGORITHM_PARITY_N:
2034 break;
2035 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002036 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002037 if (sh->pd_idx == 0)
2038 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002039 else {
2040 /* D D Q P D */
2041 if (i < sh->pd_idx)
2042 i += raid_disks;
2043 i -= (sh->pd_idx + 1);
2044 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002045 break;
2046 case ALGORITHM_LEFT_ASYMMETRIC_6:
2047 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2048 if (i > sh->pd_idx)
2049 i--;
2050 break;
2051 case ALGORITHM_LEFT_SYMMETRIC_6:
2052 case ALGORITHM_RIGHT_SYMMETRIC_6:
2053 if (i < sh->pd_idx)
2054 i += data_disks + 1;
2055 i -= (sh->pd_idx + 1);
2056 break;
2057 case ALGORITHM_PARITY_0_6:
2058 i -= 1;
2059 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002060 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002061 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002062 }
2063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
2066 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002067 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
NeilBrown112bf892009-03-31 14:39:38 +11002069 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002070 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002071 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2072 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002073 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2074 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076 }
2077 return r_sector;
2078}
2079
2080
Dan Williams600aa102008-06-28 08:32:05 +10002081static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002082schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002083 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002084{
2085 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002086 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002087 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002088
Dan Williamse33129d2007-01-02 13:52:30 -07002089 if (rcw) {
2090 /* if we are not expanding this is a proper write request, and
2091 * there will be bios with new data to be drained into the
2092 * stripe cache
2093 */
2094 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002095 sh->reconstruct_state = reconstruct_state_drain_run;
2096 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2097 } else
2098 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002099
Dan Williamsac6b53b2009-07-14 13:40:19 -07002100 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002101
2102 for (i = disks; i--; ) {
2103 struct r5dev *dev = &sh->dev[i];
2104
2105 if (dev->towrite) {
2106 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002107 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002108 if (!expand)
2109 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002110 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002111 }
2112 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002113 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002114 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002115 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002116 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002117 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002118 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2119 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2120
Dan Williamsd8ee0722008-06-28 08:32:06 +10002121 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002122 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2123 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002124 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002125
2126 for (i = disks; i--; ) {
2127 struct r5dev *dev = &sh->dev[i];
2128 if (i == pd_idx)
2129 continue;
2130
Dan Williamse33129d2007-01-02 13:52:30 -07002131 if (dev->towrite &&
2132 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002133 test_bit(R5_Wantcompute, &dev->flags))) {
2134 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002135 set_bit(R5_LOCKED, &dev->flags);
2136 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002137 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002138 }
2139 }
2140 }
2141
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002142 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002143 * are in flight
2144 */
2145 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2146 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002147 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002148
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002149 if (level == 6) {
2150 int qd_idx = sh->qd_idx;
2151 struct r5dev *dev = &sh->dev[qd_idx];
2152
2153 set_bit(R5_LOCKED, &dev->flags);
2154 clear_bit(R5_UPTODATE, &dev->flags);
2155 s->locked++;
2156 }
2157
Dan Williams600aa102008-06-28 08:32:05 +10002158 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002159 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002160 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002161}
NeilBrown16a53ec2006-06-26 00:27:38 -07002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163/*
2164 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002165 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * The bi_next chain must be in order.
2167 */
2168static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2169{
2170 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002171 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002172 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
NeilBrowncbe47ec2011-07-26 11:20:35 +10002174 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 (unsigned long long)bi->bi_sector,
2176 (unsigned long long)sh->sector);
2177
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002180 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002182 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2183 firstwrite = 1;
2184 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 bip = &sh->dev[dd_idx].toread;
2186 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2187 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2188 goto overlap;
2189 bip = & (*bip)->bi_next;
2190 }
2191 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2192 goto overlap;
2193
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002194 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (*bip)
2196 bi->bi_next = *bip;
2197 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002198 bi->bi_phys_segments++;
NeilBrown72626682005-09-09 16:23:54 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (forwrite) {
2201 /* check if page is covered */
2202 sector_t sector = sh->dev[dd_idx].sector;
2203 for (bi=sh->dev[dd_idx].towrite;
2204 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2205 bi && bi->bi_sector <= sector;
2206 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2207 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2208 sector = bi->bi_sector + (bi->bi_size>>9);
2209 }
2210 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2211 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2212 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002213 spin_unlock_irq(&conf->device_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002214
2215 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2216 (unsigned long long)(*bip)->bi_sector,
2217 (unsigned long long)sh->sector, dd_idx);
2218
2219 if (conf->mddev->bitmap && firstwrite) {
2220 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2221 STRIPE_SECTORS, 0);
2222 sh->bm_seq = conf->seq_flush+1;
2223 set_bit(STRIPE_BIT_DELAY, &sh->state);
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 1;
2226
2227 overlap:
2228 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2229 spin_unlock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return 0;
2231}
2232
NeilBrownd1688a62011-10-11 16:49:52 +11002233static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002234
NeilBrownd1688a62011-10-11 16:49:52 +11002235static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002236 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002237{
NeilBrown784052e2009-03-31 15:19:07 +11002238 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002239 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002240 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002241 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002242 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002243
NeilBrown112bf892009-03-31 14:39:38 +11002244 raid5_compute_sector(conf,
2245 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002246 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002247 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002248 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002249}
2250
Dan Williamsa4456852007-07-09 11:56:43 -07002251static void
NeilBrownd1688a62011-10-11 16:49:52 +11002252handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002253 struct stripe_head_state *s, int disks,
2254 struct bio **return_bi)
2255{
2256 int i;
2257 for (i = disks; i--; ) {
2258 struct bio *bi;
2259 int bitmap_end = 0;
2260
2261 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002262 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002263 rcu_read_lock();
2264 rdev = rcu_dereference(conf->disks[i].rdev);
2265 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002266 atomic_inc(&rdev->nr_pending);
2267 else
2268 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002269 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002270 if (rdev) {
2271 if (!rdev_set_badblocks(
2272 rdev,
2273 sh->sector,
2274 STRIPE_SECTORS, 0))
2275 md_error(conf->mddev, rdev);
2276 rdev_dec_pending(rdev, conf->mddev);
2277 }
Dan Williamsa4456852007-07-09 11:56:43 -07002278 }
2279 spin_lock_irq(&conf->device_lock);
2280 /* fail all writes first */
2281 bi = sh->dev[i].towrite;
2282 sh->dev[i].towrite = NULL;
2283 if (bi) {
2284 s->to_write--;
2285 bitmap_end = 1;
2286 }
2287
2288 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2289 wake_up(&conf->wait_for_overlap);
2290
2291 while (bi && bi->bi_sector <
2292 sh->dev[i].sector + STRIPE_SECTORS) {
2293 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2294 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002295 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002296 md_write_end(conf->mddev);
2297 bi->bi_next = *return_bi;
2298 *return_bi = bi;
2299 }
2300 bi = nextbi;
2301 }
2302 /* and fail all 'written' */
2303 bi = sh->dev[i].written;
2304 sh->dev[i].written = NULL;
2305 if (bi) bitmap_end = 1;
2306 while (bi && bi->bi_sector <
2307 sh->dev[i].sector + STRIPE_SECTORS) {
2308 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2309 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002310 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002311 md_write_end(conf->mddev);
2312 bi->bi_next = *return_bi;
2313 *return_bi = bi;
2314 }
2315 bi = bi2;
2316 }
2317
Dan Williamsb5e98d62007-01-02 13:52:31 -07002318 /* fail any reads if this device is non-operational and
2319 * the data has not reached the cache yet.
2320 */
2321 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2322 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2323 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002324 bi = sh->dev[i].toread;
2325 sh->dev[i].toread = NULL;
2326 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2327 wake_up(&conf->wait_for_overlap);
2328 if (bi) s->to_read--;
2329 while (bi && bi->bi_sector <
2330 sh->dev[i].sector + STRIPE_SECTORS) {
2331 struct bio *nextbi =
2332 r5_next_bio(bi, sh->dev[i].sector);
2333 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002334 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002335 bi->bi_next = *return_bi;
2336 *return_bi = bi;
2337 }
2338 bi = nextbi;
2339 }
2340 }
2341 spin_unlock_irq(&conf->device_lock);
2342 if (bitmap_end)
2343 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2344 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002345 /* If we were in the middle of a write the parity block might
2346 * still be locked - so just clear all R5_LOCKED flags
2347 */
2348 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002349 }
2350
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002351 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2352 if (atomic_dec_and_test(&conf->pending_full_writes))
2353 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002354}
2355
NeilBrown7f0da592011-07-28 11:39:22 +10002356static void
NeilBrownd1688a62011-10-11 16:49:52 +11002357handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002358 struct stripe_head_state *s)
2359{
2360 int abort = 0;
2361 int i;
2362
2363 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2364 clear_bit(STRIPE_SYNCING, &sh->state);
2365 s->syncing = 0;
2366 /* There is nothing more to do for sync/check/repair.
2367 * For recover we need to record a bad block on all
2368 * non-sync devices, or abort the recovery
2369 */
2370 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2371 return;
2372 /* During recovery devices cannot be removed, so locking and
2373 * refcounting of rdevs is not needed
2374 */
2375 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +11002376 struct md_rdev *rdev = conf->disks[i].rdev;
NeilBrown7f0da592011-07-28 11:39:22 +10002377 if (!rdev
2378 || test_bit(Faulty, &rdev->flags)
2379 || test_bit(In_sync, &rdev->flags))
2380 continue;
2381 if (!rdev_set_badblocks(rdev, sh->sector,
2382 STRIPE_SECTORS, 0))
2383 abort = 1;
2384 }
2385 if (abort) {
2386 conf->recovery_disabled = conf->mddev->recovery_disabled;
2387 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2388 }
2389}
2390
NeilBrown93b3dbc2011-07-27 11:00:36 +10002391/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002392 * to be read or computed to satisfy a request.
2393 *
2394 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002395 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002396 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002397static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2398 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002399{
2400 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002401 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2402 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002403
Dan Williamsf38e1212007-01-02 13:52:30 -07002404 /* is the data in this block needed, and can we get it? */
2405 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002406 !test_bit(R5_UPTODATE, &dev->flags) &&
2407 (dev->toread ||
2408 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2409 s->syncing || s->expanding ||
NeilBrown5d35e092011-07-27 11:00:36 +10002410 (s->failed >= 1 && fdev[0]->toread) ||
2411 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002412 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2413 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2414 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002415 /* we would like to get this block, possibly by computing it,
2416 * otherwise read it if the backing disk is insync
2417 */
2418 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2419 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2420 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002421 (s->failed && (disk_idx == s->failed_num[0] ||
2422 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002423 /* have disk failed, and we're requested to fetch it;
2424 * do compute it
2425 */
2426 pr_debug("Computing stripe %llu block %d\n",
2427 (unsigned long long)sh->sector, disk_idx);
2428 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2429 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2430 set_bit(R5_Wantcompute, &dev->flags);
2431 sh->ops.target = disk_idx;
2432 sh->ops.target2 = -1; /* no 2nd target */
2433 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002434 /* Careful: from this point on 'uptodate' is in the eye
2435 * of raid_run_ops which services 'compute' operations
2436 * before writes. R5_Wantcompute flags a block that will
2437 * be R5_UPTODATE by the time it is needed for a
2438 * subsequent operation.
2439 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002440 s->uptodate++;
2441 return 1;
2442 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2443 /* Computing 2-failure is *very* expensive; only
2444 * do it if failed >= 2
2445 */
2446 int other;
2447 for (other = disks; other--; ) {
2448 if (other == disk_idx)
2449 continue;
2450 if (!test_bit(R5_UPTODATE,
2451 &sh->dev[other].flags))
2452 break;
2453 }
2454 BUG_ON(other < 0);
2455 pr_debug("Computing stripe %llu blocks %d,%d\n",
2456 (unsigned long long)sh->sector,
2457 disk_idx, other);
2458 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2459 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2460 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2461 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2462 sh->ops.target = disk_idx;
2463 sh->ops.target2 = other;
2464 s->uptodate += 2;
2465 s->req_compute = 1;
2466 return 1;
2467 } else if (test_bit(R5_Insync, &dev->flags)) {
2468 set_bit(R5_LOCKED, &dev->flags);
2469 set_bit(R5_Wantread, &dev->flags);
2470 s->locked++;
2471 pr_debug("Reading block %d (sync=%d)\n",
2472 disk_idx, s->syncing);
2473 }
2474 }
2475
2476 return 0;
2477}
2478
2479/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002480 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002481 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002482static void handle_stripe_fill(struct stripe_head *sh,
2483 struct stripe_head_state *s,
2484 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002485{
2486 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002487
2488 /* look for blocks to read/compute, skip this if a compute
2489 * is already in flight, or if the stripe contents are in the
2490 * midst of changing due to a write
2491 */
2492 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2493 !sh->reconstruct_state)
2494 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002495 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002496 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002497 set_bit(STRIPE_HANDLE, &sh->state);
2498}
2499
2500
Dan Williams1fe797e2008-06-28 09:16:30 +10002501/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002502 * any written block on an uptodate or failed drive can be returned.
2503 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2504 * never LOCKED, so we don't need to test 'failed' directly.
2505 */
NeilBrownd1688a62011-10-11 16:49:52 +11002506static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002507 struct stripe_head *sh, int disks, struct bio **return_bi)
2508{
2509 int i;
2510 struct r5dev *dev;
2511
2512 for (i = disks; i--; )
2513 if (sh->dev[i].written) {
2514 dev = &sh->dev[i];
2515 if (!test_bit(R5_LOCKED, &dev->flags) &&
2516 test_bit(R5_UPTODATE, &dev->flags)) {
2517 /* We can return any write requests */
2518 struct bio *wbi, *wbi2;
2519 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002520 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002521 spin_lock_irq(&conf->device_lock);
2522 wbi = dev->written;
2523 dev->written = NULL;
2524 while (wbi && wbi->bi_sector <
2525 dev->sector + STRIPE_SECTORS) {
2526 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002527 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002528 md_write_end(conf->mddev);
2529 wbi->bi_next = *return_bi;
2530 *return_bi = wbi;
2531 }
2532 wbi = wbi2;
2533 }
2534 if (dev->towrite == NULL)
2535 bitmap_end = 1;
2536 spin_unlock_irq(&conf->device_lock);
2537 if (bitmap_end)
2538 bitmap_endwrite(conf->mddev->bitmap,
2539 sh->sector,
2540 STRIPE_SECTORS,
2541 !test_bit(STRIPE_DEGRADED, &sh->state),
2542 0);
2543 }
2544 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002545
2546 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2547 if (atomic_dec_and_test(&conf->pending_full_writes))
2548 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002549}
2550
NeilBrownd1688a62011-10-11 16:49:52 +11002551static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002552 struct stripe_head *sh,
2553 struct stripe_head_state *s,
2554 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002555{
2556 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002557 if (conf->max_degraded == 2) {
2558 /* RAID6 requires 'rcw' in current implementation
2559 * Calculate the real rcw later - for now fake it
2560 * look like rcw is cheaper
2561 */
2562 rcw = 1; rmw = 2;
2563 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002564 /* would I have to read this buffer for read_modify_write */
2565 struct r5dev *dev = &sh->dev[i];
2566 if ((dev->towrite || i == sh->pd_idx) &&
2567 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002568 !(test_bit(R5_UPTODATE, &dev->flags) ||
2569 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002570 if (test_bit(R5_Insync, &dev->flags))
2571 rmw++;
2572 else
2573 rmw += 2*disks; /* cannot read it */
2574 }
2575 /* Would I have to read this buffer for reconstruct_write */
2576 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2577 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002578 !(test_bit(R5_UPTODATE, &dev->flags) ||
2579 test_bit(R5_Wantcompute, &dev->flags))) {
2580 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002581 else
2582 rcw += 2*disks;
2583 }
2584 }
Dan Williams45b42332007-07-09 11:56:43 -07002585 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002586 (unsigned long long)sh->sector, rmw, rcw);
2587 set_bit(STRIPE_HANDLE, &sh->state);
2588 if (rmw < rcw && rmw > 0)
2589 /* prefer read-modify-write, but need to get some data */
2590 for (i = disks; i--; ) {
2591 struct r5dev *dev = &sh->dev[i];
2592 if ((dev->towrite || i == sh->pd_idx) &&
2593 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002594 !(test_bit(R5_UPTODATE, &dev->flags) ||
2595 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002596 test_bit(R5_Insync, &dev->flags)) {
2597 if (
2598 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002599 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002600 "%d for r-m-w\n", i);
2601 set_bit(R5_LOCKED, &dev->flags);
2602 set_bit(R5_Wantread, &dev->flags);
2603 s->locked++;
2604 } else {
2605 set_bit(STRIPE_DELAYED, &sh->state);
2606 set_bit(STRIPE_HANDLE, &sh->state);
2607 }
2608 }
2609 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002610 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002611 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002612 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002613 for (i = disks; i--; ) {
2614 struct r5dev *dev = &sh->dev[i];
2615 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002616 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002617 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002618 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002619 test_bit(R5_Wantcompute, &dev->flags))) {
2620 rcw++;
2621 if (!test_bit(R5_Insync, &dev->flags))
2622 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002623 if (
2624 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002625 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002626 "%d for Reconstruct\n", i);
2627 set_bit(R5_LOCKED, &dev->flags);
2628 set_bit(R5_Wantread, &dev->flags);
2629 s->locked++;
2630 } else {
2631 set_bit(STRIPE_DELAYED, &sh->state);
2632 set_bit(STRIPE_HANDLE, &sh->state);
2633 }
2634 }
2635 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002636 }
Dan Williamsa4456852007-07-09 11:56:43 -07002637 /* now if nothing is locked, and if we have enough data,
2638 * we can start a write request
2639 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002640 /* since handle_stripe can be called at any time we need to handle the
2641 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002642 * subsequent call wants to start a write request. raid_run_ops only
2643 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002644 * simultaneously. If this is not the case then new writes need to be
2645 * held off until the compute completes.
2646 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002647 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2648 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2649 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002650 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002651}
2652
NeilBrownd1688a62011-10-11 16:49:52 +11002653static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002654 struct stripe_head_state *s, int disks)
2655{
Dan Williamsecc65c92008-06-28 08:31:57 +10002656 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002657
Dan Williamsbd2ab672008-04-10 21:29:27 -07002658 set_bit(STRIPE_HANDLE, &sh->state);
2659
Dan Williamsecc65c92008-06-28 08:31:57 +10002660 switch (sh->check_state) {
2661 case check_state_idle:
2662 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002663 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002664 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002665 sh->check_state = check_state_run;
2666 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002667 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002668 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002669 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002670 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002671 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002672 /* fall through */
2673 case check_state_compute_result:
2674 sh->check_state = check_state_idle;
2675 if (!dev)
2676 dev = &sh->dev[sh->pd_idx];
2677
2678 /* check that a write has not made the stripe insync */
2679 if (test_bit(STRIPE_INSYNC, &sh->state))
2680 break;
2681
2682 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002683 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2684 BUG_ON(s->uptodate != disks);
2685
2686 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002687 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002688 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002689
Dan Williamsa4456852007-07-09 11:56:43 -07002690 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002691 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002692 break;
2693 case check_state_run:
2694 break; /* we will be called again upon completion */
2695 case check_state_check_result:
2696 sh->check_state = check_state_idle;
2697
2698 /* if a failure occurred during the check operation, leave
2699 * STRIPE_INSYNC not set and let the stripe be handled again
2700 */
2701 if (s->failed)
2702 break;
2703
2704 /* handle a successful check operation, if parity is correct
2705 * we are done. Otherwise update the mismatch count and repair
2706 * parity if !MD_RECOVERY_CHECK
2707 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002708 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002709 /* parity is correct (on disc,
2710 * not in buffer any more)
2711 */
2712 set_bit(STRIPE_INSYNC, &sh->state);
2713 else {
2714 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2715 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2716 /* don't try to repair!! */
2717 set_bit(STRIPE_INSYNC, &sh->state);
2718 else {
2719 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002720 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002721 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2722 set_bit(R5_Wantcompute,
2723 &sh->dev[sh->pd_idx].flags);
2724 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002725 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002726 s->uptodate++;
2727 }
2728 }
2729 break;
2730 case check_state_compute_run:
2731 break;
2732 default:
2733 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2734 __func__, sh->check_state,
2735 (unsigned long long) sh->sector);
2736 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002737 }
2738}
2739
2740
NeilBrownd1688a62011-10-11 16:49:52 +11002741static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002742 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002743 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002744{
Dan Williamsa4456852007-07-09 11:56:43 -07002745 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002746 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002747 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002748
2749 set_bit(STRIPE_HANDLE, &sh->state);
2750
2751 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002752
Dan Williamsa4456852007-07-09 11:56:43 -07002753 /* Want to check and possibly repair P and Q.
2754 * However there could be one 'failed' device, in which
2755 * case we can only check one of them, possibly using the
2756 * other to generate missing data
2757 */
2758
Dan Williamsd82dfee2009-07-14 13:40:57 -07002759 switch (sh->check_state) {
2760 case check_state_idle:
2761 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002762 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002763 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002764 * makes sense to check P (If anything else were failed,
2765 * we would have used P to recreate it).
2766 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002767 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002768 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002769 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002770 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002771 * anything, so it makes sense to check it
2772 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002773 if (sh->check_state == check_state_run)
2774 sh->check_state = check_state_run_pq;
2775 else
2776 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002777 }
Dan Williams36d1c642009-07-14 11:48:22 -07002778
Dan Williamsd82dfee2009-07-14 13:40:57 -07002779 /* discard potentially stale zero_sum_result */
2780 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002781
Dan Williamsd82dfee2009-07-14 13:40:57 -07002782 if (sh->check_state == check_state_run) {
2783 /* async_xor_zero_sum destroys the contents of P */
2784 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2785 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002786 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002787 if (sh->check_state >= check_state_run &&
2788 sh->check_state <= check_state_run_pq) {
2789 /* async_syndrome_zero_sum preserves P and Q, so
2790 * no need to mark them !uptodate here
2791 */
2792 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2793 break;
2794 }
Dan Williams36d1c642009-07-14 11:48:22 -07002795
Dan Williamsd82dfee2009-07-14 13:40:57 -07002796 /* we have 2-disk failure */
2797 BUG_ON(s->failed != 2);
2798 /* fall through */
2799 case check_state_compute_result:
2800 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002801
Dan Williamsd82dfee2009-07-14 13:40:57 -07002802 /* check that a write has not made the stripe insync */
2803 if (test_bit(STRIPE_INSYNC, &sh->state))
2804 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002805
2806 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002807 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002808 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002809 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002810 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002811 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07002812 s->locked++;
2813 set_bit(R5_LOCKED, &dev->flags);
2814 set_bit(R5_Wantwrite, &dev->flags);
2815 }
2816 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10002817 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07002818 s->locked++;
2819 set_bit(R5_LOCKED, &dev->flags);
2820 set_bit(R5_Wantwrite, &dev->flags);
2821 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002822 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002823 dev = &sh->dev[pd_idx];
2824 s->locked++;
2825 set_bit(R5_LOCKED, &dev->flags);
2826 set_bit(R5_Wantwrite, &dev->flags);
2827 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002828 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002829 dev = &sh->dev[qd_idx];
2830 s->locked++;
2831 set_bit(R5_LOCKED, &dev->flags);
2832 set_bit(R5_Wantwrite, &dev->flags);
2833 }
2834 clear_bit(STRIPE_DEGRADED, &sh->state);
2835
2836 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002837 break;
2838 case check_state_run:
2839 case check_state_run_q:
2840 case check_state_run_pq:
2841 break; /* we will be called again upon completion */
2842 case check_state_check_result:
2843 sh->check_state = check_state_idle;
2844
2845 /* handle a successful check operation, if parity is correct
2846 * we are done. Otherwise update the mismatch count and repair
2847 * parity if !MD_RECOVERY_CHECK
2848 */
2849 if (sh->ops.zero_sum_result == 0) {
2850 /* both parities are correct */
2851 if (!s->failed)
2852 set_bit(STRIPE_INSYNC, &sh->state);
2853 else {
2854 /* in contrast to the raid5 case we can validate
2855 * parity, but still have a failure to write
2856 * back
2857 */
2858 sh->check_state = check_state_compute_result;
2859 /* Returning at this point means that we may go
2860 * off and bring p and/or q uptodate again so
2861 * we make sure to check zero_sum_result again
2862 * to verify if p or q need writeback
2863 */
2864 }
2865 } else {
2866 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2867 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2868 /* don't try to repair!! */
2869 set_bit(STRIPE_INSYNC, &sh->state);
2870 else {
2871 int *target = &sh->ops.target;
2872
2873 sh->ops.target = -1;
2874 sh->ops.target2 = -1;
2875 sh->check_state = check_state_compute_run;
2876 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2877 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2878 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2879 set_bit(R5_Wantcompute,
2880 &sh->dev[pd_idx].flags);
2881 *target = pd_idx;
2882 target = &sh->ops.target2;
2883 s->uptodate++;
2884 }
2885 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2886 set_bit(R5_Wantcompute,
2887 &sh->dev[qd_idx].flags);
2888 *target = qd_idx;
2889 s->uptodate++;
2890 }
2891 }
2892 }
2893 break;
2894 case check_state_compute_run:
2895 break;
2896 default:
2897 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2898 __func__, sh->check_state,
2899 (unsigned long long) sh->sector);
2900 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002901 }
2902}
2903
NeilBrownd1688a62011-10-11 16:49:52 +11002904static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07002905{
2906 int i;
2907
2908 /* We have read all the blocks in this stripe and now we need to
2909 * copy some of them into a target stripe for expand.
2910 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002911 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002912 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2913 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002914 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002915 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002916 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002917 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002918
NeilBrown784052e2009-03-31 15:19:07 +11002919 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002920 sector_t s = raid5_compute_sector(conf, bn, 0,
2921 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002922 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002923 if (sh2 == NULL)
2924 /* so far only the early blocks of this stripe
2925 * have been requested. When later blocks
2926 * get requested, we will try again
2927 */
2928 continue;
2929 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2930 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2931 /* must have already done this block */
2932 release_stripe(sh2);
2933 continue;
2934 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002935
2936 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002937 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002938 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002939 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002940 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002941
Dan Williamsa4456852007-07-09 11:56:43 -07002942 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2943 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2944 for (j = 0; j < conf->raid_disks; j++)
2945 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10002946 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002947 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2948 break;
2949 if (j == conf->raid_disks) {
2950 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2951 set_bit(STRIPE_HANDLE, &sh2->state);
2952 }
2953 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002954
Dan Williamsa4456852007-07-09 11:56:43 -07002955 }
NeilBrowna2e08552007-09-11 15:23:36 -07002956 /* done submitting copies, wait for them to complete */
2957 if (tx) {
2958 async_tx_ack(tx);
2959 dma_wait_for_async_tx(tx);
2960 }
Dan Williamsa4456852007-07-09 11:56:43 -07002961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Dan Williams6bfe0b42008-04-30 00:52:32 -07002963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/*
2965 * handle_stripe - do things to a stripe.
2966 *
2967 * We lock the stripe and then examine the state of various bits
2968 * to see what needs to be done.
2969 * Possible results:
2970 * return some read request which now have data
2971 * return some write requests which are safely on disc
2972 * schedule a read on some buffers
2973 * schedule a write of some buffers
2974 * return confirmation of parity correctness
2975 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 * buffers are taken off read_list or write_list, and bh_cache buffers
2977 * get BH_Lock set before the stripe lock is released.
2978 *
2979 */
Dan Williamsa4456852007-07-09 11:56:43 -07002980
NeilBrownacfe7262011-07-27 11:00:36 +10002981static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07002982{
NeilBrownd1688a62011-10-11 16:49:52 +11002983 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002984 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10002985 struct r5dev *dev;
2986 int i;
NeilBrown16a53ec2006-06-26 00:27:38 -07002987
NeilBrownacfe7262011-07-27 11:00:36 +10002988 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002989
NeilBrownacfe7262011-07-27 11:00:36 +10002990 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
2991 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2992 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2993 s->failed_num[0] = -1;
2994 s->failed_num[1] = -1;
2995
2996 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07002997 rcu_read_lock();
NeilBrownc4c16632011-07-26 11:34:20 +10002998 spin_lock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07002999 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003000 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003001 sector_t first_bad;
3002 int bad_sectors;
3003 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003004
NeilBrown16a53ec2006-06-26 00:27:38 -07003005 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003006
Dan Williams45b42332007-07-09 11:56:43 -07003007 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003008 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003009 /* maybe we can reply to a read
3010 *
3011 * new wantfill requests are only permitted while
3012 * ops_complete_biofill is guaranteed to be inactive
3013 */
3014 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3015 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3016 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003017
3018 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003019 if (test_bit(R5_LOCKED, &dev->flags))
3020 s->locked++;
3021 if (test_bit(R5_UPTODATE, &dev->flags))
3022 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003023 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003024 s->compute++;
3025 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003026 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003027
NeilBrownacfe7262011-07-27 11:00:36 +10003028 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003029 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003030 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003031 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003032 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003033 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003034 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003035 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003036 }
Dan Williamsa4456852007-07-09 11:56:43 -07003037 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003038 s->written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003039 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown9283d8c2011-12-08 16:27:57 +11003040 if (rdev && test_bit(Faulty, &rdev->flags))
3041 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003042 if (rdev) {
3043 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3044 &first_bad, &bad_sectors);
3045 if (s->blocked_rdev == NULL
3046 && (test_bit(Blocked, &rdev->flags)
3047 || is_bad < 0)) {
3048 if (is_bad < 0)
3049 set_bit(BlockedBadBlocks,
3050 &rdev->flags);
3051 s->blocked_rdev = rdev;
3052 atomic_inc(&rdev->nr_pending);
3053 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003054 }
NeilBrown415e72d2010-06-17 17:25:21 +10003055 clear_bit(R5_Insync, &dev->flags);
3056 if (!rdev)
3057 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003058 else if (is_bad) {
3059 /* also not in-sync */
3060 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3061 /* treat as in-sync, but with a read error
3062 * which we can now try to correct
3063 */
3064 set_bit(R5_Insync, &dev->flags);
3065 set_bit(R5_ReadError, &dev->flags);
3066 }
3067 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003068 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003069 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003070 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003071 set_bit(R5_Insync, &dev->flags);
3072 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3073 test_bit(R5_Expanded, &dev->flags))
3074 /* If we've reshaped into here, we assume it is Insync.
3075 * We will shortly update recovery_offset to make
3076 * it official.
3077 */
3078 set_bit(R5_Insync, &dev->flags);
3079
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003080 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003081 clear_bit(R5_Insync, &dev->flags);
3082 if (!test_bit(Faulty, &rdev->flags)) {
3083 s->handle_bad_blocks = 1;
3084 atomic_inc(&rdev->nr_pending);
3085 } else
3086 clear_bit(R5_WriteError, &dev->flags);
3087 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003088 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003089 if (!test_bit(Faulty, &rdev->flags)) {
3090 s->handle_bad_blocks = 1;
3091 atomic_inc(&rdev->nr_pending);
3092 } else
3093 clear_bit(R5_MadeGood, &dev->flags);
3094 }
NeilBrown415e72d2010-06-17 17:25:21 +10003095 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003096 /* The ReadError flag will just be confusing now */
3097 clear_bit(R5_ReadError, &dev->flags);
3098 clear_bit(R5_ReWrite, &dev->flags);
3099 }
NeilBrown415e72d2010-06-17 17:25:21 +10003100 if (test_bit(R5_ReadError, &dev->flags))
3101 clear_bit(R5_Insync, &dev->flags);
3102 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003103 if (s->failed < 2)
3104 s->failed_num[s->failed] = i;
3105 s->failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003106 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003107 }
NeilBrownc4c16632011-07-26 11:34:20 +10003108 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07003109 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003110}
NeilBrownf4168852007-02-28 20:11:53 -08003111
NeilBrowncc940152011-07-26 11:35:35 +10003112static void handle_stripe(struct stripe_head *sh)
3113{
3114 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003115 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003116 int i;
NeilBrown84789552011-07-27 11:00:36 +10003117 int prexor;
3118 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003119 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003120
3121 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003122 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003123 /* already being handled, ensure it gets handled
3124 * again when current action finishes */
3125 set_bit(STRIPE_HANDLE, &sh->state);
3126 return;
3127 }
3128
3129 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3130 set_bit(STRIPE_SYNCING, &sh->state);
3131 clear_bit(STRIPE_INSYNC, &sh->state);
3132 }
3133 clear_bit(STRIPE_DELAYED, &sh->state);
3134
3135 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3136 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3137 (unsigned long long)sh->sector, sh->state,
3138 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3139 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003140
NeilBrownacfe7262011-07-27 11:00:36 +10003141 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003142
NeilBrownbc2607f2011-07-28 11:39:22 +10003143 if (s.handle_bad_blocks) {
3144 set_bit(STRIPE_HANDLE, &sh->state);
3145 goto finish;
3146 }
3147
NeilBrown474af965fe2011-07-27 11:00:36 +10003148 if (unlikely(s.blocked_rdev)) {
3149 if (s.syncing || s.expanding || s.expanded ||
3150 s.to_write || s.written) {
3151 set_bit(STRIPE_HANDLE, &sh->state);
3152 goto finish;
3153 }
3154 /* There is nothing for the blocked_rdev to block */
3155 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3156 s.blocked_rdev = NULL;
3157 }
3158
3159 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3160 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3161 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3162 }
3163
3164 pr_debug("locked=%d uptodate=%d to_read=%d"
3165 " to_write=%d failed=%d failed_num=%d,%d\n",
3166 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3167 s.failed_num[0], s.failed_num[1]);
3168 /* check if the array has lost more than max_degraded devices and,
3169 * if so, some requests might need to be failed.
3170 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003171 if (s.failed > conf->max_degraded) {
3172 sh->check_state = 0;
3173 sh->reconstruct_state = 0;
3174 if (s.to_read+s.to_write+s.written)
3175 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3176 if (s.syncing)
3177 handle_failed_sync(conf, sh, &s);
3178 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003179
3180 /*
3181 * might be able to return some write requests if the parity blocks
3182 * are safe, or on a failed drive
3183 */
3184 pdev = &sh->dev[sh->pd_idx];
3185 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3186 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3187 qdev = &sh->dev[sh->qd_idx];
3188 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3189 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3190 || conf->level < 6;
3191
3192 if (s.written &&
3193 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3194 && !test_bit(R5_LOCKED, &pdev->flags)
3195 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3196 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3197 && !test_bit(R5_LOCKED, &qdev->flags)
3198 && test_bit(R5_UPTODATE, &qdev->flags)))))
3199 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3200
3201 /* Now we might consider reading some blocks, either to check/generate
3202 * parity, or to satisfy requests
3203 * or to load a block that is being partially written.
3204 */
3205 if (s.to_read || s.non_overwrite
3206 || (conf->level == 6 && s.to_write && s.failed)
3207 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3208 handle_stripe_fill(sh, &s, disks);
3209
NeilBrown84789552011-07-27 11:00:36 +10003210 /* Now we check to see if any write operations have recently
3211 * completed
3212 */
3213 prexor = 0;
3214 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3215 prexor = 1;
3216 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3217 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3218 sh->reconstruct_state = reconstruct_state_idle;
3219
3220 /* All the 'written' buffers and the parity block are ready to
3221 * be written back to disk
3222 */
3223 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3224 BUG_ON(sh->qd_idx >= 0 &&
3225 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3226 for (i = disks; i--; ) {
3227 struct r5dev *dev = &sh->dev[i];
3228 if (test_bit(R5_LOCKED, &dev->flags) &&
3229 (i == sh->pd_idx || i == sh->qd_idx ||
3230 dev->written)) {
3231 pr_debug("Writing block %d\n", i);
3232 set_bit(R5_Wantwrite, &dev->flags);
3233 if (prexor)
3234 continue;
3235 if (!test_bit(R5_Insync, &dev->flags) ||
3236 ((i == sh->pd_idx || i == sh->qd_idx) &&
3237 s.failed == 0))
3238 set_bit(STRIPE_INSYNC, &sh->state);
3239 }
3240 }
3241 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3242 s.dec_preread_active = 1;
3243 }
3244
3245 /* Now to consider new write requests and what else, if anything
3246 * should be read. We do not handle new writes when:
3247 * 1/ A 'write' operation (copy+xor) is already in flight.
3248 * 2/ A 'check' operation is in flight, as it may clobber the parity
3249 * block.
3250 */
3251 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3252 handle_stripe_dirtying(conf, sh, &s, disks);
3253
3254 /* maybe we need to check and possibly fix the parity for this stripe
3255 * Any reads will already have been scheduled, so we just see if enough
3256 * data is available. The parity check is held off while parity
3257 * dependent operations are in flight.
3258 */
3259 if (sh->check_state ||
3260 (s.syncing && s.locked == 0 &&
3261 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3262 !test_bit(STRIPE_INSYNC, &sh->state))) {
3263 if (conf->level == 6)
3264 handle_parity_checks6(conf, sh, &s, disks);
3265 else
3266 handle_parity_checks5(conf, sh, &s, disks);
3267 }
NeilBrownc5a31002011-07-27 11:00:36 +10003268
3269 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3270 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3271 clear_bit(STRIPE_SYNCING, &sh->state);
3272 }
3273
3274 /* If the failed drives are just a ReadError, then we might need
3275 * to progress the repair/check process
3276 */
3277 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3278 for (i = 0; i < s.failed; i++) {
3279 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3280 if (test_bit(R5_ReadError, &dev->flags)
3281 && !test_bit(R5_LOCKED, &dev->flags)
3282 && test_bit(R5_UPTODATE, &dev->flags)
3283 ) {
3284 if (!test_bit(R5_ReWrite, &dev->flags)) {
3285 set_bit(R5_Wantwrite, &dev->flags);
3286 set_bit(R5_ReWrite, &dev->flags);
3287 set_bit(R5_LOCKED, &dev->flags);
3288 s.locked++;
3289 } else {
3290 /* let's read it back */
3291 set_bit(R5_Wantread, &dev->flags);
3292 set_bit(R5_LOCKED, &dev->flags);
3293 s.locked++;
3294 }
3295 }
3296 }
3297
3298
NeilBrown3687c062011-07-27 11:00:36 +10003299 /* Finish reconstruct operations initiated by the expansion process */
3300 if (sh->reconstruct_state == reconstruct_state_result) {
3301 struct stripe_head *sh_src
3302 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3303 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3304 /* sh cannot be written until sh_src has been read.
3305 * so arrange for sh to be delayed a little
3306 */
3307 set_bit(STRIPE_DELAYED, &sh->state);
3308 set_bit(STRIPE_HANDLE, &sh->state);
3309 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3310 &sh_src->state))
3311 atomic_inc(&conf->preread_active_stripes);
3312 release_stripe(sh_src);
3313 goto finish;
3314 }
3315 if (sh_src)
3316 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003317
NeilBrown3687c062011-07-27 11:00:36 +10003318 sh->reconstruct_state = reconstruct_state_idle;
3319 clear_bit(STRIPE_EXPANDING, &sh->state);
3320 for (i = conf->raid_disks; i--; ) {
3321 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3322 set_bit(R5_LOCKED, &sh->dev[i].flags);
3323 s.locked++;
3324 }
3325 }
3326
3327 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3328 !sh->reconstruct_state) {
3329 /* Need to write out all blocks after computing parity */
3330 sh->disks = conf->raid_disks;
3331 stripe_set_idx(sh->sector, conf, 0, sh);
3332 schedule_reconstruction(sh, &s, 1, 1);
3333 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3334 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3335 atomic_dec(&conf->reshape_stripes);
3336 wake_up(&conf->wait_for_overlap);
3337 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3338 }
3339
3340 if (s.expanding && s.locked == 0 &&
3341 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3342 handle_stripe_expansion(conf, sh);
3343
3344finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003345 /* wait for this device to become unblocked */
NeilBrown43220aa2011-08-31 12:49:14 +10003346 if (conf->mddev->external && unlikely(s.blocked_rdev))
NeilBrownc5709ef2011-07-26 11:35:20 +10003347 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003348
NeilBrownbc2607f2011-07-28 11:39:22 +10003349 if (s.handle_bad_blocks)
3350 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003351 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003352 struct r5dev *dev = &sh->dev[i];
3353 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3354 /* We own a safe reference to the rdev */
3355 rdev = conf->disks[i].rdev;
3356 if (!rdev_set_badblocks(rdev, sh->sector,
3357 STRIPE_SECTORS, 0))
3358 md_error(conf->mddev, rdev);
3359 rdev_dec_pending(rdev, conf->mddev);
3360 }
NeilBrownb84db562011-07-28 11:39:23 +10003361 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3362 rdev = conf->disks[i].rdev;
3363 rdev_clear_badblocks(rdev, sh->sector,
3364 STRIPE_SECTORS);
3365 rdev_dec_pending(rdev, conf->mddev);
3366 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003367 }
3368
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003369 if (s.ops_request)
3370 raid_run_ops(sh, s.ops_request);
3371
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003372 ops_run_io(sh, &s);
3373
NeilBrownc5709ef2011-07-26 11:35:20 +10003374 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003375 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003376 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003377 * have actually been submitted.
3378 */
3379 atomic_dec(&conf->preread_active_stripes);
3380 if (atomic_read(&conf->preread_active_stripes) <
3381 IO_THRESHOLD)
3382 md_wakeup_thread(conf->mddev->thread);
3383 }
3384
NeilBrownc5709ef2011-07-26 11:35:20 +10003385 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003386
Dan Williams257a4b42011-11-08 16:22:06 +11003387 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003388}
3389
NeilBrownd1688a62011-10-11 16:49:52 +11003390static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3393 while (!list_empty(&conf->delayed_list)) {
3394 struct list_head *l = conf->delayed_list.next;
3395 struct stripe_head *sh;
3396 sh = list_entry(l, struct stripe_head, lru);
3397 list_del_init(l);
3398 clear_bit(STRIPE_DELAYED, &sh->state);
3399 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3400 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003401 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
NeilBrown482c0832011-04-18 18:25:42 +10003403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
NeilBrownd1688a62011-10-11 16:49:52 +11003406static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003407{
3408 /* device_lock is held */
3409 struct list_head head;
3410 list_add(&head, &conf->bitmap_list);
3411 list_del_init(&conf->bitmap_list);
3412 while (!list_empty(&head)) {
3413 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3414 list_del_init(&sh->lru);
3415 atomic_inc(&sh->count);
3416 __release_stripe(conf, sh);
3417 }
3418}
3419
NeilBrownfd01b882011-10-11 16:47:53 +11003420int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003421{
NeilBrownd1688a62011-10-11 16:49:52 +11003422 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003423
3424 /* No difference between reads and writes. Just check
3425 * how busy the stripe_cache is
3426 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003427
NeilBrownf022b2f2006-10-03 01:15:56 -07003428 if (conf->inactive_blocked)
3429 return 1;
3430 if (conf->quiesce)
3431 return 1;
3432 if (list_empty_careful(&conf->inactive_list))
3433 return 1;
3434
3435 return 0;
3436}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003437EXPORT_SYMBOL_GPL(md_raid5_congested);
3438
3439static int raid5_congested(void *data, int bits)
3440{
NeilBrownfd01b882011-10-11 16:47:53 +11003441 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003442
3443 return mddev_congested(mddev, bits) ||
3444 md_raid5_congested(mddev, bits);
3445}
NeilBrownf022b2f2006-10-03 01:15:56 -07003446
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003447/* We want read requests to align with chunks where possible,
3448 * but write requests don't need to.
3449 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003450static int raid5_mergeable_bvec(struct request_queue *q,
3451 struct bvec_merge_data *bvm,
3452 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003453{
NeilBrownfd01b882011-10-11 16:47:53 +11003454 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003455 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003456 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003457 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003458 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003459
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003460 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003461 return biovec->bv_len; /* always allow writes to be mergeable */
3462
Andre Noll664e7c42009-06-18 08:45:27 +10003463 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3464 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003465 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3466 if (max < 0) max = 0;
3467 if (max <= biovec->bv_len && bio_sectors == 0)
3468 return biovec->bv_len;
3469 else
3470 return max;
3471}
3472
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003473
NeilBrownfd01b882011-10-11 16:47:53 +11003474static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003475{
3476 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003477 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003478 unsigned int bio_sectors = bio->bi_size >> 9;
3479
Andre Noll664e7c42009-06-18 08:45:27 +10003480 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3481 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003482 return chunk_sectors >=
3483 ((sector & (chunk_sectors - 1)) + bio_sectors);
3484}
3485
3486/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003487 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3488 * later sampled by raid5d.
3489 */
NeilBrownd1688a62011-10-11 16:49:52 +11003490static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003491{
3492 unsigned long flags;
3493
3494 spin_lock_irqsave(&conf->device_lock, flags);
3495
3496 bi->bi_next = conf->retry_read_aligned_list;
3497 conf->retry_read_aligned_list = bi;
3498
3499 spin_unlock_irqrestore(&conf->device_lock, flags);
3500 md_wakeup_thread(conf->mddev->thread);
3501}
3502
3503
NeilBrownd1688a62011-10-11 16:49:52 +11003504static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003505{
3506 struct bio *bi;
3507
3508 bi = conf->retry_read_aligned;
3509 if (bi) {
3510 conf->retry_read_aligned = NULL;
3511 return bi;
3512 }
3513 bi = conf->retry_read_aligned_list;
3514 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003515 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003516 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003517 /*
3518 * this sets the active strip count to 1 and the processed
3519 * strip count to zero (upper 8 bits)
3520 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003521 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003522 }
3523
3524 return bi;
3525}
3526
3527
3528/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003529 * The "raid5_align_endio" should check if the read succeeded and if it
3530 * did, call bio_endio on the original bio (having bio_put the new bio
3531 * first).
3532 * If the read failed..
3533 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003534static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003535{
3536 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003537 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003538 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003539 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003540 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003541
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003542 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003543
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003544 rdev = (void*)raid_bi->bi_next;
3545 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003546 mddev = rdev->mddev;
3547 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003548
3549 rdev_dec_pending(rdev, conf->mddev);
3550
3551 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003552 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003553 if (atomic_dec_and_test(&conf->active_aligned_reads))
3554 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003555 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003556 }
3557
3558
Dan Williams45b42332007-07-09 11:56:43 -07003559 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003560
3561 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003562}
3563
Neil Brown387bb172007-02-08 14:20:29 -08003564static int bio_fits_rdev(struct bio *bi)
3565{
Jens Axboe165125e2007-07-24 09:28:11 +02003566 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003567
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003568 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003569 return 0;
3570 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003571 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003572 return 0;
3573
3574 if (q->merge_bvec_fn)
3575 /* it's too hard to apply the merge_bvec_fn at this stage,
3576 * just just give up
3577 */
3578 return 0;
3579
3580 return 1;
3581}
3582
3583
NeilBrownfd01b882011-10-11 16:47:53 +11003584static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003585{
NeilBrownd1688a62011-10-11 16:49:52 +11003586 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003587 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003588 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003589 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003590 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003591
3592 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003593 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003594 return 0;
3595 }
3596 /*
NeilBrowna167f662010-10-26 18:31:13 +11003597 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003598 */
NeilBrowna167f662010-10-26 18:31:13 +11003599 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003600 if (!align_bi)
3601 return 0;
3602 /*
3603 * set bi_end_io to a new function, and set bi_private to the
3604 * original bio.
3605 */
3606 align_bi->bi_end_io = raid5_align_endio;
3607 align_bi->bi_private = raid_bio;
3608 /*
3609 * compute position
3610 */
NeilBrown112bf892009-03-31 14:39:38 +11003611 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3612 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003613 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003614
NeilBrown671488c2011-12-23 10:17:52 +11003615 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003616 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003617 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3618 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3619 rdev->recovery_offset < end_sector) {
3620 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3621 if (rdev &&
3622 (test_bit(Faulty, &rdev->flags) ||
3623 !(test_bit(In_sync, &rdev->flags) ||
3624 rdev->recovery_offset >= end_sector)))
3625 rdev = NULL;
3626 }
3627 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003628 sector_t first_bad;
3629 int bad_sectors;
3630
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003631 atomic_inc(&rdev->nr_pending);
3632 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003633 raid_bio->bi_next = (void*)rdev;
3634 align_bi->bi_bdev = rdev->bdev;
3635 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3636 align_bi->bi_sector += rdev->data_offset;
3637
NeilBrown31c176e2011-07-28 11:39:22 +10003638 if (!bio_fits_rdev(align_bi) ||
3639 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3640 &first_bad, &bad_sectors)) {
3641 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003642 bio_put(align_bi);
3643 rdev_dec_pending(rdev, mddev);
3644 return 0;
3645 }
3646
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003647 spin_lock_irq(&conf->device_lock);
3648 wait_event_lock_irq(conf->wait_for_stripe,
3649 conf->quiesce == 0,
3650 conf->device_lock, /* nothing */);
3651 atomic_inc(&conf->active_aligned_reads);
3652 spin_unlock_irq(&conf->device_lock);
3653
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003654 generic_make_request(align_bi);
3655 return 1;
3656 } else {
3657 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003658 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003659 return 0;
3660 }
3661}
3662
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003663/* __get_priority_stripe - get the next stripe to process
3664 *
3665 * Full stripe writes are allowed to pass preread active stripes up until
3666 * the bypass_threshold is exceeded. In general the bypass_count
3667 * increments when the handle_list is handled before the hold_list; however, it
3668 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3669 * stripe with in flight i/o. The bypass_count will be reset when the
3670 * head of the hold_list has changed, i.e. the head was promoted to the
3671 * handle_list.
3672 */
NeilBrownd1688a62011-10-11 16:49:52 +11003673static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003674{
3675 struct stripe_head *sh;
3676
3677 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3678 __func__,
3679 list_empty(&conf->handle_list) ? "empty" : "busy",
3680 list_empty(&conf->hold_list) ? "empty" : "busy",
3681 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3682
3683 if (!list_empty(&conf->handle_list)) {
3684 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3685
3686 if (list_empty(&conf->hold_list))
3687 conf->bypass_count = 0;
3688 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3689 if (conf->hold_list.next == conf->last_hold)
3690 conf->bypass_count++;
3691 else {
3692 conf->last_hold = conf->hold_list.next;
3693 conf->bypass_count -= conf->bypass_threshold;
3694 if (conf->bypass_count < 0)
3695 conf->bypass_count = 0;
3696 }
3697 }
3698 } else if (!list_empty(&conf->hold_list) &&
3699 ((conf->bypass_threshold &&
3700 conf->bypass_count > conf->bypass_threshold) ||
3701 atomic_read(&conf->pending_full_writes) == 0)) {
3702 sh = list_entry(conf->hold_list.next,
3703 typeof(*sh), lru);
3704 conf->bypass_count -= conf->bypass_threshold;
3705 if (conf->bypass_count < 0)
3706 conf->bypass_count = 0;
3707 } else
3708 return NULL;
3709
3710 list_del_init(&sh->lru);
3711 atomic_inc(&sh->count);
3712 BUG_ON(atomic_read(&sh->count) != 1);
3713 return sh;
3714}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003715
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07003716static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
NeilBrownd1688a62011-10-11 16:49:52 +11003718 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003719 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 sector_t new_sector;
3721 sector_t logical_sector, last_sector;
3722 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003723 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003724 int remaining;
NeilBrown7c13edc2011-04-18 18:25:43 +10003725 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Tejun Heoe9c74692010-09-03 11:56:18 +02003727 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3728 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003729 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07003730 }
3731
NeilBrown3d310eb2005-06-21 17:17:26 -07003732 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003733
NeilBrown802ba062006-12-13 00:34:13 -08003734 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003735 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11003736 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02003737 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3740 last_sector = bi->bi_sector + (bi->bi_size>>9);
3741 bi->bi_next = NULL;
3742 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003743
NeilBrown7c13edc2011-04-18 18:25:43 +10003744 plugged = mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3746 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003747 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003748 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003749
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003750 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003751 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003752 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003753 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003754 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003755 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003756 * 64bit on a 32bit platform, and so it might be
3757 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02003758 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003759 * the lock is dropped, so once we get a reference
3760 * to the stripe that we think it is, we will have
3761 * to check again.
3762 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003763 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003764 if (mddev->delta_disks < 0
3765 ? logical_sector < conf->reshape_progress
3766 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003767 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003768 previous = 1;
3769 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003770 if (mddev->delta_disks < 0
3771 ? logical_sector < conf->reshape_safe
3772 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003773 spin_unlock_irq(&conf->device_lock);
3774 schedule();
3775 goto retry;
3776 }
3777 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003778 spin_unlock_irq(&conf->device_lock);
3779 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003780 data_disks = disks - conf->max_degraded;
3781
NeilBrown112bf892009-03-31 14:39:38 +11003782 new_sector = raid5_compute_sector(conf, logical_sector,
3783 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003784 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10003785 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 (unsigned long long)new_sector,
3787 (unsigned long long)logical_sector);
3788
NeilBrownb5663ba2009-03-31 14:39:38 +11003789 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003790 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003792 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003793 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003794 * stripe, so we must do the range check again.
3795 * Expansion could still move past after this
3796 * test, but as we are holding a reference to
3797 * 'sh', we know that if that happens,
3798 * STRIPE_EXPANDING will get set and the expansion
3799 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003800 */
3801 int must_retry = 0;
3802 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003803 if (mddev->delta_disks < 0
3804 ? logical_sector >= conf->reshape_progress
3805 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003806 /* mismatch, need to try again */
3807 must_retry = 1;
3808 spin_unlock_irq(&conf->device_lock);
3809 if (must_retry) {
3810 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003811 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003812 goto retry;
3813 }
3814 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003815
Namhyung Kimffd96e32011-07-18 17:38:51 +10003816 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10003817 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003818 logical_sector < mddev->suspend_hi) {
3819 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003820 /* As the suspend_* range is controlled by
3821 * userspace, we want an interruptible
3822 * wait.
3823 */
3824 flush_signals(current);
3825 prepare_to_wait(&conf->wait_for_overlap,
3826 &w, TASK_INTERRUPTIBLE);
3827 if (logical_sector >= mddev->suspend_lo &&
3828 logical_sector < mddev->suspend_hi)
3829 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003830 goto retry;
3831 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003832
3833 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10003834 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003835 /* Stripe is busy expanding or
3836 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 * and wait a while
3838 */
NeilBrown482c0832011-04-18 18:25:42 +10003839 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 release_stripe(sh);
3841 schedule();
3842 goto retry;
3843 }
3844 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003845 set_bit(STRIPE_HANDLE, &sh->state);
3846 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02003847 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11003848 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3849 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 } else {
3852 /* cannot get stripe for read-ahead, just give-up */
3853 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3854 finish_wait(&conf->wait_for_overlap, &w);
3855 break;
3856 }
3857
3858 }
NeilBrown7c13edc2011-04-18 18:25:43 +10003859 if (!plugged)
3860 md_wakeup_thread(mddev->thread);
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003863 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003864 spin_unlock_irq(&conf->device_lock);
3865 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
NeilBrown16a53ec2006-06-26 00:27:38 -07003867 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003869
Neil Brown0e13fe232008-06-28 08:31:20 +10003870 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873
NeilBrownfd01b882011-10-11 16:47:53 +11003874static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11003875
NeilBrownfd01b882011-10-11 16:47:53 +11003876static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
NeilBrown52c03292006-06-26 00:27:43 -07003878 /* reshaping is quite different to recovery/resync so it is
3879 * handled quite separately ... here.
3880 *
3881 * On each call to sync_request, we gather one chunk worth of
3882 * destination stripes and flag them as expanding.
3883 * Then we find all the source stripes and request reads.
3884 * As the reads complete, handle_stripe will copy the data
3885 * into the destination stripe and release that stripe.
3886 */
NeilBrownd1688a62011-10-11 16:49:52 +11003887 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003889 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003890 int raid_disks = conf->previous_raid_disks;
3891 int data_disks = raid_disks - conf->max_degraded;
3892 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003893 int i;
3894 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003895 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003896 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003897 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003898 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003899
NeilBrownfef9c612009-03-31 15:16:46 +11003900 if (sector_nr == 0) {
3901 /* If restarting in the middle, skip the initial sectors */
3902 if (mddev->delta_disks < 0 &&
3903 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3904 sector_nr = raid5_size(mddev, 0, 0)
3905 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10003906 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11003907 conf->reshape_progress > 0)
3908 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003909 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003910 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11003911 mddev->curr_resync_completed = sector_nr;
3912 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11003913 *skipped = 1;
3914 return sector_nr;
3915 }
NeilBrown52c03292006-06-26 00:27:43 -07003916 }
3917
NeilBrown7a661382009-03-31 15:21:40 +11003918 /* We need to process a full chunk at a time.
3919 * If old and new chunk sizes differ, we need to process the
3920 * largest of these
3921 */
Andre Noll664e7c42009-06-18 08:45:27 +10003922 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3923 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003924 else
Andre Noll9d8f0362009-06-18 08:45:01 +10003925 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003926
NeilBrown52c03292006-06-26 00:27:43 -07003927 /* we update the metadata when there is more than 3Meg
3928 * in the block range (that is rather arbitrary, should
3929 * probably be time based) or when the data about to be
3930 * copied would over-write the source of the data at
3931 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003932 * i.e. one new_stripe along from reshape_progress new_maps
3933 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003934 */
NeilBrownfef9c612009-03-31 15:16:46 +11003935 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003936 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003937 readpos = conf->reshape_progress;
3938 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003939 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003940 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003941 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10003942 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11003943 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003944 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003945 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003946 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10003947 readpos -= min_t(sector_t, reshape_sectors, readpos);
3948 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11003949 }
NeilBrown52c03292006-06-26 00:27:43 -07003950
NeilBrownc8f517c2009-03-31 15:28:40 +11003951 /* 'writepos' is the most advanced device address we might write.
3952 * 'readpos' is the least advanced device address we might read.
3953 * 'safepos' is the least address recorded in the metadata as having
3954 * been reshaped.
3955 * If 'readpos' is behind 'writepos', then there is no way that we can
3956 * ensure safety in the face of a crash - that must be done by userspace
3957 * making a backup of the data. So in that case there is no particular
3958 * rush to update metadata.
3959 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3960 * update the metadata to advance 'safepos' to match 'readpos' so that
3961 * we can be safe in the event of a crash.
3962 * So we insist on updating metadata if safepos is behind writepos and
3963 * readpos is beyond writepos.
3964 * In any case, update the metadata every 10 seconds.
3965 * Maybe that number should be configurable, but I'm not sure it is
3966 * worth it.... maybe it could be a multiple of safemode_delay???
3967 */
NeilBrownfef9c612009-03-31 15:16:46 +11003968 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003969 ? (safepos > writepos && readpos < writepos)
3970 : (safepos < writepos && readpos > writepos)) ||
3971 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003972 /* Cannot proceed until we've updated the superblock... */
3973 wait_event(conf->wait_for_overlap,
3974 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003975 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11003976 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11003977 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003978 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003979 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003980 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003981 kthread_should_stop());
3982 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003983 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003984 spin_unlock_irq(&conf->device_lock);
3985 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10003986 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07003987 }
3988
NeilBrownec32a2b2009-03-31 15:17:38 +11003989 if (mddev->delta_disks < 0) {
3990 BUG_ON(conf->reshape_progress == 0);
3991 stripe_addr = writepos;
3992 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003993 ~((sector_t)reshape_sectors - 1))
3994 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003995 != sector_nr);
3996 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003997 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003998 stripe_addr = sector_nr;
3999 }
NeilBrownab69ae12009-03-31 15:26:47 +11004000 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004001 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004002 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004003 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004004 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004005 set_bit(STRIPE_EXPANDING, &sh->state);
4006 atomic_inc(&conf->reshape_stripes);
4007 /* If any of this stripe is beyond the end of the old
4008 * array, then we need to zero those blocks
4009 */
4010 for (j=sh->disks; j--;) {
4011 sector_t s;
4012 if (j == sh->pd_idx)
4013 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004014 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004015 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004016 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004017 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004018 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004019 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004020 continue;
4021 }
4022 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4023 set_bit(R5_Expanded, &sh->dev[j].flags);
4024 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4025 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004026 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004027 set_bit(STRIPE_EXPAND_READY, &sh->state);
4028 set_bit(STRIPE_HANDLE, &sh->state);
4029 }
NeilBrownab69ae12009-03-31 15:26:47 +11004030 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004031 }
4032 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004033 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004034 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004035 else
NeilBrown7a661382009-03-31 15:21:40 +11004036 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004037 spin_unlock_irq(&conf->device_lock);
4038 /* Ok, those stripe are ready. We can start scheduling
4039 * reads on the source stripes.
4040 * The source stripes are determined by mapping the first and last
4041 * block on the destination stripes.
4042 */
NeilBrown52c03292006-06-26 00:27:43 -07004043 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004044 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004045 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004046 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004047 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004048 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004049 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004050 if (last_sector >= mddev->dev_sectors)
4051 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004052 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004053 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004054 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4055 set_bit(STRIPE_HANDLE, &sh->state);
4056 release_stripe(sh);
4057 first_sector += STRIPE_SECTORS;
4058 }
NeilBrownab69ae12009-03-31 15:26:47 +11004059 /* Now that the sources are clearly marked, we can release
4060 * the destination stripes
4061 */
4062 while (!list_empty(&stripes)) {
4063 sh = list_entry(stripes.next, struct stripe_head, lru);
4064 list_del_init(&sh->lru);
4065 release_stripe(sh);
4066 }
NeilBrownc6207272008-02-06 01:39:52 -08004067 /* If this takes us to the resync_max point where we have to pause,
4068 * then we need to write out the superblock.
4069 */
NeilBrown7a661382009-03-31 15:21:40 +11004070 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004071 if ((sector_nr - mddev->curr_resync_completed) * 2
4072 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004073 /* Cannot proceed until we've updated the superblock... */
4074 wait_event(conf->wait_for_overlap,
4075 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004076 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004077 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004078 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004079 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4080 md_wakeup_thread(mddev->thread);
4081 wait_event(mddev->sb_wait,
4082 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4083 || kthread_should_stop());
4084 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004085 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004086 spin_unlock_irq(&conf->device_lock);
4087 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004088 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004089 }
NeilBrown7a661382009-03-31 15:21:40 +11004090 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004091}
4092
4093/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004094static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004095{
NeilBrownd1688a62011-10-11 16:49:52 +11004096 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004097 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004098 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004099 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004100 int still_degraded = 0;
4101 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
NeilBrown72626682005-09-09 16:23:54 -07004103 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004105
NeilBrown29269552006-03-27 01:18:10 -08004106 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4107 end_reshape(conf);
4108 return 0;
4109 }
NeilBrown72626682005-09-09 16:23:54 -07004110
4111 if (mddev->curr_resync < max_sector) /* aborted */
4112 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4113 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004114 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004115 conf->fullsync = 0;
4116 bitmap_close_sync(mddev->bitmap);
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return 0;
4119 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004120
NeilBrown64bd6602009-08-03 10:59:58 +10004121 /* Allow raid5_quiesce to complete */
4122 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4123
NeilBrown52c03292006-06-26 00:27:43 -07004124 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4125 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004126
NeilBrownc6207272008-02-06 01:39:52 -08004127 /* No need to check resync_max as we never do more than one
4128 * stripe, and as resync_max will always be on a chunk boundary,
4129 * if the check in md_do_sync didn't fire, there is no chance
4130 * of overstepping resync_max here
4131 */
4132
NeilBrown16a53ec2006-06-26 00:27:38 -07004133 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 * to resync, then assert that we are finished, because there is
4135 * nothing we can do.
4136 */
NeilBrown3285edf2006-06-26 00:27:55 -07004137 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004138 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004139 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004140 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return rv;
4142 }
NeilBrown72626682005-09-09 16:23:54 -07004143 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004144 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004145 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4146 /* we can skip this block, and probably more */
4147 sync_blocks /= STRIPE_SECTORS;
4148 *skipped = 1;
4149 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
NeilBrownb47490c2008-02-06 01:39:50 -08004152
4153 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4154
NeilBrowna8c906c2009-06-09 14:39:59 +10004155 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004157 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004159 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004161 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004163 /* Need to check if array will still be degraded after recovery/resync
4164 * We don't need to check the 'failed' flag as when that gets set,
4165 * recovery aborts.
4166 */
NeilBrownf001a702009-06-09 14:30:31 +10004167 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004168 if (conf->disks[i].rdev == NULL)
4169 still_degraded = 1;
4170
4171 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4172
NeilBrown83206d62011-07-26 11:19:49 +10004173 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
NeilBrown14425772009-10-16 15:55:25 +11004175 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 release_stripe(sh);
4177
4178 return STRIPE_SECTORS;
4179}
4180
NeilBrownd1688a62011-10-11 16:49:52 +11004181static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004182{
4183 /* We may not be able to submit a whole bio at once as there
4184 * may not be enough stripe_heads available.
4185 * We cannot pre-allocate enough stripe_heads as we may need
4186 * more than exist in the cache (if we allow ever large chunks).
4187 * So we do one stripe head at a time and record in
4188 * ->bi_hw_segments how many have been done.
4189 *
4190 * We *know* that this entire raid_bio is in one chunk, so
4191 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4192 */
4193 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004194 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004195 sector_t sector, logical_sector, last_sector;
4196 int scnt = 0;
4197 int remaining;
4198 int handled = 0;
4199
4200 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004201 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004202 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004203 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4204
4205 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004206 logical_sector += STRIPE_SECTORS,
4207 sector += STRIPE_SECTORS,
4208 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004209
Jens Axboe960e7392008-08-15 10:41:18 +02004210 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004211 /* already done this stripe */
4212 continue;
4213
NeilBrowna8c906c2009-06-09 14:39:59 +10004214 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004215
4216 if (!sh) {
4217 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004218 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004219 conf->retry_read_aligned = raid_bio;
4220 return handled;
4221 }
4222
4223 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004224 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4225 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004226 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004227 conf->retry_read_aligned = raid_bio;
4228 return handled;
4229 }
4230
Dan Williams36d1c642009-07-14 11:48:22 -07004231 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004232 release_stripe(sh);
4233 handled++;
4234 }
4235 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004236 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004237 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004238 if (remaining == 0)
4239 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004240 if (atomic_dec_and_test(&conf->active_aligned_reads))
4241 wake_up(&conf->wait_for_stripe);
4242 return handled;
4243}
4244
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246/*
4247 * This is our raid5 kernel thread.
4248 *
4249 * We scan the hash table for stripes which can be handled now.
4250 * During the scan, completed stripes are saved for us by the interrupt
4251 * handler, so that they will not have to wait for our next wakeup.
4252 */
NeilBrownfd01b882011-10-11 16:47:53 +11004253static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004256 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004258 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Dan Williams45b42332007-07-09 11:56:43 -07004260 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004264 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 handled = 0;
4266 spin_lock_irq(&conf->device_lock);
4267 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004268 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
NeilBrown7c13edc2011-04-18 18:25:43 +10004270 if (atomic_read(&mddev->plug_cnt) == 0 &&
4271 !list_empty(&conf->bitmap_list)) {
4272 /* Now is a good time to flush some bitmap updates */
4273 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004274 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004275 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004276 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004277 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004278 activate_bit_delay(conf);
4279 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004280 if (atomic_read(&mddev->plug_cnt) == 0)
4281 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004282
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004283 while ((bio = remove_bio_from_retry(conf))) {
4284 int ok;
4285 spin_unlock_irq(&conf->device_lock);
4286 ok = retry_aligned_read(conf, bio);
4287 spin_lock_irq(&conf->device_lock);
4288 if (!ok)
4289 break;
4290 handled++;
4291 }
4292
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004293 sh = __get_priority_stripe(conf);
4294
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004295 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 spin_unlock_irq(&conf->device_lock);
4298
4299 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004300 handle_stripe(sh);
4301 release_stripe(sh);
4302 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
NeilBrownde393cd2011-07-28 11:31:48 +10004304 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4305 md_check_recovery(mddev);
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 spin_lock_irq(&conf->device_lock);
4308 }
Dan Williams45b42332007-07-09 11:56:43 -07004309 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 spin_unlock_irq(&conf->device_lock);
4312
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004313 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004314 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Dan Williams45b42332007-07-09 11:56:43 -07004316 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
NeilBrown3f294f42005-11-08 21:39:25 -08004319static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004320raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004321{
NeilBrownd1688a62011-10-11 16:49:52 +11004322 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004323 if (conf)
4324 return sprintf(page, "%d\n", conf->max_nr_stripes);
4325 else
4326 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004327}
4328
NeilBrownc41d4ac2010-06-01 19:37:24 +10004329int
NeilBrownfd01b882011-10-11 16:47:53 +11004330raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004331{
NeilBrownd1688a62011-10-11 16:49:52 +11004332 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004333 int err;
4334
4335 if (size <= 16 || size > 32768)
4336 return -EINVAL;
4337 while (size < conf->max_nr_stripes) {
4338 if (drop_one_stripe(conf))
4339 conf->max_nr_stripes--;
4340 else
4341 break;
4342 }
4343 err = md_allow_write(mddev);
4344 if (err)
4345 return err;
4346 while (size > conf->max_nr_stripes) {
4347 if (grow_one_stripe(conf))
4348 conf->max_nr_stripes++;
4349 else break;
4350 }
4351 return 0;
4352}
4353EXPORT_SYMBOL(raid5_set_cache_size);
4354
NeilBrown3f294f42005-11-08 21:39:25 -08004355static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004356raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004357{
NeilBrownd1688a62011-10-11 16:49:52 +11004358 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004359 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004360 int err;
4361
NeilBrown3f294f42005-11-08 21:39:25 -08004362 if (len >= PAGE_SIZE)
4363 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004364 if (!conf)
4365 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004366
Dan Williams4ef197d82008-04-28 02:15:54 -07004367 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004368 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004369 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004370 if (err)
4371 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004372 return len;
4373}
NeilBrown007583c2005-11-08 21:39:30 -08004374
NeilBrown96de1e62005-11-08 21:39:39 -08004375static struct md_sysfs_entry
4376raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4377 raid5_show_stripe_cache_size,
4378 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004379
4380static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004381raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004382{
NeilBrownd1688a62011-10-11 16:49:52 +11004383 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004384 if (conf)
4385 return sprintf(page, "%d\n", conf->bypass_threshold);
4386 else
4387 return 0;
4388}
4389
4390static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004391raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004392{
NeilBrownd1688a62011-10-11 16:49:52 +11004393 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004394 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004395 if (len >= PAGE_SIZE)
4396 return -EINVAL;
4397 if (!conf)
4398 return -ENODEV;
4399
Dan Williams4ef197d82008-04-28 02:15:54 -07004400 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004401 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004402 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004403 return -EINVAL;
4404 conf->bypass_threshold = new;
4405 return len;
4406}
4407
4408static struct md_sysfs_entry
4409raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4410 S_IRUGO | S_IWUSR,
4411 raid5_show_preread_threshold,
4412 raid5_store_preread_threshold);
4413
4414static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004415stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004416{
NeilBrownd1688a62011-10-11 16:49:52 +11004417 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004418 if (conf)
4419 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4420 else
4421 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004422}
4423
NeilBrown96de1e62005-11-08 21:39:39 -08004424static struct md_sysfs_entry
4425raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004426
NeilBrown007583c2005-11-08 21:39:30 -08004427static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004428 &raid5_stripecache_size.attr,
4429 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004430 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004431 NULL,
4432};
NeilBrown007583c2005-11-08 21:39:30 -08004433static struct attribute_group raid5_attrs_group = {
4434 .name = NULL,
4435 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004436};
4437
Dan Williams80c3a6c2009-03-17 18:10:40 -07004438static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004439raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004440{
NeilBrownd1688a62011-10-11 16:49:52 +11004441 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004442
4443 if (!sectors)
4444 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004445 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004446 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004447 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004448
Andre Noll9d8f0362009-06-18 08:45:01 +10004449 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004450 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004451 return sectors * (raid_disks - conf->max_degraded);
4452}
4453
NeilBrownd1688a62011-10-11 16:49:52 +11004454static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004455{
4456 struct raid5_percpu *percpu;
4457 unsigned long cpu;
4458
4459 if (!conf->percpu)
4460 return;
4461
4462 get_online_cpus();
4463 for_each_possible_cpu(cpu) {
4464 percpu = per_cpu_ptr(conf->percpu, cpu);
4465 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004466 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004467 }
4468#ifdef CONFIG_HOTPLUG_CPU
4469 unregister_cpu_notifier(&conf->cpu_notify);
4470#endif
4471 put_online_cpus();
4472
4473 free_percpu(conf->percpu);
4474}
4475
NeilBrownd1688a62011-10-11 16:49:52 +11004476static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004477{
4478 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004479 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004480 kfree(conf->disks);
4481 kfree(conf->stripe_hashtbl);
4482 kfree(conf);
4483}
4484
Dan Williams36d1c642009-07-14 11:48:22 -07004485#ifdef CONFIG_HOTPLUG_CPU
4486static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4487 void *hcpu)
4488{
NeilBrownd1688a62011-10-11 16:49:52 +11004489 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004490 long cpu = (long)hcpu;
4491 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4492
4493 switch (action) {
4494 case CPU_UP_PREPARE:
4495 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004496 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004497 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004498 if (!percpu->scribble)
4499 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4500
4501 if (!percpu->scribble ||
4502 (conf->level == 6 && !percpu->spare_page)) {
4503 safe_put_page(percpu->spare_page);
4504 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004505 pr_err("%s: failed memory allocation for cpu%ld\n",
4506 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004507 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004508 }
4509 break;
4510 case CPU_DEAD:
4511 case CPU_DEAD_FROZEN:
4512 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004513 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004514 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004515 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004516 break;
4517 default:
4518 break;
4519 }
4520 return NOTIFY_OK;
4521}
4522#endif
4523
NeilBrownd1688a62011-10-11 16:49:52 +11004524static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004525{
4526 unsigned long cpu;
4527 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004528 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004529 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004530 int err;
4531
Dan Williams36d1c642009-07-14 11:48:22 -07004532 allcpus = alloc_percpu(struct raid5_percpu);
4533 if (!allcpus)
4534 return -ENOMEM;
4535 conf->percpu = allcpus;
4536
4537 get_online_cpus();
4538 err = 0;
4539 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004540 if (conf->level == 6) {
4541 spare_page = alloc_page(GFP_KERNEL);
4542 if (!spare_page) {
4543 err = -ENOMEM;
4544 break;
4545 }
4546 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4547 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004548 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004549 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004550 err = -ENOMEM;
4551 break;
4552 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004553 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004554 }
4555#ifdef CONFIG_HOTPLUG_CPU
4556 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4557 conf->cpu_notify.priority = 0;
4558 if (err == 0)
4559 err = register_cpu_notifier(&conf->cpu_notify);
4560#endif
4561 put_online_cpus();
4562
4563 return err;
4564}
4565
NeilBrownd1688a62011-10-11 16:49:52 +11004566static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
NeilBrownd1688a62011-10-11 16:49:52 +11004568 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004569 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004570 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
NeilBrown91adb562009-03-31 14:39:39 +11004573 if (mddev->new_level != 5
4574 && mddev->new_level != 4
4575 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004576 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004577 mdname(mddev), mddev->new_level);
4578 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 }
NeilBrown91adb562009-03-31 14:39:39 +11004580 if ((mddev->new_level == 5
4581 && !algorithm_valid_raid5(mddev->new_layout)) ||
4582 (mddev->new_level == 6
4583 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004584 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004585 mdname(mddev), mddev->new_layout);
4586 return ERR_PTR(-EIO);
4587 }
4588 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004589 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004590 mdname(mddev), mddev->raid_disks);
4591 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Andre Noll664e7c42009-06-18 08:45:27 +10004594 if (!mddev->new_chunk_sectors ||
4595 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4596 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004597 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4598 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004599 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004600 }
4601
NeilBrownd1688a62011-10-11 16:49:52 +11004602 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004603 if (conf == NULL)
4604 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004605 spin_lock_init(&conf->device_lock);
4606 init_waitqueue_head(&conf->wait_for_stripe);
4607 init_waitqueue_head(&conf->wait_for_overlap);
4608 INIT_LIST_HEAD(&conf->handle_list);
4609 INIT_LIST_HEAD(&conf->hold_list);
4610 INIT_LIST_HEAD(&conf->delayed_list);
4611 INIT_LIST_HEAD(&conf->bitmap_list);
4612 INIT_LIST_HEAD(&conf->inactive_list);
4613 atomic_set(&conf->active_stripes, 0);
4614 atomic_set(&conf->preread_active_stripes, 0);
4615 atomic_set(&conf->active_aligned_reads, 0);
4616 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004617 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004618
4619 conf->raid_disks = mddev->raid_disks;
4620 if (mddev->reshape_position == MaxSector)
4621 conf->previous_raid_disks = mddev->raid_disks;
4622 else
4623 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004624 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4625 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004626
NeilBrown5e5e3e72009-10-16 16:35:30 +11004627 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004628 GFP_KERNEL);
4629 if (!conf->disks)
4630 goto abort;
4631
4632 conf->mddev = mddev;
4633
4634 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4635 goto abort;
4636
Dan Williams36d1c642009-07-14 11:48:22 -07004637 conf->level = mddev->new_level;
4638 if (raid5_alloc_percpu(conf) != 0)
4639 goto abort;
4640
NeilBrown0c55e022010-05-03 14:09:02 +10004641 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004642
4643 list_for_each_entry(rdev, &mddev->disks, same_set) {
4644 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004645 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004646 || raid_disk < 0)
4647 continue;
4648 disk = conf->disks + raid_disk;
4649
4650 disk->rdev = rdev;
4651
4652 if (test_bit(In_sync, &rdev->flags)) {
4653 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004654 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4655 " disk %d\n",
4656 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004657 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004658 /* Cannot rely on bitmap to complete recovery */
4659 conf->fullsync = 1;
4660 }
4661
Andre Noll09c9e5f2009-06-18 08:45:55 +10004662 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004663 conf->level = mddev->new_level;
4664 if (conf->level == 6)
4665 conf->max_degraded = 2;
4666 else
4667 conf->max_degraded = 1;
4668 conf->algorithm = mddev->new_layout;
4669 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004670 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004671 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004672 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004673 conf->prev_algo = mddev->layout;
4674 }
NeilBrown91adb562009-03-31 14:39:39 +11004675
4676 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004677 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004678 if (grow_stripes(conf, conf->max_nr_stripes)) {
4679 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004680 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4681 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004682 goto abort;
4683 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004684 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4685 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004686
NeilBrown0da3c612009-09-23 18:09:45 +10004687 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004688 if (!conf->thread) {
4689 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004690 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004691 mdname(mddev));
4692 goto abort;
4693 }
4694
4695 return conf;
4696
4697 abort:
4698 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004699 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004700 return ERR_PTR(-EIO);
4701 } else
4702 return ERR_PTR(-ENOMEM);
4703}
4704
NeilBrownc148ffd2009-11-13 17:47:00 +11004705
4706static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4707{
4708 switch (algo) {
4709 case ALGORITHM_PARITY_0:
4710 if (raid_disk < max_degraded)
4711 return 1;
4712 break;
4713 case ALGORITHM_PARITY_N:
4714 if (raid_disk >= raid_disks - max_degraded)
4715 return 1;
4716 break;
4717 case ALGORITHM_PARITY_0_6:
4718 if (raid_disk == 0 ||
4719 raid_disk == raid_disks - 1)
4720 return 1;
4721 break;
4722 case ALGORITHM_LEFT_ASYMMETRIC_6:
4723 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4724 case ALGORITHM_LEFT_SYMMETRIC_6:
4725 case ALGORITHM_RIGHT_SYMMETRIC_6:
4726 if (raid_disk == raid_disks - 1)
4727 return 1;
4728 }
4729 return 0;
4730}
4731
NeilBrownfd01b882011-10-11 16:47:53 +11004732static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11004733{
NeilBrownd1688a62011-10-11 16:49:52 +11004734 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004735 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004736 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11004737 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004738 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004739
Andre Noll8c6ac862009-06-18 08:48:06 +10004740 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004741 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10004742 " -- starting background reconstruction\n",
4743 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004744 if (mddev->reshape_position != MaxSector) {
4745 /* Check that we can continue the reshape.
4746 * Currently only disks can change, it must
4747 * increase, and we must be past the point where
4748 * a stripe over-writes itself
4749 */
4750 sector_t here_new, here_old;
4751 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004752 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004753
NeilBrown88ce4932009-03-31 15:24:23 +11004754 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10004755 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08004756 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004757 mdname(mddev));
4758 return -EINVAL;
4759 }
NeilBrownf6705572006-03-27 01:18:11 -08004760 old_disks = mddev->raid_disks - mddev->delta_disks;
4761 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004762 * further up in new geometry must map after here in old
4763 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004764 */
4765 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004766 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004767 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004768 printk(KERN_ERR "md/raid:%s: reshape_position not "
4769 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004770 return -EINVAL;
4771 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004772 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004773 /* here_new is the stripe we will write to */
4774 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004775 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004776 (old_disks-max_degraded));
4777 /* here_old is the first stripe that we might need to read
4778 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004779 if (mddev->delta_disks == 0) {
4780 /* We cannot be sure it is safe to start an in-place
4781 * reshape. It is only safe if user-space if monitoring
4782 * and taking constant backups.
4783 * mdadm always starts a situation like this in
4784 * readonly mode so it can take control before
4785 * allowing any writes. So just check for that.
4786 */
4787 if ((here_new * mddev->new_chunk_sectors !=
4788 here_old * mddev->chunk_sectors) ||
4789 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10004790 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4791 " in read-only mode - aborting\n",
4792 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10004793 return -EINVAL;
4794 }
4795 } else if (mddev->delta_disks < 0
4796 ? (here_new * mddev->new_chunk_sectors <=
4797 here_old * mddev->chunk_sectors)
4798 : (here_new * mddev->new_chunk_sectors >=
4799 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004800 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10004801 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4802 "auto-recovery - aborting.\n",
4803 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004804 return -EINVAL;
4805 }
NeilBrown0c55e022010-05-03 14:09:02 +10004806 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4807 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004808 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004809 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004810 BUG_ON(mddev->level != mddev->new_level);
4811 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004812 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004813 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004814 }
4815
NeilBrown245f46c2009-03-31 14:39:39 +11004816 if (mddev->private == NULL)
4817 conf = setup_conf(mddev);
4818 else
4819 conf = mddev->private;
4820
NeilBrown91adb562009-03-31 14:39:39 +11004821 if (IS_ERR(conf))
4822 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004823
NeilBrown91adb562009-03-31 14:39:39 +11004824 mddev->thread = conf->thread;
4825 conf->thread = NULL;
4826 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004829 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004831 list_for_each_entry(rdev, &mddev->disks, same_set) {
4832 if (rdev->raid_disk < 0)
4833 continue;
NeilBrown2f115882010-06-17 17:41:03 +10004834 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11004835 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10004836 continue;
4837 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004838 /* This disc is not fully in-sync. However if it
4839 * just stored parity (beyond the recovery_offset),
4840 * when we don't need to be concerned about the
4841 * array being dirty.
4842 * When reshape goes 'backwards', we never have
4843 * partially completed devices, so we only need
4844 * to worry about reshape going forwards.
4845 */
4846 /* Hack because v0.91 doesn't store recovery_offset properly. */
4847 if (mddev->major_version == 0 &&
4848 mddev->minor_version > 90)
4849 rdev->recovery_offset = reshape_offset;
4850
NeilBrownc148ffd2009-11-13 17:47:00 +11004851 if (rdev->recovery_offset < reshape_offset) {
4852 /* We need to check old and new layout */
4853 if (!only_parity(rdev->raid_disk,
4854 conf->algorithm,
4855 conf->raid_disks,
4856 conf->max_degraded))
4857 continue;
4858 }
4859 if (!only_parity(rdev->raid_disk,
4860 conf->prev_algo,
4861 conf->previous_raid_disks,
4862 conf->max_degraded))
4863 continue;
4864 dirty_parity_disks++;
4865 }
NeilBrown91adb562009-03-31 14:39:39 +11004866
NeilBrown908f4fb2011-12-23 10:17:50 +11004867 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
NeilBrown674806d2010-06-16 17:17:53 +10004869 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004870 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004872 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 goto abort;
4874 }
4875
NeilBrown91adb562009-03-31 14:39:39 +11004876 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004877 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004878 mddev->resync_max_sectors = mddev->dev_sectors;
4879
NeilBrownc148ffd2009-11-13 17:47:00 +11004880 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004882 if (mddev->ok_start_degraded)
4883 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10004884 "md/raid:%s: starting dirty degraded array"
4885 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004886 mdname(mddev));
4887 else {
4888 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004889 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004890 mdname(mddev));
4891 goto abort;
4892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10004896 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4897 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11004898 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4899 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 else
NeilBrown0c55e022010-05-03 14:09:02 +10004901 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4902 " out of %d devices, algorithm %d\n",
4903 mdname(mddev), conf->level,
4904 mddev->raid_disks - mddev->degraded,
4905 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 print_raid5_conf(conf);
4908
NeilBrownfef9c612009-03-31 15:16:46 +11004909 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11004910 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004911 atomic_set(&conf->reshape_stripes, 0);
4912 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4913 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4914 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4915 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4916 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004917 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004918 }
4919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
4921 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10004922 if (mddev->to_remove == &raid5_attrs_group)
4923 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004924 else if (mddev->kobj.sd &&
4925 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004926 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10004927 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08004928 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10004929 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4930
4931 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10004932 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10004933 /* read-ahead size must cover two whole stripes, which
4934 * is 2 * (datadisks) * chunksize where 'n' is the
4935 * number of raid devices
4936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4938 int stripe = data_disks *
4939 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4940 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4941 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10004942
4943 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004944
4945 mddev->queue->backing_dev_info.congested_data = mddev;
4946 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10004947
4948 chunk_size = mddev->chunk_sectors << 9;
4949 blk_queue_io_min(mddev->queue, chunk_size);
4950 blk_queue_io_opt(mddev->queue, chunk_size *
4951 (conf->raid_disks - conf->max_degraded));
4952
4953 list_for_each_entry(rdev, &mddev->disks, same_set)
4954 disk_stack_limits(mddev->gendisk, rdev->bdev,
4955 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return 0;
4959abort:
NeilBrown01f96c02011-09-21 15:30:20 +10004960 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11004961 print_raid5_conf(conf);
4962 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10004964 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return -EIO;
4966}
4967
NeilBrownfd01b882011-10-11 16:47:53 +11004968static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
NeilBrownd1688a62011-10-11 16:49:52 +11004970 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
NeilBrown01f96c02011-09-21 15:30:20 +10004972 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10004973 if (mddev->queue)
4974 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10004975 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10004976 mddev->private = NULL;
4977 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return 0;
4979}
4980
NeilBrownfd01b882011-10-11 16:47:53 +11004981static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982{
NeilBrownd1688a62011-10-11 16:49:52 +11004983 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 int i;
4985
Andre Noll9d8f0362009-06-18 08:45:01 +10004986 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
4987 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004988 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 for (i = 0; i < conf->raid_disks; i++)
4990 seq_printf (seq, "%s",
4991 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004992 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
NeilBrownd1688a62011-10-11 16:49:52 +11004996static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
4998 int i;
4999 struct disk_info *tmp;
5000
NeilBrown0c55e022010-05-03 14:09:02 +10005001 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 if (!conf) {
5003 printk("(conf==NULL)\n");
5004 return;
5005 }
NeilBrown0c55e022010-05-03 14:09:02 +10005006 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5007 conf->raid_disks,
5008 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
5010 for (i = 0; i < conf->raid_disks; i++) {
5011 char b[BDEVNAME_SIZE];
5012 tmp = conf->disks + i;
5013 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005014 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5015 i, !test_bit(Faulty, &tmp->rdev->flags),
5016 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 }
5018}
5019
NeilBrownfd01b882011-10-11 16:47:53 +11005020static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
5022 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005023 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005025 int count = 0;
5026 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
5028 for (i = 0; i < conf->raid_disks; i++) {
5029 tmp = conf->disks + i;
5030 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005031 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005032 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005033 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005034 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005035 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 }
5037 }
NeilBrown6b965622010-08-18 11:56:59 +10005038 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005039 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005040 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005042 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
NeilBrownb8321b62011-12-23 10:17:51 +11005045static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
NeilBrownd1688a62011-10-11 16:49:52 +11005047 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005049 int number = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 struct disk_info *p = conf->disks + number;
5051
5052 print_raid5_conf(conf);
NeilBrownb8321b62011-12-23 10:17:51 +11005053 if (rdev == p->rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005054 if (number >= conf->raid_disks &&
5055 conf->reshape_progress == MaxSector)
5056 clear_bit(In_sync, &rdev->flags);
5057
NeilBrownb2d444d2005-11-08 21:39:31 -08005058 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 atomic_read(&rdev->nr_pending)) {
5060 err = -EBUSY;
5061 goto abort;
5062 }
NeilBrowndfc70642008-05-23 13:04:39 -07005063 /* Only remove non-faulty devices if recovery
5064 * isn't possible.
5065 */
5066 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7f0da592011-07-28 11:39:22 +10005067 mddev->recovery_disabled != conf->recovery_disabled &&
NeilBrown674806d2010-06-16 17:17:53 +10005068 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005069 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005070 err = -EBUSY;
5071 goto abort;
5072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005074 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 if (atomic_read(&rdev->nr_pending)) {
5076 /* lost the race, try later */
5077 err = -EBUSY;
5078 p->rdev = rdev;
5079 }
5080 }
5081abort:
5082
5083 print_raid5_conf(conf);
5084 return err;
5085}
5086
NeilBrownfd01b882011-10-11 16:47:53 +11005087static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088{
NeilBrownd1688a62011-10-11 16:49:52 +11005089 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005090 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 int disk;
5092 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005093 int first = 0;
5094 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
NeilBrown7f0da592011-07-28 11:39:22 +10005096 if (mddev->recovery_disabled == conf->recovery_disabled)
5097 return -EBUSY;
5098
NeilBrown674806d2010-06-16 17:17:53 +10005099 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005101 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Neil Brown6c2fce22008-06-28 08:31:31 +10005103 if (rdev->raid_disk >= 0)
5104 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
5106 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005107 * find the disk ... but prefer rdev->saved_raid_disk
5108 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005110 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005111 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005112 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5113 disk = rdev->saved_raid_disk;
5114 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005115 disk = first;
5116 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005118 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005120 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005121 if (rdev->saved_raid_disk != disk)
5122 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005123 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 break;
5125 }
5126 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128}
5129
NeilBrownfd01b882011-10-11 16:47:53 +11005130static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131{
5132 /* no resync is happening, and there is enough space
5133 * on all devices, so we can resize.
5134 * We need to make sure resync covers any new space.
5135 * If the array is shrinking we should possibly wait until
5136 * any io in the removed space completes, but it hardly seems
5137 * worth it.
5138 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005139 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005140 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5141 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005142 if (mddev->array_sectors >
5143 raid5_size(mddev, sectors, mddev->raid_disks))
5144 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005145 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005146 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005147 if (sectors > mddev->dev_sectors &&
5148 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005149 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5151 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005152 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005153 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 return 0;
5155}
5156
NeilBrownfd01b882011-10-11 16:47:53 +11005157static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005158{
5159 /* Can only proceed if there are plenty of stripe_heads.
5160 * We need a minimum of one full stripe,, and for sensible progress
5161 * it is best to have about 4 times that.
5162 * If we require 4 times, then the default 256 4K stripe_heads will
5163 * allow for chunk sizes up to 256K, which is probably OK.
5164 * If the chunk size is greater, user-space should request more
5165 * stripe_heads first.
5166 */
NeilBrownd1688a62011-10-11 16:49:52 +11005167 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005168 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5169 > conf->max_nr_stripes ||
5170 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5171 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005172 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5173 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005174 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5175 / STRIPE_SIZE)*4);
5176 return 0;
5177 }
5178 return 1;
5179}
5180
NeilBrownfd01b882011-10-11 16:47:53 +11005181static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005182{
NeilBrownd1688a62011-10-11 16:49:52 +11005183 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005184
NeilBrown88ce4932009-03-31 15:24:23 +11005185 if (mddev->delta_disks == 0 &&
5186 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005187 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005188 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005189 if (mddev->bitmap)
5190 /* Cannot grow a bitmap yet */
5191 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005192 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005193 return -EINVAL;
5194 if (mddev->delta_disks < 0) {
5195 /* We might be able to shrink, but the devices must
5196 * be made bigger first.
5197 * For raid6, 4 is the minimum size.
5198 * Otherwise 2 is the minimum
5199 */
5200 int min = 2;
5201 if (mddev->level == 6)
5202 min = 4;
5203 if (mddev->raid_disks + mddev->delta_disks < min)
5204 return -EINVAL;
5205 }
NeilBrown29269552006-03-27 01:18:10 -08005206
NeilBrown01ee22b2009-06-18 08:47:20 +10005207 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005208 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005209
NeilBrownec32a2b2009-03-31 15:17:38 +11005210 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005211}
5212
NeilBrownfd01b882011-10-11 16:47:53 +11005213static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005214{
NeilBrownd1688a62011-10-11 16:49:52 +11005215 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005216 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005217 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005218 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005219
NeilBrownf4168852007-02-28 20:11:53 -08005220 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005221 return -EBUSY;
5222
NeilBrown01ee22b2009-06-18 08:47:20 +10005223 if (!check_stripe_cache(mddev))
5224 return -ENOSPC;
5225
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005226 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005227 if (!test_bit(In_sync, &rdev->flags)
5228 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005229 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005230
NeilBrownf4168852007-02-28 20:11:53 -08005231 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005232 /* Not enough devices even to make a degraded array
5233 * of that size
5234 */
5235 return -EINVAL;
5236
NeilBrownec32a2b2009-03-31 15:17:38 +11005237 /* Refuse to reduce size of the array. Any reductions in
5238 * array size must be through explicit setting of array_size
5239 * attribute.
5240 */
5241 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5242 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005243 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005244 "before number of disks\n", mdname(mddev));
5245 return -EINVAL;
5246 }
5247
NeilBrownf6705572006-03-27 01:18:11 -08005248 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005249 spin_lock_irq(&conf->device_lock);
5250 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005251 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005252 conf->prev_chunk_sectors = conf->chunk_sectors;
5253 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005254 conf->prev_algo = conf->algorithm;
5255 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005256 if (mddev->delta_disks < 0)
5257 conf->reshape_progress = raid5_size(mddev, 0, 0);
5258 else
5259 conf->reshape_progress = 0;
5260 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005261 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005262 spin_unlock_irq(&conf->device_lock);
5263
5264 /* Add some new drives, as many as will fit.
5265 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005266 * Don't add devices if we are reducing the number of
5267 * devices in the array. This is because it is not possible
5268 * to correctly record the "partially reconstructed" state of
5269 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005270 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005271 if (mddev->delta_disks >= 0) {
5272 int added_devices = 0;
5273 list_for_each_entry(rdev, &mddev->disks, same_set)
5274 if (rdev->raid_disk < 0 &&
5275 !test_bit(Faulty, &rdev->flags)) {
5276 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005277 if (rdev->raid_disk
5278 >= conf->previous_raid_disks) {
5279 set_bit(In_sync, &rdev->flags);
5280 added_devices++;
5281 } else
5282 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005283
5284 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005285 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005286 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005287 } else if (rdev->raid_disk >= conf->previous_raid_disks
5288 && !test_bit(Faulty, &rdev->flags)) {
5289 /* This is a spare that was manually added */
5290 set_bit(In_sync, &rdev->flags);
5291 added_devices++;
5292 }
NeilBrown29269552006-03-27 01:18:10 -08005293
NeilBrown87a8dec2011-01-31 11:57:43 +11005294 /* When a reshape changes the number of devices,
5295 * ->degraded is measured against the larger of the
5296 * pre and post number of devices.
5297 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005298 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005299 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005300 spin_unlock_irqrestore(&conf->device_lock, flags);
5301 }
NeilBrown63c70c42006-03-27 01:18:13 -08005302 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005303 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005304 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005305
NeilBrown29269552006-03-27 01:18:10 -08005306 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5307 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5308 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5309 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5310 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005311 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005312 if (!mddev->sync_thread) {
5313 mddev->recovery = 0;
5314 spin_lock_irq(&conf->device_lock);
5315 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005316 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005317 spin_unlock_irq(&conf->device_lock);
5318 return -EAGAIN;
5319 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005320 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005321 md_wakeup_thread(mddev->sync_thread);
5322 md_new_event(mddev);
5323 return 0;
5324}
NeilBrown29269552006-03-27 01:18:10 -08005325
NeilBrownec32a2b2009-03-31 15:17:38 +11005326/* This is called from the reshape thread and should make any
5327 * changes needed in 'conf'
5328 */
NeilBrownd1688a62011-10-11 16:49:52 +11005329static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005330{
NeilBrown29269552006-03-27 01:18:10 -08005331
NeilBrownf6705572006-03-27 01:18:11 -08005332 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005333
NeilBrownf6705572006-03-27 01:18:11 -08005334 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005335 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005336 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005337 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005338 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005339
5340 /* read-ahead size must cover two whole stripes, which is
5341 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5342 */
NeilBrown4a5add42010-06-01 19:37:28 +10005343 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005344 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005345 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005346 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005347 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5348 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5349 }
NeilBrown29269552006-03-27 01:18:10 -08005350 }
NeilBrown29269552006-03-27 01:18:10 -08005351}
5352
NeilBrownec32a2b2009-03-31 15:17:38 +11005353/* This is called from the raid5d thread with mddev_lock held.
5354 * It makes config changes to the device.
5355 */
NeilBrownfd01b882011-10-11 16:47:53 +11005356static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005357{
NeilBrownd1688a62011-10-11 16:49:52 +11005358 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005359
5360 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5361
NeilBrownec32a2b2009-03-31 15:17:38 +11005362 if (mddev->delta_disks > 0) {
5363 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5364 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005365 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005366 } else {
5367 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005368 spin_lock_irq(&conf->device_lock);
5369 mddev->degraded = calc_degraded(conf);
5370 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005371 for (d = conf->raid_disks ;
5372 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005373 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005374 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownb8321b62011-12-23 10:17:51 +11005375 if (rdev &&
5376 raid5_remove_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10005377 sysfs_unlink_rdev(mddev, rdev);
NeilBrown1a67dde2009-08-13 10:41:49 +10005378 rdev->raid_disk = -1;
5379 }
5380 }
NeilBrowncea9c222009-03-31 15:15:05 +11005381 }
NeilBrown88ce4932009-03-31 15:24:23 +11005382 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005383 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005384 mddev->reshape_position = MaxSector;
5385 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005386 }
5387}
5388
NeilBrownfd01b882011-10-11 16:47:53 +11005389static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005390{
NeilBrownd1688a62011-10-11 16:49:52 +11005391 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005392
5393 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005394 case 2: /* resume for a suspend */
5395 wake_up(&conf->wait_for_overlap);
5396 break;
5397
NeilBrown72626682005-09-09 16:23:54 -07005398 case 1: /* stop all writes */
5399 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005400 /* '2' tells resync/reshape to pause so that all
5401 * active stripes can drain
5402 */
5403 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005404 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005405 atomic_read(&conf->active_stripes) == 0 &&
5406 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005407 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005408 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005409 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005410 /* allow reshape to continue */
5411 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005412 break;
5413
5414 case 0: /* re-enable writes */
5415 spin_lock_irq(&conf->device_lock);
5416 conf->quiesce = 0;
5417 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005418 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005419 spin_unlock_irq(&conf->device_lock);
5420 break;
5421 }
NeilBrown72626682005-09-09 16:23:54 -07005422}
NeilBrownb15c2e52006-01-06 00:20:16 -08005423
NeilBrownd562b0c2009-03-31 14:39:39 +11005424
NeilBrownfd01b882011-10-11 16:47:53 +11005425static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005426{
NeilBrowne373ab12011-10-11 16:48:59 +11005427 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005428 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005429
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005430 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005431 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005432 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5433 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005434 return ERR_PTR(-EINVAL);
5435 }
5436
NeilBrowne373ab12011-10-11 16:48:59 +11005437 sectors = raid0_conf->strip_zone[0].zone_end;
5438 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005439 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005440 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005441 mddev->new_layout = ALGORITHM_PARITY_N;
5442 mddev->new_chunk_sectors = mddev->chunk_sectors;
5443 mddev->raid_disks += 1;
5444 mddev->delta_disks = 1;
5445 /* make sure it will be not marked as dirty */
5446 mddev->recovery_cp = MaxSector;
5447
5448 return setup_conf(mddev);
5449}
5450
5451
NeilBrownfd01b882011-10-11 16:47:53 +11005452static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005453{
5454 int chunksect;
5455
5456 if (mddev->raid_disks != 2 ||
5457 mddev->degraded > 1)
5458 return ERR_PTR(-EINVAL);
5459
5460 /* Should check if there are write-behind devices? */
5461
5462 chunksect = 64*2; /* 64K by default */
5463
5464 /* The array must be an exact multiple of chunksize */
5465 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5466 chunksect >>= 1;
5467
5468 if ((chunksect<<9) < STRIPE_SIZE)
5469 /* array size does not allow a suitable chunk size */
5470 return ERR_PTR(-EINVAL);
5471
5472 mddev->new_level = 5;
5473 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005474 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005475
5476 return setup_conf(mddev);
5477}
5478
NeilBrownfd01b882011-10-11 16:47:53 +11005479static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005480{
5481 int new_layout;
5482
5483 switch (mddev->layout) {
5484 case ALGORITHM_LEFT_ASYMMETRIC_6:
5485 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5486 break;
5487 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5488 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5489 break;
5490 case ALGORITHM_LEFT_SYMMETRIC_6:
5491 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5492 break;
5493 case ALGORITHM_RIGHT_SYMMETRIC_6:
5494 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5495 break;
5496 case ALGORITHM_PARITY_0_6:
5497 new_layout = ALGORITHM_PARITY_0;
5498 break;
5499 case ALGORITHM_PARITY_N:
5500 new_layout = ALGORITHM_PARITY_N;
5501 break;
5502 default:
5503 return ERR_PTR(-EINVAL);
5504 }
5505 mddev->new_level = 5;
5506 mddev->new_layout = new_layout;
5507 mddev->delta_disks = -1;
5508 mddev->raid_disks -= 1;
5509 return setup_conf(mddev);
5510}
5511
NeilBrownd562b0c2009-03-31 14:39:39 +11005512
NeilBrownfd01b882011-10-11 16:47:53 +11005513static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005514{
NeilBrown88ce4932009-03-31 15:24:23 +11005515 /* For a 2-drive array, the layout and chunk size can be changed
5516 * immediately as not restriping is needed.
5517 * For larger arrays we record the new value - after validation
5518 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005519 */
NeilBrownd1688a62011-10-11 16:49:52 +11005520 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005521 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005522
NeilBrown597a7112009-06-18 08:47:42 +10005523 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005524 return -EINVAL;
5525 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005526 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005527 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005528 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005529 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005530 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005531 /* not factor of array size */
5532 return -EINVAL;
5533 }
5534
5535 /* They look valid */
5536
NeilBrown88ce4932009-03-31 15:24:23 +11005537 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005538 /* can make the change immediately */
5539 if (mddev->new_layout >= 0) {
5540 conf->algorithm = mddev->new_layout;
5541 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005542 }
5543 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005544 conf->chunk_sectors = new_chunk ;
5545 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005546 }
5547 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5548 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005549 }
NeilBrown50ac1682009-06-18 08:47:55 +10005550 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005551}
5552
NeilBrownfd01b882011-10-11 16:47:53 +11005553static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005554{
NeilBrown597a7112009-06-18 08:47:42 +10005555 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005556
NeilBrown597a7112009-06-18 08:47:42 +10005557 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005558 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005559 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005560 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005561 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005562 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005563 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005564 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005565 /* not factor of array size */
5566 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005567 }
NeilBrown88ce4932009-03-31 15:24:23 +11005568
5569 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005570 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005571}
5572
NeilBrownfd01b882011-10-11 16:47:53 +11005573static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005574{
5575 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005576 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005577 * raid1 - if there are two drives. We need to know the chunk size
5578 * raid4 - trivial - just use a raid4 layout.
5579 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005580 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005581 if (mddev->level == 0)
5582 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005583 if (mddev->level == 1)
5584 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005585 if (mddev->level == 4) {
5586 mddev->new_layout = ALGORITHM_PARITY_N;
5587 mddev->new_level = 5;
5588 return setup_conf(mddev);
5589 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005590 if (mddev->level == 6)
5591 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005592
5593 return ERR_PTR(-EINVAL);
5594}
5595
NeilBrownfd01b882011-10-11 16:47:53 +11005596static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11005597{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005598 /* raid4 can take over:
5599 * raid0 - if there is only one strip zone
5600 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005601 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005602 if (mddev->level == 0)
5603 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005604 if (mddev->level == 5 &&
5605 mddev->layout == ALGORITHM_PARITY_N) {
5606 mddev->new_layout = 0;
5607 mddev->new_level = 4;
5608 return setup_conf(mddev);
5609 }
5610 return ERR_PTR(-EINVAL);
5611}
NeilBrownd562b0c2009-03-31 14:39:39 +11005612
NeilBrown84fc4b52011-10-11 16:49:58 +11005613static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11005614
NeilBrownfd01b882011-10-11 16:47:53 +11005615static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11005616{
5617 /* Currently can only take over a raid5. We map the
5618 * personality to an equivalent raid6 personality
5619 * with the Q block at the end.
5620 */
5621 int new_layout;
5622
5623 if (mddev->pers != &raid5_personality)
5624 return ERR_PTR(-EINVAL);
5625 if (mddev->degraded > 1)
5626 return ERR_PTR(-EINVAL);
5627 if (mddev->raid_disks > 253)
5628 return ERR_PTR(-EINVAL);
5629 if (mddev->raid_disks < 3)
5630 return ERR_PTR(-EINVAL);
5631
5632 switch (mddev->layout) {
5633 case ALGORITHM_LEFT_ASYMMETRIC:
5634 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5635 break;
5636 case ALGORITHM_RIGHT_ASYMMETRIC:
5637 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5638 break;
5639 case ALGORITHM_LEFT_SYMMETRIC:
5640 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5641 break;
5642 case ALGORITHM_RIGHT_SYMMETRIC:
5643 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5644 break;
5645 case ALGORITHM_PARITY_0:
5646 new_layout = ALGORITHM_PARITY_0_6;
5647 break;
5648 case ALGORITHM_PARITY_N:
5649 new_layout = ALGORITHM_PARITY_N;
5650 break;
5651 default:
5652 return ERR_PTR(-EINVAL);
5653 }
5654 mddev->new_level = 6;
5655 mddev->new_layout = new_layout;
5656 mddev->delta_disks = 1;
5657 mddev->raid_disks += 1;
5658 return setup_conf(mddev);
5659}
5660
5661
NeilBrown84fc4b52011-10-11 16:49:58 +11005662static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07005663{
5664 .name = "raid6",
5665 .level = 6,
5666 .owner = THIS_MODULE,
5667 .make_request = make_request,
5668 .run = run,
5669 .stop = stop,
5670 .status = status,
5671 .error_handler = error,
5672 .hot_add_disk = raid5_add_disk,
5673 .hot_remove_disk= raid5_remove_disk,
5674 .spare_active = raid5_spare_active,
5675 .sync_request = sync_request,
5676 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005677 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005678 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005679 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005680 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005681 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005682 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005683};
NeilBrown84fc4b52011-10-11 16:49:58 +11005684static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005687 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 .owner = THIS_MODULE,
5689 .make_request = make_request,
5690 .run = run,
5691 .stop = stop,
5692 .status = status,
5693 .error_handler = error,
5694 .hot_add_disk = raid5_add_disk,
5695 .hot_remove_disk= raid5_remove_disk,
5696 .spare_active = raid5_spare_active,
5697 .sync_request = sync_request,
5698 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005699 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005700 .check_reshape = raid5_check_reshape,
5701 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005702 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005703 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005704 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705};
5706
NeilBrown84fc4b52011-10-11 16:49:58 +11005707static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
NeilBrown2604b702006-01-06 00:20:36 -08005709 .name = "raid4",
5710 .level = 4,
5711 .owner = THIS_MODULE,
5712 .make_request = make_request,
5713 .run = run,
5714 .stop = stop,
5715 .status = status,
5716 .error_handler = error,
5717 .hot_add_disk = raid5_add_disk,
5718 .hot_remove_disk= raid5_remove_disk,
5719 .spare_active = raid5_spare_active,
5720 .sync_request = sync_request,
5721 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005722 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005723 .check_reshape = raid5_check_reshape,
5724 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005725 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005726 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005727 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005728};
5729
5730static int __init raid5_init(void)
5731{
NeilBrown16a53ec2006-06-26 00:27:38 -07005732 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005733 register_md_personality(&raid5_personality);
5734 register_md_personality(&raid4_personality);
5735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736}
5737
NeilBrown2604b702006-01-06 00:20:36 -08005738static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739{
NeilBrown16a53ec2006-06-26 00:27:38 -07005740 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005741 unregister_md_personality(&raid5_personality);
5742 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743}
5744
5745module_init(raid5_init);
5746module_exit(raid5_exit);
5747MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005748MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005750MODULE_ALIAS("md-raid5");
5751MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005752MODULE_ALIAS("md-level-5");
5753MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005754MODULE_ALIAS("md-personality-8"); /* RAID6 */
5755MODULE_ALIAS("md-raid6");
5756MODULE_ALIAS("md-level-6");
5757
5758/* This used to be two separate modules, they were: */
5759MODULE_ALIAS("raid5");
5760MODULE_ALIAS("raid6");