blob: 702812824195ae7c0a6333381659575f552a8f62 [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.
30 * conf->bm_write is the number of the last batch successfully written.
31 * conf->bm_flush is the number of the last batch that was closed to
32 * 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
35 * the number of the batch it will be in. This is bm_flush+1.
36 * 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>
Dan Williams07a3b412009-08-29 19:13:13 -070050#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070052#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110055#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110056#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110057#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Stripe cache
61 */
62
63#define NR_STRIPES 256
64#define STRIPE_SIZE PAGE_SIZE
65#define STRIPE_SHIFT (PAGE_SHIFT - 9)
66#define STRIPE_SECTORS (STRIPE_SIZE>>9)
67#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070068#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080069#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define HASH_MASK (NR_HASH - 1)
71
NeilBrownfccddba2006-01-06 00:20:33 -080072#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* bio's attached to a stripe+device for I/O are linked together in bi_sector
75 * order without overlap. There may be several bio's per stripe+device, and
76 * a bio could span several devices.
77 * When walking this list for a particular stripe+device, we must never proceed
78 * beyond a bio that extends past this device, as the next bio might no longer
79 * be valid.
80 * This macro is used to determine the 'next' bio in the list, given the sector
81 * of the current stripe+device
82 */
83#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
84/*
85 * The following can be used to debug the driver
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define RAID5_PARANOIA 1
88#if RAID5_PARANOIA && defined(CONFIG_SMP)
89# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
90#else
91# define CHECK_DEVLOCK()
92#endif
93
Dan Williams45b42332007-07-09 11:56:43 -070094#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define inline
96#define __inline__
97#endif
98
Bernd Schubert6be9d492008-05-23 13:04:34 -070099#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
100
Jens Axboe960e7392008-08-15 10:41:18 +0200101/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200102 * We maintain a biased count of active stripes in the bottom 16 bits of
103 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200104 */
105static inline int raid5_bi_phys_segments(struct bio *bio)
106{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200107 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
110static inline int raid5_bi_hw_segments(struct bio *bio)
111{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200112 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200113}
114
115static inline int raid5_dec_bi_phys_segments(struct bio *bio)
116{
117 --bio->bi_phys_segments;
118 return raid5_bi_phys_segments(bio);
119}
120
121static inline int raid5_dec_bi_hw_segments(struct bio *bio)
122{
123 unsigned short val = raid5_bi_hw_segments(bio);
124
125 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200126 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200127 return val;
128}
129
130static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
131{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200132 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200133}
134
NeilBrownd0dabf72009-03-31 14:39:38 +1100135/* Find first data disk in a raid6 stripe */
136static inline int raid6_d0(struct stripe_head *sh)
137{
NeilBrown67cc2b82009-03-31 14:39:38 +1100138 if (sh->ddf_layout)
139 /* ddf always start from first device */
140 return 0;
141 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100142 if (sh->qd_idx == sh->disks - 1)
143 return 0;
144 else
145 return sh->qd_idx + 1;
146}
NeilBrown16a53ec2006-06-26 00:27:38 -0700147static inline int raid6_next_disk(int disk, int raid_disks)
148{
149 disk++;
150 return (disk < raid_disks) ? disk : 0;
151}
Dan Williamsa4456852007-07-09 11:56:43 -0700152
NeilBrownd0dabf72009-03-31 14:39:38 +1100153/* When walking through the disks in a raid5, starting at raid6_d0,
154 * We need to map each disk to a 'slot', where the data disks are slot
155 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
156 * is raid_disks-1. This help does that mapping.
157 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100158static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
159 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100160{
Dan Williams66295422009-10-19 18:09:32 -0700161 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100162
NeilBrowne4424fe2009-10-16 16:27:34 +1100163 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700164 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100165 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100166 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100167 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100168 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100169 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700170 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100171 return slot;
172}
173
Dan Williamsa4456852007-07-09 11:56:43 -0700174static void return_io(struct bio *return_bi)
175{
176 struct bio *bi = return_bi;
177 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700178
179 return_bi = bi->bi_next;
180 bi->bi_next = NULL;
181 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000182 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700183 bi = return_bi;
184 }
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void print_raid5_conf (raid5_conf_t *conf);
188
Dan Williams600aa102008-06-28 08:32:05 +1000189static int stripe_operations_active(struct stripe_head *sh)
190{
191 return sh->check_state || sh->reconstruct_state ||
192 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
193 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
194}
195
Arjan van de Ven858119e2006-01-14 13:20:43 -0800196static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200199 BUG_ON(!list_empty(&sh->lru));
200 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700202 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown2ac87402010-06-01 19:37:29 +1000204 plugger_set_plug(&conf->plug);
NeilBrown7c785b72006-07-10 04:44:16 -0700205 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700206 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700207 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown2ac87402010-06-01 19:37:29 +1000208 plugger_set_plug(&conf->plug);
NeilBrown7c785b72006-07-10 04:44:16 -0700209 } else {
NeilBrown72626682005-09-09 16:23:54 -0700210 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 md_wakeup_thread(conf->mddev->thread);
214 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000215 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
217 atomic_dec(&conf->preread_active_stripes);
218 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
219 md_wakeup_thread(conf->mddev->thread);
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
223 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800225 if (conf->retry_read_aligned)
226 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static void release_stripe(struct stripe_head *sh)
233{
234 raid5_conf_t *conf = sh->raid_conf;
235 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_lock_irqsave(&conf->device_lock, flags);
238 __release_stripe(conf, sh);
239 spin_unlock_irqrestore(&conf->device_lock, flags);
240}
241
NeilBrownfccddba2006-01-06 00:20:33 -0800242static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Dan Williams45b42332007-07-09 11:56:43 -0700244 pr_debug("remove_hash(), stripe %llu\n",
245 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
NeilBrownfccddba2006-01-06 00:20:33 -0800247 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
NeilBrown16a53ec2006-06-26 00:27:38 -0700250static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
NeilBrownfccddba2006-01-06 00:20:33 -0800252 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dan Williams45b42332007-07-09 11:56:43 -0700254 pr_debug("insert_hash(), stripe %llu\n",
255 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800258 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261
262/* find an idle stripe, make sure it is unhashed, and return it. */
263static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
264{
265 struct stripe_head *sh = NULL;
266 struct list_head *first;
267
268 CHECK_DEVLOCK();
269 if (list_empty(&conf->inactive_list))
270 goto out;
271 first = conf->inactive_list.next;
272 sh = list_entry(first, struct stripe_head, lru);
273 list_del_init(first);
274 remove_hash(sh);
275 atomic_inc(&conf->active_stripes);
276out:
277 return sh;
278}
279
NeilBrowne4e11e32010-06-16 16:45:16 +1000280static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct page *p;
283 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000284 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
NeilBrowne4e11e32010-06-16 16:45:16 +1000286 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 p = sh->dev[i].page;
288 if (!p)
289 continue;
290 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800291 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
NeilBrowne4e11e32010-06-16 16:45:16 +1000295static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000298 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
NeilBrowne4e11e32010-06-16 16:45:16 +1000300 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct page *page;
302
303 if (!(page = alloc_page(GFP_KERNEL))) {
304 return 1;
305 }
306 sh->dev[i].page = page;
307 }
308 return 0;
309}
310
NeilBrown784052e2009-03-31 15:19:07 +1100311static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100312static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
313 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
NeilBrownb5663ba2009-03-31 14:39:38 +1100315static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800318 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200320 BUG_ON(atomic_read(&sh->count) != 0);
321 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000322 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700325 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 (unsigned long long)sh->sector);
327
328 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700329
NeilBrown86b42c72009-03-31 15:19:03 +1100330 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100331 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100333 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 sh->state = 0;
335
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800336
337 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct r5dev *dev = &sh->dev[i];
339
Dan Williamsd84e0f12007-01-02 13:52:30 -0700340 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700342 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700344 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 test_bit(R5_LOCKED, &dev->flags));
346 BUG();
347 }
348 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100349 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 insert_hash(conf, sh);
352}
353
NeilBrown86b42c72009-03-31 15:19:03 +1100354static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
355 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800358 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700361 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800362 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100363 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700365 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return NULL;
367}
368
NeilBrown674806d2010-06-16 17:17:53 +1000369/*
370 * Need to check if array has failed when deciding whether to:
371 * - start an array
372 * - remove non-faulty devices
373 * - add a spare
374 * - allow a reshape
375 * This determination is simple when no reshape is happening.
376 * However if there is a reshape, we need to carefully check
377 * both the before and after sections.
378 * This is because some failed devices may only affect one
379 * of the two sections, and some non-in_sync devices may
380 * be insync in the section most affected by failed devices.
381 */
382static int has_failed(raid5_conf_t *conf)
383{
384 int degraded;
385 int i;
386 if (conf->mddev->reshape_position == MaxSector)
387 return conf->mddev->degraded > conf->max_degraded;
388
389 rcu_read_lock();
390 degraded = 0;
391 for (i = 0; i < conf->previous_raid_disks; i++) {
392 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
393 if (!rdev || test_bit(Faulty, &rdev->flags))
394 degraded++;
395 else if (test_bit(In_sync, &rdev->flags))
396 ;
397 else
398 /* not in-sync or faulty.
399 * If the reshape increases the number of devices,
400 * this is being recovered by the reshape, so
401 * this 'previous' section is not in_sync.
402 * If the number of devices is being reduced however,
403 * the device can only be part of the array if
404 * we are reverting a reshape, so this section will
405 * be in-sync.
406 */
407 if (conf->raid_disks >= conf->previous_raid_disks)
408 degraded++;
409 }
410 rcu_read_unlock();
411 if (degraded > conf->max_degraded)
412 return 1;
413 rcu_read_lock();
414 degraded = 0;
415 for (i = 0; i < conf->raid_disks; i++) {
416 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
417 if (!rdev || test_bit(Faulty, &rdev->flags))
418 degraded++;
419 else if (test_bit(In_sync, &rdev->flags))
420 ;
421 else
422 /* not in-sync or faulty.
423 * If reshape increases the number of devices, this
424 * section has already been recovered, else it
425 * almost certainly hasn't.
426 */
427 if (conf->raid_disks <= conf->previous_raid_disks)
428 degraded++;
429 }
430 rcu_read_unlock();
431 if (degraded > conf->max_degraded)
432 return 1;
433 return 0;
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void unplug_slaves(mddev_t *mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
NeilBrownb5663ba2009-03-31 14:39:38 +1100438static struct stripe_head *
439get_active_stripe(raid5_conf_t *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,
NeilBrown9f7c2222010-07-26 12:04:13 +1000466 md_raid5_unplug_device(conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 );
468 conf->inactive_blocked = 0;
469 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100470 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 } else {
472 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100473 BUG_ON(!list_empty(&sh->lru)
474 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 if (!test_bit(STRIPE_HANDLE, &sh->state))
477 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700478 if (list_empty(&sh->lru) &&
479 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700480 BUG();
481 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
484 } while (sh == NULL);
485
486 if (sh)
487 atomic_inc(&sh->count);
488
489 spin_unlock_irq(&conf->device_lock);
490 return sh;
491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void
494raid5_end_read_request(struct bio *bi, int error);
495static void
496raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700497
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000498static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700499{
500 raid5_conf_t *conf = sh->raid_conf;
501 int i, disks = sh->disks;
502
503 might_sleep();
504
505 for (i = disks; i--; ) {
506 int rw;
507 struct bio *bi;
508 mdk_rdev_t *rdev;
Tejun Heoe9c74692010-09-03 11:56:18 +0200509 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
510 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
511 rw = WRITE_FUA;
512 else
513 rw = WRITE;
514 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700515 rw = READ;
516 else
517 continue;
518
519 bi = &sh->dev[i].req;
520
521 bi->bi_rw = rw;
522 if (rw == WRITE)
523 bi->bi_end_io = raid5_end_write_request;
524 else
525 bi->bi_end_io = raid5_end_read_request;
526
527 rcu_read_lock();
528 rdev = rcu_dereference(conf->disks[i].rdev);
529 if (rdev && test_bit(Faulty, &rdev->flags))
530 rdev = NULL;
531 if (rdev)
532 atomic_inc(&rdev->nr_pending);
533 rcu_read_unlock();
534
535 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000536 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700537 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
538
Dan Williams2b7497f2008-06-28 08:31:52 +1000539 set_bit(STRIPE_IO_STARTED, &sh->state);
540
Dan Williams91c00922007-01-02 13:52:30 -0700541 bi->bi_bdev = rdev->bdev;
542 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700543 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700544 bi->bi_rw, i);
545 atomic_inc(&sh->count);
546 bi->bi_sector = sh->sector + rdev->data_offset;
547 bi->bi_flags = 1 << BIO_UPTODATE;
548 bi->bi_vcnt = 1;
549 bi->bi_max_vecs = 1;
550 bi->bi_idx = 0;
551 bi->bi_io_vec = &sh->dev[i].vec;
552 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
553 bi->bi_io_vec[0].bv_offset = 0;
554 bi->bi_size = STRIPE_SIZE;
555 bi->bi_next = NULL;
556 if (rw == WRITE &&
557 test_bit(R5_ReWrite, &sh->dev[i].flags))
558 atomic_add(STRIPE_SECTORS,
559 &rdev->corrected_errors);
560 generic_make_request(bi);
561 } else {
562 if (rw == WRITE)
563 set_bit(STRIPE_DEGRADED, &sh->state);
564 pr_debug("skip op %ld on disc %d for sector %llu\n",
565 bi->bi_rw, i, (unsigned long long)sh->sector);
566 clear_bit(R5_LOCKED, &sh->dev[i].flags);
567 set_bit(STRIPE_HANDLE, &sh->state);
568 }
569 }
570}
571
572static struct dma_async_tx_descriptor *
573async_copy_data(int frombio, struct bio *bio, struct page *page,
574 sector_t sector, struct dma_async_tx_descriptor *tx)
575{
576 struct bio_vec *bvl;
577 struct page *bio_page;
578 int i;
579 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700580 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700581 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700582
583 if (bio->bi_sector >= sector)
584 page_offset = (signed)(bio->bi_sector - sector) * 512;
585 else
586 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700587
Dan Williams0403e382009-09-08 17:42:50 -0700588 if (frombio)
589 flags |= ASYNC_TX_FENCE;
590 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
591
Dan Williams91c00922007-01-02 13:52:30 -0700592 bio_for_each_segment(bvl, bio, i) {
593 int len = bio_iovec_idx(bio, i)->bv_len;
594 int clen;
595 int b_offset = 0;
596
597 if (page_offset < 0) {
598 b_offset = -page_offset;
599 page_offset += b_offset;
600 len -= b_offset;
601 }
602
603 if (len > 0 && page_offset + len > STRIPE_SIZE)
604 clen = STRIPE_SIZE - page_offset;
605 else
606 clen = len;
607
608 if (clen > 0) {
609 b_offset += bio_iovec_idx(bio, i)->bv_offset;
610 bio_page = bio_iovec_idx(bio, i)->bv_page;
611 if (frombio)
612 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700613 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700614 else
615 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700616 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700617 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700618 /* chain the operations */
619 submit.depend_tx = tx;
620
Dan Williams91c00922007-01-02 13:52:30 -0700621 if (clen < len) /* hit end of page */
622 break;
623 page_offset += len;
624 }
625
626 return tx;
627}
628
629static void ops_complete_biofill(void *stripe_head_ref)
630{
631 struct stripe_head *sh = stripe_head_ref;
632 struct bio *return_bi = NULL;
633 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700634 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700635
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700636 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700637 (unsigned long long)sh->sector);
638
639 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000640 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700641 for (i = sh->disks; i--; ) {
642 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700643
644 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700645 /* and check if we need to reply to a read request,
646 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000647 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700648 */
649 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700650 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700651
Dan Williams91c00922007-01-02 13:52:30 -0700652 BUG_ON(!dev->read);
653 rbi = dev->read;
654 dev->read = NULL;
655 while (rbi && rbi->bi_sector <
656 dev->sector + STRIPE_SECTORS) {
657 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200658 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700659 rbi->bi_next = return_bi;
660 return_bi = rbi;
661 }
Dan Williams91c00922007-01-02 13:52:30 -0700662 rbi = rbi2;
663 }
664 }
665 }
Dan Williams83de75c2008-06-28 08:31:58 +1000666 spin_unlock_irq(&conf->device_lock);
667 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700668
669 return_io(return_bi);
670
Dan Williamse4d84902007-09-24 10:06:13 -0700671 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700672 release_stripe(sh);
673}
674
675static void ops_run_biofill(struct stripe_head *sh)
676{
677 struct dma_async_tx_descriptor *tx = NULL;
678 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700679 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700680 int i;
681
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700682 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700683 (unsigned long long)sh->sector);
684
685 for (i = sh->disks; i--; ) {
686 struct r5dev *dev = &sh->dev[i];
687 if (test_bit(R5_Wantfill, &dev->flags)) {
688 struct bio *rbi;
689 spin_lock_irq(&conf->device_lock);
690 dev->read = rbi = dev->toread;
691 dev->toread = NULL;
692 spin_unlock_irq(&conf->device_lock);
693 while (rbi && rbi->bi_sector <
694 dev->sector + STRIPE_SECTORS) {
695 tx = async_copy_data(0, rbi, dev->page,
696 dev->sector, tx);
697 rbi = r5_next_bio(rbi, dev->sector);
698 }
699 }
700 }
701
702 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700703 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
704 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700705}
706
Dan Williams4e7d2c02009-08-29 19:13:11 -0700707static void mark_target_uptodate(struct stripe_head *sh, int target)
708{
709 struct r5dev *tgt;
710
711 if (target < 0)
712 return;
713
714 tgt = &sh->dev[target];
715 set_bit(R5_UPTODATE, &tgt->flags);
716 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
717 clear_bit(R5_Wantcompute, &tgt->flags);
718}
719
Dan Williamsac6b53b2009-07-14 13:40:19 -0700720static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700721{
722 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700723
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700724 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700725 (unsigned long long)sh->sector);
726
Dan Williamsac6b53b2009-07-14 13:40:19 -0700727 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700728 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700729 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700730
Dan Williamsecc65c92008-06-28 08:31:57 +1000731 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
732 if (sh->check_state == check_state_compute_run)
733 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700734 set_bit(STRIPE_HANDLE, &sh->state);
735 release_stripe(sh);
736}
737
Dan Williamsd6f38f32009-07-14 11:50:52 -0700738/* return a pointer to the address conversion region of the scribble buffer */
739static addr_conv_t *to_addr_conv(struct stripe_head *sh,
740 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700741{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700742 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
743}
744
745static struct dma_async_tx_descriptor *
746ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
747{
Dan Williams91c00922007-01-02 13:52:30 -0700748 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700749 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700750 int target = sh->ops.target;
751 struct r5dev *tgt = &sh->dev[target];
752 struct page *xor_dest = tgt->page;
753 int count = 0;
754 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700755 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700756 int i;
757
758 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700759 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700760 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
761
762 for (i = disks; i--; )
763 if (i != target)
764 xor_srcs[count++] = sh->dev[i].page;
765
766 atomic_inc(&sh->count);
767
Dan Williams0403e382009-09-08 17:42:50 -0700768 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700769 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700770 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700771 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700772 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700773 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700774
Dan Williams91c00922007-01-02 13:52:30 -0700775 return tx;
776}
777
Dan Williamsac6b53b2009-07-14 13:40:19 -0700778/* set_syndrome_sources - populate source buffers for gen_syndrome
779 * @srcs - (struct page *) array of size sh->disks
780 * @sh - stripe_head to parse
781 *
782 * Populates srcs in proper layout order for the stripe and returns the
783 * 'count' of sources to be used in a call to async_gen_syndrome. The P
784 * destination buffer is recorded in srcs[count] and the Q destination
785 * is recorded in srcs[count+1]].
786 */
787static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
788{
789 int disks = sh->disks;
790 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
791 int d0_idx = raid6_d0(sh);
792 int count;
793 int i;
794
795 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100796 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700797
798 count = 0;
799 i = d0_idx;
800 do {
801 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
802
803 srcs[slot] = sh->dev[i].page;
804 i = raid6_next_disk(i, disks);
805 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700806
NeilBrowne4424fe2009-10-16 16:27:34 +1100807 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700808}
809
810static struct dma_async_tx_descriptor *
811ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
812{
813 int disks = sh->disks;
814 struct page **blocks = percpu->scribble;
815 int target;
816 int qd_idx = sh->qd_idx;
817 struct dma_async_tx_descriptor *tx;
818 struct async_submit_ctl submit;
819 struct r5dev *tgt;
820 struct page *dest;
821 int i;
822 int count;
823
824 if (sh->ops.target < 0)
825 target = sh->ops.target2;
826 else if (sh->ops.target2 < 0)
827 target = sh->ops.target;
828 else
829 /* we should only have one valid target */
830 BUG();
831 BUG_ON(target < 0);
832 pr_debug("%s: stripe %llu block: %d\n",
833 __func__, (unsigned long long)sh->sector, target);
834
835 tgt = &sh->dev[target];
836 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
837 dest = tgt->page;
838
839 atomic_inc(&sh->count);
840
841 if (target == qd_idx) {
842 count = set_syndrome_sources(blocks, sh);
843 blocks[count] = NULL; /* regenerating p is not necessary */
844 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700845 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
846 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700847 to_addr_conv(sh, percpu));
848 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
849 } else {
850 /* Compute any data- or p-drive using XOR */
851 count = 0;
852 for (i = disks; i-- ; ) {
853 if (i == target || i == qd_idx)
854 continue;
855 blocks[count++] = sh->dev[i].page;
856 }
857
Dan Williams0403e382009-09-08 17:42:50 -0700858 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
859 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700860 to_addr_conv(sh, percpu));
861 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
862 }
863
864 return tx;
865}
866
867static struct dma_async_tx_descriptor *
868ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
869{
870 int i, count, disks = sh->disks;
871 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
872 int d0_idx = raid6_d0(sh);
873 int faila = -1, failb = -1;
874 int target = sh->ops.target;
875 int target2 = sh->ops.target2;
876 struct r5dev *tgt = &sh->dev[target];
877 struct r5dev *tgt2 = &sh->dev[target2];
878 struct dma_async_tx_descriptor *tx;
879 struct page **blocks = percpu->scribble;
880 struct async_submit_ctl submit;
881
882 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
883 __func__, (unsigned long long)sh->sector, target, target2);
884 BUG_ON(target < 0 || target2 < 0);
885 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
886 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
887
Dan Williams6c910a72009-09-16 12:24:54 -0700888 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700889 * slot number conversion for 'faila' and 'failb'
890 */
891 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100892 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700893 count = 0;
894 i = d0_idx;
895 do {
896 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
897
898 blocks[slot] = sh->dev[i].page;
899
900 if (i == target)
901 faila = slot;
902 if (i == target2)
903 failb = slot;
904 i = raid6_next_disk(i, disks);
905 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700906
907 BUG_ON(faila == failb);
908 if (failb < faila)
909 swap(faila, failb);
910 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
911 __func__, (unsigned long long)sh->sector, faila, failb);
912
913 atomic_inc(&sh->count);
914
915 if (failb == syndrome_disks+1) {
916 /* Q disk is one of the missing disks */
917 if (faila == syndrome_disks) {
918 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700919 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
920 ops_complete_compute, sh,
921 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100922 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700923 STRIPE_SIZE, &submit);
924 } else {
925 struct page *dest;
926 int data_target;
927 int qd_idx = sh->qd_idx;
928
929 /* Missing D+Q: recompute D from P, then recompute Q */
930 if (target == qd_idx)
931 data_target = target2;
932 else
933 data_target = target;
934
935 count = 0;
936 for (i = disks; i-- ; ) {
937 if (i == data_target || i == qd_idx)
938 continue;
939 blocks[count++] = sh->dev[i].page;
940 }
941 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700942 init_async_submit(&submit,
943 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
944 NULL, NULL, NULL,
945 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700946 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
947 &submit);
948
949 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700950 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
951 ops_complete_compute, sh,
952 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700953 return async_gen_syndrome(blocks, 0, count+2,
954 STRIPE_SIZE, &submit);
955 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700956 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700957 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
958 ops_complete_compute, sh,
959 to_addr_conv(sh, percpu));
960 if (failb == syndrome_disks) {
961 /* We're missing D+P. */
962 return async_raid6_datap_recov(syndrome_disks+2,
963 STRIPE_SIZE, faila,
964 blocks, &submit);
965 } else {
966 /* We're missing D+D. */
967 return async_raid6_2data_recov(syndrome_disks+2,
968 STRIPE_SIZE, faila, failb,
969 blocks, &submit);
970 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700971 }
972}
973
974
Dan Williams91c00922007-01-02 13:52:30 -0700975static void ops_complete_prexor(void *stripe_head_ref)
976{
977 struct stripe_head *sh = stripe_head_ref;
978
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700979 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700980 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700981}
982
983static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700984ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
985 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700986{
Dan Williams91c00922007-01-02 13:52:30 -0700987 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700988 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700989 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700990 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700991
992 /* existing parity data subtracted */
993 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
994
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700995 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700996 (unsigned long long)sh->sector);
997
998 for (i = disks; i--; ) {
999 struct r5dev *dev = &sh->dev[i];
1000 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001001 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001002 xor_srcs[count++] = dev->page;
1003 }
1004
Dan Williams0403e382009-09-08 17:42:50 -07001005 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001006 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001007 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001008
1009 return tx;
1010}
1011
1012static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001013ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001014{
1015 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001016 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001017
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001018 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001019 (unsigned long long)sh->sector);
1020
1021 for (i = disks; i--; ) {
1022 struct r5dev *dev = &sh->dev[i];
1023 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001024
Dan Williamsd8ee0722008-06-28 08:32:06 +10001025 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001026 struct bio *wbi;
1027
1028 spin_lock(&sh->lock);
1029 chosen = dev->towrite;
1030 dev->towrite = NULL;
1031 BUG_ON(dev->written);
1032 wbi = dev->written = chosen;
1033 spin_unlock(&sh->lock);
1034
1035 while (wbi && wbi->bi_sector <
1036 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001037 if (wbi->bi_rw & REQ_FUA)
1038 set_bit(R5_WantFUA, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001039 tx = async_copy_data(1, wbi, dev->page,
1040 dev->sector, tx);
1041 wbi = r5_next_bio(wbi, dev->sector);
1042 }
1043 }
1044 }
1045
1046 return tx;
1047}
1048
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001050{
1051 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001052 int disks = sh->disks;
1053 int pd_idx = sh->pd_idx;
1054 int qd_idx = sh->qd_idx;
1055 int i;
Tejun Heoe9c74692010-09-03 11:56:18 +02001056 bool fua = false;
Dan Williams91c00922007-01-02 13:52:30 -07001057
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001058 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001059 (unsigned long long)sh->sector);
1060
Tejun Heoe9c74692010-09-03 11:56:18 +02001061 for (i = disks; i--; )
1062 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1063
Dan Williams91c00922007-01-02 13:52:30 -07001064 for (i = disks; i--; ) {
1065 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001066
Tejun Heoe9c74692010-09-03 11:56:18 +02001067 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001068 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001069 if (fua)
1070 set_bit(R5_WantFUA, &dev->flags);
1071 }
Dan Williams91c00922007-01-02 13:52:30 -07001072 }
1073
Dan Williamsd8ee0722008-06-28 08:32:06 +10001074 if (sh->reconstruct_state == reconstruct_state_drain_run)
1075 sh->reconstruct_state = reconstruct_state_drain_result;
1076 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1077 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1078 else {
1079 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1080 sh->reconstruct_state = reconstruct_state_result;
1081 }
Dan Williams91c00922007-01-02 13:52:30 -07001082
1083 set_bit(STRIPE_HANDLE, &sh->state);
1084 release_stripe(sh);
1085}
1086
1087static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1089 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001090{
Dan Williams91c00922007-01-02 13:52:30 -07001091 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001092 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001093 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001094 int count = 0, pd_idx = sh->pd_idx, i;
1095 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001096 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001097 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001098
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001099 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001100 (unsigned long long)sh->sector);
1101
1102 /* check if prexor is active which means only process blocks
1103 * that are part of a read-modify-write (written)
1104 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001105 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1106 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001107 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1108 for (i = disks; i--; ) {
1109 struct r5dev *dev = &sh->dev[i];
1110 if (dev->written)
1111 xor_srcs[count++] = dev->page;
1112 }
1113 } else {
1114 xor_dest = sh->dev[pd_idx].page;
1115 for (i = disks; i--; ) {
1116 struct r5dev *dev = &sh->dev[i];
1117 if (i != pd_idx)
1118 xor_srcs[count++] = dev->page;
1119 }
1120 }
1121
Dan Williams91c00922007-01-02 13:52:30 -07001122 /* 1/ if we prexor'd then the dest is reused as a source
1123 * 2/ if we did not prexor then we are redoing the parity
1124 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1125 * for the synchronous xor case
1126 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001127 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001128 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1129
1130 atomic_inc(&sh->count);
1131
Dan Williamsac6b53b2009-07-14 13:40:19 -07001132 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001133 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001134 if (unlikely(count == 1))
1135 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1136 else
1137 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001138}
1139
Dan Williamsac6b53b2009-07-14 13:40:19 -07001140static void
1141ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1142 struct dma_async_tx_descriptor *tx)
1143{
1144 struct async_submit_ctl submit;
1145 struct page **blocks = percpu->scribble;
1146 int count;
1147
1148 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1149
1150 count = set_syndrome_sources(blocks, sh);
1151
1152 atomic_inc(&sh->count);
1153
1154 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1155 sh, to_addr_conv(sh, percpu));
1156 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001157}
1158
1159static void ops_complete_check(void *stripe_head_ref)
1160{
1161 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001162
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001163 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001164 (unsigned long long)sh->sector);
1165
Dan Williamsecc65c92008-06-28 08:31:57 +10001166 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001167 set_bit(STRIPE_HANDLE, &sh->state);
1168 release_stripe(sh);
1169}
1170
Dan Williamsac6b53b2009-07-14 13:40:19 -07001171static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001172{
Dan Williams91c00922007-01-02 13:52:30 -07001173 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001174 int pd_idx = sh->pd_idx;
1175 int qd_idx = sh->qd_idx;
1176 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001177 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001178 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001179 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001180 int count;
1181 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001182
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001183 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001184 (unsigned long long)sh->sector);
1185
Dan Williamsac6b53b2009-07-14 13:40:19 -07001186 count = 0;
1187 xor_dest = sh->dev[pd_idx].page;
1188 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001189 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001190 if (i == pd_idx || i == qd_idx)
1191 continue;
1192 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001193 }
1194
Dan Williamsd6f38f32009-07-14 11:50:52 -07001195 init_async_submit(&submit, 0, NULL, NULL, NULL,
1196 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001197 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001198 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001199
Dan Williams91c00922007-01-02 13:52:30 -07001200 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001201 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1202 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001203}
1204
Dan Williamsac6b53b2009-07-14 13:40:19 -07001205static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1206{
1207 struct page **srcs = percpu->scribble;
1208 struct async_submit_ctl submit;
1209 int count;
1210
1211 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1212 (unsigned long long)sh->sector, checkp);
1213
1214 count = set_syndrome_sources(srcs, sh);
1215 if (!checkp)
1216 srcs[count] = NULL;
1217
1218 atomic_inc(&sh->count);
1219 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1220 sh, to_addr_conv(sh, percpu));
1221 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1222 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1223}
1224
Dan Williams417b8d42009-10-16 16:25:22 +11001225static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001226{
1227 int overlap_clear = 0, i, disks = sh->disks;
1228 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001229 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001231 struct raid5_percpu *percpu;
1232 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001233
Dan Williamsd6f38f32009-07-14 11:50:52 -07001234 cpu = get_cpu();
1235 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001236 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001237 ops_run_biofill(sh);
1238 overlap_clear++;
1239 }
1240
Dan Williams7b3a8712008-06-28 08:32:09 +10001241 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001242 if (level < 6)
1243 tx = ops_run_compute5(sh, percpu);
1244 else {
1245 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1246 tx = ops_run_compute6_1(sh, percpu);
1247 else
1248 tx = ops_run_compute6_2(sh, percpu);
1249 }
1250 /* terminate the chain if reconstruct is not set to be run */
1251 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001252 async_tx_ack(tx);
1253 }
Dan Williams91c00922007-01-02 13:52:30 -07001254
Dan Williams600aa102008-06-28 08:32:05 +10001255 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001256 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001257
Dan Williams600aa102008-06-28 08:32:05 +10001258 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001259 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001260 overlap_clear++;
1261 }
1262
Dan Williamsac6b53b2009-07-14 13:40:19 -07001263 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1264 if (level < 6)
1265 ops_run_reconstruct5(sh, percpu, tx);
1266 else
1267 ops_run_reconstruct6(sh, percpu, tx);
1268 }
Dan Williams91c00922007-01-02 13:52:30 -07001269
Dan Williamsac6b53b2009-07-14 13:40:19 -07001270 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1271 if (sh->check_state == check_state_run)
1272 ops_run_check_p(sh, percpu);
1273 else if (sh->check_state == check_state_run_q)
1274 ops_run_check_pq(sh, percpu, 0);
1275 else if (sh->check_state == check_state_run_pq)
1276 ops_run_check_pq(sh, percpu, 1);
1277 else
1278 BUG();
1279 }
Dan Williams91c00922007-01-02 13:52:30 -07001280
Dan Williams91c00922007-01-02 13:52:30 -07001281 if (overlap_clear)
1282 for (i = disks; i--; ) {
1283 struct r5dev *dev = &sh->dev[i];
1284 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1285 wake_up(&sh->raid_conf->wait_for_overlap);
1286 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001287 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001288}
1289
Dan Williams417b8d42009-10-16 16:25:22 +11001290#ifdef CONFIG_MULTICORE_RAID456
1291static void async_run_ops(void *param, async_cookie_t cookie)
1292{
1293 struct stripe_head *sh = param;
1294 unsigned long ops_request = sh->ops.request;
1295
1296 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1297 wake_up(&sh->ops.wait_for_ops);
1298
1299 __raid_run_ops(sh, ops_request);
1300 release_stripe(sh);
1301}
1302
1303static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1304{
1305 /* since handle_stripe can be called outside of raid5d context
1306 * we need to ensure sh->ops.request is de-staged before another
1307 * request arrives
1308 */
1309 wait_event(sh->ops.wait_for_ops,
1310 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1311 sh->ops.request = ops_request;
1312
1313 atomic_inc(&sh->count);
1314 async_schedule(async_run_ops, sh);
1315}
1316#else
1317#define raid_run_ops __raid_run_ops
1318#endif
1319
NeilBrown3f294f42005-11-08 21:39:25 -08001320static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001323 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1324 if (!sh)
1325 return 0;
NeilBrowne4e11e32010-06-16 16:45:16 +10001326 memset(sh, 0, sizeof(*sh) + (conf->pool_size-1)*sizeof(struct r5dev));
NeilBrown3f294f42005-11-08 21:39:25 -08001327 sh->raid_conf = conf;
1328 spin_lock_init(&sh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001329 #ifdef CONFIG_MULTICORE_RAID456
1330 init_waitqueue_head(&sh->ops.wait_for_ops);
1331 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001332
NeilBrowne4e11e32010-06-16 16:45:16 +10001333 if (grow_buffers(sh)) {
1334 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001335 kmem_cache_free(conf->slab_cache, sh);
1336 return 0;
1337 }
1338 /* we just created an active stripe so... */
1339 atomic_set(&sh->count, 1);
1340 atomic_inc(&conf->active_stripes);
1341 INIT_LIST_HEAD(&sh->lru);
1342 release_stripe(sh);
1343 return 1;
1344}
1345
1346static int grow_stripes(raid5_conf_t *conf, int num)
1347{
Christoph Lametere18b8902006-12-06 20:33:20 -08001348 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001349 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
NeilBrownf4be6b42010-06-01 19:37:25 +10001351 if (conf->mddev->gendisk)
1352 sprintf(conf->cache_name[0],
1353 "raid%d-%s", conf->level, mdname(conf->mddev));
1354 else
1355 sprintf(conf->cache_name[0],
1356 "raid%d-%p", conf->level, conf->mddev);
1357 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1358
NeilBrownad01c9e2006-03-27 01:18:07 -08001359 conf->active_name = 0;
1360 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001362 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!sc)
1364 return 1;
1365 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001366 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001367 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001368 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
1371}
NeilBrown29269552006-03-27 01:18:10 -08001372
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373/**
1374 * scribble_len - return the required size of the scribble region
1375 * @num - total number of disks in the array
1376 *
1377 * The size must be enough to contain:
1378 * 1/ a struct page pointer for each device in the array +2
1379 * 2/ room to convert each entry in (1) to its corresponding dma
1380 * (dma_map_page()) or page (page_address()) address.
1381 *
1382 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1383 * calculate over all devices (not just the data blocks), using zeros in place
1384 * of the P and Q blocks.
1385 */
1386static size_t scribble_len(int num)
1387{
1388 size_t len;
1389
1390 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1391
1392 return len;
1393}
1394
NeilBrownad01c9e2006-03-27 01:18:07 -08001395static int resize_stripes(raid5_conf_t *conf, int newsize)
1396{
1397 /* Make all the stripes able to hold 'newsize' devices.
1398 * New slots in each stripe get 'page' set to a new page.
1399 *
1400 * This happens in stages:
1401 * 1/ create a new kmem_cache and allocate the required number of
1402 * stripe_heads.
1403 * 2/ gather all the old stripe_heads and tranfer the pages across
1404 * to the new stripe_heads. This will have the side effect of
1405 * freezing the array as once all stripe_heads have been collected,
1406 * no IO will be possible. Old stripe heads are freed once their
1407 * pages have been transferred over, and the old kmem_cache is
1408 * freed when all stripes are done.
1409 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1410 * we simple return a failre status - no need to clean anything up.
1411 * 4/ allocate new pages for the new slots in the new stripe_heads.
1412 * If this fails, we don't bother trying the shrink the
1413 * stripe_heads down again, we just leave them as they are.
1414 * As each stripe_head is processed the new one is released into
1415 * active service.
1416 *
1417 * Once step2 is started, we cannot afford to wait for a write,
1418 * so we use GFP_NOIO allocations.
1419 */
1420 struct stripe_head *osh, *nsh;
1421 LIST_HEAD(newstripes);
1422 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001423 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001424 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001425 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001426 int i;
1427
1428 if (newsize <= conf->pool_size)
1429 return 0; /* never bother to shrink */
1430
Dan Williamsb5470dc2008-06-27 21:44:04 -07001431 err = md_allow_write(conf->mddev);
1432 if (err)
1433 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001434
NeilBrownad01c9e2006-03-27 01:18:07 -08001435 /* Step 1 */
1436 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1437 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001438 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001439 if (!sc)
1440 return -ENOMEM;
1441
1442 for (i = conf->max_nr_stripes; i; i--) {
1443 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1444 if (!nsh)
1445 break;
1446
1447 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1448
1449 nsh->raid_conf = conf;
1450 spin_lock_init(&nsh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001451 #ifdef CONFIG_MULTICORE_RAID456
1452 init_waitqueue_head(&nsh->ops.wait_for_ops);
1453 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001454
1455 list_add(&nsh->lru, &newstripes);
1456 }
1457 if (i) {
1458 /* didn't get enough, give up */
1459 while (!list_empty(&newstripes)) {
1460 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1461 list_del(&nsh->lru);
1462 kmem_cache_free(sc, nsh);
1463 }
1464 kmem_cache_destroy(sc);
1465 return -ENOMEM;
1466 }
1467 /* Step 2 - Must use GFP_NOIO now.
1468 * OK, we have enough stripes, start collecting inactive
1469 * stripes and copying them over
1470 */
1471 list_for_each_entry(nsh, &newstripes, lru) {
1472 spin_lock_irq(&conf->device_lock);
1473 wait_event_lock_irq(conf->wait_for_stripe,
1474 !list_empty(&conf->inactive_list),
1475 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001476 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001477 );
1478 osh = get_free_stripe(conf);
1479 spin_unlock_irq(&conf->device_lock);
1480 atomic_set(&nsh->count, 1);
1481 for(i=0; i<conf->pool_size; i++)
1482 nsh->dev[i].page = osh->dev[i].page;
1483 for( ; i<newsize; i++)
1484 nsh->dev[i].page = NULL;
1485 kmem_cache_free(conf->slab_cache, osh);
1486 }
1487 kmem_cache_destroy(conf->slab_cache);
1488
1489 /* Step 3.
1490 * At this point, we are holding all the stripes so the array
1491 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001492 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001493 */
1494 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1495 if (ndisks) {
1496 for (i=0; i<conf->raid_disks; i++)
1497 ndisks[i] = conf->disks[i];
1498 kfree(conf->disks);
1499 conf->disks = ndisks;
1500 } else
1501 err = -ENOMEM;
1502
Dan Williamsd6f38f32009-07-14 11:50:52 -07001503 get_online_cpus();
1504 conf->scribble_len = scribble_len(newsize);
1505 for_each_present_cpu(cpu) {
1506 struct raid5_percpu *percpu;
1507 void *scribble;
1508
1509 percpu = per_cpu_ptr(conf->percpu, cpu);
1510 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1511
1512 if (scribble) {
1513 kfree(percpu->scribble);
1514 percpu->scribble = scribble;
1515 } else {
1516 err = -ENOMEM;
1517 break;
1518 }
1519 }
1520 put_online_cpus();
1521
NeilBrownad01c9e2006-03-27 01:18:07 -08001522 /* Step 4, return new stripes to service */
1523 while(!list_empty(&newstripes)) {
1524 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1525 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001526
NeilBrownad01c9e2006-03-27 01:18:07 -08001527 for (i=conf->raid_disks; i < newsize; i++)
1528 if (nsh->dev[i].page == NULL) {
1529 struct page *p = alloc_page(GFP_NOIO);
1530 nsh->dev[i].page = p;
1531 if (!p)
1532 err = -ENOMEM;
1533 }
1534 release_stripe(nsh);
1535 }
1536 /* critical section pass, GFP_NOIO no longer needed */
1537
1538 conf->slab_cache = sc;
1539 conf->active_name = 1-conf->active_name;
1540 conf->pool_size = newsize;
1541 return err;
1542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
NeilBrown3f294f42005-11-08 21:39:25 -08001544static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct stripe_head *sh;
1547
NeilBrown3f294f42005-11-08 21:39:25 -08001548 spin_lock_irq(&conf->device_lock);
1549 sh = get_free_stripe(conf);
1550 spin_unlock_irq(&conf->device_lock);
1551 if (!sh)
1552 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001553 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001554 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001555 kmem_cache_free(conf->slab_cache, sh);
1556 atomic_dec(&conf->active_stripes);
1557 return 1;
1558}
1559
1560static void shrink_stripes(raid5_conf_t *conf)
1561{
1562 while (drop_one_stripe(conf))
1563 ;
1564
NeilBrown29fc7e32006-02-03 03:03:41 -08001565 if (conf->slab_cache)
1566 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 conf->slab_cache = NULL;
1568}
1569
NeilBrown6712ecf2007-09-27 12:47:43 +02001570static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
NeilBrown99c0fb52009-03-31 14:39:38 +11001572 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001574 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001576 char b[BDEVNAME_SIZE];
1577 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 for (i=0 ; i<disks; i++)
1581 if (bi == &sh->dev[i].req)
1582 break;
1583
Dan Williams45b42332007-07-09 11:56:43 -07001584 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1585 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 uptodate);
1587 if (i == disks) {
1588 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001594 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001595 rdev = conf->disks[i].rdev;
NeilBrown0c55e022010-05-03 14:09:02 +10001596 printk_rl(KERN_INFO "md/raid:%s: read error corrected"
Bernd Schubert6be9d492008-05-23 13:04:34 -07001597 " (%lu sectors at %llu on %s)\n",
1598 mdname(conf->mddev), STRIPE_SECTORS,
1599 (unsigned long long)(sh->sector
1600 + rdev->data_offset),
1601 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001602 clear_bit(R5_ReadError, &sh->dev[i].flags);
1603 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1604 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001605 if (atomic_read(&conf->disks[i].rdev->read_errors))
1606 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001608 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001609 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001610 rdev = conf->disks[i].rdev;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001613 atomic_inc(&rdev->read_errors);
Gabriele A. Trombetti7b0bb532010-04-28 11:51:17 +10001614 if (conf->mddev->degraded >= conf->max_degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001615 printk_rl(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001616 "md/raid:%s: read error not correctable "
Bernd Schubert6be9d492008-05-23 13:04:34 -07001617 "(sector %llu on %s).\n",
1618 mdname(conf->mddev),
1619 (unsigned long long)(sh->sector
1620 + rdev->data_offset),
1621 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001622 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001623 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001624 printk_rl(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001625 "md/raid:%s: read error NOT corrected!! "
Bernd Schubert6be9d492008-05-23 13:04:34 -07001626 "(sector %llu on %s).\n",
1627 mdname(conf->mddev),
1628 (unsigned long long)(sh->sector
1629 + rdev->data_offset),
1630 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001631 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001632 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001633 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001634 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001635 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001636 else
1637 retry = 1;
1638 if (retry)
1639 set_bit(R5_ReadError, &sh->dev[i].flags);
1640 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001641 clear_bit(R5_ReadError, &sh->dev[i].flags);
1642 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001643 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1648 set_bit(STRIPE_HANDLE, &sh->state);
1649 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
NeilBrownd710e132008-10-13 11:55:12 +11001652static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
NeilBrown99c0fb52009-03-31 14:39:38 +11001654 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001656 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 for (i=0 ; i<disks; i++)
1660 if (bi == &sh->dev[i].req)
1661 break;
1662
Dan Williams45b42332007-07-09 11:56:43 -07001663 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1665 uptodate);
1666 if (i == disks) {
1667 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001668 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (!uptodate)
1672 md_error(conf->mddev, conf->disks[i].rdev);
1673
1674 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1675
1676 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1677 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001678 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681
NeilBrown784052e2009-03-31 15:19:07 +11001682static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
NeilBrown784052e2009-03-31 15:19:07 +11001684static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct r5dev *dev = &sh->dev[i];
1687
1688 bio_init(&dev->req);
1689 dev->req.bi_io_vec = &dev->vec;
1690 dev->req.bi_vcnt++;
1691 dev->req.bi_max_vecs++;
1692 dev->vec.bv_page = dev->page;
1693 dev->vec.bv_len = STRIPE_SIZE;
1694 dev->vec.bv_offset = 0;
1695
1696 dev->req.bi_sector = sh->sector;
1697 dev->req.bi_private = sh;
1698
1699 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001700 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1704{
1705 char b[BDEVNAME_SIZE];
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001706 raid5_conf_t *conf = mddev->private;
NeilBrown0c55e022010-05-03 14:09:02 +10001707 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
NeilBrownb2d444d2005-11-08 21:39:31 -08001709 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b422006-10-03 01:15:46 -07001710 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001711 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1712 unsigned long flags;
1713 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001715 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * if recovery was running, make sure it aborts.
1718 */
NeilBrowndfc70642008-05-23 13:04:39 -07001719 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001721 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001722 printk(KERN_ALERT
NeilBrown0c55e022010-05-03 14:09:02 +10001723 "md/raid:%s: Disk failure on %s, disabling device.\n"
NeilBrown0c55e022010-05-03 14:09:02 +10001724 "md/raid:%s: Operation continuing on %d devices.\n",
1725 mdname(mddev),
1726 bdevname(rdev->bdev, b),
1727 mdname(mddev),
1728 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732/*
1733 * Input: a 'big' sector number,
1734 * Output: index of the data and parity disk, and the sector # in them.
1735 */
NeilBrown112bf892009-03-31 14:39:38 +11001736static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001737 int previous, int *dd_idx,
1738 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001740 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001741 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001743 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001744 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001746 int algorithm = previous ? conf->prev_algo
1747 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001748 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1749 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001750 int raid_disks = previous ? conf->previous_raid_disks
1751 : conf->raid_disks;
1752 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* First compute the information on this sector */
1755
1756 /*
1757 * Compute the chunk number and the sector offset inside the chunk
1758 */
1759 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1760 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
1763 * Compute the stripe number
1764 */
NeilBrown35f2a592010-04-20 14:13:34 +10001765 stripe = chunk_number;
1766 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001767 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /*
1769 * Select the parity disk based on the user selected algorithm.
1770 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001771 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001772 switch(conf->level) {
1773 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001774 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001775 break;
1776 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001777 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001779 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001780 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 (*dd_idx)++;
1782 break;
1783 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001784 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001785 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 (*dd_idx)++;
1787 break;
1788 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001789 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001790 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 break;
1792 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001793 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001794 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001796 case ALGORITHM_PARITY_0:
1797 pd_idx = 0;
1798 (*dd_idx)++;
1799 break;
1800 case ALGORITHM_PARITY_N:
1801 pd_idx = data_disks;
1802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001804 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001805 }
1806 break;
1807 case 6:
1808
NeilBrowne183eae2009-03-31 15:20:22 +11001809 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001810 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001811 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001812 qd_idx = pd_idx + 1;
1813 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001814 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001815 qd_idx = 0;
1816 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001817 (*dd_idx) += 2; /* D D P Q D */
1818 break;
1819 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001820 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001821 qd_idx = pd_idx + 1;
1822 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001823 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001824 qd_idx = 0;
1825 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001826 (*dd_idx) += 2; /* D D P Q D */
1827 break;
1828 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001829 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001830 qd_idx = (pd_idx + 1) % raid_disks;
1831 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001832 break;
1833 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001834 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001835 qd_idx = (pd_idx + 1) % raid_disks;
1836 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001837 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001838
1839 case ALGORITHM_PARITY_0:
1840 pd_idx = 0;
1841 qd_idx = 1;
1842 (*dd_idx) += 2;
1843 break;
1844 case ALGORITHM_PARITY_N:
1845 pd_idx = data_disks;
1846 qd_idx = data_disks + 1;
1847 break;
1848
1849 case ALGORITHM_ROTATING_ZERO_RESTART:
1850 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1851 * of blocks for computing Q is different.
1852 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001853 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001854 qd_idx = pd_idx + 1;
1855 if (pd_idx == raid_disks-1) {
1856 (*dd_idx)++; /* Q D D D P */
1857 qd_idx = 0;
1858 } else if (*dd_idx >= pd_idx)
1859 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001860 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001861 break;
1862
1863 case ALGORITHM_ROTATING_N_RESTART:
1864 /* Same a left_asymmetric, by first stripe is
1865 * D D D P Q rather than
1866 * Q D D D P
1867 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001868 stripe2 += 1;
1869 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001870 qd_idx = pd_idx + 1;
1871 if (pd_idx == raid_disks-1) {
1872 (*dd_idx)++; /* Q D D D P */
1873 qd_idx = 0;
1874 } else if (*dd_idx >= pd_idx)
1875 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001876 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001877 break;
1878
1879 case ALGORITHM_ROTATING_N_CONTINUE:
1880 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001881 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11001882 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1883 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001884 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001885 break;
1886
1887 case ALGORITHM_LEFT_ASYMMETRIC_6:
1888 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10001889 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001890 if (*dd_idx >= pd_idx)
1891 (*dd_idx)++;
1892 qd_idx = raid_disks - 1;
1893 break;
1894
1895 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001896 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001897 if (*dd_idx >= pd_idx)
1898 (*dd_idx)++;
1899 qd_idx = raid_disks - 1;
1900 break;
1901
1902 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001903 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001904 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1905 qd_idx = raid_disks - 1;
1906 break;
1907
1908 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001909 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11001910 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1911 qd_idx = raid_disks - 1;
1912 break;
1913
1914 case ALGORITHM_PARITY_0_6:
1915 pd_idx = 0;
1916 (*dd_idx)++;
1917 qd_idx = raid_disks - 1;
1918 break;
1919
NeilBrown16a53ec2006-06-26 00:27:38 -07001920 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001921 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001922 }
1923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
NeilBrown911d4ee2009-03-31 14:39:38 +11001926 if (sh) {
1927 sh->pd_idx = pd_idx;
1928 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001929 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
1932 * Finally, compute the new sector number
1933 */
1934 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1935 return new_sector;
1936}
1937
1938
NeilBrown784052e2009-03-31 15:19:07 +11001939static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001942 int raid_disks = sh->disks;
1943 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001945 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1946 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001947 int algorithm = previous ? conf->prev_algo
1948 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 sector_t stripe;
1950 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10001951 sector_t chunk_number;
1952 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001954 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
NeilBrown16a53ec2006-06-26 00:27:38 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1958 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
NeilBrown16a53ec2006-06-26 00:27:38 -07001960 if (i == sh->pd_idx)
1961 return 0;
1962 switch(conf->level) {
1963 case 4: break;
1964 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001965 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 case ALGORITHM_LEFT_ASYMMETRIC:
1967 case ALGORITHM_RIGHT_ASYMMETRIC:
1968 if (i > sh->pd_idx)
1969 i--;
1970 break;
1971 case ALGORITHM_LEFT_SYMMETRIC:
1972 case ALGORITHM_RIGHT_SYMMETRIC:
1973 if (i < sh->pd_idx)
1974 i += raid_disks;
1975 i -= (sh->pd_idx + 1);
1976 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001977 case ALGORITHM_PARITY_0:
1978 i -= 1;
1979 break;
1980 case ALGORITHM_PARITY_N:
1981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11001983 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001984 }
1985 break;
1986 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001987 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001988 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001989 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001990 case ALGORITHM_LEFT_ASYMMETRIC:
1991 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001992 case ALGORITHM_ROTATING_ZERO_RESTART:
1993 case ALGORITHM_ROTATING_N_RESTART:
1994 if (sh->pd_idx == raid_disks-1)
1995 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001996 else if (i > sh->pd_idx)
1997 i -= 2; /* D D P Q D */
1998 break;
1999 case ALGORITHM_LEFT_SYMMETRIC:
2000 case ALGORITHM_RIGHT_SYMMETRIC:
2001 if (sh->pd_idx == raid_disks-1)
2002 i--; /* Q D D D P */
2003 else {
2004 /* D D P Q D */
2005 if (i < sh->pd_idx)
2006 i += raid_disks;
2007 i -= (sh->pd_idx + 2);
2008 }
2009 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002010 case ALGORITHM_PARITY_0:
2011 i -= 2;
2012 break;
2013 case ALGORITHM_PARITY_N:
2014 break;
2015 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002016 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002017 if (sh->pd_idx == 0)
2018 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002019 else {
2020 /* D D Q P D */
2021 if (i < sh->pd_idx)
2022 i += raid_disks;
2023 i -= (sh->pd_idx + 1);
2024 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002025 break;
2026 case ALGORITHM_LEFT_ASYMMETRIC_6:
2027 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2028 if (i > sh->pd_idx)
2029 i--;
2030 break;
2031 case ALGORITHM_LEFT_SYMMETRIC_6:
2032 case ALGORITHM_RIGHT_SYMMETRIC_6:
2033 if (i < sh->pd_idx)
2034 i += data_disks + 1;
2035 i -= (sh->pd_idx + 1);
2036 break;
2037 case ALGORITHM_PARITY_0_6:
2038 i -= 1;
2039 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002040 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002041 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002042 }
2043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
2046 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002047 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
NeilBrown112bf892009-03-31 14:39:38 +11002049 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002050 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2052 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002053 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2054 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return 0;
2056 }
2057 return r_sector;
2058}
2059
2060
Dan Williams600aa102008-06-28 08:32:05 +10002061static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002062schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002063 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002064{
2065 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002066 raid5_conf_t *conf = sh->raid_conf;
2067 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002068
Dan Williamse33129d2007-01-02 13:52:30 -07002069 if (rcw) {
2070 /* if we are not expanding this is a proper write request, and
2071 * there will be bios with new data to be drained into the
2072 * stripe cache
2073 */
2074 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002075 sh->reconstruct_state = reconstruct_state_drain_run;
2076 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2077 } else
2078 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002079
Dan Williamsac6b53b2009-07-14 13:40:19 -07002080 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002081
2082 for (i = disks; i--; ) {
2083 struct r5dev *dev = &sh->dev[i];
2084
2085 if (dev->towrite) {
2086 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002087 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002088 if (!expand)
2089 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002090 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002091 }
2092 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002093 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002094 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002095 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002096 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002097 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002098 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2099 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2100
Dan Williamsd8ee0722008-06-28 08:32:06 +10002101 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002102 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2103 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002104 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002105
2106 for (i = disks; i--; ) {
2107 struct r5dev *dev = &sh->dev[i];
2108 if (i == pd_idx)
2109 continue;
2110
Dan Williamse33129d2007-01-02 13:52:30 -07002111 if (dev->towrite &&
2112 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002113 test_bit(R5_Wantcompute, &dev->flags))) {
2114 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002115 set_bit(R5_LOCKED, &dev->flags);
2116 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002117 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002118 }
2119 }
2120 }
2121
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002122 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002123 * are in flight
2124 */
2125 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2126 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002127 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002128
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002129 if (level == 6) {
2130 int qd_idx = sh->qd_idx;
2131 struct r5dev *dev = &sh->dev[qd_idx];
2132
2133 set_bit(R5_LOCKED, &dev->flags);
2134 clear_bit(R5_UPTODATE, &dev->flags);
2135 s->locked++;
2136 }
2137
Dan Williams600aa102008-06-28 08:32:05 +10002138 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002139 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002140 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002141}
NeilBrown16a53ec2006-06-26 00:27:38 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/*
2144 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002145 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 * The bi_next chain must be in order.
2147 */
2148static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2149{
2150 struct bio **bip;
2151 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002152 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Dan Williams45b42332007-07-09 11:56:43 -07002154 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 (unsigned long long)bi->bi_sector,
2156 (unsigned long long)sh->sector);
2157
2158
2159 spin_lock(&sh->lock);
2160 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002161 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002163 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2164 firstwrite = 1;
2165 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 bip = &sh->dev[dd_idx].toread;
2167 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2168 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2169 goto overlap;
2170 bip = & (*bip)->bi_next;
2171 }
2172 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2173 goto overlap;
2174
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002175 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (*bip)
2177 bi->bi_next = *bip;
2178 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002179 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 spin_unlock_irq(&conf->device_lock);
2181 spin_unlock(&sh->lock);
2182
Dan Williams45b42332007-07-09 11:56:43 -07002183 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 (unsigned long long)bi->bi_sector,
2185 (unsigned long long)sh->sector, dd_idx);
2186
NeilBrown72626682005-09-09 16:23:54 -07002187 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002188 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2189 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002190 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002191 set_bit(STRIPE_BIT_DELAY, &sh->state);
2192 }
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (forwrite) {
2195 /* check if page is covered */
2196 sector_t sector = sh->dev[dd_idx].sector;
2197 for (bi=sh->dev[dd_idx].towrite;
2198 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2199 bi && bi->bi_sector <= sector;
2200 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2201 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2202 sector = bi->bi_sector + (bi->bi_size>>9);
2203 }
2204 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2205 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2206 }
2207 return 1;
2208
2209 overlap:
2210 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2211 spin_unlock_irq(&conf->device_lock);
2212 spin_unlock(&sh->lock);
2213 return 0;
2214}
2215
NeilBrown29269552006-03-27 01:18:10 -08002216static void end_reshape(raid5_conf_t *conf);
2217
NeilBrown911d4ee2009-03-31 14:39:38 +11002218static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2219 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002220{
NeilBrown784052e2009-03-31 15:19:07 +11002221 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002222 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002223 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002224 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002225 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002226
NeilBrown112bf892009-03-31 14:39:38 +11002227 raid5_compute_sector(conf,
2228 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002229 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002230 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002231 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002232}
2233
Dan Williamsa4456852007-07-09 11:56:43 -07002234static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002235handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002236 struct stripe_head_state *s, int disks,
2237 struct bio **return_bi)
2238{
2239 int i;
2240 for (i = disks; i--; ) {
2241 struct bio *bi;
2242 int bitmap_end = 0;
2243
2244 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2245 mdk_rdev_t *rdev;
2246 rcu_read_lock();
2247 rdev = rcu_dereference(conf->disks[i].rdev);
2248 if (rdev && test_bit(In_sync, &rdev->flags))
2249 /* multiple read failures in one stripe */
2250 md_error(conf->mddev, rdev);
2251 rcu_read_unlock();
2252 }
2253 spin_lock_irq(&conf->device_lock);
2254 /* fail all writes first */
2255 bi = sh->dev[i].towrite;
2256 sh->dev[i].towrite = NULL;
2257 if (bi) {
2258 s->to_write--;
2259 bitmap_end = 1;
2260 }
2261
2262 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2263 wake_up(&conf->wait_for_overlap);
2264
2265 while (bi && bi->bi_sector <
2266 sh->dev[i].sector + STRIPE_SECTORS) {
2267 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2268 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002269 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002270 md_write_end(conf->mddev);
2271 bi->bi_next = *return_bi;
2272 *return_bi = bi;
2273 }
2274 bi = nextbi;
2275 }
2276 /* and fail all 'written' */
2277 bi = sh->dev[i].written;
2278 sh->dev[i].written = NULL;
2279 if (bi) bitmap_end = 1;
2280 while (bi && bi->bi_sector <
2281 sh->dev[i].sector + STRIPE_SECTORS) {
2282 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2283 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002284 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002285 md_write_end(conf->mddev);
2286 bi->bi_next = *return_bi;
2287 *return_bi = bi;
2288 }
2289 bi = bi2;
2290 }
2291
Dan Williamsb5e98d62007-01-02 13:52:31 -07002292 /* fail any reads if this device is non-operational and
2293 * the data has not reached the cache yet.
2294 */
2295 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2296 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2297 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002298 bi = sh->dev[i].toread;
2299 sh->dev[i].toread = NULL;
2300 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2301 wake_up(&conf->wait_for_overlap);
2302 if (bi) s->to_read--;
2303 while (bi && bi->bi_sector <
2304 sh->dev[i].sector + STRIPE_SECTORS) {
2305 struct bio *nextbi =
2306 r5_next_bio(bi, sh->dev[i].sector);
2307 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002308 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002309 bi->bi_next = *return_bi;
2310 *return_bi = bi;
2311 }
2312 bi = nextbi;
2313 }
2314 }
2315 spin_unlock_irq(&conf->device_lock);
2316 if (bitmap_end)
2317 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2318 STRIPE_SECTORS, 0, 0);
2319 }
2320
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002321 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2322 if (atomic_dec_and_test(&conf->pending_full_writes))
2323 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002324}
2325
Dan Williams1fe797e2008-06-28 09:16:30 +10002326/* fetch_block5 - checks the given member device to see if its data needs
2327 * to be read or computed to satisfy a request.
2328 *
2329 * Returns 1 when no more member devices need to be checked, otherwise returns
2330 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002331 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002332static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2333 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002334{
2335 struct r5dev *dev = &sh->dev[disk_idx];
2336 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2337
Dan Williamsf38e1212007-01-02 13:52:30 -07002338 /* is the data in this block needed, and can we get it? */
2339 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002340 !test_bit(R5_UPTODATE, &dev->flags) &&
2341 (dev->toread ||
2342 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2343 s->syncing || s->expanding ||
2344 (s->failed &&
2345 (failed_dev->toread ||
2346 (failed_dev->towrite &&
2347 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002348 /* We would like to get this block, possibly by computing it,
2349 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002350 */
2351 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002352 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002353 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2354 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002355 set_bit(R5_Wantcompute, &dev->flags);
2356 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002357 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002358 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002359 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002360 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002361 * before writes. R5_Wantcompute flags a block that will
2362 * be R5_UPTODATE by the time it is needed for a
2363 * subsequent operation.
2364 */
2365 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002366 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002367 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002368 set_bit(R5_LOCKED, &dev->flags);
2369 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002370 s->locked++;
2371 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2372 s->syncing);
2373 }
2374 }
2375
Dan Williams1fe797e2008-06-28 09:16:30 +10002376 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002377}
2378
Dan Williams1fe797e2008-06-28 09:16:30 +10002379/**
2380 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2381 */
2382static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002383 struct stripe_head_state *s, int disks)
2384{
2385 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002386
Dan Williamsf38e1212007-01-02 13:52:30 -07002387 /* look for blocks to read/compute, skip this if a compute
2388 * is already in flight, or if the stripe contents are in the
2389 * midst of changing due to a write
2390 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002391 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002392 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002393 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002394 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002395 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002396 set_bit(STRIPE_HANDLE, &sh->state);
2397}
2398
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002399/* fetch_block6 - checks the given member device to see if its data needs
2400 * to be read or computed to satisfy a request.
2401 *
2402 * Returns 1 when no more member devices need to be checked, otherwise returns
2403 * 0 to tell the loop in handle_stripe_fill6 to continue
2404 */
2405static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2406 struct r6_state *r6s, int disk_idx, int disks)
2407{
2408 struct r5dev *dev = &sh->dev[disk_idx];
2409 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2410 &sh->dev[r6s->failed_num[1]] };
2411
2412 if (!test_bit(R5_LOCKED, &dev->flags) &&
2413 !test_bit(R5_UPTODATE, &dev->flags) &&
2414 (dev->toread ||
2415 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2416 s->syncing || s->expanding ||
2417 (s->failed >= 1 &&
2418 (fdev[0]->toread || s->to_write)) ||
2419 (s->failed >= 2 &&
2420 (fdev[1]->toread || s->to_write)))) {
2421 /* we would like to get this block, possibly by computing it,
2422 * otherwise read it if the backing disk is insync
2423 */
2424 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2425 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2426 if ((s->uptodate == disks - 1) &&
2427 (s->failed && (disk_idx == r6s->failed_num[0] ||
2428 disk_idx == r6s->failed_num[1]))) {
2429 /* have disk failed, and we're requested to fetch it;
2430 * do compute it
2431 */
2432 pr_debug("Computing stripe %llu block %d\n",
2433 (unsigned long long)sh->sector, disk_idx);
2434 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2435 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2436 set_bit(R5_Wantcompute, &dev->flags);
2437 sh->ops.target = disk_idx;
2438 sh->ops.target2 = -1; /* no 2nd target */
2439 s->req_compute = 1;
2440 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/**
2480 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2481 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002482static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002483 struct stripe_head_state *s, struct r6_state *r6s,
2484 int disks)
2485{
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--; )
2495 if (fetch_block6(sh, s, r6s, i, disks))
2496 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 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002506static void handle_stripe_clean_event(raid5_conf_t *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
Dan Williams1fe797e2008-06-28 09:16:30 +10002551static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002552 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2553{
2554 int rmw = 0, rcw = 0, i;
2555 for (i = disks; i--; ) {
2556 /* would I have to read this buffer for read_modify_write */
2557 struct r5dev *dev = &sh->dev[i];
2558 if ((dev->towrite || i == sh->pd_idx) &&
2559 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002560 !(test_bit(R5_UPTODATE, &dev->flags) ||
2561 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002562 if (test_bit(R5_Insync, &dev->flags))
2563 rmw++;
2564 else
2565 rmw += 2*disks; /* cannot read it */
2566 }
2567 /* Would I have to read this buffer for reconstruct_write */
2568 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2569 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002570 !(test_bit(R5_UPTODATE, &dev->flags) ||
2571 test_bit(R5_Wantcompute, &dev->flags))) {
2572 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002573 else
2574 rcw += 2*disks;
2575 }
2576 }
Dan Williams45b42332007-07-09 11:56:43 -07002577 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002578 (unsigned long long)sh->sector, rmw, rcw);
2579 set_bit(STRIPE_HANDLE, &sh->state);
2580 if (rmw < rcw && rmw > 0)
2581 /* prefer read-modify-write, but need to get some data */
2582 for (i = disks; i--; ) {
2583 struct r5dev *dev = &sh->dev[i];
2584 if ((dev->towrite || i == sh->pd_idx) &&
2585 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002586 !(test_bit(R5_UPTODATE, &dev->flags) ||
2587 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002588 test_bit(R5_Insync, &dev->flags)) {
2589 if (
2590 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002591 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002592 "%d for r-m-w\n", i);
2593 set_bit(R5_LOCKED, &dev->flags);
2594 set_bit(R5_Wantread, &dev->flags);
2595 s->locked++;
2596 } else {
2597 set_bit(STRIPE_DELAYED, &sh->state);
2598 set_bit(STRIPE_HANDLE, &sh->state);
2599 }
2600 }
2601 }
2602 if (rcw <= rmw && rcw > 0)
2603 /* want reconstruct write, but need to get some data */
2604 for (i = disks; i--; ) {
2605 struct r5dev *dev = &sh->dev[i];
2606 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2607 i != sh->pd_idx &&
2608 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002609 !(test_bit(R5_UPTODATE, &dev->flags) ||
2610 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002611 test_bit(R5_Insync, &dev->flags)) {
2612 if (
2613 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002614 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002615 "%d for Reconstruct\n", i);
2616 set_bit(R5_LOCKED, &dev->flags);
2617 set_bit(R5_Wantread, &dev->flags);
2618 s->locked++;
2619 } else {
2620 set_bit(STRIPE_DELAYED, &sh->state);
2621 set_bit(STRIPE_HANDLE, &sh->state);
2622 }
2623 }
2624 }
2625 /* now if nothing is locked, and if we have enough data,
2626 * we can start a write request
2627 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002628 /* since handle_stripe can be called at any time we need to handle the
2629 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002630 * subsequent call wants to start a write request. raid_run_ops only
2631 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002632 * simultaneously. If this is not the case then new writes need to be
2633 * held off until the compute completes.
2634 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002635 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2636 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2637 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002638 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002639}
2640
Dan Williams1fe797e2008-06-28 09:16:30 +10002641static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002642 struct stripe_head *sh, struct stripe_head_state *s,
2643 struct r6_state *r6s, int disks)
2644{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002645 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002646 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002647
2648 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002649 for (i = disks; i--; ) {
2650 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002651 /* check if we haven't enough data */
2652 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2653 i != pd_idx && i != qd_idx &&
2654 !test_bit(R5_LOCKED, &dev->flags) &&
2655 !(test_bit(R5_UPTODATE, &dev->flags) ||
2656 test_bit(R5_Wantcompute, &dev->flags))) {
2657 rcw++;
2658 if (!test_bit(R5_Insync, &dev->flags))
2659 continue; /* it's a failed drive */
2660
2661 if (
2662 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2663 pr_debug("Read_old stripe %llu "
2664 "block %d for Reconstruct\n",
2665 (unsigned long long)sh->sector, i);
2666 set_bit(R5_LOCKED, &dev->flags);
2667 set_bit(R5_Wantread, &dev->flags);
2668 s->locked++;
2669 } else {
2670 pr_debug("Request delayed stripe %llu "
2671 "block %d for Reconstruct\n",
2672 (unsigned long long)sh->sector, i);
2673 set_bit(STRIPE_DELAYED, &sh->state);
2674 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002675 }
2676 }
2677 }
Dan Williamsa4456852007-07-09 11:56:43 -07002678 /* now if nothing is locked, and if we have enough data, we can start a
2679 * write request
2680 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002681 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2682 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002683 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002684 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002685 }
2686}
2687
2688static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2689 struct stripe_head_state *s, int disks)
2690{
Dan Williamsecc65c92008-06-28 08:31:57 +10002691 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002692
Dan Williamsbd2ab672008-04-10 21:29:27 -07002693 set_bit(STRIPE_HANDLE, &sh->state);
2694
Dan Williamsecc65c92008-06-28 08:31:57 +10002695 switch (sh->check_state) {
2696 case check_state_idle:
2697 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002698 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002699 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002700 sh->check_state = check_state_run;
2701 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002702 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002703 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002704 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002705 }
Dan Williamsa4456852007-07-09 11:56:43 -07002706 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002707 /* fall through */
2708 case check_state_compute_result:
2709 sh->check_state = check_state_idle;
2710 if (!dev)
2711 dev = &sh->dev[sh->pd_idx];
2712
2713 /* check that a write has not made the stripe insync */
2714 if (test_bit(STRIPE_INSYNC, &sh->state))
2715 break;
2716
2717 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002718 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2719 BUG_ON(s->uptodate != disks);
2720
2721 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002722 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002723 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002724
Dan Williamsa4456852007-07-09 11:56:43 -07002725 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002726 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002727 break;
2728 case check_state_run:
2729 break; /* we will be called again upon completion */
2730 case check_state_check_result:
2731 sh->check_state = check_state_idle;
2732
2733 /* if a failure occurred during the check operation, leave
2734 * STRIPE_INSYNC not set and let the stripe be handled again
2735 */
2736 if (s->failed)
2737 break;
2738
2739 /* handle a successful check operation, if parity is correct
2740 * we are done. Otherwise update the mismatch count and repair
2741 * parity if !MD_RECOVERY_CHECK
2742 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002743 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002744 /* parity is correct (on disc,
2745 * not in buffer any more)
2746 */
2747 set_bit(STRIPE_INSYNC, &sh->state);
2748 else {
2749 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2750 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2751 /* don't try to repair!! */
2752 set_bit(STRIPE_INSYNC, &sh->state);
2753 else {
2754 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002755 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002756 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2757 set_bit(R5_Wantcompute,
2758 &sh->dev[sh->pd_idx].flags);
2759 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002760 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002761 s->uptodate++;
2762 }
2763 }
2764 break;
2765 case check_state_compute_run:
2766 break;
2767 default:
2768 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2769 __func__, sh->check_state,
2770 (unsigned long long) sh->sector);
2771 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002772 }
2773}
2774
2775
2776static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002777 struct stripe_head_state *s,
2778 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002779{
Dan Williamsa4456852007-07-09 11:56:43 -07002780 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002781 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002782 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002783
2784 set_bit(STRIPE_HANDLE, &sh->state);
2785
2786 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002787
Dan Williamsa4456852007-07-09 11:56:43 -07002788 /* Want to check and possibly repair P and Q.
2789 * However there could be one 'failed' device, in which
2790 * case we can only check one of them, possibly using the
2791 * other to generate missing data
2792 */
2793
Dan Williamsd82dfee2009-07-14 13:40:57 -07002794 switch (sh->check_state) {
2795 case check_state_idle:
2796 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002797 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002798 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002799 * makes sense to check P (If anything else were failed,
2800 * we would have used P to recreate it).
2801 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002802 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002803 }
2804 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002805 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002806 * anything, so it makes sense to check it
2807 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002808 if (sh->check_state == check_state_run)
2809 sh->check_state = check_state_run_pq;
2810 else
2811 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002812 }
Dan Williams36d1c642009-07-14 11:48:22 -07002813
Dan Williamsd82dfee2009-07-14 13:40:57 -07002814 /* discard potentially stale zero_sum_result */
2815 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002816
Dan Williamsd82dfee2009-07-14 13:40:57 -07002817 if (sh->check_state == check_state_run) {
2818 /* async_xor_zero_sum destroys the contents of P */
2819 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2820 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002821 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002822 if (sh->check_state >= check_state_run &&
2823 sh->check_state <= check_state_run_pq) {
2824 /* async_syndrome_zero_sum preserves P and Q, so
2825 * no need to mark them !uptodate here
2826 */
2827 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2828 break;
2829 }
Dan Williams36d1c642009-07-14 11:48:22 -07002830
Dan Williamsd82dfee2009-07-14 13:40:57 -07002831 /* we have 2-disk failure */
2832 BUG_ON(s->failed != 2);
2833 /* fall through */
2834 case check_state_compute_result:
2835 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002836
Dan Williamsd82dfee2009-07-14 13:40:57 -07002837 /* check that a write has not made the stripe insync */
2838 if (test_bit(STRIPE_INSYNC, &sh->state))
2839 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002840
2841 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002842 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002843 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002844 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002845 if (s->failed == 2) {
2846 dev = &sh->dev[r6s->failed_num[1]];
2847 s->locked++;
2848 set_bit(R5_LOCKED, &dev->flags);
2849 set_bit(R5_Wantwrite, &dev->flags);
2850 }
2851 if (s->failed >= 1) {
2852 dev = &sh->dev[r6s->failed_num[0]];
2853 s->locked++;
2854 set_bit(R5_LOCKED, &dev->flags);
2855 set_bit(R5_Wantwrite, &dev->flags);
2856 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002857 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002858 dev = &sh->dev[pd_idx];
2859 s->locked++;
2860 set_bit(R5_LOCKED, &dev->flags);
2861 set_bit(R5_Wantwrite, &dev->flags);
2862 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002863 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002864 dev = &sh->dev[qd_idx];
2865 s->locked++;
2866 set_bit(R5_LOCKED, &dev->flags);
2867 set_bit(R5_Wantwrite, &dev->flags);
2868 }
2869 clear_bit(STRIPE_DEGRADED, &sh->state);
2870
2871 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002872 break;
2873 case check_state_run:
2874 case check_state_run_q:
2875 case check_state_run_pq:
2876 break; /* we will be called again upon completion */
2877 case check_state_check_result:
2878 sh->check_state = check_state_idle;
2879
2880 /* handle a successful check operation, if parity is correct
2881 * we are done. Otherwise update the mismatch count and repair
2882 * parity if !MD_RECOVERY_CHECK
2883 */
2884 if (sh->ops.zero_sum_result == 0) {
2885 /* both parities are correct */
2886 if (!s->failed)
2887 set_bit(STRIPE_INSYNC, &sh->state);
2888 else {
2889 /* in contrast to the raid5 case we can validate
2890 * parity, but still have a failure to write
2891 * back
2892 */
2893 sh->check_state = check_state_compute_result;
2894 /* Returning at this point means that we may go
2895 * off and bring p and/or q uptodate again so
2896 * we make sure to check zero_sum_result again
2897 * to verify if p or q need writeback
2898 */
2899 }
2900 } else {
2901 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2902 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2903 /* don't try to repair!! */
2904 set_bit(STRIPE_INSYNC, &sh->state);
2905 else {
2906 int *target = &sh->ops.target;
2907
2908 sh->ops.target = -1;
2909 sh->ops.target2 = -1;
2910 sh->check_state = check_state_compute_run;
2911 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2912 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2913 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2914 set_bit(R5_Wantcompute,
2915 &sh->dev[pd_idx].flags);
2916 *target = pd_idx;
2917 target = &sh->ops.target2;
2918 s->uptodate++;
2919 }
2920 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2921 set_bit(R5_Wantcompute,
2922 &sh->dev[qd_idx].flags);
2923 *target = qd_idx;
2924 s->uptodate++;
2925 }
2926 }
2927 }
2928 break;
2929 case check_state_compute_run:
2930 break;
2931 default:
2932 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2933 __func__, sh->check_state,
2934 (unsigned long long) sh->sector);
2935 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002936 }
2937}
2938
2939static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2940 struct r6_state *r6s)
2941{
2942 int i;
2943
2944 /* We have read all the blocks in this stripe and now we need to
2945 * copy some of them into a target stripe for expand.
2946 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002947 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002948 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2949 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002950 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002951 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002952 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002953 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002954
NeilBrown784052e2009-03-31 15:19:07 +11002955 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002956 sector_t s = raid5_compute_sector(conf, bn, 0,
2957 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002958 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002959 if (sh2 == NULL)
2960 /* so far only the early blocks of this stripe
2961 * have been requested. When later blocks
2962 * get requested, we will try again
2963 */
2964 continue;
2965 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2966 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2967 /* must have already done this block */
2968 release_stripe(sh2);
2969 continue;
2970 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002971
2972 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002973 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002974 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002975 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002976 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002977
Dan Williamsa4456852007-07-09 11:56:43 -07002978 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2979 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2980 for (j = 0; j < conf->raid_disks; j++)
2981 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002982 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002983 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2984 break;
2985 if (j == conf->raid_disks) {
2986 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2987 set_bit(STRIPE_HANDLE, &sh2->state);
2988 }
2989 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002990
Dan Williamsa4456852007-07-09 11:56:43 -07002991 }
NeilBrowna2e08552007-09-11 15:23:36 -07002992 /* done submitting copies, wait for them to complete */
2993 if (tx) {
2994 async_tx_ack(tx);
2995 dma_wait_for_async_tx(tx);
2996 }
Dan Williamsa4456852007-07-09 11:56:43 -07002997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Dan Williams6bfe0b42008-04-30 00:52:32 -07002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000/*
3001 * handle_stripe - do things to a stripe.
3002 *
3003 * We lock the stripe and then examine the state of various bits
3004 * to see what needs to be done.
3005 * Possible results:
3006 * return some read request which now have data
3007 * return some write requests which are safely on disc
3008 * schedule a read on some buffers
3009 * schedule a write of some buffers
3010 * return confirmation of parity correctness
3011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 * buffers are taken off read_list or write_list, and bh_cache buffers
3013 * get BH_Lock set before the stripe lock is released.
3014 *
3015 */
Dan Williamsa4456852007-07-09 11:56:43 -07003016
NeilBrown14425772009-10-16 15:55:25 +11003017static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
3019 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07003020 int disks = sh->disks, i;
3021 struct bio *return_bi = NULL;
3022 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003024 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07003025 int prexor;
NeilBrown729a1862009-12-14 12:49:50 +11003026 int dec_preread_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Dan Williamsa4456852007-07-09 11:56:43 -07003028 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10003029 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
3030 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
3031 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
3032 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 spin_lock(&sh->lock);
3035 clear_bit(STRIPE_HANDLE, &sh->state);
3036 clear_bit(STRIPE_DELAYED, &sh->state);
3037
Dan Williamsa4456852007-07-09 11:56:43 -07003038 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3039 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3040 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08003043 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 for (i=disks; i--; ) {
3045 mdk_rdev_t *rdev;
NeilBrowna9f326e2009-09-23 18:06:41 +10003046
3047 dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Dan Williamsb5e98d62007-01-02 13:52:31 -07003049 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
3050 "written %p\n", i, dev->flags, dev->toread, dev->read,
3051 dev->towrite, dev->written);
3052
3053 /* maybe we can request a biofill operation
3054 *
3055 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10003056 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07003057 */
3058 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10003059 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07003060 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003063 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3064 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07003065 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Dan Williamsb5e98d62007-01-02 13:52:31 -07003067 if (test_bit(R5_Wantfill, &dev->flags))
3068 s.to_fill++;
3069 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003070 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003072 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003074 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Dan Williamsa4456852007-07-09 11:56:43 -07003076 if (dev->written)
3077 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08003078 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003079 if (blocked_rdev == NULL &&
3080 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003081 blocked_rdev = rdev;
3082 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003083 }
NeilBrown415e72d2010-06-17 17:25:21 +10003084 clear_bit(R5_Insync, &dev->flags);
3085 if (!rdev)
3086 /* Not in-sync */;
3087 else if (test_bit(In_sync, &rdev->flags))
3088 set_bit(R5_Insync, &dev->flags);
3089 else {
3090 /* could be in-sync depending on recovery/reshape status */
3091 if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3092 set_bit(R5_Insync, &dev->flags);
3093 }
3094 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08003095 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08003096 clear_bit(R5_ReadError, &dev->flags);
3097 clear_bit(R5_ReWrite, &dev->flags);
3098 }
NeilBrown415e72d2010-06-17 17:25:21 +10003099 if (test_bit(R5_ReadError, &dev->flags))
3100 clear_bit(R5_Insync, &dev->flags);
3101 if (!test_bit(R5_Insync, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003102 s.failed++;
3103 s.failed_num = i;
NeilBrown415e72d2010-06-17 17:25:21 +10003104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
NeilBrown9910f162006-01-06 00:20:24 -08003106 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07003107
Dan Williams6bfe0b42008-04-30 00:52:32 -07003108 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003109 if (s.syncing || s.expanding || s.expanded ||
3110 s.to_write || s.written) {
3111 set_bit(STRIPE_HANDLE, &sh->state);
3112 goto unlock;
3113 }
3114 /* There is nothing for the blocked_rdev to block */
3115 rdev_dec_pending(blocked_rdev, conf->mddev);
3116 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003117 }
3118
Dan Williams83de75c2008-06-28 08:31:58 +10003119 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3120 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3121 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3122 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07003123
Dan Williams45b42332007-07-09 11:56:43 -07003124 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003126 s.locked, s.uptodate, s.to_read, s.to_write,
3127 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 /* check if the array has lost two devices and, if so, some requests might
3129 * need to be failed
3130 */
Dan Williamsa4456852007-07-09 11:56:43 -07003131 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003132 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003133 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3135 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003136 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
3138
3139 /* might be able to return some write requests if the parity block
3140 * is safe, or on a failed drive
3141 */
3142 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003143 if ( s.written &&
3144 ((test_bit(R5_Insync, &dev->flags) &&
3145 !test_bit(R5_LOCKED, &dev->flags) &&
3146 test_bit(R5_UPTODATE, &dev->flags)) ||
3147 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003148 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 /* Now we might consider reading some blocks, either to check/generate
3151 * parity, or to satisfy requests
3152 * or to load a block that is being partially written.
3153 */
Dan Williamsa4456852007-07-09 11:56:43 -07003154 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003155 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003156 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Dan Williamse33129d2007-01-02 13:52:30 -07003158 /* Now we check to see if any write operations have recently
3159 * completed
3160 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003161 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003162 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003163 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003164 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3165 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003166 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003167
3168 /* All the 'written' buffers and the parity block are ready to
3169 * be written back to disk
3170 */
3171 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3172 for (i = disks; i--; ) {
3173 dev = &sh->dev[i];
3174 if (test_bit(R5_LOCKED, &dev->flags) &&
3175 (i == sh->pd_idx || dev->written)) {
3176 pr_debug("Writing block %d\n", i);
3177 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003178 if (prexor)
3179 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003180 if (!test_bit(R5_Insync, &dev->flags) ||
3181 (i == sh->pd_idx && s.failed == 0))
3182 set_bit(STRIPE_INSYNC, &sh->state);
3183 }
3184 }
NeilBrown729a1862009-12-14 12:49:50 +11003185 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3186 dec_preread_active = 1;
Dan Williamse33129d2007-01-02 13:52:30 -07003187 }
3188
3189 /* Now to consider new write requests and what else, if anything
3190 * should be read. We do not handle new writes when:
3191 * 1/ A 'write' operation (copy+xor) is already in flight.
3192 * 2/ A 'check' operation is in flight, as it may clobber the parity
3193 * block.
3194 */
Dan Williams600aa102008-06-28 08:32:05 +10003195 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003196 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003199 * Any reads will already have been scheduled, so we just see if enough
3200 * data is available. The parity check is held off while parity
3201 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003203 if (sh->check_state ||
3204 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003205 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003206 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003207 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003208
Dan Williamsa4456852007-07-09 11:56:43 -07003209 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3211 clear_bit(STRIPE_SYNCING, &sh->state);
3212 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003213
3214 /* If the failed drive is just a ReadError, then we might need to progress
3215 * the repair/check process
3216 */
Dan Williamsa4456852007-07-09 11:56:43 -07003217 if (s.failed == 1 && !conf->mddev->ro &&
3218 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3219 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3220 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003221 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003222 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003223 if (!test_bit(R5_ReWrite, &dev->flags)) {
3224 set_bit(R5_Wantwrite, &dev->flags);
3225 set_bit(R5_ReWrite, &dev->flags);
3226 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003227 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003228 } else {
3229 /* let's read it back */
3230 set_bit(R5_Wantread, &dev->flags);
3231 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003232 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003233 }
3234 }
3235
Dan Williams600aa102008-06-28 08:32:05 +10003236 /* Finish reconstruct operations initiated by the expansion process */
3237 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003238 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003239 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003240 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3241 /* sh cannot be written until sh2 has been read.
3242 * so arrange for sh to be delayed a little
3243 */
3244 set_bit(STRIPE_DELAYED, &sh->state);
3245 set_bit(STRIPE_HANDLE, &sh->state);
3246 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3247 &sh2->state))
3248 atomic_inc(&conf->preread_active_stripes);
3249 release_stripe(sh2);
3250 goto unlock;
3251 }
3252 if (sh2)
3253 release_stripe(sh2);
3254
Dan Williams600aa102008-06-28 08:32:05 +10003255 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003256 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003257 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003258 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003259 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003260 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003261 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003262 }
3263
3264 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003265 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003266 /* Need to write out all blocks after computing parity */
3267 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003268 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003269 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003270 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003271 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003272 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003273 wake_up(&conf->wait_for_overlap);
3274 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3275 }
3276
Dan Williams0f94e87c2008-01-08 15:32:53 -08003277 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003278 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003279 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003280
Dan Williams6bfe0b42008-04-30 00:52:32 -07003281 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 spin_unlock(&sh->lock);
3283
Dan Williams6bfe0b42008-04-30 00:52:32 -07003284 /* wait for this device to become unblocked */
3285 if (unlikely(blocked_rdev))
3286 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3287
Dan Williams600aa102008-06-28 08:32:05 +10003288 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003289 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003290
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003291 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
NeilBrown729a1862009-12-14 12:49:50 +11003293 if (dec_preread_active) {
3294 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003295 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003296 * have actually been submitted.
3297 */
3298 atomic_dec(&conf->preread_active_stripes);
3299 if (atomic_read(&conf->preread_active_stripes) <
3300 IO_THRESHOLD)
3301 md_wakeup_thread(conf->mddev->thread);
3302 }
Dan Williamsa4456852007-07-09 11:56:43 -07003303 return_io(return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
NeilBrown14425772009-10-16 15:55:25 +11003306static void handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003307{
NeilBrownbff61972009-03-31 14:33:13 +11003308 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003309 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003310 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003311 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003312 struct stripe_head_state s;
3313 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003314 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003315 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown729a1862009-12-14 12:49:50 +11003316 int dec_preread_active = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003317
Dan Williams45b42332007-07-09 11:56:43 -07003318 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003319 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003320 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003321 atomic_read(&sh->count), pd_idx, qd_idx,
3322 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003323 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003324
3325 spin_lock(&sh->lock);
3326 clear_bit(STRIPE_HANDLE, &sh->state);
3327 clear_bit(STRIPE_DELAYED, &sh->state);
3328
Dan Williamsa4456852007-07-09 11:56:43 -07003329 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3330 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3331 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003332 /* Now to look around and see what can be done */
3333
3334 rcu_read_lock();
3335 for (i=disks; i--; ) {
3336 mdk_rdev_t *rdev;
3337 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003338
Dan Williams45b42332007-07-09 11:56:43 -07003339 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003340 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003341 /* maybe we can reply to a read
3342 *
3343 * new wantfill requests are only permitted while
3344 * ops_complete_biofill is guaranteed to be inactive
3345 */
3346 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3347 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3348 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003349
3350 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003351 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3352 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003353 if (test_bit(R5_Wantcompute, &dev->flags)) {
3354 s.compute++;
3355 BUG_ON(s.compute > 2);
3356 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003357
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003358 if (test_bit(R5_Wantfill, &dev->flags)) {
3359 s.to_fill++;
3360 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003361 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003362 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003363 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003364 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003365 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003366 }
Dan Williamsa4456852007-07-09 11:56:43 -07003367 if (dev->written)
3368 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003369 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003370 if (blocked_rdev == NULL &&
3371 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003372 blocked_rdev = rdev;
3373 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003374 }
NeilBrown415e72d2010-06-17 17:25:21 +10003375 clear_bit(R5_Insync, &dev->flags);
3376 if (!rdev)
3377 /* Not in-sync */;
3378 else if (test_bit(In_sync, &rdev->flags))
3379 set_bit(R5_Insync, &dev->flags);
3380 else {
3381 /* in sync if before recovery_offset */
3382 if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3383 set_bit(R5_Insync, &dev->flags);
3384 }
3385 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003386 /* The ReadError flag will just be confusing now */
3387 clear_bit(R5_ReadError, &dev->flags);
3388 clear_bit(R5_ReWrite, &dev->flags);
3389 }
NeilBrown415e72d2010-06-17 17:25:21 +10003390 if (test_bit(R5_ReadError, &dev->flags))
3391 clear_bit(R5_Insync, &dev->flags);
3392 if (!test_bit(R5_Insync, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003393 if (s.failed < 2)
3394 r6s.failed_num[s.failed] = i;
3395 s.failed++;
NeilBrown415e72d2010-06-17 17:25:21 +10003396 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003397 }
3398 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003399
3400 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003401 if (s.syncing || s.expanding || s.expanded ||
3402 s.to_write || s.written) {
3403 set_bit(STRIPE_HANDLE, &sh->state);
3404 goto unlock;
3405 }
3406 /* There is nothing for the blocked_rdev to block */
3407 rdev_dec_pending(blocked_rdev, conf->mddev);
3408 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003409 }
NeilBrownac4090d2008-08-05 15:54:13 +10003410
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003411 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3412 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3413 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3414 }
3415
Dan Williams45b42332007-07-09 11:56:43 -07003416 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003417 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003418 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3419 r6s.failed_num[0], r6s.failed_num[1]);
3420 /* check if the array has lost >2 devices and, if so, some requests
3421 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003422 */
Dan Williamsa4456852007-07-09 11:56:43 -07003423 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003424 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003425 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003426 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3427 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003428 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003429 }
3430
3431 /*
3432 * might be able to return some write requests if the parity blocks
3433 * are safe, or on a failed drive
3434 */
3435 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003436 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3437 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003438 qdev = &sh->dev[qd_idx];
3439 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3440 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003441
Dan Williamsa4456852007-07-09 11:56:43 -07003442 if ( s.written &&
3443 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003444 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003445 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3446 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003447 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003448 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003449 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003450
3451 /* Now we might consider reading some blocks, either to check/generate
3452 * parity, or to satisfy requests
3453 * or to load a block that is being partially written.
3454 */
Dan Williamsa4456852007-07-09 11:56:43 -07003455 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003456 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003457 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003458
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003459 /* Now we check to see if any write operations have recently
3460 * completed
3461 */
3462 if (sh->reconstruct_state == reconstruct_state_drain_result) {
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003463
3464 sh->reconstruct_state = reconstruct_state_idle;
3465 /* All the 'written' buffers and the parity blocks are ready to
3466 * be written back to disk
3467 */
3468 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3469 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3470 for (i = disks; i--; ) {
3471 dev = &sh->dev[i];
3472 if (test_bit(R5_LOCKED, &dev->flags) &&
3473 (i == sh->pd_idx || i == qd_idx ||
3474 dev->written)) {
3475 pr_debug("Writing block %d\n", i);
3476 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3477 set_bit(R5_Wantwrite, &dev->flags);
3478 if (!test_bit(R5_Insync, &dev->flags) ||
3479 ((i == sh->pd_idx || i == qd_idx) &&
3480 s.failed == 0))
3481 set_bit(STRIPE_INSYNC, &sh->state);
3482 }
3483 }
NeilBrown729a1862009-12-14 12:49:50 +11003484 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3485 dec_preread_active = 1;
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003486 }
3487
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003488 /* Now to consider new write requests and what else, if anything
3489 * should be read. We do not handle new writes when:
3490 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3491 * 2/ A 'check' operation is in flight, as it may clobber the parity
3492 * block.
3493 */
3494 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003495 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003496
3497 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003498 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003499 * data is available. The parity check is held off while parity
3500 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003501 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003502 if (sh->check_state ||
3503 (s.syncing && s.locked == 0 &&
3504 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3505 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003506 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003507
Dan Williamsa4456852007-07-09 11:56:43 -07003508 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003509 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3510 clear_bit(STRIPE_SYNCING, &sh->state);
3511 }
3512
3513 /* If the failed drives are just a ReadError, then we might need
3514 * to progress the repair/check process
3515 */
Dan Williamsa4456852007-07-09 11:56:43 -07003516 if (s.failed <= 2 && !conf->mddev->ro)
3517 for (i = 0; i < s.failed; i++) {
3518 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003519 if (test_bit(R5_ReadError, &dev->flags)
3520 && !test_bit(R5_LOCKED, &dev->flags)
3521 && test_bit(R5_UPTODATE, &dev->flags)
3522 ) {
3523 if (!test_bit(R5_ReWrite, &dev->flags)) {
3524 set_bit(R5_Wantwrite, &dev->flags);
3525 set_bit(R5_ReWrite, &dev->flags);
3526 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003527 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003528 } else {
3529 /* let's read it back */
3530 set_bit(R5_Wantread, &dev->flags);
3531 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003532 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003533 }
3534 }
3535 }
NeilBrownf4168852007-02-28 20:11:53 -08003536
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003537 /* Finish reconstruct operations initiated by the expansion process */
3538 if (sh->reconstruct_state == reconstruct_state_result) {
3539 sh->reconstruct_state = reconstruct_state_idle;
3540 clear_bit(STRIPE_EXPANDING, &sh->state);
3541 for (i = conf->raid_disks; i--; ) {
3542 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3543 set_bit(R5_LOCKED, &sh->dev[i].flags);
3544 s.locked++;
3545 }
3546 }
3547
3548 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3549 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003550 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003551 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003552 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3553 /* sh cannot be written until sh2 has been read.
3554 * so arrange for sh to be delayed a little
3555 */
3556 set_bit(STRIPE_DELAYED, &sh->state);
3557 set_bit(STRIPE_HANDLE, &sh->state);
3558 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3559 &sh2->state))
3560 atomic_inc(&conf->preread_active_stripes);
3561 release_stripe(sh2);
3562 goto unlock;
3563 }
3564 if (sh2)
3565 release_stripe(sh2);
3566
NeilBrownf4168852007-02-28 20:11:53 -08003567 /* Need to write out all blocks after computing P&Q */
3568 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003569 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003570 schedule_reconstruction(sh, &s, 1, 1);
3571 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003572 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3573 atomic_dec(&conf->reshape_stripes);
3574 wake_up(&conf->wait_for_overlap);
3575 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3576 }
3577
Dan Williams0f94e87c2008-01-08 15:32:53 -08003578 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003579 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003580 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003581
Dan Williams6bfe0b42008-04-30 00:52:32 -07003582 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003583 spin_unlock(&sh->lock);
3584
Dan Williams6bfe0b42008-04-30 00:52:32 -07003585 /* wait for this device to become unblocked */
3586 if (unlikely(blocked_rdev))
3587 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3588
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003589 if (s.ops_request)
3590 raid_run_ops(sh, s.ops_request);
3591
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003592 ops_run_io(sh, &s);
3593
NeilBrown729a1862009-12-14 12:49:50 +11003594
3595 if (dec_preread_active) {
3596 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003597 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003598 * have actually been submitted.
3599 */
3600 atomic_dec(&conf->preread_active_stripes);
3601 if (atomic_read(&conf->preread_active_stripes) <
3602 IO_THRESHOLD)
3603 md_wakeup_thread(conf->mddev->thread);
3604 }
3605
Dan Williamsa4456852007-07-09 11:56:43 -07003606 return_io(return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003607}
3608
NeilBrown14425772009-10-16 15:55:25 +11003609static void handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003610{
3611 if (sh->raid_conf->level == 6)
NeilBrown14425772009-10-16 15:55:25 +11003612 handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003613 else
NeilBrown14425772009-10-16 15:55:25 +11003614 handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003615}
3616
Arjan van de Ven858119e2006-01-14 13:20:43 -08003617static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
3619 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3620 while (!list_empty(&conf->delayed_list)) {
3621 struct list_head *l = conf->delayed_list.next;
3622 struct stripe_head *sh;
3623 sh = list_entry(l, struct stripe_head, lru);
3624 list_del_init(l);
3625 clear_bit(STRIPE_DELAYED, &sh->state);
3626 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3627 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003628 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
NeilBrown6ed30032008-02-06 01:40:00 -08003630 } else
NeilBrown2ac87402010-06-01 19:37:29 +10003631 plugger_set_plug(&conf->plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Arjan van de Ven858119e2006-01-14 13:20:43 -08003634static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003635{
3636 /* device_lock is held */
3637 struct list_head head;
3638 list_add(&head, &conf->bitmap_list);
3639 list_del_init(&conf->bitmap_list);
3640 while (!list_empty(&head)) {
3641 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3642 list_del_init(&sh->lru);
3643 atomic_inc(&sh->count);
3644 __release_stripe(conf, sh);
3645 }
3646}
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648static void unplug_slaves(mddev_t *mddev)
3649{
NeilBrown070ec552009-06-16 16:54:21 +10003650 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 int i;
NeilBrown5e5e3e72009-10-16 16:35:30 +11003652 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
3654 rcu_read_lock();
NeilBrown5e5e3e72009-10-16 16:35:30 +11003655 for (i = 0; i < devs; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003656 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003657 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003658 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660 atomic_inc(&rdev->nr_pending);
3661 rcu_read_unlock();
3662
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003663 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 rdev_dec_pending(rdev, mddev);
3666 rcu_read_lock();
3667 }
3668 }
3669 rcu_read_unlock();
3670}
3671
NeilBrown9f7c2222010-07-26 12:04:13 +10003672void md_raid5_unplug_device(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 unsigned long flags;
3675
3676 spin_lock_irqsave(&conf->device_lock, flags);
3677
NeilBrown2ac87402010-06-01 19:37:29 +10003678 if (plugger_remove_plug(&conf->plug)) {
NeilBrown72626682005-09-09 16:23:54 -07003679 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003681 }
NeilBrown2ac87402010-06-01 19:37:29 +10003682 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 spin_unlock_irqrestore(&conf->device_lock, flags);
3685
NeilBrown2ac87402010-06-01 19:37:29 +10003686 unplug_slaves(conf->mddev);
3687}
NeilBrown9f7c2222010-07-26 12:04:13 +10003688EXPORT_SYMBOL_GPL(md_raid5_unplug_device);
NeilBrown2ac87402010-06-01 19:37:29 +10003689
3690static void raid5_unplug(struct plug_handle *plug)
3691{
3692 raid5_conf_t *conf = container_of(plug, raid5_conf_t, plug);
NeilBrown9f7c2222010-07-26 12:04:13 +10003693 md_raid5_unplug_device(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
NeilBrown2ac87402010-06-01 19:37:29 +10003696static void raid5_unplug_queue(struct request_queue *q)
NeilBrownf022b2f2006-10-03 01:15:56 -07003697{
NeilBrown2ac87402010-06-01 19:37:29 +10003698 mddev_t *mddev = q->queuedata;
NeilBrown9f7c2222010-07-26 12:04:13 +10003699 md_raid5_unplug_device(mddev->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
NeilBrown11d8a6e2010-07-26 11:57:07 +10003702int md_raid5_congested(mddev_t *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003703{
NeilBrown070ec552009-06-16 16:54:21 +10003704 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003705
3706 /* No difference between reads and writes. Just check
3707 * how busy the stripe_cache is
3708 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003709
NeilBrownf022b2f2006-10-03 01:15:56 -07003710 if (conf->inactive_blocked)
3711 return 1;
3712 if (conf->quiesce)
3713 return 1;
3714 if (list_empty_careful(&conf->inactive_list))
3715 return 1;
3716
3717 return 0;
3718}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003719EXPORT_SYMBOL_GPL(md_raid5_congested);
3720
3721static int raid5_congested(void *data, int bits)
3722{
3723 mddev_t *mddev = data;
3724
3725 return mddev_congested(mddev, bits) ||
3726 md_raid5_congested(mddev, bits);
3727}
NeilBrownf022b2f2006-10-03 01:15:56 -07003728
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003729/* We want read requests to align with chunks where possible,
3730 * but write requests don't need to.
3731 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003732static int raid5_mergeable_bvec(struct request_queue *q,
3733 struct bvec_merge_data *bvm,
3734 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003735{
3736 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003737 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003738 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003739 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003740 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003741
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003742 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003743 return biovec->bv_len; /* always allow writes to be mergeable */
3744
Andre Noll664e7c42009-06-18 08:45:27 +10003745 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3746 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003747 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3748 if (max < 0) max = 0;
3749 if (max <= biovec->bv_len && bio_sectors == 0)
3750 return biovec->bv_len;
3751 else
3752 return max;
3753}
3754
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003755
3756static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3757{
3758 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003759 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003760 unsigned int bio_sectors = bio->bi_size >> 9;
3761
Andre Noll664e7c42009-06-18 08:45:27 +10003762 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3763 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003764 return chunk_sectors >=
3765 ((sector & (chunk_sectors - 1)) + bio_sectors);
3766}
3767
3768/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003769 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3770 * later sampled by raid5d.
3771 */
3772static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3773{
3774 unsigned long flags;
3775
3776 spin_lock_irqsave(&conf->device_lock, flags);
3777
3778 bi->bi_next = conf->retry_read_aligned_list;
3779 conf->retry_read_aligned_list = bi;
3780
3781 spin_unlock_irqrestore(&conf->device_lock, flags);
3782 md_wakeup_thread(conf->mddev->thread);
3783}
3784
3785
3786static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3787{
3788 struct bio *bi;
3789
3790 bi = conf->retry_read_aligned;
3791 if (bi) {
3792 conf->retry_read_aligned = NULL;
3793 return bi;
3794 }
3795 bi = conf->retry_read_aligned_list;
3796 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003797 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003798 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003799 /*
3800 * this sets the active strip count to 1 and the processed
3801 * strip count to zero (upper 8 bits)
3802 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003803 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003804 }
3805
3806 return bi;
3807}
3808
3809
3810/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811 * The "raid5_align_endio" should check if the read succeeded and if it
3812 * did, call bio_endio on the original bio (having bio_put the new bio
3813 * first).
3814 * If the read failed..
3815 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003816static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003817{
3818 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003819 mddev_t *mddev;
3820 raid5_conf_t *conf;
3821 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3822 mdk_rdev_t *rdev;
3823
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003824 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003825
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003826 rdev = (void*)raid_bi->bi_next;
3827 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003828 mddev = rdev->mddev;
3829 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003830
3831 rdev_dec_pending(rdev, conf->mddev);
3832
3833 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003834 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003835 if (atomic_dec_and_test(&conf->active_aligned_reads))
3836 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003837 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003838 }
3839
3840
Dan Williams45b42332007-07-09 11:56:43 -07003841 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003842
3843 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003844}
3845
Neil Brown387bb172007-02-08 14:20:29 -08003846static int bio_fits_rdev(struct bio *bi)
3847{
Jens Axboe165125e2007-07-24 09:28:11 +02003848 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003849
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003850 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003851 return 0;
3852 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003853 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003854 return 0;
3855
3856 if (q->merge_bvec_fn)
3857 /* it's too hard to apply the merge_bvec_fn at this stage,
3858 * just just give up
3859 */
3860 return 0;
3861
3862 return 1;
3863}
3864
3865
NeilBrown21a52c62010-04-01 15:02:13 +11003866static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003867{
NeilBrown070ec552009-06-16 16:54:21 +10003868 raid5_conf_t *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003869 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003870 struct bio* align_bi;
3871 mdk_rdev_t *rdev;
3872
3873 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003874 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003875 return 0;
3876 }
3877 /*
NeilBrowna167f662010-10-26 18:31:13 +11003878 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003879 */
NeilBrowna167f662010-10-26 18:31:13 +11003880 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003881 if (!align_bi)
3882 return 0;
3883 /*
3884 * set bi_end_io to a new function, and set bi_private to the
3885 * original bio.
3886 */
3887 align_bi->bi_end_io = raid5_align_endio;
3888 align_bi->bi_private = raid_bio;
3889 /*
3890 * compute position
3891 */
NeilBrown112bf892009-03-31 14:39:38 +11003892 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3893 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003894 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895
3896 rcu_read_lock();
3897 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3898 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003899 atomic_inc(&rdev->nr_pending);
3900 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003901 raid_bio->bi_next = (void*)rdev;
3902 align_bi->bi_bdev = rdev->bdev;
3903 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3904 align_bi->bi_sector += rdev->data_offset;
3905
Neil Brown387bb172007-02-08 14:20:29 -08003906 if (!bio_fits_rdev(align_bi)) {
3907 /* too big in some way */
3908 bio_put(align_bi);
3909 rdev_dec_pending(rdev, mddev);
3910 return 0;
3911 }
3912
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003913 spin_lock_irq(&conf->device_lock);
3914 wait_event_lock_irq(conf->wait_for_stripe,
3915 conf->quiesce == 0,
3916 conf->device_lock, /* nothing */);
3917 atomic_inc(&conf->active_aligned_reads);
3918 spin_unlock_irq(&conf->device_lock);
3919
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003920 generic_make_request(align_bi);
3921 return 1;
3922 } else {
3923 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003924 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003925 return 0;
3926 }
3927}
3928
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003929/* __get_priority_stripe - get the next stripe to process
3930 *
3931 * Full stripe writes are allowed to pass preread active stripes up until
3932 * the bypass_threshold is exceeded. In general the bypass_count
3933 * increments when the handle_list is handled before the hold_list; however, it
3934 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3935 * stripe with in flight i/o. The bypass_count will be reset when the
3936 * head of the hold_list has changed, i.e. the head was promoted to the
3937 * handle_list.
3938 */
3939static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3940{
3941 struct stripe_head *sh;
3942
3943 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3944 __func__,
3945 list_empty(&conf->handle_list) ? "empty" : "busy",
3946 list_empty(&conf->hold_list) ? "empty" : "busy",
3947 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3948
3949 if (!list_empty(&conf->handle_list)) {
3950 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3951
3952 if (list_empty(&conf->hold_list))
3953 conf->bypass_count = 0;
3954 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3955 if (conf->hold_list.next == conf->last_hold)
3956 conf->bypass_count++;
3957 else {
3958 conf->last_hold = conf->hold_list.next;
3959 conf->bypass_count -= conf->bypass_threshold;
3960 if (conf->bypass_count < 0)
3961 conf->bypass_count = 0;
3962 }
3963 }
3964 } else if (!list_empty(&conf->hold_list) &&
3965 ((conf->bypass_threshold &&
3966 conf->bypass_count > conf->bypass_threshold) ||
3967 atomic_read(&conf->pending_full_writes) == 0)) {
3968 sh = list_entry(conf->hold_list.next,
3969 typeof(*sh), lru);
3970 conf->bypass_count -= conf->bypass_threshold;
3971 if (conf->bypass_count < 0)
3972 conf->bypass_count = 0;
3973 } else
3974 return NULL;
3975
3976 list_del_init(&sh->lru);
3977 atomic_inc(&sh->count);
3978 BUG_ON(atomic_read(&sh->count) != 1);
3979 return sh;
3980}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003981
NeilBrown21a52c62010-04-01 15:02:13 +11003982static int make_request(mddev_t *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
NeilBrown070ec552009-06-16 16:54:21 +10003984 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003985 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 sector_t new_sector;
3987 sector_t logical_sector, last_sector;
3988 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003989 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003990 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Tejun Heoe9c74692010-09-03 11:56:18 +02003992 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3993 md_flush_request(mddev, bi);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003994 return 0;
3995 }
3996
NeilBrown3d310eb2005-06-21 17:17:26 -07003997 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003998
NeilBrown802ba062006-12-13 00:34:13 -08003999 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004000 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004001 chunk_aligned_read(mddev,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11004002 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4005 last_sector = bi->bi_sector + (bi->bi_size>>9);
4006 bi->bi_next = NULL;
4007 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4010 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07004011 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11004012 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004013
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004014 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004015 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11004016 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08004017 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004018 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004019 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004020 * 64bit on a 32bit platform, and so it might be
4021 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11004022 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004023 * the lock is dropped, so once we get a reference
4024 * to the stripe that we think it is, we will have
4025 * to check again.
4026 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004027 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004028 if (mddev->delta_disks < 0
4029 ? logical_sector < conf->reshape_progress
4030 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004031 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11004032 previous = 1;
4033 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11004034 if (mddev->delta_disks < 0
4035 ? logical_sector < conf->reshape_safe
4036 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004037 spin_unlock_irq(&conf->device_lock);
4038 schedule();
4039 goto retry;
4040 }
4041 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004042 spin_unlock_irq(&conf->device_lock);
4043 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004044 data_disks = disks - conf->max_degraded;
4045
NeilBrown112bf892009-03-31 14:39:38 +11004046 new_sector = raid5_compute_sector(conf, logical_sector,
4047 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004048 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004049 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 (unsigned long long)new_sector,
4051 (unsigned long long)logical_sector);
4052
NeilBrownb5663ba2009-03-31 14:39:38 +11004053 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004054 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004056 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004057 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004058 * stripe, so we must do the range check again.
4059 * Expansion could still move past after this
4060 * test, but as we are holding a reference to
4061 * 'sh', we know that if that happens,
4062 * STRIPE_EXPANDING will get set and the expansion
4063 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004064 */
4065 int must_retry = 0;
4066 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004067 if (mddev->delta_disks < 0
4068 ? logical_sector >= conf->reshape_progress
4069 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004070 /* mismatch, need to try again */
4071 must_retry = 1;
4072 spin_unlock_irq(&conf->device_lock);
4073 if (must_retry) {
4074 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004075 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004076 goto retry;
4077 }
4078 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004079
NeilBrowna5c308d2009-07-01 13:15:35 +10004080 if (bio_data_dir(bi) == WRITE &&
4081 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004082 logical_sector < mddev->suspend_hi) {
4083 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004084 /* As the suspend_* range is controlled by
4085 * userspace, we want an interruptible
4086 * wait.
4087 */
4088 flush_signals(current);
4089 prepare_to_wait(&conf->wait_for_overlap,
4090 &w, TASK_INTERRUPTIBLE);
4091 if (logical_sector >= mddev->suspend_lo &&
4092 logical_sector < mddev->suspend_hi)
4093 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004094 goto retry;
4095 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004096
4097 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4098 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
4099 /* Stripe is busy expanding or
4100 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 * and wait a while
4102 */
NeilBrown9f7c2222010-07-26 12:04:13 +10004103 md_raid5_unplug_device(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 release_stripe(sh);
4105 schedule();
4106 goto retry;
4107 }
4108 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004109 set_bit(STRIPE_HANDLE, &sh->state);
4110 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004111 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004112 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4113 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 } else {
4116 /* cannot get stripe for read-ahead, just give-up */
4117 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4118 finish_wait(&conf->wait_for_overlap, &w);
4119 break;
4120 }
4121
4122 }
4123 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004124 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004125 spin_unlock_irq(&conf->device_lock);
4126 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
NeilBrown16a53ec2006-06-26 00:27:38 -07004128 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004130
Neil Brown0e13fe232008-06-28 08:31:20 +10004131 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
NeilBrown729a1862009-12-14 12:49:50 +11004133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 return 0;
4135}
4136
Dan Williamsb522adc2009-03-31 15:00:31 +11004137static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
4138
NeilBrown52c03292006-06-26 00:27:43 -07004139static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140{
NeilBrown52c03292006-06-26 00:27:43 -07004141 /* reshaping is quite different to recovery/resync so it is
4142 * handled quite separately ... here.
4143 *
4144 * On each call to sync_request, we gather one chunk worth of
4145 * destination stripes and flag them as expanding.
4146 * Then we find all the source stripes and request reads.
4147 * As the reads complete, handle_stripe will copy the data
4148 * into the destination stripe and release that stripe.
4149 */
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004150 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004152 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004153 int raid_disks = conf->previous_raid_disks;
4154 int data_disks = raid_disks - conf->max_degraded;
4155 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004156 int i;
4157 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004158 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004159 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004160 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004161 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004162
NeilBrownfef9c612009-03-31 15:16:46 +11004163 if (sector_nr == 0) {
4164 /* If restarting in the middle, skip the initial sectors */
4165 if (mddev->delta_disks < 0 &&
4166 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4167 sector_nr = raid5_size(mddev, 0, 0)
4168 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004169 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004170 conf->reshape_progress > 0)
4171 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004172 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004173 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004174 mddev->curr_resync_completed = sector_nr;
4175 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004176 *skipped = 1;
4177 return sector_nr;
4178 }
NeilBrown52c03292006-06-26 00:27:43 -07004179 }
4180
NeilBrown7a661382009-03-31 15:21:40 +11004181 /* We need to process a full chunk at a time.
4182 * If old and new chunk sizes differ, we need to process the
4183 * largest of these
4184 */
Andre Noll664e7c42009-06-18 08:45:27 +10004185 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4186 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004187 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004188 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004189
NeilBrown52c03292006-06-26 00:27:43 -07004190 /* we update the metadata when there is more than 3Meg
4191 * in the block range (that is rather arbitrary, should
4192 * probably be time based) or when the data about to be
4193 * copied would over-write the source of the data at
4194 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004195 * i.e. one new_stripe along from reshape_progress new_maps
4196 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004197 */
NeilBrownfef9c612009-03-31 15:16:46 +11004198 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004199 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004200 readpos = conf->reshape_progress;
4201 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004202 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004203 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004204 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004205 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004206 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004207 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004208 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004209 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004210 readpos -= min_t(sector_t, reshape_sectors, readpos);
4211 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004212 }
NeilBrown52c03292006-06-26 00:27:43 -07004213
NeilBrownc8f517c2009-03-31 15:28:40 +11004214 /* 'writepos' is the most advanced device address we might write.
4215 * 'readpos' is the least advanced device address we might read.
4216 * 'safepos' is the least address recorded in the metadata as having
4217 * been reshaped.
4218 * If 'readpos' is behind 'writepos', then there is no way that we can
4219 * ensure safety in the face of a crash - that must be done by userspace
4220 * making a backup of the data. So in that case there is no particular
4221 * rush to update metadata.
4222 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4223 * update the metadata to advance 'safepos' to match 'readpos' so that
4224 * we can be safe in the event of a crash.
4225 * So we insist on updating metadata if safepos is behind writepos and
4226 * readpos is beyond writepos.
4227 * In any case, update the metadata every 10 seconds.
4228 * Maybe that number should be configurable, but I'm not sure it is
4229 * worth it.... maybe it could be a multiple of safemode_delay???
4230 */
NeilBrownfef9c612009-03-31 15:16:46 +11004231 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004232 ? (safepos > writepos && readpos < writepos)
4233 : (safepos < writepos && readpos > writepos)) ||
4234 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004235 /* Cannot proceed until we've updated the superblock... */
4236 wait_event(conf->wait_for_overlap,
4237 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004238 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004239 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004240 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004241 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004242 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004243 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004244 kthread_should_stop());
4245 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004246 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004247 spin_unlock_irq(&conf->device_lock);
4248 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004249 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004250 }
4251
NeilBrownec32a2b2009-03-31 15:17:38 +11004252 if (mddev->delta_disks < 0) {
4253 BUG_ON(conf->reshape_progress == 0);
4254 stripe_addr = writepos;
4255 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004256 ~((sector_t)reshape_sectors - 1))
4257 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004258 != sector_nr);
4259 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004260 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004261 stripe_addr = sector_nr;
4262 }
NeilBrownab69ae12009-03-31 15:26:47 +11004263 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004264 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004265 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004266 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004267 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004268 set_bit(STRIPE_EXPANDING, &sh->state);
4269 atomic_inc(&conf->reshape_stripes);
4270 /* If any of this stripe is beyond the end of the old
4271 * array, then we need to zero those blocks
4272 */
4273 for (j=sh->disks; j--;) {
4274 sector_t s;
4275 if (j == sh->pd_idx)
4276 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004277 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004278 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004279 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004280 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004281 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004282 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004283 continue;
4284 }
4285 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4286 set_bit(R5_Expanded, &sh->dev[j].flags);
4287 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4288 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004289 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004290 set_bit(STRIPE_EXPAND_READY, &sh->state);
4291 set_bit(STRIPE_HANDLE, &sh->state);
4292 }
NeilBrownab69ae12009-03-31 15:26:47 +11004293 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004294 }
4295 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004296 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004297 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004298 else
NeilBrown7a661382009-03-31 15:21:40 +11004299 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004300 spin_unlock_irq(&conf->device_lock);
4301 /* Ok, those stripe are ready. We can start scheduling
4302 * reads on the source stripes.
4303 * The source stripes are determined by mapping the first and last
4304 * block on the destination stripes.
4305 */
NeilBrown52c03292006-06-26 00:27:43 -07004306 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004307 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004308 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004309 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004310 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004311 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004312 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004313 if (last_sector >= mddev->dev_sectors)
4314 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004315 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004316 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004317 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4318 set_bit(STRIPE_HANDLE, &sh->state);
4319 release_stripe(sh);
4320 first_sector += STRIPE_SECTORS;
4321 }
NeilBrownab69ae12009-03-31 15:26:47 +11004322 /* Now that the sources are clearly marked, we can release
4323 * the destination stripes
4324 */
4325 while (!list_empty(&stripes)) {
4326 sh = list_entry(stripes.next, struct stripe_head, lru);
4327 list_del_init(&sh->lru);
4328 release_stripe(sh);
4329 }
NeilBrownc6207272008-02-06 01:39:52 -08004330 /* If this takes us to the resync_max point where we have to pause,
4331 * then we need to write out the superblock.
4332 */
NeilBrown7a661382009-03-31 15:21:40 +11004333 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004334 if ((sector_nr - mddev->curr_resync_completed) * 2
4335 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004336 /* Cannot proceed until we've updated the superblock... */
4337 wait_event(conf->wait_for_overlap,
4338 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004339 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004340 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004341 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004342 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4343 md_wakeup_thread(mddev->thread);
4344 wait_event(mddev->sb_wait,
4345 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4346 || kthread_should_stop());
4347 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004348 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004349 spin_unlock_irq(&conf->device_lock);
4350 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004351 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004352 }
NeilBrown7a661382009-03-31 15:21:40 +11004353 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004354}
4355
4356/* FIXME go_faster isn't used */
4357static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4358{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004359 raid5_conf_t *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004360 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004361 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004362 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004363 int still_degraded = 0;
4364 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
NeilBrown72626682005-09-09 16:23:54 -07004366 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 /* just being told to finish up .. nothing much to do */
4368 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004369
NeilBrown29269552006-03-27 01:18:10 -08004370 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4371 end_reshape(conf);
4372 return 0;
4373 }
NeilBrown72626682005-09-09 16:23:54 -07004374
4375 if (mddev->curr_resync < max_sector) /* aborted */
4376 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4377 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004378 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004379 conf->fullsync = 0;
4380 bitmap_close_sync(mddev->bitmap);
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return 0;
4383 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004384
NeilBrown64bd6602009-08-03 10:59:58 +10004385 /* Allow raid5_quiesce to complete */
4386 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4387
NeilBrown52c03292006-06-26 00:27:43 -07004388 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4389 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004390
NeilBrownc6207272008-02-06 01:39:52 -08004391 /* No need to check resync_max as we never do more than one
4392 * stripe, and as resync_max will always be on a chunk boundary,
4393 * if the check in md_do_sync didn't fire, there is no chance
4394 * of overstepping resync_max here
4395 */
4396
NeilBrown16a53ec2006-06-26 00:27:38 -07004397 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 * to resync, then assert that we are finished, because there is
4399 * nothing we can do.
4400 */
NeilBrown3285edf2006-06-26 00:27:55 -07004401 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004402 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004403 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004404 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return rv;
4406 }
NeilBrown72626682005-09-09 16:23:54 -07004407 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004408 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004409 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4410 /* we can skip this block, and probably more */
4411 sync_blocks /= STRIPE_SECTORS;
4412 *skipped = 1;
4413 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
NeilBrownb47490c2008-02-06 01:39:50 -08004416
4417 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4418
NeilBrowna8c906c2009-06-09 14:39:59 +10004419 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004421 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004423 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004425 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004427 /* Need to check if array will still be degraded after recovery/resync
4428 * We don't need to check the 'failed' flag as when that gets set,
4429 * recovery aborts.
4430 */
NeilBrownf001a702009-06-09 14:30:31 +10004431 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004432 if (conf->disks[i].rdev == NULL)
4433 still_degraded = 1;
4434
4435 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4436
4437 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 set_bit(STRIPE_SYNCING, &sh->state);
4439 clear_bit(STRIPE_INSYNC, &sh->state);
4440 spin_unlock(&sh->lock);
4441
NeilBrown14425772009-10-16 15:55:25 +11004442 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 release_stripe(sh);
4444
4445 return STRIPE_SECTORS;
4446}
4447
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004448static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4449{
4450 /* We may not be able to submit a whole bio at once as there
4451 * may not be enough stripe_heads available.
4452 * We cannot pre-allocate enough stripe_heads as we may need
4453 * more than exist in the cache (if we allow ever large chunks).
4454 * So we do one stripe head at a time and record in
4455 * ->bi_hw_segments how many have been done.
4456 *
4457 * We *know* that this entire raid_bio is in one chunk, so
4458 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4459 */
4460 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004461 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004462 sector_t sector, logical_sector, last_sector;
4463 int scnt = 0;
4464 int remaining;
4465 int handled = 0;
4466
4467 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004468 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004469 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004470 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4471
4472 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004473 logical_sector += STRIPE_SECTORS,
4474 sector += STRIPE_SECTORS,
4475 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004476
Jens Axboe960e7392008-08-15 10:41:18 +02004477 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004478 /* already done this stripe */
4479 continue;
4480
NeilBrowna8c906c2009-06-09 14:39:59 +10004481 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004482
4483 if (!sh) {
4484 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004485 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004486 conf->retry_read_aligned = raid_bio;
4487 return handled;
4488 }
4489
4490 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004491 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4492 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004493 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004494 conf->retry_read_aligned = raid_bio;
4495 return handled;
4496 }
4497
Dan Williams36d1c642009-07-14 11:48:22 -07004498 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004499 release_stripe(sh);
4500 handled++;
4501 }
4502 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004503 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004504 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004505 if (remaining == 0)
4506 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004507 if (atomic_dec_and_test(&conf->active_aligned_reads))
4508 wake_up(&conf->wait_for_stripe);
4509 return handled;
4510}
4511
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513/*
4514 * This is our raid5 kernel thread.
4515 *
4516 * We scan the hash table for stripes which can be handled now.
4517 * During the scan, completed stripes are saved for us by the interrupt
4518 * handler, so that they will not have to wait for our next wakeup.
4519 */
NeilBrown6ed30032008-02-06 01:40:00 -08004520static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
4522 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004523 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 int handled;
4525
Dan Williams45b42332007-07-09 11:56:43 -07004526 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 handled = 0;
4531 spin_lock_irq(&conf->device_lock);
4532 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004533 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
NeilBrownae3c20c2006-07-10 04:44:17 -07004535 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004536 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004537 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004538 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004539 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004540 conf->seq_write = seq;
4541 activate_bit_delay(conf);
4542 }
4543
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004544 while ((bio = remove_bio_from_retry(conf))) {
4545 int ok;
4546 spin_unlock_irq(&conf->device_lock);
4547 ok = retry_aligned_read(conf, bio);
4548 spin_lock_irq(&conf->device_lock);
4549 if (!ok)
4550 break;
4551 handled++;
4552 }
4553
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004554 sh = __get_priority_stripe(conf);
4555
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004556 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 spin_unlock_irq(&conf->device_lock);
4559
4560 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004561 handle_stripe(sh);
4562 release_stripe(sh);
4563 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 spin_lock_irq(&conf->device_lock);
4566 }
Dan Williams45b42332007-07-09 11:56:43 -07004567 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 spin_unlock_irq(&conf->device_lock);
4570
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004571 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 unplug_slaves(mddev);
4573
Dan Williams45b42332007-07-09 11:56:43 -07004574 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
NeilBrown3f294f42005-11-08 21:39:25 -08004577static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004578raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004579{
NeilBrown070ec552009-06-16 16:54:21 +10004580 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004581 if (conf)
4582 return sprintf(page, "%d\n", conf->max_nr_stripes);
4583 else
4584 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004585}
4586
NeilBrownc41d4ac2010-06-01 19:37:24 +10004587int
4588raid5_set_cache_size(mddev_t *mddev, int size)
4589{
4590 raid5_conf_t *conf = mddev->private;
4591 int err;
4592
4593 if (size <= 16 || size > 32768)
4594 return -EINVAL;
4595 while (size < conf->max_nr_stripes) {
4596 if (drop_one_stripe(conf))
4597 conf->max_nr_stripes--;
4598 else
4599 break;
4600 }
4601 err = md_allow_write(mddev);
4602 if (err)
4603 return err;
4604 while (size > conf->max_nr_stripes) {
4605 if (grow_one_stripe(conf))
4606 conf->max_nr_stripes++;
4607 else break;
4608 }
4609 return 0;
4610}
4611EXPORT_SYMBOL(raid5_set_cache_size);
4612
NeilBrown3f294f42005-11-08 21:39:25 -08004613static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004614raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004615{
NeilBrown070ec552009-06-16 16:54:21 +10004616 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004617 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004618 int err;
4619
NeilBrown3f294f42005-11-08 21:39:25 -08004620 if (len >= PAGE_SIZE)
4621 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004622 if (!conf)
4623 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004624
Dan Williams4ef197d82008-04-28 02:15:54 -07004625 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004626 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004627 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004628 if (err)
4629 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004630 return len;
4631}
NeilBrown007583c2005-11-08 21:39:30 -08004632
NeilBrown96de1e62005-11-08 21:39:39 -08004633static struct md_sysfs_entry
4634raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4635 raid5_show_stripe_cache_size,
4636 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004637
4638static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004639raid5_show_preread_threshold(mddev_t *mddev, char *page)
4640{
NeilBrown070ec552009-06-16 16:54:21 +10004641 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004642 if (conf)
4643 return sprintf(page, "%d\n", conf->bypass_threshold);
4644 else
4645 return 0;
4646}
4647
4648static ssize_t
4649raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4650{
NeilBrown070ec552009-06-16 16:54:21 +10004651 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004652 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004653 if (len >= PAGE_SIZE)
4654 return -EINVAL;
4655 if (!conf)
4656 return -ENODEV;
4657
Dan Williams4ef197d82008-04-28 02:15:54 -07004658 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004659 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004660 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004661 return -EINVAL;
4662 conf->bypass_threshold = new;
4663 return len;
4664}
4665
4666static struct md_sysfs_entry
4667raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4668 S_IRUGO | S_IWUSR,
4669 raid5_show_preread_threshold,
4670 raid5_store_preread_threshold);
4671
4672static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004673stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004674{
NeilBrown070ec552009-06-16 16:54:21 +10004675 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004676 if (conf)
4677 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4678 else
4679 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004680}
4681
NeilBrown96de1e62005-11-08 21:39:39 -08004682static struct md_sysfs_entry
4683raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004684
NeilBrown007583c2005-11-08 21:39:30 -08004685static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004686 &raid5_stripecache_size.attr,
4687 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004688 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004689 NULL,
4690};
NeilBrown007583c2005-11-08 21:39:30 -08004691static struct attribute_group raid5_attrs_group = {
4692 .name = NULL,
4693 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004694};
4695
Dan Williams80c3a6c2009-03-17 18:10:40 -07004696static sector_t
4697raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4698{
NeilBrown070ec552009-06-16 16:54:21 +10004699 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004700
4701 if (!sectors)
4702 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004703 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004704 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004705 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004706
Andre Noll9d8f0362009-06-18 08:45:01 +10004707 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004708 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004709 return sectors * (raid_disks - conf->max_degraded);
4710}
4711
Dan Williams36d1c642009-07-14 11:48:22 -07004712static void raid5_free_percpu(raid5_conf_t *conf)
4713{
4714 struct raid5_percpu *percpu;
4715 unsigned long cpu;
4716
4717 if (!conf->percpu)
4718 return;
4719
4720 get_online_cpus();
4721 for_each_possible_cpu(cpu) {
4722 percpu = per_cpu_ptr(conf->percpu, cpu);
4723 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004724 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004725 }
4726#ifdef CONFIG_HOTPLUG_CPU
4727 unregister_cpu_notifier(&conf->cpu_notify);
4728#endif
4729 put_online_cpus();
4730
4731 free_percpu(conf->percpu);
4732}
4733
Dan Williams95fc17a2009-07-31 12:39:15 +10004734static void free_conf(raid5_conf_t *conf)
4735{
4736 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004737 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004738 kfree(conf->disks);
4739 kfree(conf->stripe_hashtbl);
4740 kfree(conf);
4741}
4742
Dan Williams36d1c642009-07-14 11:48:22 -07004743#ifdef CONFIG_HOTPLUG_CPU
4744static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4745 void *hcpu)
4746{
4747 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4748 long cpu = (long)hcpu;
4749 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4750
4751 switch (action) {
4752 case CPU_UP_PREPARE:
4753 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004754 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004755 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004756 if (!percpu->scribble)
4757 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4758
4759 if (!percpu->scribble ||
4760 (conf->level == 6 && !percpu->spare_page)) {
4761 safe_put_page(percpu->spare_page);
4762 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004763 pr_err("%s: failed memory allocation for cpu%ld\n",
4764 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004765 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004766 }
4767 break;
4768 case CPU_DEAD:
4769 case CPU_DEAD_FROZEN:
4770 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004771 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004772 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004773 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004774 break;
4775 default:
4776 break;
4777 }
4778 return NOTIFY_OK;
4779}
4780#endif
4781
4782static int raid5_alloc_percpu(raid5_conf_t *conf)
4783{
4784 unsigned long cpu;
4785 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004786 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004787 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004788 int err;
4789
Dan Williams36d1c642009-07-14 11:48:22 -07004790 allcpus = alloc_percpu(struct raid5_percpu);
4791 if (!allcpus)
4792 return -ENOMEM;
4793 conf->percpu = allcpus;
4794
4795 get_online_cpus();
4796 err = 0;
4797 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004798 if (conf->level == 6) {
4799 spare_page = alloc_page(GFP_KERNEL);
4800 if (!spare_page) {
4801 err = -ENOMEM;
4802 break;
4803 }
4804 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4805 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004806 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004807 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004808 err = -ENOMEM;
4809 break;
4810 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004811 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004812 }
4813#ifdef CONFIG_HOTPLUG_CPU
4814 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4815 conf->cpu_notify.priority = 0;
4816 if (err == 0)
4817 err = register_cpu_notifier(&conf->cpu_notify);
4818#endif
4819 put_online_cpus();
4820
4821 return err;
4822}
4823
NeilBrown91adb562009-03-31 14:39:39 +11004824static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
4826 raid5_conf_t *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004827 int raid_disk, memory, max_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 mdk_rdev_t *rdev;
4829 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
NeilBrown91adb562009-03-31 14:39:39 +11004831 if (mddev->new_level != 5
4832 && mddev->new_level != 4
4833 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004834 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004835 mdname(mddev), mddev->new_level);
4836 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 }
NeilBrown91adb562009-03-31 14:39:39 +11004838 if ((mddev->new_level == 5
4839 && !algorithm_valid_raid5(mddev->new_layout)) ||
4840 (mddev->new_level == 6
4841 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004842 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004843 mdname(mddev), mddev->new_layout);
4844 return ERR_PTR(-EIO);
4845 }
4846 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004847 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004848 mdname(mddev), mddev->raid_disks);
4849 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Andre Noll664e7c42009-06-18 08:45:27 +10004852 if (!mddev->new_chunk_sectors ||
4853 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4854 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004855 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4856 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004857 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004858 }
4859
NeilBrown91adb562009-03-31 14:39:39 +11004860 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4861 if (conf == NULL)
4862 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004863 spin_lock_init(&conf->device_lock);
4864 init_waitqueue_head(&conf->wait_for_stripe);
4865 init_waitqueue_head(&conf->wait_for_overlap);
4866 INIT_LIST_HEAD(&conf->handle_list);
4867 INIT_LIST_HEAD(&conf->hold_list);
4868 INIT_LIST_HEAD(&conf->delayed_list);
4869 INIT_LIST_HEAD(&conf->bitmap_list);
4870 INIT_LIST_HEAD(&conf->inactive_list);
4871 atomic_set(&conf->active_stripes, 0);
4872 atomic_set(&conf->preread_active_stripes, 0);
4873 atomic_set(&conf->active_aligned_reads, 0);
4874 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004875
4876 conf->raid_disks = mddev->raid_disks;
4877 if (mddev->reshape_position == MaxSector)
4878 conf->previous_raid_disks = mddev->raid_disks;
4879 else
4880 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004881 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4882 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004883
NeilBrown5e5e3e72009-10-16 16:35:30 +11004884 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004885 GFP_KERNEL);
4886 if (!conf->disks)
4887 goto abort;
4888
4889 conf->mddev = mddev;
4890
4891 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4892 goto abort;
4893
Dan Williams36d1c642009-07-14 11:48:22 -07004894 conf->level = mddev->new_level;
4895 if (raid5_alloc_percpu(conf) != 0)
4896 goto abort;
4897
NeilBrown0c55e022010-05-03 14:09:02 +10004898 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004899
4900 list_for_each_entry(rdev, &mddev->disks, same_set) {
4901 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004902 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004903 || raid_disk < 0)
4904 continue;
4905 disk = conf->disks + raid_disk;
4906
4907 disk->rdev = rdev;
4908
4909 if (test_bit(In_sync, &rdev->flags)) {
4910 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004911 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4912 " disk %d\n",
4913 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
NeilBrown91adb562009-03-31 14:39:39 +11004914 } else
4915 /* Cannot rely on bitmap to complete recovery */
4916 conf->fullsync = 1;
4917 }
4918
Andre Noll09c9e5f2009-06-18 08:45:55 +10004919 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004920 conf->level = mddev->new_level;
4921 if (conf->level == 6)
4922 conf->max_degraded = 2;
4923 else
4924 conf->max_degraded = 1;
4925 conf->algorithm = mddev->new_layout;
4926 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004927 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004928 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004929 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004930 conf->prev_algo = mddev->layout;
4931 }
NeilBrown91adb562009-03-31 14:39:39 +11004932
4933 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004934 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004935 if (grow_stripes(conf, conf->max_nr_stripes)) {
4936 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004937 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4938 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004939 goto abort;
4940 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004941 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4942 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004943
NeilBrown0da3c612009-09-23 18:09:45 +10004944 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004945 if (!conf->thread) {
4946 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004947 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004948 mdname(mddev));
4949 goto abort;
4950 }
4951
4952 return conf;
4953
4954 abort:
4955 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004956 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004957 return ERR_PTR(-EIO);
4958 } else
4959 return ERR_PTR(-ENOMEM);
4960}
4961
NeilBrownc148ffd2009-11-13 17:47:00 +11004962
4963static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4964{
4965 switch (algo) {
4966 case ALGORITHM_PARITY_0:
4967 if (raid_disk < max_degraded)
4968 return 1;
4969 break;
4970 case ALGORITHM_PARITY_N:
4971 if (raid_disk >= raid_disks - max_degraded)
4972 return 1;
4973 break;
4974 case ALGORITHM_PARITY_0_6:
4975 if (raid_disk == 0 ||
4976 raid_disk == raid_disks - 1)
4977 return 1;
4978 break;
4979 case ALGORITHM_LEFT_ASYMMETRIC_6:
4980 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4981 case ALGORITHM_LEFT_SYMMETRIC_6:
4982 case ALGORITHM_RIGHT_SYMMETRIC_6:
4983 if (raid_disk == raid_disks - 1)
4984 return 1;
4985 }
4986 return 0;
4987}
4988
NeilBrown91adb562009-03-31 14:39:39 +11004989static int run(mddev_t *mddev)
4990{
4991 raid5_conf_t *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10004992 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11004993 int dirty_parity_disks = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004994 mdk_rdev_t *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004995 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004996
Andre Noll8c6ac8682009-06-18 08:48:06 +10004997 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10004998 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10004999 " -- starting background reconstruction\n",
5000 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005001 if (mddev->reshape_position != MaxSector) {
5002 /* Check that we can continue the reshape.
5003 * Currently only disks can change, it must
5004 * increase, and we must be past the point where
5005 * a stripe over-writes itself
5006 */
5007 sector_t here_new, here_old;
5008 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005009 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005010
NeilBrown88ce4932009-03-31 15:24:23 +11005011 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005012 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005013 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005014 mdname(mddev));
5015 return -EINVAL;
5016 }
NeilBrownf6705572006-03-27 01:18:11 -08005017 old_disks = mddev->raid_disks - mddev->delta_disks;
5018 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005019 * further up in new geometry must map after here in old
5020 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005021 */
5022 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005023 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005024 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005025 printk(KERN_ERR "md/raid:%s: reshape_position not "
5026 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005027 return -EINVAL;
5028 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005029 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005030 /* here_new is the stripe we will write to */
5031 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005032 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005033 (old_disks-max_degraded));
5034 /* here_old is the first stripe that we might need to read
5035 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005036 if (mddev->delta_disks == 0) {
5037 /* We cannot be sure it is safe to start an in-place
5038 * reshape. It is only safe if user-space if monitoring
5039 * and taking constant backups.
5040 * mdadm always starts a situation like this in
5041 * readonly mode so it can take control before
5042 * allowing any writes. So just check for that.
5043 */
5044 if ((here_new * mddev->new_chunk_sectors !=
5045 here_old * mddev->chunk_sectors) ||
5046 mddev->ro == 0) {
NeilBrown0c55e022010-05-03 14:09:02 +10005047 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
5048 " in read-only mode - aborting\n",
5049 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005050 return -EINVAL;
5051 }
5052 } else if (mddev->delta_disks < 0
5053 ? (here_new * mddev->new_chunk_sectors <=
5054 here_old * mddev->chunk_sectors)
5055 : (here_new * mddev->new_chunk_sectors >=
5056 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08005057 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005058 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5059 "auto-recovery - aborting.\n",
5060 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005061 return -EINVAL;
5062 }
NeilBrown0c55e022010-05-03 14:09:02 +10005063 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5064 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005065 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005066 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005067 BUG_ON(mddev->level != mddev->new_level);
5068 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005069 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005070 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005071 }
5072
NeilBrown245f46c2009-03-31 14:39:39 +11005073 if (mddev->private == NULL)
5074 conf = setup_conf(mddev);
5075 else
5076 conf = mddev->private;
5077
NeilBrown91adb562009-03-31 14:39:39 +11005078 if (IS_ERR(conf))
5079 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005080
NeilBrown91adb562009-03-31 14:39:39 +11005081 mddev->thread = conf->thread;
5082 conf->thread = NULL;
5083 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005086 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
NeilBrownc148ffd2009-11-13 17:47:00 +11005088 list_for_each_entry(rdev, &mddev->disks, same_set) {
5089 if (rdev->raid_disk < 0)
5090 continue;
NeilBrown2f115882010-06-17 17:41:03 +10005091 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005092 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005093 continue;
5094 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005095 /* This disc is not fully in-sync. However if it
5096 * just stored parity (beyond the recovery_offset),
5097 * when we don't need to be concerned about the
5098 * array being dirty.
5099 * When reshape goes 'backwards', we never have
5100 * partially completed devices, so we only need
5101 * to worry about reshape going forwards.
5102 */
5103 /* Hack because v0.91 doesn't store recovery_offset properly. */
5104 if (mddev->major_version == 0 &&
5105 mddev->minor_version > 90)
5106 rdev->recovery_offset = reshape_offset;
5107
NeilBrownc148ffd2009-11-13 17:47:00 +11005108 if (rdev->recovery_offset < reshape_offset) {
5109 /* We need to check old and new layout */
5110 if (!only_parity(rdev->raid_disk,
5111 conf->algorithm,
5112 conf->raid_disks,
5113 conf->max_degraded))
5114 continue;
5115 }
5116 if (!only_parity(rdev->raid_disk,
5117 conf->prev_algo,
5118 conf->previous_raid_disks,
5119 conf->max_degraded))
5120 continue;
5121 dirty_parity_disks++;
5122 }
NeilBrown91adb562009-03-31 14:39:39 +11005123
NeilBrown5e5e3e72009-10-16 16:35:30 +11005124 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
5125 - working_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
NeilBrown674806d2010-06-16 17:17:53 +10005127 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005128 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005130 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 goto abort;
5132 }
5133
NeilBrown91adb562009-03-31 14:39:39 +11005134 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005135 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005136 mddev->resync_max_sectors = mddev->dev_sectors;
5137
NeilBrownc148ffd2009-11-13 17:47:00 +11005138 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005140 if (mddev->ok_start_degraded)
5141 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005142 "md/raid:%s: starting dirty degraded array"
5143 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005144 mdname(mddev));
5145 else {
5146 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005147 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005148 mdname(mddev));
5149 goto abort;
5150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005154 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5155 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005156 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5157 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 else
NeilBrown0c55e022010-05-03 14:09:02 +10005159 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5160 " out of %d devices, algorithm %d\n",
5161 mdname(mddev), conf->level,
5162 mddev->raid_disks - mddev->degraded,
5163 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
5165 print_raid5_conf(conf);
5166
NeilBrownfef9c612009-03-31 15:16:46 +11005167 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005168 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005169 atomic_set(&conf->reshape_stripes, 0);
5170 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5171 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5172 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5173 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5174 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005175 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005176 }
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
5179 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005180 if (mddev->to_remove == &raid5_attrs_group)
5181 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005182 else if (mddev->kobj.sd &&
5183 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005184 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005185 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005186 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005187 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5188
NeilBrown2ac87402010-06-01 19:37:29 +10005189 plugger_init(&conf->plug, raid5_unplug);
NeilBrown252ac522010-06-01 19:37:29 +10005190 mddev->plug = &conf->plug;
NeilBrown4a5add42010-06-01 19:37:28 +10005191 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005192 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005193 /* read-ahead size must cover two whole stripes, which
5194 * is 2 * (datadisks) * chunksize where 'n' is the
5195 * number of raid devices
5196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5198 int stripe = data_disks *
5199 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5200 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5201 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005202
5203 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005204
5205 mddev->queue->backing_dev_info.congested_data = mddev;
5206 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005207 mddev->queue->queue_lock = &conf->device_lock;
5208 mddev->queue->unplug_fn = raid5_unplug_queue;
5209
5210 chunk_size = mddev->chunk_sectors << 9;
5211 blk_queue_io_min(mddev->queue, chunk_size);
5212 blk_queue_io_opt(mddev->queue, chunk_size *
5213 (conf->raid_disks - conf->max_degraded));
5214
5215 list_for_each_entry(rdev, &mddev->disks, same_set)
5216 disk_stack_limits(mddev->gendisk, rdev->bdev,
5217 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 }
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return 0;
5221abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005222 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005223 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (conf) {
5225 print_raid5_conf(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005226 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
5228 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005229 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return -EIO;
5231}
5232
NeilBrown3f294f42005-11-08 21:39:25 -08005233static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11005235 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
5237 md_unregister_thread(mddev->thread);
5238 mddev->thread = NULL;
NeilBrown11d8a6e2010-07-26 11:57:07 +10005239 if (mddev->queue)
5240 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown2ac87402010-06-01 19:37:29 +10005241 plugger_flush(&conf->plug); /* the unplug fn references 'conf'*/
Dan Williams95fc17a2009-07-31 12:39:15 +10005242 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005243 mddev->private = NULL;
5244 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 return 0;
5246}
5247
Dan Williams45b42332007-07-09 11:56:43 -07005248#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005249static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250{
5251 int i;
5252
NeilBrown16a53ec2006-06-26 00:27:38 -07005253 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5254 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5255 seq_printf(seq, "sh %llu, count %d.\n",
5256 (unsigned long long)sh->sector, atomic_read(&sh->count));
5257 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005258 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005259 seq_printf(seq, "(cache%d: %p %ld) ",
5260 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005262 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263}
5264
NeilBrownd710e132008-10-13 11:55:12 +11005265static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005268 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 int i;
5270
5271 spin_lock_irq(&conf->device_lock);
5272 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005273 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 if (sh->raid_conf != conf)
5275 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005276 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
5278 }
5279 spin_unlock_irq(&conf->device_lock);
5280}
5281#endif
5282
NeilBrownd710e132008-10-13 11:55:12 +11005283static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11005285 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 int i;
5287
Andre Noll9d8f0362009-06-18 08:45:01 +10005288 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5289 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005290 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 for (i = 0; i < conf->raid_disks; i++)
5292 seq_printf (seq, "%s",
5293 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005294 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005296#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005297 seq_printf (seq, "\n");
5298 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299#endif
5300}
5301
5302static void print_raid5_conf (raid5_conf_t *conf)
5303{
5304 int i;
5305 struct disk_info *tmp;
5306
NeilBrown0c55e022010-05-03 14:09:02 +10005307 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 if (!conf) {
5309 printk("(conf==NULL)\n");
5310 return;
5311 }
NeilBrown0c55e022010-05-03 14:09:02 +10005312 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5313 conf->raid_disks,
5314 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
5316 for (i = 0; i < conf->raid_disks; i++) {
5317 char b[BDEVNAME_SIZE];
5318 tmp = conf->disks + i;
5319 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005320 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5321 i, !test_bit(Faulty, &tmp->rdev->flags),
5322 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 }
5324}
5325
5326static int raid5_spare_active(mddev_t *mddev)
5327{
5328 int i;
5329 raid5_conf_t *conf = mddev->private;
5330 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005331 int count = 0;
5332 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 for (i = 0; i < conf->raid_disks; i++) {
5335 tmp = conf->disks + i;
5336 if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005337 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005338 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005339 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005340 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005341 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 }
5343 }
NeilBrown6b965622010-08-18 11:56:59 +10005344 spin_lock_irqsave(&conf->device_lock, flags);
5345 mddev->degraded -= count;
5346 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005348 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
5351static int raid5_remove_disk(mddev_t *mddev, int number)
5352{
5353 raid5_conf_t *conf = mddev->private;
5354 int err = 0;
5355 mdk_rdev_t *rdev;
5356 struct disk_info *p = conf->disks + number;
5357
5358 print_raid5_conf(conf);
5359 rdev = p->rdev;
5360 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005361 if (number >= conf->raid_disks &&
5362 conf->reshape_progress == MaxSector)
5363 clear_bit(In_sync, &rdev->flags);
5364
NeilBrownb2d444d2005-11-08 21:39:31 -08005365 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 atomic_read(&rdev->nr_pending)) {
5367 err = -EBUSY;
5368 goto abort;
5369 }
NeilBrowndfc70642008-05-23 13:04:39 -07005370 /* Only remove non-faulty devices if recovery
5371 * isn't possible.
5372 */
5373 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown674806d2010-06-16 17:17:53 +10005374 !has_failed(conf) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005375 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005376 err = -EBUSY;
5377 goto abort;
5378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005380 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (atomic_read(&rdev->nr_pending)) {
5382 /* lost the race, try later */
5383 err = -EBUSY;
5384 p->rdev = rdev;
5385 }
5386 }
5387abort:
5388
5389 print_raid5_conf(conf);
5390 return err;
5391}
5392
5393static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5394{
5395 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005396 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 int disk;
5398 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005399 int first = 0;
5400 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
NeilBrown674806d2010-06-16 17:17:53 +10005402 if (has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Neil Brown6c2fce22008-06-28 08:31:31 +10005406 if (rdev->raid_disk >= 0)
5407 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
5409 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005410 * find the disk ... but prefer rdev->saved_raid_disk
5411 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005413 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005414 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005415 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5416 disk = rdev->saved_raid_disk;
5417 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005418 disk = first;
5419 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005421 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005423 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005424 if (rdev->saved_raid_disk != disk)
5425 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005426 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 break;
5428 }
5429 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431}
5432
5433static int raid5_resize(mddev_t *mddev, sector_t sectors)
5434{
5435 /* no resync is happening, and there is enough space
5436 * on all devices, so we can resize.
5437 * We need to make sure resync covers any new space.
5438 * If the array is shrinking we should possibly wait until
5439 * any io in the removed space completes, but it hardly seems
5440 * worth it.
5441 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005442 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005443 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5444 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005445 if (mddev->array_sectors >
5446 raid5_size(mddev, sectors, mddev->raid_disks))
5447 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005448 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005449 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005450 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5451 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5453 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005454 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005455 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 return 0;
5457}
5458
NeilBrown01ee22b2009-06-18 08:47:20 +10005459static int check_stripe_cache(mddev_t *mddev)
5460{
5461 /* Can only proceed if there are plenty of stripe_heads.
5462 * We need a minimum of one full stripe,, and for sensible progress
5463 * it is best to have about 4 times that.
5464 * If we require 4 times, then the default 256 4K stripe_heads will
5465 * allow for chunk sizes up to 256K, which is probably OK.
5466 * If the chunk size is greater, user-space should request more
5467 * stripe_heads first.
5468 */
5469 raid5_conf_t *conf = mddev->private;
5470 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5471 > conf->max_nr_stripes ||
5472 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5473 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005474 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5475 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005476 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5477 / STRIPE_SIZE)*4);
5478 return 0;
5479 }
5480 return 1;
5481}
5482
NeilBrown50ac1682009-06-18 08:47:55 +10005483static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005484{
NeilBrown070ec552009-06-16 16:54:21 +10005485 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005486
NeilBrown88ce4932009-03-31 15:24:23 +11005487 if (mddev->delta_disks == 0 &&
5488 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005489 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005490 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005491 if (mddev->bitmap)
5492 /* Cannot grow a bitmap yet */
5493 return -EBUSY;
NeilBrown674806d2010-06-16 17:17:53 +10005494 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005495 return -EINVAL;
5496 if (mddev->delta_disks < 0) {
5497 /* We might be able to shrink, but the devices must
5498 * be made bigger first.
5499 * For raid6, 4 is the minimum size.
5500 * Otherwise 2 is the minimum
5501 */
5502 int min = 2;
5503 if (mddev->level == 6)
5504 min = 4;
5505 if (mddev->raid_disks + mddev->delta_disks < min)
5506 return -EINVAL;
5507 }
NeilBrown29269552006-03-27 01:18:10 -08005508
NeilBrown01ee22b2009-06-18 08:47:20 +10005509 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005510 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005511
NeilBrownec32a2b2009-03-31 15:17:38 +11005512 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005513}
5514
5515static int raid5_start_reshape(mddev_t *mddev)
5516{
NeilBrown070ec552009-06-16 16:54:21 +10005517 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005518 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005519 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005520 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005521
NeilBrownf4168852007-02-28 20:11:53 -08005522 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005523 return -EBUSY;
5524
NeilBrown01ee22b2009-06-18 08:47:20 +10005525 if (!check_stripe_cache(mddev))
5526 return -ENOSPC;
5527
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005528 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown469518a2011-01-31 11:57:43 +11005529 if (!test_bit(In_sync, &rdev->flags)
5530 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005531 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005532
NeilBrownf4168852007-02-28 20:11:53 -08005533 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005534 /* Not enough devices even to make a degraded array
5535 * of that size
5536 */
5537 return -EINVAL;
5538
NeilBrownec32a2b2009-03-31 15:17:38 +11005539 /* Refuse to reduce size of the array. Any reductions in
5540 * array size must be through explicit setting of array_size
5541 * attribute.
5542 */
5543 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5544 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005545 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005546 "before number of disks\n", mdname(mddev));
5547 return -EINVAL;
5548 }
5549
NeilBrownf6705572006-03-27 01:18:11 -08005550 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005551 spin_lock_irq(&conf->device_lock);
5552 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005553 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005554 conf->prev_chunk_sectors = conf->chunk_sectors;
5555 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005556 conf->prev_algo = conf->algorithm;
5557 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005558 if (mddev->delta_disks < 0)
5559 conf->reshape_progress = raid5_size(mddev, 0, 0);
5560 else
5561 conf->reshape_progress = 0;
5562 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005563 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005564 spin_unlock_irq(&conf->device_lock);
5565
5566 /* Add some new drives, as many as will fit.
5567 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005568 * Don't add devices if we are reducing the number of
5569 * devices in the array. This is because it is not possible
5570 * to correctly record the "partially reconstructed" state of
5571 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005572 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005573 if (mddev->delta_disks >= 0) {
5574 int added_devices = 0;
5575 list_for_each_entry(rdev, &mddev->disks, same_set)
5576 if (rdev->raid_disk < 0 &&
5577 !test_bit(Faulty, &rdev->flags)) {
5578 if (raid5_add_disk(mddev, rdev) == 0) {
5579 char nm[20];
5580 if (rdev->raid_disk
5581 >= conf->previous_raid_disks) {
5582 set_bit(In_sync, &rdev->flags);
5583 added_devices++;
5584 } else
5585 rdev->recovery_offset = 0;
5586 sprintf(nm, "rd%d", rdev->raid_disk);
5587 if (sysfs_create_link(&mddev->kobj,
5588 &rdev->kobj, nm))
5589 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005590 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005591 } else if (rdev->raid_disk >= conf->previous_raid_disks
5592 && !test_bit(Faulty, &rdev->flags)) {
5593 /* This is a spare that was manually added */
5594 set_bit(In_sync, &rdev->flags);
5595 added_devices++;
5596 }
NeilBrown29269552006-03-27 01:18:10 -08005597
NeilBrown87a8dec2011-01-31 11:57:43 +11005598 /* When a reshape changes the number of devices,
5599 * ->degraded is measured against the larger of the
5600 * pre and post number of devices.
5601 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005602 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005603 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
NeilBrownec32a2b2009-03-31 15:17:38 +11005604 - added_devices;
5605 spin_unlock_irqrestore(&conf->device_lock, flags);
5606 }
NeilBrown63c70c42006-03-27 01:18:13 -08005607 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005608 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005609 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005610
NeilBrown29269552006-03-27 01:18:10 -08005611 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5612 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5613 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5614 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5615 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005616 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005617 if (!mddev->sync_thread) {
5618 mddev->recovery = 0;
5619 spin_lock_irq(&conf->device_lock);
5620 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005621 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005622 spin_unlock_irq(&conf->device_lock);
5623 return -EAGAIN;
5624 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005625 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005626 md_wakeup_thread(mddev->sync_thread);
5627 md_new_event(mddev);
5628 return 0;
5629}
NeilBrown29269552006-03-27 01:18:10 -08005630
NeilBrownec32a2b2009-03-31 15:17:38 +11005631/* This is called from the reshape thread and should make any
5632 * changes needed in 'conf'
5633 */
NeilBrown29269552006-03-27 01:18:10 -08005634static void end_reshape(raid5_conf_t *conf)
5635{
NeilBrown29269552006-03-27 01:18:10 -08005636
NeilBrownf6705572006-03-27 01:18:11 -08005637 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005638
NeilBrownf6705572006-03-27 01:18:11 -08005639 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005640 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005641 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005642 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005643 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005644
5645 /* read-ahead size must cover two whole stripes, which is
5646 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5647 */
NeilBrown4a5add42010-06-01 19:37:28 +10005648 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005649 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005650 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005651 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005652 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5653 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5654 }
NeilBrown29269552006-03-27 01:18:10 -08005655 }
NeilBrown29269552006-03-27 01:18:10 -08005656}
5657
NeilBrownec32a2b2009-03-31 15:17:38 +11005658/* This is called from the raid5d thread with mddev_lock held.
5659 * It makes config changes to the device.
5660 */
NeilBrowncea9c222009-03-31 15:15:05 +11005661static void raid5_finish_reshape(mddev_t *mddev)
5662{
NeilBrown070ec552009-06-16 16:54:21 +10005663 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005664
5665 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5666
NeilBrownec32a2b2009-03-31 15:17:38 +11005667 if (mddev->delta_disks > 0) {
5668 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5669 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005670 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005671 } else {
5672 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005673 mddev->degraded = conf->raid_disks;
5674 for (d = 0; d < conf->raid_disks ; d++)
5675 if (conf->disks[d].rdev &&
5676 test_bit(In_sync,
5677 &conf->disks[d].rdev->flags))
5678 mddev->degraded--;
5679 for (d = conf->raid_disks ;
5680 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005681 d++) {
5682 mdk_rdev_t *rdev = conf->disks[d].rdev;
5683 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5684 char nm[20];
5685 sprintf(nm, "rd%d", rdev->raid_disk);
5686 sysfs_remove_link(&mddev->kobj, nm);
5687 rdev->raid_disk = -1;
5688 }
5689 }
NeilBrowncea9c222009-03-31 15:15:05 +11005690 }
NeilBrown88ce4932009-03-31 15:24:23 +11005691 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005692 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005693 mddev->reshape_position = MaxSector;
5694 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005695 }
5696}
5697
NeilBrown72626682005-09-09 16:23:54 -07005698static void raid5_quiesce(mddev_t *mddev, int state)
5699{
NeilBrown070ec552009-06-16 16:54:21 +10005700 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005701
5702 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005703 case 2: /* resume for a suspend */
5704 wake_up(&conf->wait_for_overlap);
5705 break;
5706
NeilBrown72626682005-09-09 16:23:54 -07005707 case 1: /* stop all writes */
5708 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005709 /* '2' tells resync/reshape to pause so that all
5710 * active stripes can drain
5711 */
5712 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005713 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005714 atomic_read(&conf->active_stripes) == 0 &&
5715 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005716 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005717 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005718 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005719 /* allow reshape to continue */
5720 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005721 break;
5722
5723 case 0: /* re-enable writes */
5724 spin_lock_irq(&conf->device_lock);
5725 conf->quiesce = 0;
5726 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005727 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005728 spin_unlock_irq(&conf->device_lock);
5729 break;
5730 }
NeilBrown72626682005-09-09 16:23:54 -07005731}
NeilBrownb15c2e52006-01-06 00:20:16 -08005732
NeilBrownd562b0c2009-03-31 14:39:39 +11005733
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005734static void *raid45_takeover_raid0(mddev_t *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005735{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005736 struct raid0_private_data *raid0_priv = mddev->private;
Trela Maciej54071b32010-03-08 16:02:42 +11005737
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005738 /* for raid0 takeover only one zone is supported */
5739 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005740 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5741 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005742 return ERR_PTR(-EINVAL);
5743 }
5744
5745 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005746 mddev->new_layout = ALGORITHM_PARITY_N;
5747 mddev->new_chunk_sectors = mddev->chunk_sectors;
5748 mddev->raid_disks += 1;
5749 mddev->delta_disks = 1;
5750 /* make sure it will be not marked as dirty */
5751 mddev->recovery_cp = MaxSector;
5752
5753 return setup_conf(mddev);
5754}
5755
5756
NeilBrownd562b0c2009-03-31 14:39:39 +11005757static void *raid5_takeover_raid1(mddev_t *mddev)
5758{
5759 int chunksect;
5760
5761 if (mddev->raid_disks != 2 ||
5762 mddev->degraded > 1)
5763 return ERR_PTR(-EINVAL);
5764
5765 /* Should check if there are write-behind devices? */
5766
5767 chunksect = 64*2; /* 64K by default */
5768
5769 /* The array must be an exact multiple of chunksize */
5770 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5771 chunksect >>= 1;
5772
5773 if ((chunksect<<9) < STRIPE_SIZE)
5774 /* array size does not allow a suitable chunk size */
5775 return ERR_PTR(-EINVAL);
5776
5777 mddev->new_level = 5;
5778 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005779 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005780
5781 return setup_conf(mddev);
5782}
5783
NeilBrownfc9739c2009-03-31 14:57:20 +11005784static void *raid5_takeover_raid6(mddev_t *mddev)
5785{
5786 int new_layout;
5787
5788 switch (mddev->layout) {
5789 case ALGORITHM_LEFT_ASYMMETRIC_6:
5790 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5791 break;
5792 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5793 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5794 break;
5795 case ALGORITHM_LEFT_SYMMETRIC_6:
5796 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5797 break;
5798 case ALGORITHM_RIGHT_SYMMETRIC_6:
5799 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5800 break;
5801 case ALGORITHM_PARITY_0_6:
5802 new_layout = ALGORITHM_PARITY_0;
5803 break;
5804 case ALGORITHM_PARITY_N:
5805 new_layout = ALGORITHM_PARITY_N;
5806 break;
5807 default:
5808 return ERR_PTR(-EINVAL);
5809 }
5810 mddev->new_level = 5;
5811 mddev->new_layout = new_layout;
5812 mddev->delta_disks = -1;
5813 mddev->raid_disks -= 1;
5814 return setup_conf(mddev);
5815}
5816
NeilBrownd562b0c2009-03-31 14:39:39 +11005817
NeilBrown50ac1682009-06-18 08:47:55 +10005818static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005819{
NeilBrown88ce4932009-03-31 15:24:23 +11005820 /* For a 2-drive array, the layout and chunk size can be changed
5821 * immediately as not restriping is needed.
5822 * For larger arrays we record the new value - after validation
5823 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005824 */
NeilBrown070ec552009-06-16 16:54:21 +10005825 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005826 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005827
NeilBrown597a7112009-06-18 08:47:42 +10005828 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005829 return -EINVAL;
5830 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005831 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005832 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005833 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005834 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005835 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005836 /* not factor of array size */
5837 return -EINVAL;
5838 }
5839
5840 /* They look valid */
5841
NeilBrown88ce4932009-03-31 15:24:23 +11005842 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005843 /* can make the change immediately */
5844 if (mddev->new_layout >= 0) {
5845 conf->algorithm = mddev->new_layout;
5846 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005847 }
5848 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005849 conf->chunk_sectors = new_chunk ;
5850 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005851 }
5852 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5853 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005854 }
NeilBrown50ac1682009-06-18 08:47:55 +10005855 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005856}
5857
NeilBrown50ac1682009-06-18 08:47:55 +10005858static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005859{
NeilBrown597a7112009-06-18 08:47:42 +10005860 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005861
NeilBrown597a7112009-06-18 08:47:42 +10005862 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005863 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005864 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005865 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005866 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005867 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005868 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005869 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005870 /* not factor of array size */
5871 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005872 }
NeilBrown88ce4932009-03-31 15:24:23 +11005873
5874 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005875 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005876}
5877
NeilBrownd562b0c2009-03-31 14:39:39 +11005878static void *raid5_takeover(mddev_t *mddev)
5879{
5880 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005881 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005882 * raid1 - if there are two drives. We need to know the chunk size
5883 * raid4 - trivial - just use a raid4 layout.
5884 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005885 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005886 if (mddev->level == 0)
5887 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11005888 if (mddev->level == 1)
5889 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005890 if (mddev->level == 4) {
5891 mddev->new_layout = ALGORITHM_PARITY_N;
5892 mddev->new_level = 5;
5893 return setup_conf(mddev);
5894 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005895 if (mddev->level == 6)
5896 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005897
5898 return ERR_PTR(-EINVAL);
5899}
5900
NeilBrowna78d38a2010-03-22 16:53:49 +11005901static void *raid4_takeover(mddev_t *mddev)
5902{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005903 /* raid4 can take over:
5904 * raid0 - if there is only one strip zone
5905 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11005906 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005907 if (mddev->level == 0)
5908 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11005909 if (mddev->level == 5 &&
5910 mddev->layout == ALGORITHM_PARITY_N) {
5911 mddev->new_layout = 0;
5912 mddev->new_level = 4;
5913 return setup_conf(mddev);
5914 }
5915 return ERR_PTR(-EINVAL);
5916}
NeilBrownd562b0c2009-03-31 14:39:39 +11005917
NeilBrown245f46c2009-03-31 14:39:39 +11005918static struct mdk_personality raid5_personality;
5919
5920static void *raid6_takeover(mddev_t *mddev)
5921{
5922 /* Currently can only take over a raid5. We map the
5923 * personality to an equivalent raid6 personality
5924 * with the Q block at the end.
5925 */
5926 int new_layout;
5927
5928 if (mddev->pers != &raid5_personality)
5929 return ERR_PTR(-EINVAL);
5930 if (mddev->degraded > 1)
5931 return ERR_PTR(-EINVAL);
5932 if (mddev->raid_disks > 253)
5933 return ERR_PTR(-EINVAL);
5934 if (mddev->raid_disks < 3)
5935 return ERR_PTR(-EINVAL);
5936
5937 switch (mddev->layout) {
5938 case ALGORITHM_LEFT_ASYMMETRIC:
5939 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5940 break;
5941 case ALGORITHM_RIGHT_ASYMMETRIC:
5942 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5943 break;
5944 case ALGORITHM_LEFT_SYMMETRIC:
5945 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5946 break;
5947 case ALGORITHM_RIGHT_SYMMETRIC:
5948 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5949 break;
5950 case ALGORITHM_PARITY_0:
5951 new_layout = ALGORITHM_PARITY_0_6;
5952 break;
5953 case ALGORITHM_PARITY_N:
5954 new_layout = ALGORITHM_PARITY_N;
5955 break;
5956 default:
5957 return ERR_PTR(-EINVAL);
5958 }
5959 mddev->new_level = 6;
5960 mddev->new_layout = new_layout;
5961 mddev->delta_disks = 1;
5962 mddev->raid_disks += 1;
5963 return setup_conf(mddev);
5964}
5965
5966
NeilBrown16a53ec2006-06-26 00:27:38 -07005967static struct mdk_personality raid6_personality =
5968{
5969 .name = "raid6",
5970 .level = 6,
5971 .owner = THIS_MODULE,
5972 .make_request = make_request,
5973 .run = run,
5974 .stop = stop,
5975 .status = status,
5976 .error_handler = error,
5977 .hot_add_disk = raid5_add_disk,
5978 .hot_remove_disk= raid5_remove_disk,
5979 .spare_active = raid5_spare_active,
5980 .sync_request = sync_request,
5981 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005982 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005983 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005984 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005985 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005986 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005987 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005988};
NeilBrown2604b702006-01-06 00:20:36 -08005989static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990{
5991 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005992 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 .owner = THIS_MODULE,
5994 .make_request = make_request,
5995 .run = run,
5996 .stop = stop,
5997 .status = status,
5998 .error_handler = error,
5999 .hot_add_disk = raid5_add_disk,
6000 .hot_remove_disk= raid5_remove_disk,
6001 .spare_active = raid5_spare_active,
6002 .sync_request = sync_request,
6003 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006004 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006005 .check_reshape = raid5_check_reshape,
6006 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006007 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006008 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006009 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010};
6011
NeilBrown2604b702006-01-06 00:20:36 -08006012static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
NeilBrown2604b702006-01-06 00:20:36 -08006014 .name = "raid4",
6015 .level = 4,
6016 .owner = THIS_MODULE,
6017 .make_request = make_request,
6018 .run = run,
6019 .stop = stop,
6020 .status = status,
6021 .error_handler = error,
6022 .hot_add_disk = raid5_add_disk,
6023 .hot_remove_disk= raid5_remove_disk,
6024 .spare_active = raid5_spare_active,
6025 .sync_request = sync_request,
6026 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006027 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006028 .check_reshape = raid5_check_reshape,
6029 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006030 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006031 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006032 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006033};
6034
6035static int __init raid5_init(void)
6036{
NeilBrown16a53ec2006-06-26 00:27:38 -07006037 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006038 register_md_personality(&raid5_personality);
6039 register_md_personality(&raid4_personality);
6040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041}
6042
NeilBrown2604b702006-01-06 00:20:36 -08006043static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
NeilBrown16a53ec2006-06-26 00:27:38 -07006045 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006046 unregister_md_personality(&raid5_personality);
6047 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048}
6049
6050module_init(raid5_init);
6051module_exit(raid5_exit);
6052MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006053MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006055MODULE_ALIAS("md-raid5");
6056MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006057MODULE_ALIAS("md-level-5");
6058MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006059MODULE_ALIAS("md-personality-8"); /* RAID6 */
6060MODULE_ALIAS("md-raid6");
6061MODULE_ALIAS("md-level-6");
6062
6063/* This used to be two separate modules, they were: */
6064MODULE_ALIAS("raid5");
6065MODULE_ALIAS("raid6");