blob: c6ae7c19491585622e8d2d1a89112a74f3ebbb5f [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>
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110054#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110055#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110056#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * Stripe cache
60 */
61
62#define NR_STRIPES 256
63#define STRIPE_SIZE PAGE_SIZE
64#define STRIPE_SHIFT (PAGE_SHIFT - 9)
65#define STRIPE_SECTORS (STRIPE_SIZE>>9)
66#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070067#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080068#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define HASH_MASK (NR_HASH - 1)
70
NeilBrownfccddba2006-01-06 00:20:33 -080071#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* bio's attached to a stripe+device for I/O are linked together in bi_sector
74 * order without overlap. There may be several bio's per stripe+device, and
75 * a bio could span several devices.
76 * When walking this list for a particular stripe+device, we must never proceed
77 * beyond a bio that extends past this device, as the next bio might no longer
78 * be valid.
79 * This macro is used to determine the 'next' bio in the list, given the sector
80 * of the current stripe+device
81 */
82#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
83/*
84 * The following can be used to debug the driver
85 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define RAID5_PARANOIA 1
87#if RAID5_PARANOIA && defined(CONFIG_SMP)
88# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
89#else
90# define CHECK_DEVLOCK()
91#endif
92
Dan Williams45b42332007-07-09 11:56:43 -070093#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define inline
95#define __inline__
96#endif
97
Bernd Schubert6be9d492008-05-23 13:04:34 -070098#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
99
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
104static inline int raid5_bi_phys_segments(struct bio *bio)
105{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200106 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200107}
108
109static inline int raid5_bi_hw_segments(struct bio *bio)
110{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200111 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
114static inline int raid5_dec_bi_phys_segments(struct bio *bio)
115{
116 --bio->bi_phys_segments;
117 return raid5_bi_phys_segments(bio);
118}
119
120static inline int raid5_dec_bi_hw_segments(struct bio *bio)
121{
122 unsigned short val = raid5_bi_hw_segments(bio);
123
124 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200125 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200126 return val;
127}
128
129static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
130{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200131 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
NeilBrownd0dabf72009-03-31 14:39:38 +1100134/* Find first data disk in a raid6 stripe */
135static inline int raid6_d0(struct stripe_head *sh)
136{
NeilBrown67cc2b82009-03-31 14:39:38 +1100137 if (sh->ddf_layout)
138 /* ddf always start from first device */
139 return 0;
140 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100141 if (sh->qd_idx == sh->disks - 1)
142 return 0;
143 else
144 return sh->qd_idx + 1;
145}
NeilBrown16a53ec2006-06-26 00:27:38 -0700146static inline int raid6_next_disk(int disk, int raid_disks)
147{
148 disk++;
149 return (disk < raid_disks) ? disk : 0;
150}
Dan Williamsa4456852007-07-09 11:56:43 -0700151
NeilBrownd0dabf72009-03-31 14:39:38 +1100152/* When walking through the disks in a raid5, starting at raid6_d0,
153 * We need to map each disk to a 'slot', where the data disks are slot
154 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
155 * is raid_disks-1. This help does that mapping.
156 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100157static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
158 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100159{
Dan Williams66295422009-10-19 18:09:32 -0700160 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100161
NeilBrowne4424fe2009-10-16 16:27:34 +1100162 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700163 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100166 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100167 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 return slot;
171}
172
Dan Williamsa4456852007-07-09 11:56:43 -0700173static void return_io(struct bio *return_bi)
174{
175 struct bio *bi = return_bi;
176 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700177
178 return_bi = bi->bi_next;
179 bi->bi_next = NULL;
180 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000181 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700182 bi = return_bi;
183 }
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static void print_raid5_conf (raid5_conf_t *conf);
187
Dan Williams600aa102008-06-28 08:32:05 +1000188static int stripe_operations_active(struct stripe_head *sh)
189{
190 return sh->check_state || sh->reconstruct_state ||
191 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
192 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
193}
194
Arjan van de Ven858119e2006-01-14 13:20:43 -0800195static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200198 BUG_ON(!list_empty(&sh->lru));
199 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700201 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700203 blk_plug_device(conf->mddev->queue);
204 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700205 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700206 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700207 blk_plug_device(conf->mddev->queue);
208 } else {
NeilBrown72626682005-09-09 16:23:54 -0700209 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 md_wakeup_thread(conf->mddev->thread);
213 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000214 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
216 atomic_dec(&conf->preread_active_stripes);
217 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
218 md_wakeup_thread(conf->mddev->thread);
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800221 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
222 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800224 if (conf->retry_read_aligned)
225 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 }
229}
NeilBrownd0dabf72009-03-31 14:39:38 +1100230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void release_stripe(struct stripe_head *sh)
232{
233 raid5_conf_t *conf = sh->raid_conf;
234 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_lock_irqsave(&conf->device_lock, flags);
237 __release_stripe(conf, sh);
238 spin_unlock_irqrestore(&conf->device_lock, flags);
239}
240
NeilBrownfccddba2006-01-06 00:20:33 -0800241static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Dan Williams45b42332007-07-09 11:56:43 -0700243 pr_debug("remove_hash(), stripe %llu\n",
244 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
NeilBrownfccddba2006-01-06 00:20:33 -0800246 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
NeilBrown16a53ec2006-06-26 00:27:38 -0700249static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
NeilBrownfccddba2006-01-06 00:20:33 -0800251 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("insert_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800257 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260
261/* find an idle stripe, make sure it is unhashed, and return it. */
262static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
263{
264 struct stripe_head *sh = NULL;
265 struct list_head *first;
266
267 CHECK_DEVLOCK();
268 if (list_empty(&conf->inactive_list))
269 goto out;
270 first = conf->inactive_list.next;
271 sh = list_entry(first, struct stripe_head, lru);
272 list_del_init(first);
273 remove_hash(sh);
274 atomic_inc(&conf->active_stripes);
275out:
276 return sh;
277}
278
279static void shrink_buffers(struct stripe_head *sh, int num)
280{
281 struct page *p;
282 int i;
283
284 for (i=0; i<num ; i++) {
285 p = sh->dev[i].page;
286 if (!p)
287 continue;
288 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800289 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292
293static int grow_buffers(struct stripe_head *sh, int num)
294{
295 int i;
296
297 for (i=0; i<num; i++) {
298 struct page *page;
299
300 if (!(page = alloc_page(GFP_KERNEL))) {
301 return 1;
302 }
303 sh->dev[i].page = page;
304 }
305 return 0;
306}
307
NeilBrown784052e2009-03-31 15:19:07 +1100308static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100309static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
310 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
NeilBrownb5663ba2009-03-31 14:39:38 +1100312static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800315 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200317 BUG_ON(atomic_read(&sh->count) != 0);
318 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000319 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700322 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (unsigned long long)sh->sector);
324
325 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700326
NeilBrown86b42c72009-03-31 15:19:03 +1100327 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100328 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100330 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 sh->state = 0;
332
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800333
334 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct r5dev *dev = &sh->dev[i];
336
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700339 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700341 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 test_bit(R5_LOCKED, &dev->flags));
343 BUG();
344 }
345 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100346 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 insert_hash(conf, sh);
349}
350
NeilBrown86b42c72009-03-31 15:19:03 +1100351static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
352 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800355 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700358 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800359 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100360 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700362 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return NULL;
364}
365
366static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200367static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
NeilBrownb5663ba2009-03-31 14:39:38 +1100369static struct stripe_head *
370get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000371 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct stripe_head *sh;
374
Dan Williams45b42332007-07-09 11:56:43 -0700375 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 spin_lock_irq(&conf->device_lock);
378
379 do {
NeilBrown72626682005-09-09 16:23:54 -0700380 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000381 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700382 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100383 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!sh) {
385 if (!conf->inactive_blocked)
386 sh = get_free_stripe(conf);
387 if (noblock && sh == NULL)
388 break;
389 if (!sh) {
390 conf->inactive_blocked = 1;
391 wait_event_lock_irq(conf->wait_for_stripe,
392 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800393 (atomic_read(&conf->active_stripes)
394 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 || !conf->inactive_blocked),
396 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700397 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 );
399 conf->inactive_blocked = 0;
400 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100401 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } else {
403 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100404 BUG_ON(!list_empty(&sh->lru)
405 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else {
407 if (!test_bit(STRIPE_HANDLE, &sh->state))
408 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700409 if (list_empty(&sh->lru) &&
410 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700411 BUG();
412 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415 } while (sh == NULL);
416
417 if (sh)
418 atomic_inc(&sh->count);
419
420 spin_unlock_irq(&conf->device_lock);
421 return sh;
422}
423
NeilBrown6712ecf2007-09-27 12:47:43 +0200424static void
425raid5_end_read_request(struct bio *bi, int error);
426static void
427raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700428
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000429static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700430{
431 raid5_conf_t *conf = sh->raid_conf;
432 int i, disks = sh->disks;
433
434 might_sleep();
435
436 for (i = disks; i--; ) {
437 int rw;
438 struct bio *bi;
439 mdk_rdev_t *rdev;
440 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
441 rw = WRITE;
442 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
443 rw = READ;
444 else
445 continue;
446
447 bi = &sh->dev[i].req;
448
449 bi->bi_rw = rw;
450 if (rw == WRITE)
451 bi->bi_end_io = raid5_end_write_request;
452 else
453 bi->bi_end_io = raid5_end_read_request;
454
455 rcu_read_lock();
456 rdev = rcu_dereference(conf->disks[i].rdev);
457 if (rdev && test_bit(Faulty, &rdev->flags))
458 rdev = NULL;
459 if (rdev)
460 atomic_inc(&rdev->nr_pending);
461 rcu_read_unlock();
462
463 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000464 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700465 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
466
Dan Williams2b7497f2008-06-28 08:31:52 +1000467 set_bit(STRIPE_IO_STARTED, &sh->state);
468
Dan Williams91c00922007-01-02 13:52:30 -0700469 bi->bi_bdev = rdev->bdev;
470 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700471 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700472 bi->bi_rw, i);
473 atomic_inc(&sh->count);
474 bi->bi_sector = sh->sector + rdev->data_offset;
475 bi->bi_flags = 1 << BIO_UPTODATE;
476 bi->bi_vcnt = 1;
477 bi->bi_max_vecs = 1;
478 bi->bi_idx = 0;
479 bi->bi_io_vec = &sh->dev[i].vec;
480 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
481 bi->bi_io_vec[0].bv_offset = 0;
482 bi->bi_size = STRIPE_SIZE;
483 bi->bi_next = NULL;
484 if (rw == WRITE &&
485 test_bit(R5_ReWrite, &sh->dev[i].flags))
486 atomic_add(STRIPE_SECTORS,
487 &rdev->corrected_errors);
488 generic_make_request(bi);
489 } else {
490 if (rw == WRITE)
491 set_bit(STRIPE_DEGRADED, &sh->state);
492 pr_debug("skip op %ld on disc %d for sector %llu\n",
493 bi->bi_rw, i, (unsigned long long)sh->sector);
494 clear_bit(R5_LOCKED, &sh->dev[i].flags);
495 set_bit(STRIPE_HANDLE, &sh->state);
496 }
497 }
498}
499
500static struct dma_async_tx_descriptor *
501async_copy_data(int frombio, struct bio *bio, struct page *page,
502 sector_t sector, struct dma_async_tx_descriptor *tx)
503{
504 struct bio_vec *bvl;
505 struct page *bio_page;
506 int i;
507 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700508 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700509 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700510
511 if (bio->bi_sector >= sector)
512 page_offset = (signed)(bio->bi_sector - sector) * 512;
513 else
514 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700515
Dan Williams0403e382009-09-08 17:42:50 -0700516 if (frombio)
517 flags |= ASYNC_TX_FENCE;
518 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
519
Dan Williams91c00922007-01-02 13:52:30 -0700520 bio_for_each_segment(bvl, bio, i) {
521 int len = bio_iovec_idx(bio, i)->bv_len;
522 int clen;
523 int b_offset = 0;
524
525 if (page_offset < 0) {
526 b_offset = -page_offset;
527 page_offset += b_offset;
528 len -= b_offset;
529 }
530
531 if (len > 0 && page_offset + len > STRIPE_SIZE)
532 clen = STRIPE_SIZE - page_offset;
533 else
534 clen = len;
535
536 if (clen > 0) {
537 b_offset += bio_iovec_idx(bio, i)->bv_offset;
538 bio_page = bio_iovec_idx(bio, i)->bv_page;
539 if (frombio)
540 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700541 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700542 else
543 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700544 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700545 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700546 /* chain the operations */
547 submit.depend_tx = tx;
548
Dan Williams91c00922007-01-02 13:52:30 -0700549 if (clen < len) /* hit end of page */
550 break;
551 page_offset += len;
552 }
553
554 return tx;
555}
556
557static void ops_complete_biofill(void *stripe_head_ref)
558{
559 struct stripe_head *sh = stripe_head_ref;
560 struct bio *return_bi = NULL;
561 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700562 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700563
Harvey Harrisone46b2722008-04-28 02:15:50 -0700564 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700565 (unsigned long long)sh->sector);
566
567 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000568 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700569 for (i = sh->disks; i--; ) {
570 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700571
572 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700573 /* and check if we need to reply to a read request,
574 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000575 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700576 */
577 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700578 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700579
Dan Williams91c00922007-01-02 13:52:30 -0700580 BUG_ON(!dev->read);
581 rbi = dev->read;
582 dev->read = NULL;
583 while (rbi && rbi->bi_sector <
584 dev->sector + STRIPE_SECTORS) {
585 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200586 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700587 rbi->bi_next = return_bi;
588 return_bi = rbi;
589 }
Dan Williams91c00922007-01-02 13:52:30 -0700590 rbi = rbi2;
591 }
592 }
593 }
Dan Williams83de75c2008-06-28 08:31:58 +1000594 spin_unlock_irq(&conf->device_lock);
595 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700596
597 return_io(return_bi);
598
Dan Williamse4d84902007-09-24 10:06:13 -0700599 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700600 release_stripe(sh);
601}
602
603static void ops_run_biofill(struct stripe_head *sh)
604{
605 struct dma_async_tx_descriptor *tx = NULL;
606 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700607 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700608 int i;
609
Harvey Harrisone46b2722008-04-28 02:15:50 -0700610 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700611 (unsigned long long)sh->sector);
612
613 for (i = sh->disks; i--; ) {
614 struct r5dev *dev = &sh->dev[i];
615 if (test_bit(R5_Wantfill, &dev->flags)) {
616 struct bio *rbi;
617 spin_lock_irq(&conf->device_lock);
618 dev->read = rbi = dev->toread;
619 dev->toread = NULL;
620 spin_unlock_irq(&conf->device_lock);
621 while (rbi && rbi->bi_sector <
622 dev->sector + STRIPE_SECTORS) {
623 tx = async_copy_data(0, rbi, dev->page,
624 dev->sector, tx);
625 rbi = r5_next_bio(rbi, dev->sector);
626 }
627 }
628 }
629
630 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700631 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
632 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700633}
634
Dan Williams4e7d2c02009-08-29 19:13:11 -0700635static void mark_target_uptodate(struct stripe_head *sh, int target)
636{
637 struct r5dev *tgt;
638
639 if (target < 0)
640 return;
641
642 tgt = &sh->dev[target];
643 set_bit(R5_UPTODATE, &tgt->flags);
644 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
645 clear_bit(R5_Wantcompute, &tgt->flags);
646}
647
Dan Williamsac6b53b2009-07-14 13:40:19 -0700648static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700649{
650 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700651
Harvey Harrisone46b2722008-04-28 02:15:50 -0700652 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700653 (unsigned long long)sh->sector);
654
Dan Williamsac6b53b2009-07-14 13:40:19 -0700655 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700656 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700657 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700658
Dan Williamsecc65c92008-06-28 08:31:57 +1000659 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
660 if (sh->check_state == check_state_compute_run)
661 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700662 set_bit(STRIPE_HANDLE, &sh->state);
663 release_stripe(sh);
664}
665
Dan Williamsd6f38f32009-07-14 11:50:52 -0700666/* return a pointer to the address conversion region of the scribble buffer */
667static addr_conv_t *to_addr_conv(struct stripe_head *sh,
668 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700669{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700670 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
671}
672
673static struct dma_async_tx_descriptor *
674ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
675{
Dan Williams91c00922007-01-02 13:52:30 -0700676 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700677 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700678 int target = sh->ops.target;
679 struct r5dev *tgt = &sh->dev[target];
680 struct page *xor_dest = tgt->page;
681 int count = 0;
682 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700683 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700684 int i;
685
686 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700687 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700688 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
689
690 for (i = disks; i--; )
691 if (i != target)
692 xor_srcs[count++] = sh->dev[i].page;
693
694 atomic_inc(&sh->count);
695
Dan Williams0403e382009-09-08 17:42:50 -0700696 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700697 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700698 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700699 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700700 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700701 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700702
Dan Williams91c00922007-01-02 13:52:30 -0700703 return tx;
704}
705
Dan Williamsac6b53b2009-07-14 13:40:19 -0700706/* set_syndrome_sources - populate source buffers for gen_syndrome
707 * @srcs - (struct page *) array of size sh->disks
708 * @sh - stripe_head to parse
709 *
710 * Populates srcs in proper layout order for the stripe and returns the
711 * 'count' of sources to be used in a call to async_gen_syndrome. The P
712 * destination buffer is recorded in srcs[count] and the Q destination
713 * is recorded in srcs[count+1]].
714 */
715static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
716{
717 int disks = sh->disks;
718 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
719 int d0_idx = raid6_d0(sh);
720 int count;
721 int i;
722
723 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100724 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700725
726 count = 0;
727 i = d0_idx;
728 do {
729 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
730
731 srcs[slot] = sh->dev[i].page;
732 i = raid6_next_disk(i, disks);
733 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700734
NeilBrowne4424fe2009-10-16 16:27:34 +1100735 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700736}
737
738static struct dma_async_tx_descriptor *
739ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
740{
741 int disks = sh->disks;
742 struct page **blocks = percpu->scribble;
743 int target;
744 int qd_idx = sh->qd_idx;
745 struct dma_async_tx_descriptor *tx;
746 struct async_submit_ctl submit;
747 struct r5dev *tgt;
748 struct page *dest;
749 int i;
750 int count;
751
752 if (sh->ops.target < 0)
753 target = sh->ops.target2;
754 else if (sh->ops.target2 < 0)
755 target = sh->ops.target;
756 else
757 /* we should only have one valid target */
758 BUG();
759 BUG_ON(target < 0);
760 pr_debug("%s: stripe %llu block: %d\n",
761 __func__, (unsigned long long)sh->sector, target);
762
763 tgt = &sh->dev[target];
764 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
765 dest = tgt->page;
766
767 atomic_inc(&sh->count);
768
769 if (target == qd_idx) {
770 count = set_syndrome_sources(blocks, sh);
771 blocks[count] = NULL; /* regenerating p is not necessary */
772 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700773 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
774 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700775 to_addr_conv(sh, percpu));
776 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
777 } else {
778 /* Compute any data- or p-drive using XOR */
779 count = 0;
780 for (i = disks; i-- ; ) {
781 if (i == target || i == qd_idx)
782 continue;
783 blocks[count++] = sh->dev[i].page;
784 }
785
Dan Williams0403e382009-09-08 17:42:50 -0700786 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
787 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700788 to_addr_conv(sh, percpu));
789 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
790 }
791
792 return tx;
793}
794
795static struct dma_async_tx_descriptor *
796ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
797{
798 int i, count, disks = sh->disks;
799 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
800 int d0_idx = raid6_d0(sh);
801 int faila = -1, failb = -1;
802 int target = sh->ops.target;
803 int target2 = sh->ops.target2;
804 struct r5dev *tgt = &sh->dev[target];
805 struct r5dev *tgt2 = &sh->dev[target2];
806 struct dma_async_tx_descriptor *tx;
807 struct page **blocks = percpu->scribble;
808 struct async_submit_ctl submit;
809
810 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
811 __func__, (unsigned long long)sh->sector, target, target2);
812 BUG_ON(target < 0 || target2 < 0);
813 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
814 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
815
Dan Williams6c910a72009-09-16 12:24:54 -0700816 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700817 * slot number conversion for 'faila' and 'failb'
818 */
819 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100820 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700821 count = 0;
822 i = d0_idx;
823 do {
824 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
825
826 blocks[slot] = sh->dev[i].page;
827
828 if (i == target)
829 faila = slot;
830 if (i == target2)
831 failb = slot;
832 i = raid6_next_disk(i, disks);
833 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700834
835 BUG_ON(faila == failb);
836 if (failb < faila)
837 swap(faila, failb);
838 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
839 __func__, (unsigned long long)sh->sector, faila, failb);
840
841 atomic_inc(&sh->count);
842
843 if (failb == syndrome_disks+1) {
844 /* Q disk is one of the missing disks */
845 if (faila == syndrome_disks) {
846 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700847 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
848 ops_complete_compute, sh,
849 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100850 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700851 STRIPE_SIZE, &submit);
852 } else {
853 struct page *dest;
854 int data_target;
855 int qd_idx = sh->qd_idx;
856
857 /* Missing D+Q: recompute D from P, then recompute Q */
858 if (target == qd_idx)
859 data_target = target2;
860 else
861 data_target = target;
862
863 count = 0;
864 for (i = disks; i-- ; ) {
865 if (i == data_target || i == qd_idx)
866 continue;
867 blocks[count++] = sh->dev[i].page;
868 }
869 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700870 init_async_submit(&submit,
871 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
872 NULL, NULL, NULL,
873 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700874 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
875 &submit);
876
877 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700878 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
879 ops_complete_compute, sh,
880 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700881 return async_gen_syndrome(blocks, 0, count+2,
882 STRIPE_SIZE, &submit);
883 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700884 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700885 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
886 ops_complete_compute, sh,
887 to_addr_conv(sh, percpu));
888 if (failb == syndrome_disks) {
889 /* We're missing D+P. */
890 return async_raid6_datap_recov(syndrome_disks+2,
891 STRIPE_SIZE, faila,
892 blocks, &submit);
893 } else {
894 /* We're missing D+D. */
895 return async_raid6_2data_recov(syndrome_disks+2,
896 STRIPE_SIZE, faila, failb,
897 blocks, &submit);
898 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700899 }
900}
901
902
Dan Williams91c00922007-01-02 13:52:30 -0700903static void ops_complete_prexor(void *stripe_head_ref)
904{
905 struct stripe_head *sh = stripe_head_ref;
906
Harvey Harrisone46b2722008-04-28 02:15:50 -0700907 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700908 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700909}
910
911static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700912ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
913 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700914{
Dan Williams91c00922007-01-02 13:52:30 -0700915 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700916 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700917 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700918 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700919
920 /* existing parity data subtracted */
921 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
922
Harvey Harrisone46b2722008-04-28 02:15:50 -0700923 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700924 (unsigned long long)sh->sector);
925
926 for (i = disks; i--; ) {
927 struct r5dev *dev = &sh->dev[i];
928 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000929 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700930 xor_srcs[count++] = dev->page;
931 }
932
Dan Williams0403e382009-09-08 17:42:50 -0700933 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700934 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700935 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700936
937 return tx;
938}
939
940static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000941ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700942{
943 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000944 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700945
Harvey Harrisone46b2722008-04-28 02:15:50 -0700946 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700947 (unsigned long long)sh->sector);
948
949 for (i = disks; i--; ) {
950 struct r5dev *dev = &sh->dev[i];
951 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700952
Dan Williamsd8ee0722008-06-28 08:32:06 +1000953 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700954 struct bio *wbi;
955
956 spin_lock(&sh->lock);
957 chosen = dev->towrite;
958 dev->towrite = NULL;
959 BUG_ON(dev->written);
960 wbi = dev->written = chosen;
961 spin_unlock(&sh->lock);
962
963 while (wbi && wbi->bi_sector <
964 dev->sector + STRIPE_SECTORS) {
965 tx = async_copy_data(1, wbi, dev->page,
966 dev->sector, tx);
967 wbi = r5_next_bio(wbi, dev->sector);
968 }
969 }
970 }
971
972 return tx;
973}
974
Dan Williamsac6b53b2009-07-14 13:40:19 -0700975static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700976{
977 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700978 int disks = sh->disks;
979 int pd_idx = sh->pd_idx;
980 int qd_idx = sh->qd_idx;
981 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700982
Harvey Harrisone46b2722008-04-28 02:15:50 -0700983 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700984 (unsigned long long)sh->sector);
985
986 for (i = disks; i--; ) {
987 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700988
989 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700990 set_bit(R5_UPTODATE, &dev->flags);
991 }
992
Dan Williamsd8ee0722008-06-28 08:32:06 +1000993 if (sh->reconstruct_state == reconstruct_state_drain_run)
994 sh->reconstruct_state = reconstruct_state_drain_result;
995 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
996 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
997 else {
998 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
999 sh->reconstruct_state = reconstruct_state_result;
1000 }
Dan Williams91c00922007-01-02 13:52:30 -07001001
1002 set_bit(STRIPE_HANDLE, &sh->state);
1003 release_stripe(sh);
1004}
1005
1006static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001007ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1008 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001009{
Dan Williams91c00922007-01-02 13:52:30 -07001010 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001011 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001012 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001013 int count = 0, pd_idx = sh->pd_idx, i;
1014 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001015 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001016 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001017
Harvey Harrisone46b2722008-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 /* check if prexor is active which means only process blocks
1022 * that are part of a read-modify-write (written)
1023 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001024 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1025 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001026 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1027 for (i = disks; i--; ) {
1028 struct r5dev *dev = &sh->dev[i];
1029 if (dev->written)
1030 xor_srcs[count++] = dev->page;
1031 }
1032 } else {
1033 xor_dest = sh->dev[pd_idx].page;
1034 for (i = disks; i--; ) {
1035 struct r5dev *dev = &sh->dev[i];
1036 if (i != pd_idx)
1037 xor_srcs[count++] = dev->page;
1038 }
1039 }
1040
Dan Williams91c00922007-01-02 13:52:30 -07001041 /* 1/ if we prexor'd then the dest is reused as a source
1042 * 2/ if we did not prexor then we are redoing the parity
1043 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1044 * for the synchronous xor case
1045 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001046 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001047 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1048
1049 atomic_inc(&sh->count);
1050
Dan Williamsac6b53b2009-07-14 13:40:19 -07001051 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001052 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001053 if (unlikely(count == 1))
1054 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1055 else
1056 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001057}
1058
Dan Williamsac6b53b2009-07-14 13:40:19 -07001059static void
1060ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1061 struct dma_async_tx_descriptor *tx)
1062{
1063 struct async_submit_ctl submit;
1064 struct page **blocks = percpu->scribble;
1065 int count;
1066
1067 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1068
1069 count = set_syndrome_sources(blocks, sh);
1070
1071 atomic_inc(&sh->count);
1072
1073 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1074 sh, to_addr_conv(sh, percpu));
1075 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001076}
1077
1078static void ops_complete_check(void *stripe_head_ref)
1079{
1080 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001081
Harvey Harrisone46b2722008-04-28 02:15:50 -07001082 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001083 (unsigned long long)sh->sector);
1084
Dan Williamsecc65c92008-06-28 08:31:57 +10001085 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001086 set_bit(STRIPE_HANDLE, &sh->state);
1087 release_stripe(sh);
1088}
1089
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001091{
Dan Williams91c00922007-01-02 13:52:30 -07001092 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001093 int pd_idx = sh->pd_idx;
1094 int qd_idx = sh->qd_idx;
1095 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001096 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001097 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001098 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001099 int count;
1100 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001101
Harvey Harrisone46b2722008-04-28 02:15:50 -07001102 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001103 (unsigned long long)sh->sector);
1104
Dan Williamsac6b53b2009-07-14 13:40:19 -07001105 count = 0;
1106 xor_dest = sh->dev[pd_idx].page;
1107 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001108 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001109 if (i == pd_idx || i == qd_idx)
1110 continue;
1111 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001112 }
1113
Dan Williamsd6f38f32009-07-14 11:50:52 -07001114 init_async_submit(&submit, 0, NULL, NULL, NULL,
1115 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001116 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001117 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001118
Dan Williams91c00922007-01-02 13:52:30 -07001119 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001120 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1121 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001122}
1123
Dan Williamsac6b53b2009-07-14 13:40:19 -07001124static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1125{
1126 struct page **srcs = percpu->scribble;
1127 struct async_submit_ctl submit;
1128 int count;
1129
1130 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1131 (unsigned long long)sh->sector, checkp);
1132
1133 count = set_syndrome_sources(srcs, sh);
1134 if (!checkp)
1135 srcs[count] = NULL;
1136
1137 atomic_inc(&sh->count);
1138 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1139 sh, to_addr_conv(sh, percpu));
1140 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1141 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1142}
1143
Dan Williams417b8d42009-10-16 16:25:22 +11001144static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001145{
1146 int overlap_clear = 0, i, disks = sh->disks;
1147 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001148 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001149 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001150 struct raid5_percpu *percpu;
1151 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001152
Dan Williamsd6f38f32009-07-14 11:50:52 -07001153 cpu = get_cpu();
1154 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001155 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001156 ops_run_biofill(sh);
1157 overlap_clear++;
1158 }
1159
Dan Williams7b3a8712008-06-28 08:32:09 +10001160 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001161 if (level < 6)
1162 tx = ops_run_compute5(sh, percpu);
1163 else {
1164 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1165 tx = ops_run_compute6_1(sh, percpu);
1166 else
1167 tx = ops_run_compute6_2(sh, percpu);
1168 }
1169 /* terminate the chain if reconstruct is not set to be run */
1170 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001171 async_tx_ack(tx);
1172 }
Dan Williams91c00922007-01-02 13:52:30 -07001173
Dan Williams600aa102008-06-28 08:32:05 +10001174 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001175 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001176
Dan Williams600aa102008-06-28 08:32:05 +10001177 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001178 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001179 overlap_clear++;
1180 }
1181
Dan Williamsac6b53b2009-07-14 13:40:19 -07001182 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1183 if (level < 6)
1184 ops_run_reconstruct5(sh, percpu, tx);
1185 else
1186 ops_run_reconstruct6(sh, percpu, tx);
1187 }
Dan Williams91c00922007-01-02 13:52:30 -07001188
Dan Williamsac6b53b2009-07-14 13:40:19 -07001189 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1190 if (sh->check_state == check_state_run)
1191 ops_run_check_p(sh, percpu);
1192 else if (sh->check_state == check_state_run_q)
1193 ops_run_check_pq(sh, percpu, 0);
1194 else if (sh->check_state == check_state_run_pq)
1195 ops_run_check_pq(sh, percpu, 1);
1196 else
1197 BUG();
1198 }
Dan Williams91c00922007-01-02 13:52:30 -07001199
Dan Williams91c00922007-01-02 13:52:30 -07001200 if (overlap_clear)
1201 for (i = disks; i--; ) {
1202 struct r5dev *dev = &sh->dev[i];
1203 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1204 wake_up(&sh->raid_conf->wait_for_overlap);
1205 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001206 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001207}
1208
Dan Williams417b8d42009-10-16 16:25:22 +11001209#ifdef CONFIG_MULTICORE_RAID456
1210static void async_run_ops(void *param, async_cookie_t cookie)
1211{
1212 struct stripe_head *sh = param;
1213 unsigned long ops_request = sh->ops.request;
1214
1215 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1216 wake_up(&sh->ops.wait_for_ops);
1217
1218 __raid_run_ops(sh, ops_request);
1219 release_stripe(sh);
1220}
1221
1222static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1223{
1224 /* since handle_stripe can be called outside of raid5d context
1225 * we need to ensure sh->ops.request is de-staged before another
1226 * request arrives
1227 */
1228 wait_event(sh->ops.wait_for_ops,
1229 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1230 sh->ops.request = ops_request;
1231
1232 atomic_inc(&sh->count);
1233 async_schedule(async_run_ops, sh);
1234}
1235#else
1236#define raid_run_ops __raid_run_ops
1237#endif
1238
NeilBrown3f294f42005-11-08 21:39:25 -08001239static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct stripe_head *sh;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001242 int disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown3f294f42005-11-08 21:39:25 -08001243 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1244 if (!sh)
1245 return 0;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001246 memset(sh, 0, sizeof(*sh) + (disks-1)*sizeof(struct r5dev));
NeilBrown3f294f42005-11-08 21:39:25 -08001247 sh->raid_conf = conf;
1248 spin_lock_init(&sh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001249 #ifdef CONFIG_MULTICORE_RAID456
1250 init_waitqueue_head(&sh->ops.wait_for_ops);
1251 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001252
NeilBrown5e5e3e72009-10-16 16:35:30 +11001253 if (grow_buffers(sh, disks)) {
1254 shrink_buffers(sh, disks);
NeilBrown3f294f42005-11-08 21:39:25 -08001255 kmem_cache_free(conf->slab_cache, sh);
1256 return 0;
1257 }
1258 /* we just created an active stripe so... */
1259 atomic_set(&sh->count, 1);
1260 atomic_inc(&conf->active_stripes);
1261 INIT_LIST_HEAD(&sh->lru);
1262 release_stripe(sh);
1263 return 1;
1264}
1265
1266static int grow_stripes(raid5_conf_t *conf, int num)
1267{
Christoph Lametere18b8902006-12-06 20:33:20 -08001268 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001269 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
NeilBrown245f46c2009-03-31 14:39:39 +11001271 sprintf(conf->cache_name[0],
1272 "raid%d-%s", conf->level, mdname(conf->mddev));
1273 sprintf(conf->cache_name[1],
1274 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001275 conf->active_name = 0;
1276 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001278 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!sc)
1280 return 1;
1281 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001282 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001283 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001284 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
NeilBrown29269552006-03-27 01:18:10 -08001288
Dan Williamsd6f38f32009-07-14 11:50:52 -07001289/**
1290 * scribble_len - return the required size of the scribble region
1291 * @num - total number of disks in the array
1292 *
1293 * The size must be enough to contain:
1294 * 1/ a struct page pointer for each device in the array +2
1295 * 2/ room to convert each entry in (1) to its corresponding dma
1296 * (dma_map_page()) or page (page_address()) address.
1297 *
1298 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1299 * calculate over all devices (not just the data blocks), using zeros in place
1300 * of the P and Q blocks.
1301 */
1302static size_t scribble_len(int num)
1303{
1304 size_t len;
1305
1306 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1307
1308 return len;
1309}
1310
NeilBrownad01c9e2006-03-27 01:18:07 -08001311static int resize_stripes(raid5_conf_t *conf, int newsize)
1312{
1313 /* Make all the stripes able to hold 'newsize' devices.
1314 * New slots in each stripe get 'page' set to a new page.
1315 *
1316 * This happens in stages:
1317 * 1/ create a new kmem_cache and allocate the required number of
1318 * stripe_heads.
1319 * 2/ gather all the old stripe_heads and tranfer the pages across
1320 * to the new stripe_heads. This will have the side effect of
1321 * freezing the array as once all stripe_heads have been collected,
1322 * no IO will be possible. Old stripe heads are freed once their
1323 * pages have been transferred over, and the old kmem_cache is
1324 * freed when all stripes are done.
1325 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1326 * we simple return a failre status - no need to clean anything up.
1327 * 4/ allocate new pages for the new slots in the new stripe_heads.
1328 * If this fails, we don't bother trying the shrink the
1329 * stripe_heads down again, we just leave them as they are.
1330 * As each stripe_head is processed the new one is released into
1331 * active service.
1332 *
1333 * Once step2 is started, we cannot afford to wait for a write,
1334 * so we use GFP_NOIO allocations.
1335 */
1336 struct stripe_head *osh, *nsh;
1337 LIST_HEAD(newstripes);
1338 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001339 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001340 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001341 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001342 int i;
1343
1344 if (newsize <= conf->pool_size)
1345 return 0; /* never bother to shrink */
1346
Dan Williamsb5470dc2008-06-27 21:44:04 -07001347 err = md_allow_write(conf->mddev);
1348 if (err)
1349 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001350
NeilBrownad01c9e2006-03-27 01:18:07 -08001351 /* Step 1 */
1352 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1353 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001354 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001355 if (!sc)
1356 return -ENOMEM;
1357
1358 for (i = conf->max_nr_stripes; i; i--) {
1359 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1360 if (!nsh)
1361 break;
1362
1363 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1364
1365 nsh->raid_conf = conf;
1366 spin_lock_init(&nsh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001367 #ifdef CONFIG_MULTICORE_RAID456
1368 init_waitqueue_head(&nsh->ops.wait_for_ops);
1369 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001370
1371 list_add(&nsh->lru, &newstripes);
1372 }
1373 if (i) {
1374 /* didn't get enough, give up */
1375 while (!list_empty(&newstripes)) {
1376 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1377 list_del(&nsh->lru);
1378 kmem_cache_free(sc, nsh);
1379 }
1380 kmem_cache_destroy(sc);
1381 return -ENOMEM;
1382 }
1383 /* Step 2 - Must use GFP_NOIO now.
1384 * OK, we have enough stripes, start collecting inactive
1385 * stripes and copying them over
1386 */
1387 list_for_each_entry(nsh, &newstripes, lru) {
1388 spin_lock_irq(&conf->device_lock);
1389 wait_event_lock_irq(conf->wait_for_stripe,
1390 !list_empty(&conf->inactive_list),
1391 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001392 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001393 );
1394 osh = get_free_stripe(conf);
1395 spin_unlock_irq(&conf->device_lock);
1396 atomic_set(&nsh->count, 1);
1397 for(i=0; i<conf->pool_size; i++)
1398 nsh->dev[i].page = osh->dev[i].page;
1399 for( ; i<newsize; i++)
1400 nsh->dev[i].page = NULL;
1401 kmem_cache_free(conf->slab_cache, osh);
1402 }
1403 kmem_cache_destroy(conf->slab_cache);
1404
1405 /* Step 3.
1406 * At this point, we are holding all the stripes so the array
1407 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001408 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001409 */
1410 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1411 if (ndisks) {
1412 for (i=0; i<conf->raid_disks; i++)
1413 ndisks[i] = conf->disks[i];
1414 kfree(conf->disks);
1415 conf->disks = ndisks;
1416 } else
1417 err = -ENOMEM;
1418
Dan Williamsd6f38f32009-07-14 11:50:52 -07001419 get_online_cpus();
1420 conf->scribble_len = scribble_len(newsize);
1421 for_each_present_cpu(cpu) {
1422 struct raid5_percpu *percpu;
1423 void *scribble;
1424
1425 percpu = per_cpu_ptr(conf->percpu, cpu);
1426 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1427
1428 if (scribble) {
1429 kfree(percpu->scribble);
1430 percpu->scribble = scribble;
1431 } else {
1432 err = -ENOMEM;
1433 break;
1434 }
1435 }
1436 put_online_cpus();
1437
NeilBrownad01c9e2006-03-27 01:18:07 -08001438 /* Step 4, return new stripes to service */
1439 while(!list_empty(&newstripes)) {
1440 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1441 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001442
NeilBrownad01c9e2006-03-27 01:18:07 -08001443 for (i=conf->raid_disks; i < newsize; i++)
1444 if (nsh->dev[i].page == NULL) {
1445 struct page *p = alloc_page(GFP_NOIO);
1446 nsh->dev[i].page = p;
1447 if (!p)
1448 err = -ENOMEM;
1449 }
1450 release_stripe(nsh);
1451 }
1452 /* critical section pass, GFP_NOIO no longer needed */
1453
1454 conf->slab_cache = sc;
1455 conf->active_name = 1-conf->active_name;
1456 conf->pool_size = newsize;
1457 return err;
1458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
NeilBrown3f294f42005-11-08 21:39:25 -08001460static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct stripe_head *sh;
1463
NeilBrown3f294f42005-11-08 21:39:25 -08001464 spin_lock_irq(&conf->device_lock);
1465 sh = get_free_stripe(conf);
1466 spin_unlock_irq(&conf->device_lock);
1467 if (!sh)
1468 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001469 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001470 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001471 kmem_cache_free(conf->slab_cache, sh);
1472 atomic_dec(&conf->active_stripes);
1473 return 1;
1474}
1475
1476static void shrink_stripes(raid5_conf_t *conf)
1477{
1478 while (drop_one_stripe(conf))
1479 ;
1480
NeilBrown29fc7e32006-02-03 03:03:41 -08001481 if (conf->slab_cache)
1482 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 conf->slab_cache = NULL;
1484}
1485
NeilBrown6712ecf2007-09-27 12:47:43 +02001486static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
NeilBrown99c0fb52009-03-31 14:39:38 +11001488 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001490 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001492 char b[BDEVNAME_SIZE];
1493 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 for (i=0 ; i<disks; i++)
1497 if (bi == &sh->dev[i].req)
1498 break;
1499
Dan Williams45b42332007-07-09 11:56:43 -07001500 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1501 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 uptodate);
1503 if (i == disks) {
1504 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001510 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001511 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001512 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1513 " (%lu sectors at %llu on %s)\n",
1514 mdname(conf->mddev), STRIPE_SECTORS,
1515 (unsigned long long)(sh->sector
1516 + rdev->data_offset),
1517 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001518 clear_bit(R5_ReadError, &sh->dev[i].flags);
1519 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1520 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001521 if (atomic_read(&conf->disks[i].rdev->read_errors))
1522 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001524 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001525 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001526 rdev = conf->disks[i].rdev;
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001529 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001530 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001531 printk_rl(KERN_WARNING
1532 "raid5:%s: read error not correctable "
1533 "(sector %llu on %s).\n",
1534 mdname(conf->mddev),
1535 (unsigned long long)(sh->sector
1536 + rdev->data_offset),
1537 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001538 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001539 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001540 printk_rl(KERN_WARNING
1541 "raid5:%s: read error NOT corrected!! "
1542 "(sector %llu on %s).\n",
1543 mdname(conf->mddev),
1544 (unsigned long long)(sh->sector
1545 + rdev->data_offset),
1546 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001547 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001548 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001549 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001550 "raid5:%s: Too many read errors, failing device %s.\n",
1551 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001552 else
1553 retry = 1;
1554 if (retry)
1555 set_bit(R5_ReadError, &sh->dev[i].flags);
1556 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001557 clear_bit(R5_ReadError, &sh->dev[i].flags);
1558 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001559 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1564 set_bit(STRIPE_HANDLE, &sh->state);
1565 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
NeilBrownd710e132008-10-13 11:55:12 +11001568static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
NeilBrown99c0fb52009-03-31 14:39:38 +11001570 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001572 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 for (i=0 ; i<disks; i++)
1576 if (bi == &sh->dev[i].req)
1577 break;
1578
Dan Williams45b42332007-07-09 11:56:43 -07001579 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1581 uptodate);
1582 if (i == disks) {
1583 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001584 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!uptodate)
1588 md_error(conf->mddev, conf->disks[i].rdev);
1589
1590 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1591
1592 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1593 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001594 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597
NeilBrown784052e2009-03-31 15:19:07 +11001598static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
NeilBrown784052e2009-03-31 15:19:07 +11001600static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct r5dev *dev = &sh->dev[i];
1603
1604 bio_init(&dev->req);
1605 dev->req.bi_io_vec = &dev->vec;
1606 dev->req.bi_vcnt++;
1607 dev->req.bi_max_vecs++;
1608 dev->vec.bv_page = dev->page;
1609 dev->vec.bv_len = STRIPE_SIZE;
1610 dev->vec.bv_offset = 0;
1611
1612 dev->req.bi_sector = sh->sector;
1613 dev->req.bi_private = sh;
1614
1615 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001616 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1620{
1621 char b[BDEVNAME_SIZE];
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001622 raid5_conf_t *conf = mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001623 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
NeilBrownb2d444d2005-11-08 21:39:31 -08001625 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001626 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001627 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1628 unsigned long flags;
1629 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001631 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * if recovery was running, make sure it aborts.
1634 */
NeilBrowndfc70642008-05-23 13:04:39 -07001635 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001637 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001638 printk(KERN_ALERT
1639 "raid5: Disk failure on %s, disabling device.\n"
1640 "raid5: Operation continuing on %d devices.\n",
1641 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001643}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645/*
1646 * Input: a 'big' sector number,
1647 * Output: index of the data and parity disk, and the sector # in them.
1648 */
NeilBrown112bf892009-03-31 14:39:38 +11001649static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001650 int previous, int *dd_idx,
1651 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 long stripe;
1654 unsigned long chunk_number;
1655 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001656 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001657 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001659 int algorithm = previous ? conf->prev_algo
1660 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001661 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1662 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001663 int raid_disks = previous ? conf->previous_raid_disks
1664 : conf->raid_disks;
1665 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* First compute the information on this sector */
1668
1669 /*
1670 * Compute the chunk number and the sector offset inside the chunk
1671 */
1672 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1673 chunk_number = r_sector;
1674 BUG_ON(r_sector != chunk_number);
1675
1676 /*
1677 * Compute the stripe number
1678 */
1679 stripe = chunk_number / data_disks;
1680
1681 /*
1682 * Compute the data disk and parity disk indexes inside the stripe
1683 */
1684 *dd_idx = chunk_number % data_disks;
1685
1686 /*
1687 * Select the parity disk based on the user selected algorithm.
1688 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001689 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001690 switch(conf->level) {
1691 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001692 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001693 break;
1694 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001695 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001697 pd_idx = data_disks - stripe % raid_disks;
1698 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 (*dd_idx)++;
1700 break;
1701 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001702 pd_idx = stripe % raid_disks;
1703 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 (*dd_idx)++;
1705 break;
1706 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001707 pd_idx = data_disks - stripe % raid_disks;
1708 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001711 pd_idx = stripe % raid_disks;
1712 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001714 case ALGORITHM_PARITY_0:
1715 pd_idx = 0;
1716 (*dd_idx)++;
1717 break;
1718 case ALGORITHM_PARITY_N:
1719 pd_idx = data_disks;
1720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001722 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001723 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001724 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001725 }
1726 break;
1727 case 6:
1728
NeilBrowne183eae2009-03-31 15:20:22 +11001729 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001730 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001731 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1732 qd_idx = pd_idx + 1;
1733 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001734 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001735 qd_idx = 0;
1736 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001737 (*dd_idx) += 2; /* D D P Q D */
1738 break;
1739 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001740 pd_idx = stripe % raid_disks;
1741 qd_idx = pd_idx + 1;
1742 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001743 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001744 qd_idx = 0;
1745 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001746 (*dd_idx) += 2; /* D D P Q D */
1747 break;
1748 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001749 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1750 qd_idx = (pd_idx + 1) % raid_disks;
1751 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001752 break;
1753 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001754 pd_idx = stripe % raid_disks;
1755 qd_idx = (pd_idx + 1) % raid_disks;
1756 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001757 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001758
1759 case ALGORITHM_PARITY_0:
1760 pd_idx = 0;
1761 qd_idx = 1;
1762 (*dd_idx) += 2;
1763 break;
1764 case ALGORITHM_PARITY_N:
1765 pd_idx = data_disks;
1766 qd_idx = data_disks + 1;
1767 break;
1768
1769 case ALGORITHM_ROTATING_ZERO_RESTART:
1770 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1771 * of blocks for computing Q is different.
1772 */
1773 pd_idx = stripe % raid_disks;
1774 qd_idx = pd_idx + 1;
1775 if (pd_idx == raid_disks-1) {
1776 (*dd_idx)++; /* Q D D D P */
1777 qd_idx = 0;
1778 } else if (*dd_idx >= pd_idx)
1779 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001780 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001781 break;
1782
1783 case ALGORITHM_ROTATING_N_RESTART:
1784 /* Same a left_asymmetric, by first stripe is
1785 * D D D P Q rather than
1786 * Q D D D P
1787 */
1788 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1789 qd_idx = pd_idx + 1;
1790 if (pd_idx == raid_disks-1) {
1791 (*dd_idx)++; /* Q D D D P */
1792 qd_idx = 0;
1793 } else if (*dd_idx >= pd_idx)
1794 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001795 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001796 break;
1797
1798 case ALGORITHM_ROTATING_N_CONTINUE:
1799 /* Same as left_symmetric but Q is before P */
1800 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1801 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1802 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001803 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001804 break;
1805
1806 case ALGORITHM_LEFT_ASYMMETRIC_6:
1807 /* RAID5 left_asymmetric, with Q on last device */
1808 pd_idx = data_disks - stripe % (raid_disks-1);
1809 if (*dd_idx >= pd_idx)
1810 (*dd_idx)++;
1811 qd_idx = raid_disks - 1;
1812 break;
1813
1814 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1815 pd_idx = stripe % (raid_disks-1);
1816 if (*dd_idx >= pd_idx)
1817 (*dd_idx)++;
1818 qd_idx = raid_disks - 1;
1819 break;
1820
1821 case ALGORITHM_LEFT_SYMMETRIC_6:
1822 pd_idx = data_disks - stripe % (raid_disks-1);
1823 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1824 qd_idx = raid_disks - 1;
1825 break;
1826
1827 case ALGORITHM_RIGHT_SYMMETRIC_6:
1828 pd_idx = stripe % (raid_disks-1);
1829 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1830 qd_idx = raid_disks - 1;
1831 break;
1832
1833 case ALGORITHM_PARITY_0_6:
1834 pd_idx = 0;
1835 (*dd_idx)++;
1836 qd_idx = raid_disks - 1;
1837 break;
1838
1839
NeilBrown16a53ec2006-06-26 00:27:38 -07001840 default:
NeilBrownd710e132008-10-13 11:55:12 +11001841 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001842 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001843 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001844 }
1845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
NeilBrown911d4ee2009-03-31 14:39:38 +11001848 if (sh) {
1849 sh->pd_idx = pd_idx;
1850 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001851 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
1854 * Finally, compute the new sector number
1855 */
1856 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1857 return new_sector;
1858}
1859
1860
NeilBrown784052e2009-03-31 15:19:07 +11001861static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001864 int raid_disks = sh->disks;
1865 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001867 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1868 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001869 int algorithm = previous ? conf->prev_algo
1870 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 sector_t stripe;
1872 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001873 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001875 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
NeilBrown16a53ec2006-06-26 00:27:38 -07001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1879 stripe = new_sector;
1880 BUG_ON(new_sector != stripe);
1881
NeilBrown16a53ec2006-06-26 00:27:38 -07001882 if (i == sh->pd_idx)
1883 return 0;
1884 switch(conf->level) {
1885 case 4: break;
1886 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001887 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 case ALGORITHM_LEFT_ASYMMETRIC:
1889 case ALGORITHM_RIGHT_ASYMMETRIC:
1890 if (i > sh->pd_idx)
1891 i--;
1892 break;
1893 case ALGORITHM_LEFT_SYMMETRIC:
1894 case ALGORITHM_RIGHT_SYMMETRIC:
1895 if (i < sh->pd_idx)
1896 i += raid_disks;
1897 i -= (sh->pd_idx + 1);
1898 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001899 case ALGORITHM_PARITY_0:
1900 i -= 1;
1901 break;
1902 case ALGORITHM_PARITY_N:
1903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001905 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001906 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001907 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001908 }
1909 break;
1910 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001911 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001913 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 case ALGORITHM_LEFT_ASYMMETRIC:
1915 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001916 case ALGORITHM_ROTATING_ZERO_RESTART:
1917 case ALGORITHM_ROTATING_N_RESTART:
1918 if (sh->pd_idx == raid_disks-1)
1919 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001920 else if (i > sh->pd_idx)
1921 i -= 2; /* D D P Q D */
1922 break;
1923 case ALGORITHM_LEFT_SYMMETRIC:
1924 case ALGORITHM_RIGHT_SYMMETRIC:
1925 if (sh->pd_idx == raid_disks-1)
1926 i--; /* Q D D D P */
1927 else {
1928 /* D D P Q D */
1929 if (i < sh->pd_idx)
1930 i += raid_disks;
1931 i -= (sh->pd_idx + 2);
1932 }
1933 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001934 case ALGORITHM_PARITY_0:
1935 i -= 2;
1936 break;
1937 case ALGORITHM_PARITY_N:
1938 break;
1939 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11001940 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11001941 if (sh->pd_idx == 0)
1942 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11001943 else {
1944 /* D D Q P D */
1945 if (i < sh->pd_idx)
1946 i += raid_disks;
1947 i -= (sh->pd_idx + 1);
1948 }
NeilBrown99c0fb52009-03-31 14:39:38 +11001949 break;
1950 case ALGORITHM_LEFT_ASYMMETRIC_6:
1951 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1952 if (i > sh->pd_idx)
1953 i--;
1954 break;
1955 case ALGORITHM_LEFT_SYMMETRIC_6:
1956 case ALGORITHM_RIGHT_SYMMETRIC_6:
1957 if (i < sh->pd_idx)
1958 i += data_disks + 1;
1959 i -= (sh->pd_idx + 1);
1960 break;
1961 case ALGORITHM_PARITY_0_6:
1962 i -= 1;
1963 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001964 default:
NeilBrownd710e132008-10-13 11:55:12 +11001965 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001966 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001967 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001968 }
1969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971
1972 chunk_number = stripe * data_disks + i;
1973 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1974
NeilBrown112bf892009-03-31 14:39:38 +11001975 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001976 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001977 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1978 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001979 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return 0;
1981 }
1982 return r_sector;
1983}
1984
1985
Dan Williams600aa102008-06-28 08:32:05 +10001986static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001987schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001988 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001989{
1990 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001991 raid5_conf_t *conf = sh->raid_conf;
1992 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07001993
Dan Williamse33129d2007-01-02 13:52:30 -07001994 if (rcw) {
1995 /* if we are not expanding this is a proper write request, and
1996 * there will be bios with new data to be drained into the
1997 * stripe cache
1998 */
1999 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002000 sh->reconstruct_state = reconstruct_state_drain_run;
2001 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2002 } else
2003 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002004
Dan Williamsac6b53b2009-07-14 13:40:19 -07002005 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002006
2007 for (i = disks; i--; ) {
2008 struct r5dev *dev = &sh->dev[i];
2009
2010 if (dev->towrite) {
2011 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002012 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002013 if (!expand)
2014 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002015 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002016 }
2017 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002018 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002019 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002020 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002021 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002022 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002023 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2024 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2025
Dan Williamsd8ee0722008-06-28 08:32:06 +10002026 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002027 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2028 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002029 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002030
2031 for (i = disks; i--; ) {
2032 struct r5dev *dev = &sh->dev[i];
2033 if (i == pd_idx)
2034 continue;
2035
Dan Williamse33129d2007-01-02 13:52:30 -07002036 if (dev->towrite &&
2037 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002038 test_bit(R5_Wantcompute, &dev->flags))) {
2039 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002040 set_bit(R5_LOCKED, &dev->flags);
2041 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002042 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002043 }
2044 }
2045 }
2046
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002047 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002048 * are in flight
2049 */
2050 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2051 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002052 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002053
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002054 if (level == 6) {
2055 int qd_idx = sh->qd_idx;
2056 struct r5dev *dev = &sh->dev[qd_idx];
2057
2058 set_bit(R5_LOCKED, &dev->flags);
2059 clear_bit(R5_UPTODATE, &dev->flags);
2060 s->locked++;
2061 }
2062
Dan Williams600aa102008-06-28 08:32:05 +10002063 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002064 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002065 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002066}
NeilBrown16a53ec2006-06-26 00:27:38 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068/*
2069 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002070 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * The bi_next chain must be in order.
2072 */
2073static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2074{
2075 struct bio **bip;
2076 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002077 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Dan Williams45b42332007-07-09 11:56:43 -07002079 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 (unsigned long long)bi->bi_sector,
2081 (unsigned long long)sh->sector);
2082
2083
2084 spin_lock(&sh->lock);
2085 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002086 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002088 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2089 firstwrite = 1;
2090 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 bip = &sh->dev[dd_idx].toread;
2092 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2093 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2094 goto overlap;
2095 bip = & (*bip)->bi_next;
2096 }
2097 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2098 goto overlap;
2099
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002100 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (*bip)
2102 bi->bi_next = *bip;
2103 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002104 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 spin_unlock_irq(&conf->device_lock);
2106 spin_unlock(&sh->lock);
2107
Dan Williams45b42332007-07-09 11:56:43 -07002108 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 (unsigned long long)bi->bi_sector,
2110 (unsigned long long)sh->sector, dd_idx);
2111
NeilBrown72626682005-09-09 16:23:54 -07002112 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002113 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2114 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002115 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002116 set_bit(STRIPE_BIT_DELAY, &sh->state);
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (forwrite) {
2120 /* check if page is covered */
2121 sector_t sector = sh->dev[dd_idx].sector;
2122 for (bi=sh->dev[dd_idx].towrite;
2123 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2124 bi && bi->bi_sector <= sector;
2125 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2126 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2127 sector = bi->bi_sector + (bi->bi_size>>9);
2128 }
2129 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2130 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2131 }
2132 return 1;
2133
2134 overlap:
2135 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2136 spin_unlock_irq(&conf->device_lock);
2137 spin_unlock(&sh->lock);
2138 return 0;
2139}
2140
NeilBrown29269552006-03-27 01:18:10 -08002141static void end_reshape(raid5_conf_t *conf);
2142
NeilBrown911d4ee2009-03-31 14:39:38 +11002143static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2144 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002145{
NeilBrown784052e2009-03-31 15:19:07 +11002146 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002147 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002148 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002149 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002150 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002151
NeilBrown112bf892009-03-31 14:39:38 +11002152 raid5_compute_sector(conf,
2153 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002154 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002155 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002156 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002157}
2158
Dan Williamsa4456852007-07-09 11:56:43 -07002159static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002160handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002161 struct stripe_head_state *s, int disks,
2162 struct bio **return_bi)
2163{
2164 int i;
2165 for (i = disks; i--; ) {
2166 struct bio *bi;
2167 int bitmap_end = 0;
2168
2169 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2170 mdk_rdev_t *rdev;
2171 rcu_read_lock();
2172 rdev = rcu_dereference(conf->disks[i].rdev);
2173 if (rdev && test_bit(In_sync, &rdev->flags))
2174 /* multiple read failures in one stripe */
2175 md_error(conf->mddev, rdev);
2176 rcu_read_unlock();
2177 }
2178 spin_lock_irq(&conf->device_lock);
2179 /* fail all writes first */
2180 bi = sh->dev[i].towrite;
2181 sh->dev[i].towrite = NULL;
2182 if (bi) {
2183 s->to_write--;
2184 bitmap_end = 1;
2185 }
2186
2187 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2188 wake_up(&conf->wait_for_overlap);
2189
2190 while (bi && bi->bi_sector <
2191 sh->dev[i].sector + STRIPE_SECTORS) {
2192 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2193 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002194 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002195 md_write_end(conf->mddev);
2196 bi->bi_next = *return_bi;
2197 *return_bi = bi;
2198 }
2199 bi = nextbi;
2200 }
2201 /* and fail all 'written' */
2202 bi = sh->dev[i].written;
2203 sh->dev[i].written = NULL;
2204 if (bi) bitmap_end = 1;
2205 while (bi && bi->bi_sector <
2206 sh->dev[i].sector + STRIPE_SECTORS) {
2207 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2208 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002209 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002210 md_write_end(conf->mddev);
2211 bi->bi_next = *return_bi;
2212 *return_bi = bi;
2213 }
2214 bi = bi2;
2215 }
2216
Dan Williamsb5e98d62007-01-02 13:52:31 -07002217 /* fail any reads if this device is non-operational and
2218 * the data has not reached the cache yet.
2219 */
2220 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2221 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2222 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002223 bi = sh->dev[i].toread;
2224 sh->dev[i].toread = NULL;
2225 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2226 wake_up(&conf->wait_for_overlap);
2227 if (bi) s->to_read--;
2228 while (bi && bi->bi_sector <
2229 sh->dev[i].sector + STRIPE_SECTORS) {
2230 struct bio *nextbi =
2231 r5_next_bio(bi, sh->dev[i].sector);
2232 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002233 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002234 bi->bi_next = *return_bi;
2235 *return_bi = bi;
2236 }
2237 bi = nextbi;
2238 }
2239 }
2240 spin_unlock_irq(&conf->device_lock);
2241 if (bitmap_end)
2242 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2243 STRIPE_SECTORS, 0, 0);
2244 }
2245
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002246 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2247 if (atomic_dec_and_test(&conf->pending_full_writes))
2248 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002249}
2250
Dan Williams1fe797e2008-06-28 09:16:30 +10002251/* fetch_block5 - checks the given member device to see if its data needs
2252 * to be read or computed to satisfy a request.
2253 *
2254 * Returns 1 when no more member devices need to be checked, otherwise returns
2255 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002256 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002257static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2258 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002259{
2260 struct r5dev *dev = &sh->dev[disk_idx];
2261 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2262
Dan Williamsf38e1212007-01-02 13:52:30 -07002263 /* is the data in this block needed, and can we get it? */
2264 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002265 !test_bit(R5_UPTODATE, &dev->flags) &&
2266 (dev->toread ||
2267 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2268 s->syncing || s->expanding ||
2269 (s->failed &&
2270 (failed_dev->toread ||
2271 (failed_dev->towrite &&
2272 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002273 /* We would like to get this block, possibly by computing it,
2274 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002275 */
2276 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002277 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002278 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2279 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002280 set_bit(R5_Wantcompute, &dev->flags);
2281 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002282 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002283 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002284 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002285 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002286 * before writes. R5_Wantcompute flags a block that will
2287 * be R5_UPTODATE by the time it is needed for a
2288 * subsequent operation.
2289 */
2290 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002291 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002292 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002293 set_bit(R5_LOCKED, &dev->flags);
2294 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002295 s->locked++;
2296 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2297 s->syncing);
2298 }
2299 }
2300
Dan Williams1fe797e2008-06-28 09:16:30 +10002301 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002302}
2303
Dan Williams1fe797e2008-06-28 09:16:30 +10002304/**
2305 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2306 */
2307static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002308 struct stripe_head_state *s, int disks)
2309{
2310 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002311
Dan Williamsf38e1212007-01-02 13:52:30 -07002312 /* look for blocks to read/compute, skip this if a compute
2313 * is already in flight, or if the stripe contents are in the
2314 * midst of changing due to a write
2315 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002316 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002317 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002318 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002319 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002320 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002321 set_bit(STRIPE_HANDLE, &sh->state);
2322}
2323
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002324/* fetch_block6 - checks the given member device to see if its data needs
2325 * to be read or computed to satisfy a request.
2326 *
2327 * Returns 1 when no more member devices need to be checked, otherwise returns
2328 * 0 to tell the loop in handle_stripe_fill6 to continue
2329 */
2330static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2331 struct r6_state *r6s, int disk_idx, int disks)
2332{
2333 struct r5dev *dev = &sh->dev[disk_idx];
2334 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2335 &sh->dev[r6s->failed_num[1]] };
2336
2337 if (!test_bit(R5_LOCKED, &dev->flags) &&
2338 !test_bit(R5_UPTODATE, &dev->flags) &&
2339 (dev->toread ||
2340 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2341 s->syncing || s->expanding ||
2342 (s->failed >= 1 &&
2343 (fdev[0]->toread || s->to_write)) ||
2344 (s->failed >= 2 &&
2345 (fdev[1]->toread || s->to_write)))) {
2346 /* we would like to get this block, possibly by computing it,
2347 * otherwise read it if the backing disk is insync
2348 */
2349 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2350 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2351 if ((s->uptodate == disks - 1) &&
2352 (s->failed && (disk_idx == r6s->failed_num[0] ||
2353 disk_idx == r6s->failed_num[1]))) {
2354 /* have disk failed, and we're requested to fetch it;
2355 * do compute it
2356 */
2357 pr_debug("Computing stripe %llu block %d\n",
2358 (unsigned long long)sh->sector, disk_idx);
2359 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2360 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2361 set_bit(R5_Wantcompute, &dev->flags);
2362 sh->ops.target = disk_idx;
2363 sh->ops.target2 = -1; /* no 2nd target */
2364 s->req_compute = 1;
2365 s->uptodate++;
2366 return 1;
2367 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2368 /* Computing 2-failure is *very* expensive; only
2369 * do it if failed >= 2
2370 */
2371 int other;
2372 for (other = disks; other--; ) {
2373 if (other == disk_idx)
2374 continue;
2375 if (!test_bit(R5_UPTODATE,
2376 &sh->dev[other].flags))
2377 break;
2378 }
2379 BUG_ON(other < 0);
2380 pr_debug("Computing stripe %llu blocks %d,%d\n",
2381 (unsigned long long)sh->sector,
2382 disk_idx, other);
2383 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2384 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2385 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2386 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2387 sh->ops.target = disk_idx;
2388 sh->ops.target2 = other;
2389 s->uptodate += 2;
2390 s->req_compute = 1;
2391 return 1;
2392 } else if (test_bit(R5_Insync, &dev->flags)) {
2393 set_bit(R5_LOCKED, &dev->flags);
2394 set_bit(R5_Wantread, &dev->flags);
2395 s->locked++;
2396 pr_debug("Reading block %d (sync=%d)\n",
2397 disk_idx, s->syncing);
2398 }
2399 }
2400
2401 return 0;
2402}
2403
2404/**
2405 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2406 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002407static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002408 struct stripe_head_state *s, struct r6_state *r6s,
2409 int disks)
2410{
2411 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002412
2413 /* look for blocks to read/compute, skip this if a compute
2414 * is already in flight, or if the stripe contents are in the
2415 * midst of changing due to a write
2416 */
2417 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2418 !sh->reconstruct_state)
2419 for (i = disks; i--; )
2420 if (fetch_block6(sh, s, r6s, i, disks))
2421 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002422 set_bit(STRIPE_HANDLE, &sh->state);
2423}
2424
2425
Dan Williams1fe797e2008-06-28 09:16:30 +10002426/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002427 * any written block on an uptodate or failed drive can be returned.
2428 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2429 * never LOCKED, so we don't need to test 'failed' directly.
2430 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002431static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002432 struct stripe_head *sh, int disks, struct bio **return_bi)
2433{
2434 int i;
2435 struct r5dev *dev;
2436
2437 for (i = disks; i--; )
2438 if (sh->dev[i].written) {
2439 dev = &sh->dev[i];
2440 if (!test_bit(R5_LOCKED, &dev->flags) &&
2441 test_bit(R5_UPTODATE, &dev->flags)) {
2442 /* We can return any write requests */
2443 struct bio *wbi, *wbi2;
2444 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002445 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002446 spin_lock_irq(&conf->device_lock);
2447 wbi = dev->written;
2448 dev->written = NULL;
2449 while (wbi && wbi->bi_sector <
2450 dev->sector + STRIPE_SECTORS) {
2451 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002452 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002453 md_write_end(conf->mddev);
2454 wbi->bi_next = *return_bi;
2455 *return_bi = wbi;
2456 }
2457 wbi = wbi2;
2458 }
2459 if (dev->towrite == NULL)
2460 bitmap_end = 1;
2461 spin_unlock_irq(&conf->device_lock);
2462 if (bitmap_end)
2463 bitmap_endwrite(conf->mddev->bitmap,
2464 sh->sector,
2465 STRIPE_SECTORS,
2466 !test_bit(STRIPE_DEGRADED, &sh->state),
2467 0);
2468 }
2469 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002470
2471 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2472 if (atomic_dec_and_test(&conf->pending_full_writes))
2473 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002474}
2475
Dan Williams1fe797e2008-06-28 09:16:30 +10002476static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002477 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2478{
2479 int rmw = 0, rcw = 0, i;
2480 for (i = disks; i--; ) {
2481 /* would I have to read this buffer for read_modify_write */
2482 struct r5dev *dev = &sh->dev[i];
2483 if ((dev->towrite || i == sh->pd_idx) &&
2484 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002485 !(test_bit(R5_UPTODATE, &dev->flags) ||
2486 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002487 if (test_bit(R5_Insync, &dev->flags))
2488 rmw++;
2489 else
2490 rmw += 2*disks; /* cannot read it */
2491 }
2492 /* Would I have to read this buffer for reconstruct_write */
2493 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2494 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002495 !(test_bit(R5_UPTODATE, &dev->flags) ||
2496 test_bit(R5_Wantcompute, &dev->flags))) {
2497 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002498 else
2499 rcw += 2*disks;
2500 }
2501 }
Dan Williams45b42332007-07-09 11:56:43 -07002502 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002503 (unsigned long long)sh->sector, rmw, rcw);
2504 set_bit(STRIPE_HANDLE, &sh->state);
2505 if (rmw < rcw && rmw > 0)
2506 /* prefer read-modify-write, but need to get some data */
2507 for (i = disks; i--; ) {
2508 struct r5dev *dev = &sh->dev[i];
2509 if ((dev->towrite || i == sh->pd_idx) &&
2510 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002511 !(test_bit(R5_UPTODATE, &dev->flags) ||
2512 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002513 test_bit(R5_Insync, &dev->flags)) {
2514 if (
2515 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002516 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002517 "%d for r-m-w\n", i);
2518 set_bit(R5_LOCKED, &dev->flags);
2519 set_bit(R5_Wantread, &dev->flags);
2520 s->locked++;
2521 } else {
2522 set_bit(STRIPE_DELAYED, &sh->state);
2523 set_bit(STRIPE_HANDLE, &sh->state);
2524 }
2525 }
2526 }
2527 if (rcw <= rmw && rcw > 0)
2528 /* want reconstruct write, but need to get some data */
2529 for (i = disks; i--; ) {
2530 struct r5dev *dev = &sh->dev[i];
2531 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2532 i != sh->pd_idx &&
2533 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002534 !(test_bit(R5_UPTODATE, &dev->flags) ||
2535 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002536 test_bit(R5_Insync, &dev->flags)) {
2537 if (
2538 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002539 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002540 "%d for Reconstruct\n", i);
2541 set_bit(R5_LOCKED, &dev->flags);
2542 set_bit(R5_Wantread, &dev->flags);
2543 s->locked++;
2544 } else {
2545 set_bit(STRIPE_DELAYED, &sh->state);
2546 set_bit(STRIPE_HANDLE, &sh->state);
2547 }
2548 }
2549 }
2550 /* now if nothing is locked, and if we have enough data,
2551 * we can start a write request
2552 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002553 /* since handle_stripe can be called at any time we need to handle the
2554 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002555 * subsequent call wants to start a write request. raid_run_ops only
2556 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002557 * simultaneously. If this is not the case then new writes need to be
2558 * held off until the compute completes.
2559 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002560 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2561 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2562 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002563 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002564}
2565
Dan Williams1fe797e2008-06-28 09:16:30 +10002566static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002567 struct stripe_head *sh, struct stripe_head_state *s,
2568 struct r6_state *r6s, int disks)
2569{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002570 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002571 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002572
2573 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002574 for (i = disks; i--; ) {
2575 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002576 /* check if we haven't enough data */
2577 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2578 i != pd_idx && i != qd_idx &&
2579 !test_bit(R5_LOCKED, &dev->flags) &&
2580 !(test_bit(R5_UPTODATE, &dev->flags) ||
2581 test_bit(R5_Wantcompute, &dev->flags))) {
2582 rcw++;
2583 if (!test_bit(R5_Insync, &dev->flags))
2584 continue; /* it's a failed drive */
2585
2586 if (
2587 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2588 pr_debug("Read_old stripe %llu "
2589 "block %d for Reconstruct\n",
2590 (unsigned long long)sh->sector, i);
2591 set_bit(R5_LOCKED, &dev->flags);
2592 set_bit(R5_Wantread, &dev->flags);
2593 s->locked++;
2594 } else {
2595 pr_debug("Request delayed stripe %llu "
2596 "block %d for Reconstruct\n",
2597 (unsigned long long)sh->sector, i);
2598 set_bit(STRIPE_DELAYED, &sh->state);
2599 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002600 }
2601 }
2602 }
Dan Williamsa4456852007-07-09 11:56:43 -07002603 /* now if nothing is locked, and if we have enough data, we can start a
2604 * write request
2605 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002606 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2607 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002608 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002609 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002610 }
2611}
2612
2613static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2614 struct stripe_head_state *s, int disks)
2615{
Dan Williamsecc65c92008-06-28 08:31:57 +10002616 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002617
Dan Williamsbd2ab672008-04-10 21:29:27 -07002618 set_bit(STRIPE_HANDLE, &sh->state);
2619
Dan Williamsecc65c92008-06-28 08:31:57 +10002620 switch (sh->check_state) {
2621 case check_state_idle:
2622 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002623 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002624 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002625 sh->check_state = check_state_run;
2626 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002627 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002628 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002629 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002630 }
Dan Williamsa4456852007-07-09 11:56:43 -07002631 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002632 /* fall through */
2633 case check_state_compute_result:
2634 sh->check_state = check_state_idle;
2635 if (!dev)
2636 dev = &sh->dev[sh->pd_idx];
2637
2638 /* check that a write has not made the stripe insync */
2639 if (test_bit(STRIPE_INSYNC, &sh->state))
2640 break;
2641
2642 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002643 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2644 BUG_ON(s->uptodate != disks);
2645
2646 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002647 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002648 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002649
Dan Williamsa4456852007-07-09 11:56:43 -07002650 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002651 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002652 break;
2653 case check_state_run:
2654 break; /* we will be called again upon completion */
2655 case check_state_check_result:
2656 sh->check_state = check_state_idle;
2657
2658 /* if a failure occurred during the check operation, leave
2659 * STRIPE_INSYNC not set and let the stripe be handled again
2660 */
2661 if (s->failed)
2662 break;
2663
2664 /* handle a successful check operation, if parity is correct
2665 * we are done. Otherwise update the mismatch count and repair
2666 * parity if !MD_RECOVERY_CHECK
2667 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002668 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002669 /* parity is correct (on disc,
2670 * not in buffer any more)
2671 */
2672 set_bit(STRIPE_INSYNC, &sh->state);
2673 else {
2674 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2675 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2676 /* don't try to repair!! */
2677 set_bit(STRIPE_INSYNC, &sh->state);
2678 else {
2679 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002680 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002681 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2682 set_bit(R5_Wantcompute,
2683 &sh->dev[sh->pd_idx].flags);
2684 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002685 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002686 s->uptodate++;
2687 }
2688 }
2689 break;
2690 case check_state_compute_run:
2691 break;
2692 default:
2693 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2694 __func__, sh->check_state,
2695 (unsigned long long) sh->sector);
2696 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002697 }
2698}
2699
2700
2701static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002702 struct stripe_head_state *s,
2703 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002704{
Dan Williamsa4456852007-07-09 11:56:43 -07002705 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002706 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002707 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002708
2709 set_bit(STRIPE_HANDLE, &sh->state);
2710
2711 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002712
Dan Williamsa4456852007-07-09 11:56:43 -07002713 /* Want to check and possibly repair P and Q.
2714 * However there could be one 'failed' device, in which
2715 * case we can only check one of them, possibly using the
2716 * other to generate missing data
2717 */
2718
Dan Williamsd82dfee2009-07-14 13:40:57 -07002719 switch (sh->check_state) {
2720 case check_state_idle:
2721 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002722 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002723 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002724 * makes sense to check P (If anything else were failed,
2725 * we would have used P to recreate it).
2726 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002727 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002728 }
2729 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002730 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002731 * anything, so it makes sense to check it
2732 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002733 if (sh->check_state == check_state_run)
2734 sh->check_state = check_state_run_pq;
2735 else
2736 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002737 }
Dan Williams36d1c642009-07-14 11:48:22 -07002738
Dan Williamsd82dfee2009-07-14 13:40:57 -07002739 /* discard potentially stale zero_sum_result */
2740 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002741
Dan Williamsd82dfee2009-07-14 13:40:57 -07002742 if (sh->check_state == check_state_run) {
2743 /* async_xor_zero_sum destroys the contents of P */
2744 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2745 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002746 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002747 if (sh->check_state >= check_state_run &&
2748 sh->check_state <= check_state_run_pq) {
2749 /* async_syndrome_zero_sum preserves P and Q, so
2750 * no need to mark them !uptodate here
2751 */
2752 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2753 break;
2754 }
Dan Williams36d1c642009-07-14 11:48:22 -07002755
Dan Williamsd82dfee2009-07-14 13:40:57 -07002756 /* we have 2-disk failure */
2757 BUG_ON(s->failed != 2);
2758 /* fall through */
2759 case check_state_compute_result:
2760 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002761
Dan Williamsd82dfee2009-07-14 13:40:57 -07002762 /* check that a write has not made the stripe insync */
2763 if (test_bit(STRIPE_INSYNC, &sh->state))
2764 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002765
2766 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002767 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002768 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002769 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002770 if (s->failed == 2) {
2771 dev = &sh->dev[r6s->failed_num[1]];
2772 s->locked++;
2773 set_bit(R5_LOCKED, &dev->flags);
2774 set_bit(R5_Wantwrite, &dev->flags);
2775 }
2776 if (s->failed >= 1) {
2777 dev = &sh->dev[r6s->failed_num[0]];
2778 s->locked++;
2779 set_bit(R5_LOCKED, &dev->flags);
2780 set_bit(R5_Wantwrite, &dev->flags);
2781 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002782 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002783 dev = &sh->dev[pd_idx];
2784 s->locked++;
2785 set_bit(R5_LOCKED, &dev->flags);
2786 set_bit(R5_Wantwrite, &dev->flags);
2787 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002788 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002789 dev = &sh->dev[qd_idx];
2790 s->locked++;
2791 set_bit(R5_LOCKED, &dev->flags);
2792 set_bit(R5_Wantwrite, &dev->flags);
2793 }
2794 clear_bit(STRIPE_DEGRADED, &sh->state);
2795
2796 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002797 break;
2798 case check_state_run:
2799 case check_state_run_q:
2800 case check_state_run_pq:
2801 break; /* we will be called again upon completion */
2802 case check_state_check_result:
2803 sh->check_state = check_state_idle;
2804
2805 /* handle a successful check operation, if parity is correct
2806 * we are done. Otherwise update the mismatch count and repair
2807 * parity if !MD_RECOVERY_CHECK
2808 */
2809 if (sh->ops.zero_sum_result == 0) {
2810 /* both parities are correct */
2811 if (!s->failed)
2812 set_bit(STRIPE_INSYNC, &sh->state);
2813 else {
2814 /* in contrast to the raid5 case we can validate
2815 * parity, but still have a failure to write
2816 * back
2817 */
2818 sh->check_state = check_state_compute_result;
2819 /* Returning at this point means that we may go
2820 * off and bring p and/or q uptodate again so
2821 * we make sure to check zero_sum_result again
2822 * to verify if p or q need writeback
2823 */
2824 }
2825 } else {
2826 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2827 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2828 /* don't try to repair!! */
2829 set_bit(STRIPE_INSYNC, &sh->state);
2830 else {
2831 int *target = &sh->ops.target;
2832
2833 sh->ops.target = -1;
2834 sh->ops.target2 = -1;
2835 sh->check_state = check_state_compute_run;
2836 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2837 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2838 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2839 set_bit(R5_Wantcompute,
2840 &sh->dev[pd_idx].flags);
2841 *target = pd_idx;
2842 target = &sh->ops.target2;
2843 s->uptodate++;
2844 }
2845 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2846 set_bit(R5_Wantcompute,
2847 &sh->dev[qd_idx].flags);
2848 *target = qd_idx;
2849 s->uptodate++;
2850 }
2851 }
2852 }
2853 break;
2854 case check_state_compute_run:
2855 break;
2856 default:
2857 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2858 __func__, sh->check_state,
2859 (unsigned long long) sh->sector);
2860 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002861 }
2862}
2863
2864static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2865 struct r6_state *r6s)
2866{
2867 int i;
2868
2869 /* We have read all the blocks in this stripe and now we need to
2870 * copy some of them into a target stripe for expand.
2871 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002872 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002873 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2874 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002875 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002876 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002877 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002878 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002879
NeilBrown784052e2009-03-31 15:19:07 +11002880 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002881 sector_t s = raid5_compute_sector(conf, bn, 0,
2882 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002883 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002884 if (sh2 == NULL)
2885 /* so far only the early blocks of this stripe
2886 * have been requested. When later blocks
2887 * get requested, we will try again
2888 */
2889 continue;
2890 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2891 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2892 /* must have already done this block */
2893 release_stripe(sh2);
2894 continue;
2895 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002896
2897 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002898 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002899 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002900 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002901 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002902
Dan Williamsa4456852007-07-09 11:56:43 -07002903 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2904 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2905 for (j = 0; j < conf->raid_disks; j++)
2906 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002907 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002908 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2909 break;
2910 if (j == conf->raid_disks) {
2911 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2912 set_bit(STRIPE_HANDLE, &sh2->state);
2913 }
2914 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002915
Dan Williamsa4456852007-07-09 11:56:43 -07002916 }
NeilBrowna2e08552007-09-11 15:23:36 -07002917 /* done submitting copies, wait for them to complete */
2918 if (tx) {
2919 async_tx_ack(tx);
2920 dma_wait_for_async_tx(tx);
2921 }
Dan Williamsa4456852007-07-09 11:56:43 -07002922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Dan Williams6bfe0b42008-04-30 00:52:32 -07002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925/*
2926 * handle_stripe - do things to a stripe.
2927 *
2928 * We lock the stripe and then examine the state of various bits
2929 * to see what needs to be done.
2930 * Possible results:
2931 * return some read request which now have data
2932 * return some write requests which are safely on disc
2933 * schedule a read on some buffers
2934 * schedule a write of some buffers
2935 * return confirmation of parity correctness
2936 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 * buffers are taken off read_list or write_list, and bh_cache buffers
2938 * get BH_Lock set before the stripe lock is released.
2939 *
2940 */
Dan Williamsa4456852007-07-09 11:56:43 -07002941
NeilBrown14425772009-10-16 15:55:25 +11002942static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002945 int disks = sh->disks, i;
2946 struct bio *return_bi = NULL;
2947 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002949 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002950 int prexor;
NeilBrown729a1862009-12-14 12:49:50 +11002951 int dec_preread_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Dan Williamsa4456852007-07-09 11:56:43 -07002953 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002954 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2955 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2956 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2957 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 spin_lock(&sh->lock);
2960 clear_bit(STRIPE_HANDLE, &sh->state);
2961 clear_bit(STRIPE_DELAYED, &sh->state);
2962
Dan Williamsa4456852007-07-09 11:56:43 -07002963 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2964 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2965 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002968 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 for (i=disks; i--; ) {
2970 mdk_rdev_t *rdev;
NeilBrowna9f326e2009-09-23 18:06:41 +10002971
2972 dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Dan Williamsb5e98d62007-01-02 13:52:31 -07002975 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2976 "written %p\n", i, dev->flags, dev->toread, dev->read,
2977 dev->towrite, dev->written);
2978
2979 /* maybe we can request a biofill operation
2980 *
2981 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002982 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002983 */
2984 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002985 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002986 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002989 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2990 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002991 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Dan Williamsb5e98d62007-01-02 13:52:31 -07002993 if (test_bit(R5_Wantfill, &dev->flags))
2994 s.to_fill++;
2995 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002996 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002998 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003000 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
Dan Williamsa4456852007-07-09 11:56:43 -07003002 if (dev->written)
3003 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08003004 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003005 if (blocked_rdev == NULL &&
3006 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003007 blocked_rdev = rdev;
3008 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003009 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003010 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08003011 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08003012 clear_bit(R5_ReadError, &dev->flags);
3013 clear_bit(R5_ReWrite, &dev->flags);
3014 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003015 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003016 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003017 s.failed++;
3018 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 } else
3020 set_bit(R5_Insync, &dev->flags);
3021 }
NeilBrown9910f162006-01-06 00:20:24 -08003022 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07003023
Dan Williams6bfe0b42008-04-30 00:52:32 -07003024 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003025 if (s.syncing || s.expanding || s.expanded ||
3026 s.to_write || s.written) {
3027 set_bit(STRIPE_HANDLE, &sh->state);
3028 goto unlock;
3029 }
3030 /* There is nothing for the blocked_rdev to block */
3031 rdev_dec_pending(blocked_rdev, conf->mddev);
3032 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003033 }
3034
Dan Williams83de75c2008-06-28 08:31:58 +10003035 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3036 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3037 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3038 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07003039
Dan Williams45b42332007-07-09 11:56:43 -07003040 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003042 s.locked, s.uptodate, s.to_read, s.to_write,
3043 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* check if the array has lost two devices and, if so, some requests might
3045 * need to be failed
3046 */
Dan Williamsa4456852007-07-09 11:56:43 -07003047 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003048 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003049 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3051 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003052 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054
3055 /* might be able to return some write requests if the parity block
3056 * is safe, or on a failed drive
3057 */
3058 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003059 if ( s.written &&
3060 ((test_bit(R5_Insync, &dev->flags) &&
3061 !test_bit(R5_LOCKED, &dev->flags) &&
3062 test_bit(R5_UPTODATE, &dev->flags)) ||
3063 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003064 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /* Now we might consider reading some blocks, either to check/generate
3067 * parity, or to satisfy requests
3068 * or to load a block that is being partially written.
3069 */
Dan Williamsa4456852007-07-09 11:56:43 -07003070 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003071 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003072 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Dan Williamse33129d2007-01-02 13:52:30 -07003074 /* Now we check to see if any write operations have recently
3075 * completed
3076 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003077 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003078 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003079 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003080 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3081 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003082 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003083
3084 /* All the 'written' buffers and the parity block are ready to
3085 * be written back to disk
3086 */
3087 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3088 for (i = disks; i--; ) {
3089 dev = &sh->dev[i];
3090 if (test_bit(R5_LOCKED, &dev->flags) &&
3091 (i == sh->pd_idx || dev->written)) {
3092 pr_debug("Writing block %d\n", i);
3093 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003094 if (prexor)
3095 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003096 if (!test_bit(R5_Insync, &dev->flags) ||
3097 (i == sh->pd_idx && s.failed == 0))
3098 set_bit(STRIPE_INSYNC, &sh->state);
3099 }
3100 }
NeilBrown729a1862009-12-14 12:49:50 +11003101 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3102 dec_preread_active = 1;
Dan Williamse33129d2007-01-02 13:52:30 -07003103 }
3104
3105 /* Now to consider new write requests and what else, if anything
3106 * should be read. We do not handle new writes when:
3107 * 1/ A 'write' operation (copy+xor) is already in flight.
3108 * 2/ A 'check' operation is in flight, as it may clobber the parity
3109 * block.
3110 */
Dan Williams600aa102008-06-28 08:32:05 +10003111 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003112 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003115 * Any reads will already have been scheduled, so we just see if enough
3116 * data is available. The parity check is held off while parity
3117 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003119 if (sh->check_state ||
3120 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003121 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003122 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003123 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003124
Dan Williamsa4456852007-07-09 11:56:43 -07003125 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3127 clear_bit(STRIPE_SYNCING, &sh->state);
3128 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003129
3130 /* If the failed drive is just a ReadError, then we might need to progress
3131 * the repair/check process
3132 */
Dan Williamsa4456852007-07-09 11:56:43 -07003133 if (s.failed == 1 && !conf->mddev->ro &&
3134 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3135 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3136 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003137 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003138 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003139 if (!test_bit(R5_ReWrite, &dev->flags)) {
3140 set_bit(R5_Wantwrite, &dev->flags);
3141 set_bit(R5_ReWrite, &dev->flags);
3142 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003143 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003144 } else {
3145 /* let's read it back */
3146 set_bit(R5_Wantread, &dev->flags);
3147 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003148 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003149 }
3150 }
3151
Dan Williams600aa102008-06-28 08:32:05 +10003152 /* Finish reconstruct operations initiated by the expansion process */
3153 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003154 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003155 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003156 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3157 /* sh cannot be written until sh2 has been read.
3158 * so arrange for sh to be delayed a little
3159 */
3160 set_bit(STRIPE_DELAYED, &sh->state);
3161 set_bit(STRIPE_HANDLE, &sh->state);
3162 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3163 &sh2->state))
3164 atomic_inc(&conf->preread_active_stripes);
3165 release_stripe(sh2);
3166 goto unlock;
3167 }
3168 if (sh2)
3169 release_stripe(sh2);
3170
Dan Williams600aa102008-06-28 08:32:05 +10003171 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003172 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003173 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003174 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003175 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003176 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003177 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003178 }
3179
3180 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003181 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003182 /* Need to write out all blocks after computing parity */
3183 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003184 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003185 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003186 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003187 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003188 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003189 wake_up(&conf->wait_for_overlap);
3190 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3191 }
3192
Dan Williams0f94e872008-01-08 15:32:53 -08003193 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003194 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003195 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003196
Dan Williams6bfe0b42008-04-30 00:52:32 -07003197 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 spin_unlock(&sh->lock);
3199
Dan Williams6bfe0b42008-04-30 00:52:32 -07003200 /* wait for this device to become unblocked */
3201 if (unlikely(blocked_rdev))
3202 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3203
Dan Williams600aa102008-06-28 08:32:05 +10003204 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003205 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003206
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003207 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
NeilBrown729a1862009-12-14 12:49:50 +11003209 if (dec_preread_active) {
3210 /* We delay this until after ops_run_io so that if make_request
3211 * is waiting on a barrier, it won't continue until the writes
3212 * have actually been submitted.
3213 */
3214 atomic_dec(&conf->preread_active_stripes);
3215 if (atomic_read(&conf->preread_active_stripes) <
3216 IO_THRESHOLD)
3217 md_wakeup_thread(conf->mddev->thread);
3218 }
Dan Williamsa4456852007-07-09 11:56:43 -07003219 return_io(return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
NeilBrown14425772009-10-16 15:55:25 +11003222static void handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003223{
NeilBrownbff61972009-03-31 14:33:13 +11003224 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003225 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003226 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003227 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003228 struct stripe_head_state s;
3229 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003230 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003231 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown729a1862009-12-14 12:49:50 +11003232 int dec_preread_active = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003233
Dan Williams45b42332007-07-09 11:56:43 -07003234 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003235 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003236 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003237 atomic_read(&sh->count), pd_idx, qd_idx,
3238 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003239 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003240
3241 spin_lock(&sh->lock);
3242 clear_bit(STRIPE_HANDLE, &sh->state);
3243 clear_bit(STRIPE_DELAYED, &sh->state);
3244
Dan Williamsa4456852007-07-09 11:56:43 -07003245 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3246 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3247 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 /* Now to look around and see what can be done */
3249
3250 rcu_read_lock();
3251 for (i=disks; i--; ) {
3252 mdk_rdev_t *rdev;
3253 dev = &sh->dev[i];
3254 clear_bit(R5_Insync, &dev->flags);
3255
Dan Williams45b42332007-07-09 11:56:43 -07003256 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003257 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003258 /* maybe we can reply to a read
3259 *
3260 * new wantfill requests are only permitted while
3261 * ops_complete_biofill is guaranteed to be inactive
3262 */
3263 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3264 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3265 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003266
3267 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003268 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3269 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003270 if (test_bit(R5_Wantcompute, &dev->flags)) {
3271 s.compute++;
3272 BUG_ON(s.compute > 2);
3273 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003274
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003275 if (test_bit(R5_Wantfill, &dev->flags)) {
3276 s.to_fill++;
3277 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003278 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003279 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003280 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003281 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003282 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 }
Dan Williamsa4456852007-07-09 11:56:43 -07003284 if (dev->written)
3285 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003286 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003287 if (blocked_rdev == NULL &&
3288 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003289 blocked_rdev = rdev;
3290 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003291 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003292 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3293 /* The ReadError flag will just be confusing now */
3294 clear_bit(R5_ReadError, &dev->flags);
3295 clear_bit(R5_ReWrite, &dev->flags);
3296 }
3297 if (!rdev || !test_bit(In_sync, &rdev->flags)
3298 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003299 if (s.failed < 2)
3300 r6s.failed_num[s.failed] = i;
3301 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 } else
3303 set_bit(R5_Insync, &dev->flags);
3304 }
3305 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003306
3307 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003308 if (s.syncing || s.expanding || s.expanded ||
3309 s.to_write || s.written) {
3310 set_bit(STRIPE_HANDLE, &sh->state);
3311 goto unlock;
3312 }
3313 /* There is nothing for the blocked_rdev to block */
3314 rdev_dec_pending(blocked_rdev, conf->mddev);
3315 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003316 }
NeilBrownac4090d2008-08-05 15:54:13 +10003317
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003318 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3319 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3320 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3321 }
3322
Dan Williams45b42332007-07-09 11:56:43 -07003323 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003324 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003325 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3326 r6s.failed_num[0], r6s.failed_num[1]);
3327 /* check if the array has lost >2 devices and, if so, some requests
3328 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003329 */
Dan Williamsa4456852007-07-09 11:56:43 -07003330 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003331 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003332 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003333 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3334 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003335 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003336 }
3337
3338 /*
3339 * might be able to return some write requests if the parity blocks
3340 * are safe, or on a failed drive
3341 */
3342 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003343 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3344 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003345 qdev = &sh->dev[qd_idx];
3346 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3347 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003348
Dan Williamsa4456852007-07-09 11:56:43 -07003349 if ( s.written &&
3350 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003351 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003352 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3353 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003354 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003355 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003356 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003357
3358 /* Now we might consider reading some blocks, either to check/generate
3359 * parity, or to satisfy requests
3360 * or to load a block that is being partially written.
3361 */
Dan Williamsa4456852007-07-09 11:56:43 -07003362 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003363 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003364 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003365
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003366 /* Now we check to see if any write operations have recently
3367 * completed
3368 */
3369 if (sh->reconstruct_state == reconstruct_state_drain_result) {
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003370
3371 sh->reconstruct_state = reconstruct_state_idle;
3372 /* All the 'written' buffers and the parity blocks are ready to
3373 * be written back to disk
3374 */
3375 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3376 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3377 for (i = disks; i--; ) {
3378 dev = &sh->dev[i];
3379 if (test_bit(R5_LOCKED, &dev->flags) &&
3380 (i == sh->pd_idx || i == qd_idx ||
3381 dev->written)) {
3382 pr_debug("Writing block %d\n", i);
3383 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3384 set_bit(R5_Wantwrite, &dev->flags);
3385 if (!test_bit(R5_Insync, &dev->flags) ||
3386 ((i == sh->pd_idx || i == qd_idx) &&
3387 s.failed == 0))
3388 set_bit(STRIPE_INSYNC, &sh->state);
3389 }
3390 }
NeilBrown729a1862009-12-14 12:49:50 +11003391 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3392 dec_preread_active = 1;
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003393 }
3394
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003395 /* Now to consider new write requests and what else, if anything
3396 * should be read. We do not handle new writes when:
3397 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3398 * 2/ A 'check' operation is in flight, as it may clobber the parity
3399 * block.
3400 */
3401 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003402 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003403
3404 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003405 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003406 * data is available. The parity check is held off while parity
3407 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003408 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003409 if (sh->check_state ||
3410 (s.syncing && s.locked == 0 &&
3411 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3412 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003413 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003414
Dan Williamsa4456852007-07-09 11:56:43 -07003415 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003416 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3417 clear_bit(STRIPE_SYNCING, &sh->state);
3418 }
3419
3420 /* If the failed drives are just a ReadError, then we might need
3421 * to progress the repair/check process
3422 */
Dan Williamsa4456852007-07-09 11:56:43 -07003423 if (s.failed <= 2 && !conf->mddev->ro)
3424 for (i = 0; i < s.failed; i++) {
3425 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003426 if (test_bit(R5_ReadError, &dev->flags)
3427 && !test_bit(R5_LOCKED, &dev->flags)
3428 && test_bit(R5_UPTODATE, &dev->flags)
3429 ) {
3430 if (!test_bit(R5_ReWrite, &dev->flags)) {
3431 set_bit(R5_Wantwrite, &dev->flags);
3432 set_bit(R5_ReWrite, &dev->flags);
3433 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003434 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003435 } else {
3436 /* let's read it back */
3437 set_bit(R5_Wantread, &dev->flags);
3438 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003439 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003440 }
3441 }
3442 }
NeilBrownf4168852007-02-28 20:11:53 -08003443
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003444 /* Finish reconstruct operations initiated by the expansion process */
3445 if (sh->reconstruct_state == reconstruct_state_result) {
3446 sh->reconstruct_state = reconstruct_state_idle;
3447 clear_bit(STRIPE_EXPANDING, &sh->state);
3448 for (i = conf->raid_disks; i--; ) {
3449 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3450 set_bit(R5_LOCKED, &sh->dev[i].flags);
3451 s.locked++;
3452 }
3453 }
3454
3455 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3456 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003457 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003458 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003459 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3460 /* sh cannot be written until sh2 has been read.
3461 * so arrange for sh to be delayed a little
3462 */
3463 set_bit(STRIPE_DELAYED, &sh->state);
3464 set_bit(STRIPE_HANDLE, &sh->state);
3465 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3466 &sh2->state))
3467 atomic_inc(&conf->preread_active_stripes);
3468 release_stripe(sh2);
3469 goto unlock;
3470 }
3471 if (sh2)
3472 release_stripe(sh2);
3473
NeilBrownf4168852007-02-28 20:11:53 -08003474 /* Need to write out all blocks after computing P&Q */
3475 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003476 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003477 schedule_reconstruction(sh, &s, 1, 1);
3478 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003479 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3480 atomic_dec(&conf->reshape_stripes);
3481 wake_up(&conf->wait_for_overlap);
3482 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3483 }
3484
Dan Williams0f94e872008-01-08 15:32:53 -08003485 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003486 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003487 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003488
Dan Williams6bfe0b42008-04-30 00:52:32 -07003489 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003490 spin_unlock(&sh->lock);
3491
Dan Williams6bfe0b42008-04-30 00:52:32 -07003492 /* wait for this device to become unblocked */
3493 if (unlikely(blocked_rdev))
3494 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3495
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003496 if (s.ops_request)
3497 raid_run_ops(sh, s.ops_request);
3498
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003499 ops_run_io(sh, &s);
3500
NeilBrown729a1862009-12-14 12:49:50 +11003501
3502 if (dec_preread_active) {
3503 /* We delay this until after ops_run_io so that if make_request
3504 * is waiting on a barrier, it won't continue until the writes
3505 * have actually been submitted.
3506 */
3507 atomic_dec(&conf->preread_active_stripes);
3508 if (atomic_read(&conf->preread_active_stripes) <
3509 IO_THRESHOLD)
3510 md_wakeup_thread(conf->mddev->thread);
3511 }
3512
Dan Williamsa4456852007-07-09 11:56:43 -07003513 return_io(return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003514}
3515
NeilBrown14425772009-10-16 15:55:25 +11003516static void handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003517{
3518 if (sh->raid_conf->level == 6)
NeilBrown14425772009-10-16 15:55:25 +11003519 handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003520 else
NeilBrown14425772009-10-16 15:55:25 +11003521 handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003522}
3523
Arjan van de Ven858119e2006-01-14 13:20:43 -08003524static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3527 while (!list_empty(&conf->delayed_list)) {
3528 struct list_head *l = conf->delayed_list.next;
3529 struct stripe_head *sh;
3530 sh = list_entry(l, struct stripe_head, lru);
3531 list_del_init(l);
3532 clear_bit(STRIPE_DELAYED, &sh->state);
3533 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3534 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003535 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
NeilBrown6ed30032008-02-06 01:40:00 -08003537 } else
3538 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539}
3540
Arjan van de Ven858119e2006-01-14 13:20:43 -08003541static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003542{
3543 /* device_lock is held */
3544 struct list_head head;
3545 list_add(&head, &conf->bitmap_list);
3546 list_del_init(&conf->bitmap_list);
3547 while (!list_empty(&head)) {
3548 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3549 list_del_init(&sh->lru);
3550 atomic_inc(&sh->count);
3551 __release_stripe(conf, sh);
3552 }
3553}
3554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555static void unplug_slaves(mddev_t *mddev)
3556{
NeilBrown070ec552009-06-16 16:54:21 +10003557 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 int i;
NeilBrown5e5e3e72009-10-16 16:35:30 +11003559 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561 rcu_read_lock();
NeilBrown5e5e3e72009-10-16 16:35:30 +11003562 for (i = 0; i < devs; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003563 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003564 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003565 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
3567 atomic_inc(&rdev->nr_pending);
3568 rcu_read_unlock();
3569
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003570 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 rdev_dec_pending(rdev, mddev);
3573 rcu_read_lock();
3574 }
3575 }
3576 rcu_read_unlock();
3577}
3578
Jens Axboe165125e2007-07-24 09:28:11 +02003579static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
3581 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003582 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 unsigned long flags;
3584
3585 spin_lock_irqsave(&conf->device_lock, flags);
3586
NeilBrown72626682005-09-09 16:23:54 -07003587 if (blk_remove_plug(q)) {
3588 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 md_wakeup_thread(mddev->thread);
3592
3593 spin_unlock_irqrestore(&conf->device_lock, flags);
3594
3595 unplug_slaves(mddev);
3596}
3597
NeilBrownf022b2f2006-10-03 01:15:56 -07003598static int raid5_congested(void *data, int bits)
3599{
3600 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +10003601 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003602
3603 /* No difference between reads and writes. Just check
3604 * how busy the stripe_cache is
3605 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003606
3607 if (mddev_congested(mddev, bits))
3608 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07003609 if (conf->inactive_blocked)
3610 return 1;
3611 if (conf->quiesce)
3612 return 1;
3613 if (list_empty_careful(&conf->inactive_list))
3614 return 1;
3615
3616 return 0;
3617}
3618
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003619/* We want read requests to align with chunks where possible,
3620 * but write requests don't need to.
3621 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003622static int raid5_mergeable_bvec(struct request_queue *q,
3623 struct bvec_merge_data *bvm,
3624 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003625{
3626 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003627 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003628 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003629 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003630 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003631
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003632 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003633 return biovec->bv_len; /* always allow writes to be mergeable */
3634
Andre Noll664e7c42009-06-18 08:45:27 +10003635 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3636 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003637 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3638 if (max < 0) max = 0;
3639 if (max <= biovec->bv_len && bio_sectors == 0)
3640 return biovec->bv_len;
3641 else
3642 return max;
3643}
3644
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003645
3646static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3647{
3648 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003649 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003650 unsigned int bio_sectors = bio->bi_size >> 9;
3651
Andre Noll664e7c42009-06-18 08:45:27 +10003652 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3653 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003654 return chunk_sectors >=
3655 ((sector & (chunk_sectors - 1)) + bio_sectors);
3656}
3657
3658/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003659 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3660 * later sampled by raid5d.
3661 */
3662static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3663{
3664 unsigned long flags;
3665
3666 spin_lock_irqsave(&conf->device_lock, flags);
3667
3668 bi->bi_next = conf->retry_read_aligned_list;
3669 conf->retry_read_aligned_list = bi;
3670
3671 spin_unlock_irqrestore(&conf->device_lock, flags);
3672 md_wakeup_thread(conf->mddev->thread);
3673}
3674
3675
3676static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3677{
3678 struct bio *bi;
3679
3680 bi = conf->retry_read_aligned;
3681 if (bi) {
3682 conf->retry_read_aligned = NULL;
3683 return bi;
3684 }
3685 bi = conf->retry_read_aligned_list;
3686 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003687 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003688 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003689 /*
3690 * this sets the active strip count to 1 and the processed
3691 * strip count to zero (upper 8 bits)
3692 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003693 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003694 }
3695
3696 return bi;
3697}
3698
3699
3700/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003701 * The "raid5_align_endio" should check if the read succeeded and if it
3702 * did, call bio_endio on the original bio (having bio_put the new bio
3703 * first).
3704 * If the read failed..
3705 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003706static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003707{
3708 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003709 mddev_t *mddev;
3710 raid5_conf_t *conf;
3711 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3712 mdk_rdev_t *rdev;
3713
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003714 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003715
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003716 rdev = (void*)raid_bi->bi_next;
3717 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003718 mddev = rdev->mddev;
3719 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003720
3721 rdev_dec_pending(rdev, conf->mddev);
3722
3723 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003724 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003725 if (atomic_dec_and_test(&conf->active_aligned_reads))
3726 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003727 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003728 }
3729
3730
Dan Williams45b42332007-07-09 11:56:43 -07003731 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003732
3733 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003734}
3735
Neil Brown387bb172007-02-08 14:20:29 -08003736static int bio_fits_rdev(struct bio *bi)
3737{
Jens Axboe165125e2007-07-24 09:28:11 +02003738 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003739
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003740 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003741 return 0;
3742 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003743 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003744 return 0;
3745
3746 if (q->merge_bvec_fn)
3747 /* it's too hard to apply the merge_bvec_fn at this stage,
3748 * just just give up
3749 */
3750 return 0;
3751
3752 return 1;
3753}
3754
3755
Jens Axboe165125e2007-07-24 09:28:11 +02003756static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003757{
3758 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003759 raid5_conf_t *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003760 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003761 struct bio* align_bi;
3762 mdk_rdev_t *rdev;
3763
3764 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003765 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003766 return 0;
3767 }
3768 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003769 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003770 */
3771 align_bi = bio_clone(raid_bio, GFP_NOIO);
3772 if (!align_bi)
3773 return 0;
3774 /*
3775 * set bi_end_io to a new function, and set bi_private to the
3776 * original bio.
3777 */
3778 align_bi->bi_end_io = raid5_align_endio;
3779 align_bi->bi_private = raid_bio;
3780 /*
3781 * compute position
3782 */
NeilBrown112bf892009-03-31 14:39:38 +11003783 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3784 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003785 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003786
3787 rcu_read_lock();
3788 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3789 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003790 atomic_inc(&rdev->nr_pending);
3791 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003792 raid_bio->bi_next = (void*)rdev;
3793 align_bi->bi_bdev = rdev->bdev;
3794 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3795 align_bi->bi_sector += rdev->data_offset;
3796
Neil Brown387bb172007-02-08 14:20:29 -08003797 if (!bio_fits_rdev(align_bi)) {
3798 /* too big in some way */
3799 bio_put(align_bi);
3800 rdev_dec_pending(rdev, mddev);
3801 return 0;
3802 }
3803
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003804 spin_lock_irq(&conf->device_lock);
3805 wait_event_lock_irq(conf->wait_for_stripe,
3806 conf->quiesce == 0,
3807 conf->device_lock, /* nothing */);
3808 atomic_inc(&conf->active_aligned_reads);
3809 spin_unlock_irq(&conf->device_lock);
3810
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811 generic_make_request(align_bi);
3812 return 1;
3813 } else {
3814 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003815 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003816 return 0;
3817 }
3818}
3819
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003820/* __get_priority_stripe - get the next stripe to process
3821 *
3822 * Full stripe writes are allowed to pass preread active stripes up until
3823 * the bypass_threshold is exceeded. In general the bypass_count
3824 * increments when the handle_list is handled before the hold_list; however, it
3825 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3826 * stripe with in flight i/o. The bypass_count will be reset when the
3827 * head of the hold_list has changed, i.e. the head was promoted to the
3828 * handle_list.
3829 */
3830static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3831{
3832 struct stripe_head *sh;
3833
3834 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3835 __func__,
3836 list_empty(&conf->handle_list) ? "empty" : "busy",
3837 list_empty(&conf->hold_list) ? "empty" : "busy",
3838 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3839
3840 if (!list_empty(&conf->handle_list)) {
3841 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3842
3843 if (list_empty(&conf->hold_list))
3844 conf->bypass_count = 0;
3845 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3846 if (conf->hold_list.next == conf->last_hold)
3847 conf->bypass_count++;
3848 else {
3849 conf->last_hold = conf->hold_list.next;
3850 conf->bypass_count -= conf->bypass_threshold;
3851 if (conf->bypass_count < 0)
3852 conf->bypass_count = 0;
3853 }
3854 }
3855 } else if (!list_empty(&conf->hold_list) &&
3856 ((conf->bypass_threshold &&
3857 conf->bypass_count > conf->bypass_threshold) ||
3858 atomic_read(&conf->pending_full_writes) == 0)) {
3859 sh = list_entry(conf->hold_list.next,
3860 typeof(*sh), lru);
3861 conf->bypass_count -= conf->bypass_threshold;
3862 if (conf->bypass_count < 0)
3863 conf->bypass_count = 0;
3864 } else
3865 return NULL;
3866
3867 list_del_init(&sh->lru);
3868 atomic_inc(&sh->count);
3869 BUG_ON(atomic_read(&sh->count) != 1);
3870 return sh;
3871}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003872
Jens Axboe165125e2007-07-24 09:28:11 +02003873static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
3875 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003876 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003877 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 sector_t new_sector;
3879 sector_t logical_sector, last_sector;
3880 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003881 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11003882 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
Jens Axboe1f98a132009-09-11 14:32:04 +02003884 if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) {
NeilBrowna2826aa2009-12-14 12:49:49 +11003885 /* Drain all pending writes. We only really need
3886 * to ensure they have been submitted, but this is
3887 * easier.
3888 */
3889 mddev->pers->quiesce(mddev, 1);
3890 mddev->pers->quiesce(mddev, 0);
3891 md_barrier_request(mddev, bi);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003892 return 0;
3893 }
3894
NeilBrown3d310eb2005-06-21 17:17:26 -07003895 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003896
NeilBrown802ba062006-12-13 00:34:13 -08003897 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003898 mddev->reshape_position == MaxSector &&
3899 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003900 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3903 last_sector = bi->bi_sector + (bi->bi_size>>9);
3904 bi->bi_next = NULL;
3905 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3908 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003909 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003910 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003911
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003912 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003913 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003914 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003915 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003916 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003917 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003918 * 64bit on a 32bit platform, and so it might be
3919 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003920 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003921 * the lock is dropped, so once we get a reference
3922 * to the stripe that we think it is, we will have
3923 * to check again.
3924 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003925 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003926 if (mddev->delta_disks < 0
3927 ? logical_sector < conf->reshape_progress
3928 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003929 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003930 previous = 1;
3931 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003932 if (mddev->delta_disks < 0
3933 ? logical_sector < conf->reshape_safe
3934 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003935 spin_unlock_irq(&conf->device_lock);
3936 schedule();
3937 goto retry;
3938 }
3939 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003940 spin_unlock_irq(&conf->device_lock);
3941 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003942 data_disks = disks - conf->max_degraded;
3943
NeilBrown112bf892009-03-31 14:39:38 +11003944 new_sector = raid5_compute_sector(conf, logical_sector,
3945 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003946 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003947 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 (unsigned long long)new_sector,
3949 (unsigned long long)logical_sector);
3950
NeilBrownb5663ba2009-03-31 14:39:38 +11003951 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003952 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003954 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003955 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003956 * stripe, so we must do the range check again.
3957 * Expansion could still move past after this
3958 * test, but as we are holding a reference to
3959 * 'sh', we know that if that happens,
3960 * STRIPE_EXPANDING will get set and the expansion
3961 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003962 */
3963 int must_retry = 0;
3964 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003965 if (mddev->delta_disks < 0
3966 ? logical_sector >= conf->reshape_progress
3967 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003968 /* mismatch, need to try again */
3969 must_retry = 1;
3970 spin_unlock_irq(&conf->device_lock);
3971 if (must_retry) {
3972 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003973 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003974 goto retry;
3975 }
3976 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003977
NeilBrowna5c308d2009-07-01 13:15:35 +10003978 if (bio_data_dir(bi) == WRITE &&
3979 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003980 logical_sector < mddev->suspend_hi) {
3981 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003982 /* As the suspend_* range is controlled by
3983 * userspace, we want an interruptible
3984 * wait.
3985 */
3986 flush_signals(current);
3987 prepare_to_wait(&conf->wait_for_overlap,
3988 &w, TASK_INTERRUPTIBLE);
3989 if (logical_sector >= mddev->suspend_lo &&
3990 logical_sector < mddev->suspend_hi)
3991 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003992 goto retry;
3993 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003994
3995 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3996 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3997 /* Stripe is busy expanding or
3998 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 * and wait a while
4000 */
4001 raid5_unplug_device(mddev->queue);
4002 release_stripe(sh);
4003 schedule();
4004 goto retry;
4005 }
4006 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004007 set_bit(STRIPE_HANDLE, &sh->state);
4008 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrown729a1862009-12-14 12:49:50 +11004009 if (mddev->barrier &&
4010 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4011 atomic_inc(&conf->preread_active_stripes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 } else {
4014 /* cannot get stripe for read-ahead, just give-up */
4015 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4016 finish_wait(&conf->wait_for_overlap, &w);
4017 break;
4018 }
4019
4020 }
4021 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004022 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004023 spin_unlock_irq(&conf->device_lock);
4024 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
NeilBrown16a53ec2006-06-26 00:27:38 -07004026 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004028
Neil Brown0e13fe232008-06-28 08:31:20 +10004029 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
NeilBrown729a1862009-12-14 12:49:50 +11004031
4032 if (mddev->barrier) {
4033 /* We need to wait for the stripes to all be handled.
4034 * So: wait for preread_active_stripes to drop to 0.
4035 */
4036 wait_event(mddev->thread->wqueue,
4037 atomic_read(&conf->preread_active_stripes) == 0);
4038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return 0;
4040}
4041
Dan Williamsb522adc2009-03-31 15:00:31 +11004042static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
4043
NeilBrown52c03292006-06-26 00:27:43 -07004044static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
NeilBrown52c03292006-06-26 00:27:43 -07004046 /* reshaping is quite different to recovery/resync so it is
4047 * handled quite separately ... here.
4048 *
4049 * On each call to sync_request, we gather one chunk worth of
4050 * destination stripes and flag them as expanding.
4051 * Then we find all the source stripes and request reads.
4052 * As the reads complete, handle_stripe will copy the data
4053 * into the destination stripe and release that stripe.
4054 */
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004055 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004057 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004058 int raid_disks = conf->previous_raid_disks;
4059 int data_disks = raid_disks - conf->max_degraded;
4060 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004061 int i;
4062 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004063 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004064 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004065 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004066 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004067
NeilBrownfef9c612009-03-31 15:16:46 +11004068 if (sector_nr == 0) {
4069 /* If restarting in the middle, skip the initial sectors */
4070 if (mddev->delta_disks < 0 &&
4071 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4072 sector_nr = raid5_size(mddev, 0, 0)
4073 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004074 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004075 conf->reshape_progress > 0)
4076 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004077 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004078 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004079 mddev->curr_resync_completed = sector_nr;
4080 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004081 *skipped = 1;
4082 return sector_nr;
4083 }
NeilBrown52c03292006-06-26 00:27:43 -07004084 }
4085
NeilBrown7a661382009-03-31 15:21:40 +11004086 /* We need to process a full chunk at a time.
4087 * If old and new chunk sizes differ, we need to process the
4088 * largest of these
4089 */
Andre Noll664e7c42009-06-18 08:45:27 +10004090 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4091 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004092 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004093 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004094
NeilBrown52c03292006-06-26 00:27:43 -07004095 /* we update the metadata when there is more than 3Meg
4096 * in the block range (that is rather arbitrary, should
4097 * probably be time based) or when the data about to be
4098 * copied would over-write the source of the data at
4099 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004100 * i.e. one new_stripe along from reshape_progress new_maps
4101 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004102 */
NeilBrownfef9c612009-03-31 15:16:46 +11004103 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004104 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004105 readpos = conf->reshape_progress;
4106 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004107 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004108 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004109 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004110 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004111 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004112 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004113 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004114 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004115 readpos -= min_t(sector_t, reshape_sectors, readpos);
4116 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004117 }
NeilBrown52c03292006-06-26 00:27:43 -07004118
NeilBrownc8f517c2009-03-31 15:28:40 +11004119 /* 'writepos' is the most advanced device address we might write.
4120 * 'readpos' is the least advanced device address we might read.
4121 * 'safepos' is the least address recorded in the metadata as having
4122 * been reshaped.
4123 * If 'readpos' is behind 'writepos', then there is no way that we can
4124 * ensure safety in the face of a crash - that must be done by userspace
4125 * making a backup of the data. So in that case there is no particular
4126 * rush to update metadata.
4127 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4128 * update the metadata to advance 'safepos' to match 'readpos' so that
4129 * we can be safe in the event of a crash.
4130 * So we insist on updating metadata if safepos is behind writepos and
4131 * readpos is beyond writepos.
4132 * In any case, update the metadata every 10 seconds.
4133 * Maybe that number should be configurable, but I'm not sure it is
4134 * worth it.... maybe it could be a multiple of safemode_delay???
4135 */
NeilBrownfef9c612009-03-31 15:16:46 +11004136 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004137 ? (safepos > writepos && readpos < writepos)
4138 : (safepos < writepos && readpos > writepos)) ||
4139 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004140 /* Cannot proceed until we've updated the superblock... */
4141 wait_event(conf->wait_for_overlap,
4142 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004143 mddev->reshape_position = conf->reshape_progress;
NeilBrownacb180b2009-04-14 16:28:34 +10004144 mddev->curr_resync_completed = mddev->curr_resync;
NeilBrownc8f517c2009-03-31 15:28:40 +11004145 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004146 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004147 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004148 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004149 kthread_should_stop());
4150 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004151 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004152 spin_unlock_irq(&conf->device_lock);
4153 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004154 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004155 }
4156
NeilBrownec32a2b2009-03-31 15:17:38 +11004157 if (mddev->delta_disks < 0) {
4158 BUG_ON(conf->reshape_progress == 0);
4159 stripe_addr = writepos;
4160 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004161 ~((sector_t)reshape_sectors - 1))
4162 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004163 != sector_nr);
4164 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004165 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004166 stripe_addr = sector_nr;
4167 }
NeilBrownab69ae12009-03-31 15:26:47 +11004168 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004169 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004170 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004171 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004172 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004173 set_bit(STRIPE_EXPANDING, &sh->state);
4174 atomic_inc(&conf->reshape_stripes);
4175 /* If any of this stripe is beyond the end of the old
4176 * array, then we need to zero those blocks
4177 */
4178 for (j=sh->disks; j--;) {
4179 sector_t s;
4180 if (j == sh->pd_idx)
4181 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004182 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004183 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004184 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004185 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004186 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004187 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004188 continue;
4189 }
4190 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4191 set_bit(R5_Expanded, &sh->dev[j].flags);
4192 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4193 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004194 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004195 set_bit(STRIPE_EXPAND_READY, &sh->state);
4196 set_bit(STRIPE_HANDLE, &sh->state);
4197 }
NeilBrownab69ae12009-03-31 15:26:47 +11004198 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004199 }
4200 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004201 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004202 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004203 else
NeilBrown7a661382009-03-31 15:21:40 +11004204 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004205 spin_unlock_irq(&conf->device_lock);
4206 /* Ok, those stripe are ready. We can start scheduling
4207 * reads on the source stripes.
4208 * The source stripes are determined by mapping the first and last
4209 * block on the destination stripes.
4210 */
NeilBrown52c03292006-06-26 00:27:43 -07004211 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004212 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004213 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004214 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004215 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004216 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004217 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004218 if (last_sector >= mddev->dev_sectors)
4219 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004220 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004221 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004222 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4223 set_bit(STRIPE_HANDLE, &sh->state);
4224 release_stripe(sh);
4225 first_sector += STRIPE_SECTORS;
4226 }
NeilBrownab69ae12009-03-31 15:26:47 +11004227 /* Now that the sources are clearly marked, we can release
4228 * the destination stripes
4229 */
4230 while (!list_empty(&stripes)) {
4231 sh = list_entry(stripes.next, struct stripe_head, lru);
4232 list_del_init(&sh->lru);
4233 release_stripe(sh);
4234 }
NeilBrownc6207272008-02-06 01:39:52 -08004235 /* If this takes us to the resync_max point where we have to pause,
4236 * then we need to write out the superblock.
4237 */
NeilBrown7a661382009-03-31 15:21:40 +11004238 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004239 if ((sector_nr - mddev->curr_resync_completed) * 2
4240 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004241 /* Cannot proceed until we've updated the superblock... */
4242 wait_event(conf->wait_for_overlap,
4243 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004244 mddev->reshape_position = conf->reshape_progress;
NeilBrown48606a92009-06-18 09:14:12 +10004245 mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004246 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004247 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4248 md_wakeup_thread(mddev->thread);
4249 wait_event(mddev->sb_wait,
4250 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4251 || kthread_should_stop());
4252 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004253 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004254 spin_unlock_irq(&conf->device_lock);
4255 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004256 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004257 }
NeilBrown7a661382009-03-31 15:21:40 +11004258 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004259}
4260
4261/* FIXME go_faster isn't used */
4262static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4263{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11004264 raid5_conf_t *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004265 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004266 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004267 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004268 int still_degraded = 0;
4269 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
NeilBrown72626682005-09-09 16:23:54 -07004271 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 /* just being told to finish up .. nothing much to do */
4273 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004274
NeilBrown29269552006-03-27 01:18:10 -08004275 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4276 end_reshape(conf);
4277 return 0;
4278 }
NeilBrown72626682005-09-09 16:23:54 -07004279
4280 if (mddev->curr_resync < max_sector) /* aborted */
4281 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4282 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004283 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004284 conf->fullsync = 0;
4285 bitmap_close_sync(mddev->bitmap);
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return 0;
4288 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004289
NeilBrown64bd6602009-08-03 10:59:58 +10004290 /* Allow raid5_quiesce to complete */
4291 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4292
NeilBrown52c03292006-06-26 00:27:43 -07004293 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4294 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004295
NeilBrownc6207272008-02-06 01:39:52 -08004296 /* No need to check resync_max as we never do more than one
4297 * stripe, and as resync_max will always be on a chunk boundary,
4298 * if the check in md_do_sync didn't fire, there is no chance
4299 * of overstepping resync_max here
4300 */
4301
NeilBrown16a53ec2006-06-26 00:27:38 -07004302 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 * to resync, then assert that we are finished, because there is
4304 * nothing we can do.
4305 */
NeilBrown3285edf2006-06-26 00:27:55 -07004306 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004307 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004308 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004309 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 return rv;
4311 }
NeilBrown72626682005-09-09 16:23:54 -07004312 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004313 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004314 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4315 /* we can skip this block, and probably more */
4316 sync_blocks /= STRIPE_SECTORS;
4317 *skipped = 1;
4318 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
NeilBrownb47490c2008-02-06 01:39:50 -08004321
4322 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4323
NeilBrowna8c906c2009-06-09 14:39:59 +10004324 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004326 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004328 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004330 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004332 /* Need to check if array will still be degraded after recovery/resync
4333 * We don't need to check the 'failed' flag as when that gets set,
4334 * recovery aborts.
4335 */
NeilBrownf001a702009-06-09 14:30:31 +10004336 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004337 if (conf->disks[i].rdev == NULL)
4338 still_degraded = 1;
4339
4340 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4341
4342 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 set_bit(STRIPE_SYNCING, &sh->state);
4344 clear_bit(STRIPE_INSYNC, &sh->state);
4345 spin_unlock(&sh->lock);
4346
NeilBrown14425772009-10-16 15:55:25 +11004347 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 release_stripe(sh);
4349
4350 return STRIPE_SECTORS;
4351}
4352
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004353static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4354{
4355 /* We may not be able to submit a whole bio at once as there
4356 * may not be enough stripe_heads available.
4357 * We cannot pre-allocate enough stripe_heads as we may need
4358 * more than exist in the cache (if we allow ever large chunks).
4359 * So we do one stripe head at a time and record in
4360 * ->bi_hw_segments how many have been done.
4361 *
4362 * We *know* that this entire raid_bio is in one chunk, so
4363 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4364 */
4365 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004366 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004367 sector_t sector, logical_sector, last_sector;
4368 int scnt = 0;
4369 int remaining;
4370 int handled = 0;
4371
4372 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004373 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004374 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004375 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4376
4377 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004378 logical_sector += STRIPE_SECTORS,
4379 sector += STRIPE_SECTORS,
4380 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004381
Jens Axboe960e7392008-08-15 10:41:18 +02004382 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004383 /* already done this stripe */
4384 continue;
4385
NeilBrowna8c906c2009-06-09 14:39:59 +10004386 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004387
4388 if (!sh) {
4389 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004390 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004391 conf->retry_read_aligned = raid_bio;
4392 return handled;
4393 }
4394
4395 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004396 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4397 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004398 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004399 conf->retry_read_aligned = raid_bio;
4400 return handled;
4401 }
4402
Dan Williams36d1c642009-07-14 11:48:22 -07004403 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004404 release_stripe(sh);
4405 handled++;
4406 }
4407 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004408 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004409 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004410 if (remaining == 0)
4411 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004412 if (atomic_dec_and_test(&conf->active_aligned_reads))
4413 wake_up(&conf->wait_for_stripe);
4414 return handled;
4415}
4416
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004417
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418/*
4419 * This is our raid5 kernel thread.
4420 *
4421 * We scan the hash table for stripes which can be handled now.
4422 * During the scan, completed stripes are saved for us by the interrupt
4423 * handler, so that they will not have to wait for our next wakeup.
4424 */
NeilBrown6ed30032008-02-06 01:40:00 -08004425static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004428 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 int handled;
4430
Dan Williams45b42332007-07-09 11:56:43 -07004431 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 handled = 0;
4436 spin_lock_irq(&conf->device_lock);
4437 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004438 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
NeilBrownae3c20c2006-07-10 04:44:17 -07004440 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004441 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004442 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004443 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004444 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004445 conf->seq_write = seq;
4446 activate_bit_delay(conf);
4447 }
4448
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004449 while ((bio = remove_bio_from_retry(conf))) {
4450 int ok;
4451 spin_unlock_irq(&conf->device_lock);
4452 ok = retry_aligned_read(conf, bio);
4453 spin_lock_irq(&conf->device_lock);
4454 if (!ok)
4455 break;
4456 handled++;
4457 }
4458
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004459 sh = __get_priority_stripe(conf);
4460
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004461 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 spin_unlock_irq(&conf->device_lock);
4464
4465 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004466 handle_stripe(sh);
4467 release_stripe(sh);
4468 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 spin_lock_irq(&conf->device_lock);
4471 }
Dan Williams45b42332007-07-09 11:56:43 -07004472 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 spin_unlock_irq(&conf->device_lock);
4475
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004476 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 unplug_slaves(mddev);
4478
Dan Williams45b42332007-07-09 11:56:43 -07004479 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
NeilBrown3f294f42005-11-08 21:39:25 -08004482static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004483raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004484{
NeilBrown070ec552009-06-16 16:54:21 +10004485 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004486 if (conf)
4487 return sprintf(page, "%d\n", conf->max_nr_stripes);
4488 else
4489 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004490}
4491
4492static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004493raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004494{
NeilBrown070ec552009-06-16 16:54:21 +10004495 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004496 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004497 int err;
4498
NeilBrown3f294f42005-11-08 21:39:25 -08004499 if (len >= PAGE_SIZE)
4500 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004501 if (!conf)
4502 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004503
Dan Williams4ef197d82008-04-28 02:15:54 -07004504 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004505 return -EINVAL;
4506 if (new <= 16 || new > 32768)
4507 return -EINVAL;
4508 while (new < conf->max_nr_stripes) {
4509 if (drop_one_stripe(conf))
4510 conf->max_nr_stripes--;
4511 else
4512 break;
4513 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004514 err = md_allow_write(mddev);
4515 if (err)
4516 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004517 while (new > conf->max_nr_stripes) {
4518 if (grow_one_stripe(conf))
4519 conf->max_nr_stripes++;
4520 else break;
4521 }
4522 return len;
4523}
NeilBrown007583c2005-11-08 21:39:30 -08004524
NeilBrown96de1e62005-11-08 21:39:39 -08004525static struct md_sysfs_entry
4526raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4527 raid5_show_stripe_cache_size,
4528 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004529
4530static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004531raid5_show_preread_threshold(mddev_t *mddev, char *page)
4532{
NeilBrown070ec552009-06-16 16:54:21 +10004533 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004534 if (conf)
4535 return sprintf(page, "%d\n", conf->bypass_threshold);
4536 else
4537 return 0;
4538}
4539
4540static ssize_t
4541raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4542{
NeilBrown070ec552009-06-16 16:54:21 +10004543 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004544 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004545 if (len >= PAGE_SIZE)
4546 return -EINVAL;
4547 if (!conf)
4548 return -ENODEV;
4549
Dan Williams4ef197d82008-04-28 02:15:54 -07004550 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004551 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004552 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004553 return -EINVAL;
4554 conf->bypass_threshold = new;
4555 return len;
4556}
4557
4558static struct md_sysfs_entry
4559raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4560 S_IRUGO | S_IWUSR,
4561 raid5_show_preread_threshold,
4562 raid5_store_preread_threshold);
4563
4564static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004565stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004566{
NeilBrown070ec552009-06-16 16:54:21 +10004567 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004568 if (conf)
4569 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4570 else
4571 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004572}
4573
NeilBrown96de1e62005-11-08 21:39:39 -08004574static struct md_sysfs_entry
4575raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004576
NeilBrown007583c2005-11-08 21:39:30 -08004577static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004578 &raid5_stripecache_size.attr,
4579 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004580 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004581 NULL,
4582};
NeilBrown007583c2005-11-08 21:39:30 -08004583static struct attribute_group raid5_attrs_group = {
4584 .name = NULL,
4585 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004586};
4587
Dan Williams80c3a6c2009-03-17 18:10:40 -07004588static sector_t
4589raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4590{
NeilBrown070ec552009-06-16 16:54:21 +10004591 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004592
4593 if (!sectors)
4594 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004595 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004596 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004597 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004598
Andre Noll9d8f0362009-06-18 08:45:01 +10004599 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004600 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004601 return sectors * (raid_disks - conf->max_degraded);
4602}
4603
Dan Williams36d1c642009-07-14 11:48:22 -07004604static void raid5_free_percpu(raid5_conf_t *conf)
4605{
4606 struct raid5_percpu *percpu;
4607 unsigned long cpu;
4608
4609 if (!conf->percpu)
4610 return;
4611
4612 get_online_cpus();
4613 for_each_possible_cpu(cpu) {
4614 percpu = per_cpu_ptr(conf->percpu, cpu);
4615 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004616 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004617 }
4618#ifdef CONFIG_HOTPLUG_CPU
4619 unregister_cpu_notifier(&conf->cpu_notify);
4620#endif
4621 put_online_cpus();
4622
4623 free_percpu(conf->percpu);
4624}
4625
Dan Williams95fc17a2009-07-31 12:39:15 +10004626static void free_conf(raid5_conf_t *conf)
4627{
4628 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004629 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004630 kfree(conf->disks);
4631 kfree(conf->stripe_hashtbl);
4632 kfree(conf);
4633}
4634
Dan Williams36d1c642009-07-14 11:48:22 -07004635#ifdef CONFIG_HOTPLUG_CPU
4636static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4637 void *hcpu)
4638{
4639 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4640 long cpu = (long)hcpu;
4641 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4642
4643 switch (action) {
4644 case CPU_UP_PREPARE:
4645 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004646 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004647 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004648 if (!percpu->scribble)
4649 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4650
4651 if (!percpu->scribble ||
4652 (conf->level == 6 && !percpu->spare_page)) {
4653 safe_put_page(percpu->spare_page);
4654 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004655 pr_err("%s: failed memory allocation for cpu%ld\n",
4656 __func__, cpu);
4657 return NOTIFY_BAD;
4658 }
4659 break;
4660 case CPU_DEAD:
4661 case CPU_DEAD_FROZEN:
4662 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004663 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004664 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004665 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004666 break;
4667 default:
4668 break;
4669 }
4670 return NOTIFY_OK;
4671}
4672#endif
4673
4674static int raid5_alloc_percpu(raid5_conf_t *conf)
4675{
4676 unsigned long cpu;
4677 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004678 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004679 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004680 int err;
4681
Dan Williams36d1c642009-07-14 11:48:22 -07004682 allcpus = alloc_percpu(struct raid5_percpu);
4683 if (!allcpus)
4684 return -ENOMEM;
4685 conf->percpu = allcpus;
4686
4687 get_online_cpus();
4688 err = 0;
4689 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004690 if (conf->level == 6) {
4691 spare_page = alloc_page(GFP_KERNEL);
4692 if (!spare_page) {
4693 err = -ENOMEM;
4694 break;
4695 }
4696 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4697 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004698 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004699 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004700 err = -ENOMEM;
4701 break;
4702 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004703 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004704 }
4705#ifdef CONFIG_HOTPLUG_CPU
4706 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4707 conf->cpu_notify.priority = 0;
4708 if (err == 0)
4709 err = register_cpu_notifier(&conf->cpu_notify);
4710#endif
4711 put_online_cpus();
4712
4713 return err;
4714}
4715
NeilBrown91adb562009-03-31 14:39:39 +11004716static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
4718 raid5_conf_t *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004719 int raid_disk, memory, max_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 mdk_rdev_t *rdev;
4721 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
NeilBrown91adb562009-03-31 14:39:39 +11004723 if (mddev->new_level != 5
4724 && mddev->new_level != 4
4725 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004726 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004727 mdname(mddev), mddev->new_level);
4728 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
NeilBrown91adb562009-03-31 14:39:39 +11004730 if ((mddev->new_level == 5
4731 && !algorithm_valid_raid5(mddev->new_layout)) ||
4732 (mddev->new_level == 6
4733 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004734 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004735 mdname(mddev), mddev->new_layout);
4736 return ERR_PTR(-EIO);
4737 }
4738 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4739 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4740 mdname(mddev), mddev->raid_disks);
4741 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Andre Noll664e7c42009-06-18 08:45:27 +10004744 if (!mddev->new_chunk_sectors ||
4745 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4746 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown91adb562009-03-31 14:39:39 +11004747 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
Andre Noll664e7c42009-06-18 08:45:27 +10004748 mddev->new_chunk_sectors << 9, mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004749 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004750 }
4751
NeilBrown91adb562009-03-31 14:39:39 +11004752 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4753 if (conf == NULL)
4754 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004755 spin_lock_init(&conf->device_lock);
4756 init_waitqueue_head(&conf->wait_for_stripe);
4757 init_waitqueue_head(&conf->wait_for_overlap);
4758 INIT_LIST_HEAD(&conf->handle_list);
4759 INIT_LIST_HEAD(&conf->hold_list);
4760 INIT_LIST_HEAD(&conf->delayed_list);
4761 INIT_LIST_HEAD(&conf->bitmap_list);
4762 INIT_LIST_HEAD(&conf->inactive_list);
4763 atomic_set(&conf->active_stripes, 0);
4764 atomic_set(&conf->preread_active_stripes, 0);
4765 atomic_set(&conf->active_aligned_reads, 0);
4766 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004767
4768 conf->raid_disks = mddev->raid_disks;
4769 if (mddev->reshape_position == MaxSector)
4770 conf->previous_raid_disks = mddev->raid_disks;
4771 else
4772 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004773 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4774 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004775
NeilBrown5e5e3e72009-10-16 16:35:30 +11004776 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004777 GFP_KERNEL);
4778 if (!conf->disks)
4779 goto abort;
4780
4781 conf->mddev = mddev;
4782
4783 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4784 goto abort;
4785
Dan Williams36d1c642009-07-14 11:48:22 -07004786 conf->level = mddev->new_level;
4787 if (raid5_alloc_percpu(conf) != 0)
4788 goto abort;
4789
NeilBrown91adb562009-03-31 14:39:39 +11004790 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4791
4792 list_for_each_entry(rdev, &mddev->disks, same_set) {
4793 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004794 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004795 || raid_disk < 0)
4796 continue;
4797 disk = conf->disks + raid_disk;
4798
4799 disk->rdev = rdev;
4800
4801 if (test_bit(In_sync, &rdev->flags)) {
4802 char b[BDEVNAME_SIZE];
4803 printk(KERN_INFO "raid5: device %s operational as raid"
4804 " disk %d\n", bdevname(rdev->bdev,b),
4805 raid_disk);
4806 } else
4807 /* Cannot rely on bitmap to complete recovery */
4808 conf->fullsync = 1;
4809 }
4810
Andre Noll09c9e5f2009-06-18 08:45:55 +10004811 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004812 conf->level = mddev->new_level;
4813 if (conf->level == 6)
4814 conf->max_degraded = 2;
4815 else
4816 conf->max_degraded = 1;
4817 conf->algorithm = mddev->new_layout;
4818 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004819 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004820 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004821 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004822 conf->prev_algo = mddev->layout;
4823 }
NeilBrown91adb562009-03-31 14:39:39 +11004824
4825 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004826 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004827 if (grow_stripes(conf, conf->max_nr_stripes)) {
4828 printk(KERN_ERR
4829 "raid5: couldn't allocate %dkB for buffers\n", memory);
4830 goto abort;
4831 } else
4832 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4833 memory, mdname(mddev));
4834
NeilBrown0da3c612009-09-23 18:09:45 +10004835 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004836 if (!conf->thread) {
4837 printk(KERN_ERR
4838 "raid5: couldn't allocate thread for %s\n",
4839 mdname(mddev));
4840 goto abort;
4841 }
4842
4843 return conf;
4844
4845 abort:
4846 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004847 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004848 return ERR_PTR(-EIO);
4849 } else
4850 return ERR_PTR(-ENOMEM);
4851}
4852
NeilBrownc148ffd2009-11-13 17:47:00 +11004853
4854static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4855{
4856 switch (algo) {
4857 case ALGORITHM_PARITY_0:
4858 if (raid_disk < max_degraded)
4859 return 1;
4860 break;
4861 case ALGORITHM_PARITY_N:
4862 if (raid_disk >= raid_disks - max_degraded)
4863 return 1;
4864 break;
4865 case ALGORITHM_PARITY_0_6:
4866 if (raid_disk == 0 ||
4867 raid_disk == raid_disks - 1)
4868 return 1;
4869 break;
4870 case ALGORITHM_LEFT_ASYMMETRIC_6:
4871 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4872 case ALGORITHM_LEFT_SYMMETRIC_6:
4873 case ALGORITHM_RIGHT_SYMMETRIC_6:
4874 if (raid_disk == raid_disks - 1)
4875 return 1;
4876 }
4877 return 0;
4878}
4879
NeilBrown91adb562009-03-31 14:39:39 +11004880static int run(mddev_t *mddev)
4881{
4882 raid5_conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004883 int working_disks = 0, chunk_size;
NeilBrownc148ffd2009-11-13 17:47:00 +11004884 int dirty_parity_disks = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004885 mdk_rdev_t *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11004886 sector_t reshape_offset = 0;
NeilBrown91adb562009-03-31 14:39:39 +11004887
Andre Noll8c6ac862009-06-18 08:48:06 +10004888 if (mddev->recovery_cp != MaxSector)
4889 printk(KERN_NOTICE "raid5: %s is not clean"
4890 " -- starting background reconstruction\n",
4891 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004892 if (mddev->reshape_position != MaxSector) {
4893 /* Check that we can continue the reshape.
4894 * Currently only disks can change, it must
4895 * increase, and we must be past the point where
4896 * a stripe over-writes itself
4897 */
4898 sector_t here_new, here_old;
4899 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004900 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004901
NeilBrown88ce4932009-03-31 15:24:23 +11004902 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004903 printk(KERN_ERR "raid5: %s: unsupported reshape "
4904 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004905 mdname(mddev));
4906 return -EINVAL;
4907 }
NeilBrownf6705572006-03-27 01:18:11 -08004908 old_disks = mddev->raid_disks - mddev->delta_disks;
4909 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004910 * further up in new geometry must map after here in old
4911 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004912 */
4913 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004914 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004915 (mddev->raid_disks - max_degraded))) {
4916 printk(KERN_ERR "raid5: reshape_position not "
4917 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004918 return -EINVAL;
4919 }
NeilBrownc148ffd2009-11-13 17:47:00 +11004920 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08004921 /* here_new is the stripe we will write to */
4922 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004923 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004924 (old_disks-max_degraded));
4925 /* here_old is the first stripe that we might need to read
4926 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004927 if (mddev->delta_disks == 0) {
4928 /* We cannot be sure it is safe to start an in-place
4929 * reshape. It is only safe if user-space if monitoring
4930 * and taking constant backups.
4931 * mdadm always starts a situation like this in
4932 * readonly mode so it can take control before
4933 * allowing any writes. So just check for that.
4934 */
4935 if ((here_new * mddev->new_chunk_sectors !=
4936 here_old * mddev->chunk_sectors) ||
4937 mddev->ro == 0) {
4938 printk(KERN_ERR "raid5: in-place reshape must be started"
4939 " in read-only mode - aborting\n");
4940 return -EINVAL;
4941 }
4942 } else if (mddev->delta_disks < 0
4943 ? (here_new * mddev->new_chunk_sectors <=
4944 here_old * mddev->chunk_sectors)
4945 : (here_new * mddev->new_chunk_sectors >=
4946 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004947 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004948 printk(KERN_ERR "raid5: reshape_position too early for "
4949 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004950 return -EINVAL;
4951 }
4952 printk(KERN_INFO "raid5: reshape will continue\n");
4953 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004954 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004955 BUG_ON(mddev->level != mddev->new_level);
4956 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004957 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004958 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004959 }
4960
NeilBrown245f46c2009-03-31 14:39:39 +11004961 if (mddev->private == NULL)
4962 conf = setup_conf(mddev);
4963 else
4964 conf = mddev->private;
4965
NeilBrown91adb562009-03-31 14:39:39 +11004966 if (IS_ERR(conf))
4967 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004968
NeilBrown91adb562009-03-31 14:39:39 +11004969 mddev->thread = conf->thread;
4970 conf->thread = NULL;
4971 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004974 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 */
NeilBrownc148ffd2009-11-13 17:47:00 +11004976 list_for_each_entry(rdev, &mddev->disks, same_set) {
4977 if (rdev->raid_disk < 0)
4978 continue;
4979 if (test_bit(In_sync, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11004980 working_disks++;
NeilBrownc148ffd2009-11-13 17:47:00 +11004981 /* This disc is not fully in-sync. However if it
4982 * just stored parity (beyond the recovery_offset),
4983 * when we don't need to be concerned about the
4984 * array being dirty.
4985 * When reshape goes 'backwards', we never have
4986 * partially completed devices, so we only need
4987 * to worry about reshape going forwards.
4988 */
4989 /* Hack because v0.91 doesn't store recovery_offset properly. */
4990 if (mddev->major_version == 0 &&
4991 mddev->minor_version > 90)
4992 rdev->recovery_offset = reshape_offset;
4993
4994 printk("%d: w=%d pa=%d pr=%d m=%d a=%d r=%d op1=%d op2=%d\n",
4995 rdev->raid_disk, working_disks, conf->prev_algo,
4996 conf->previous_raid_disks, conf->max_degraded,
4997 conf->algorithm, conf->raid_disks,
4998 only_parity(rdev->raid_disk,
4999 conf->prev_algo,
5000 conf->previous_raid_disks,
5001 conf->max_degraded),
5002 only_parity(rdev->raid_disk,
5003 conf->algorithm,
5004 conf->raid_disks,
5005 conf->max_degraded));
5006 if (rdev->recovery_offset < reshape_offset) {
5007 /* We need to check old and new layout */
5008 if (!only_parity(rdev->raid_disk,
5009 conf->algorithm,
5010 conf->raid_disks,
5011 conf->max_degraded))
5012 continue;
5013 }
5014 if (!only_parity(rdev->raid_disk,
5015 conf->prev_algo,
5016 conf->previous_raid_disks,
5017 conf->max_degraded))
5018 continue;
5019 dirty_parity_disks++;
5020 }
NeilBrown91adb562009-03-31 14:39:39 +11005021
NeilBrown5e5e3e72009-10-16 16:35:30 +11005022 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
5023 - working_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
NeilBrown16a53ec2006-06-26 00:27:38 -07005025 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 printk(KERN_ERR "raid5: not enough operational devices for %s"
5027 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005028 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 goto abort;
5030 }
5031
NeilBrown91adb562009-03-31 14:39:39 +11005032 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005033 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005034 mddev->resync_max_sectors = mddev->dev_sectors;
5035
NeilBrownc148ffd2009-11-13 17:47:00 +11005036 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005038 if (mddev->ok_start_degraded)
5039 printk(KERN_WARNING
5040 "raid5: starting dirty degraded array: %s"
5041 "- data corruption possible.\n",
5042 mdname(mddev));
5043 else {
5044 printk(KERN_ERR
5045 "raid5: cannot start dirty degraded array for %s\n",
5046 mdname(mddev));
5047 goto abort;
5048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 }
5050
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 if (mddev->degraded == 0)
5052 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11005053 " devices, algorithm %d\n", conf->level, mdname(mddev),
5054 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5055 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 else
5057 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
5058 " out of %d devices, algorithm %d\n", conf->level,
5059 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11005060 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
5062 print_raid5_conf(conf);
5063
NeilBrownfef9c612009-03-31 15:16:46 +11005064 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08005065 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11005066 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005067 atomic_set(&conf->reshape_stripes, 0);
5068 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5069 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5070 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5071 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5072 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005073 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005074 }
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07005077 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 */
5079 {
NeilBrown16a53ec2006-06-26 00:27:38 -07005080 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5081 int stripe = data_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10005082 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5084 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5085 }
5086
5087 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005088 if (mddev->to_remove == &raid5_attrs_group)
5089 mddev->to_remove = NULL;
5090 else if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005091 printk(KERN_WARNING
5092 "raid5: failed to create sysfs attributes for %s\n",
5093 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07005094
NeilBrown91adb562009-03-31 14:39:39 +11005095 mddev->queue->queue_lock = &conf->device_lock;
5096
NeilBrown7a5febe2005-05-16 21:53:16 -07005097 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07005098 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08005099 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07005100
Dan Williams1f403622009-03-31 14:59:03 +11005101 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07005102
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005103 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10005104 chunk_size = mddev->chunk_sectors << 9;
5105 blk_queue_io_min(mddev->queue, chunk_size);
5106 blk_queue_io_opt(mddev->queue, chunk_size *
5107 (conf->raid_disks - conf->max_degraded));
5108
5109 list_for_each_entry(rdev, &mddev->disks, same_set)
5110 disk_stack_limits(mddev->gendisk, rdev->bdev,
5111 rdev->data_offset << 9);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return 0;
5114abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005115 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005116 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (conf) {
5118 print_raid5_conf(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005119 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 }
5121 mddev->private = NULL;
5122 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5123 return -EIO;
5124}
5125
5126
5127
NeilBrown3f294f42005-11-08 21:39:25 -08005128static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11005130 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 md_unregister_thread(mddev->thread);
5133 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005134 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
Dan Williams95fc17a2009-07-31 12:39:15 +10005136 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005137 mddev->private = NULL;
5138 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 return 0;
5140}
5141
Dan Williams45b42332007-07-09 11:56:43 -07005142#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005143static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
5145 int i;
5146
NeilBrown16a53ec2006-06-26 00:27:38 -07005147 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5148 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5149 seq_printf(seq, "sh %llu, count %d.\n",
5150 (unsigned long long)sh->sector, atomic_read(&sh->count));
5151 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005152 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005153 seq_printf(seq, "(cache%d: %p %ld) ",
5154 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005156 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157}
5158
NeilBrownd710e132008-10-13 11:55:12 +11005159static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
5161 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005162 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 int i;
5164
5165 spin_lock_irq(&conf->device_lock);
5166 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005167 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (sh->raid_conf != conf)
5169 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005170 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
5172 }
5173 spin_unlock_irq(&conf->device_lock);
5174}
5175#endif
5176
NeilBrownd710e132008-10-13 11:55:12 +11005177static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11005179 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 int i;
5181
Andre Noll9d8f0362009-06-18 08:45:01 +10005182 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5183 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005184 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 for (i = 0; i < conf->raid_disks; i++)
5186 seq_printf (seq, "%s",
5187 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005188 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005190#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005191 seq_printf (seq, "\n");
5192 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193#endif
5194}
5195
5196static void print_raid5_conf (raid5_conf_t *conf)
5197{
5198 int i;
5199 struct disk_info *tmp;
5200
5201 printk("RAID5 conf printout:\n");
5202 if (!conf) {
5203 printk("(conf==NULL)\n");
5204 return;
5205 }
NeilBrown02c2de82006-10-03 01:15:47 -07005206 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5207 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 for (i = 0; i < conf->raid_disks; i++) {
5210 char b[BDEVNAME_SIZE];
5211 tmp = conf->disks + i;
5212 if (tmp->rdev)
5213 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005214 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 bdevname(tmp->rdev->bdev,b));
5216 }
5217}
5218
5219static int raid5_spare_active(mddev_t *mddev)
5220{
5221 int i;
5222 raid5_conf_t *conf = mddev->private;
5223 struct disk_info *tmp;
5224
5225 for (i = 0; i < conf->raid_disks; i++) {
5226 tmp = conf->disks + i;
5227 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005228 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005229 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5230 unsigned long flags;
5231 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005233 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
5235 }
5236 print_raid5_conf(conf);
5237 return 0;
5238}
5239
5240static int raid5_remove_disk(mddev_t *mddev, int number)
5241{
5242 raid5_conf_t *conf = mddev->private;
5243 int err = 0;
5244 mdk_rdev_t *rdev;
5245 struct disk_info *p = conf->disks + number;
5246
5247 print_raid5_conf(conf);
5248 rdev = p->rdev;
5249 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005250 if (number >= conf->raid_disks &&
5251 conf->reshape_progress == MaxSector)
5252 clear_bit(In_sync, &rdev->flags);
5253
NeilBrownb2d444d2005-11-08 21:39:31 -08005254 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 atomic_read(&rdev->nr_pending)) {
5256 err = -EBUSY;
5257 goto abort;
5258 }
NeilBrowndfc70642008-05-23 13:04:39 -07005259 /* Only remove non-faulty devices if recovery
5260 * isn't possible.
5261 */
5262 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005263 mddev->degraded <= conf->max_degraded &&
5264 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005265 err = -EBUSY;
5266 goto abort;
5267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005269 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (atomic_read(&rdev->nr_pending)) {
5271 /* lost the race, try later */
5272 err = -EBUSY;
5273 p->rdev = rdev;
5274 }
5275 }
5276abort:
5277
5278 print_raid5_conf(conf);
5279 return err;
5280}
5281
5282static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5283{
5284 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005285 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 int disk;
5287 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005288 int first = 0;
5289 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
NeilBrown16a53ec2006-06-26 00:27:38 -07005291 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
Neil Brown6c2fce22008-06-28 08:31:31 +10005295 if (rdev->raid_disk >= 0)
5296 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005299 * find the disk ... but prefer rdev->saved_raid_disk
5300 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005302 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005303 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005304 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5305 disk = rdev->saved_raid_disk;
5306 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005307 disk = first;
5308 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005310 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005312 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005313 if (rdev->saved_raid_disk != disk)
5314 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005315 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 break;
5317 }
5318 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320}
5321
5322static int raid5_resize(mddev_t *mddev, sector_t sectors)
5323{
5324 /* no resync is happening, and there is enough space
5325 * on all devices, so we can resize.
5326 * We need to make sure resync covers any new space.
5327 * If the array is shrinking we should possibly wait until
5328 * any io in the removed space completes, but it hardly seems
5329 * worth it.
5330 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005331 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005332 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5333 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005334 if (mddev->array_sectors >
5335 raid5_size(mddev, sectors, mddev->raid_disks))
5336 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005337 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07005338 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005339 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005340 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5341 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5343 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005344 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005345 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 return 0;
5347}
5348
NeilBrown01ee22b2009-06-18 08:47:20 +10005349static int check_stripe_cache(mddev_t *mddev)
5350{
5351 /* Can only proceed if there are plenty of stripe_heads.
5352 * We need a minimum of one full stripe,, and for sensible progress
5353 * it is best to have about 4 times that.
5354 * If we require 4 times, then the default 256 4K stripe_heads will
5355 * allow for chunk sizes up to 256K, which is probably OK.
5356 * If the chunk size is greater, user-space should request more
5357 * stripe_heads first.
5358 */
5359 raid5_conf_t *conf = mddev->private;
5360 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5361 > conf->max_nr_stripes ||
5362 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5363 > conf->max_nr_stripes) {
5364 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
5365 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5366 / STRIPE_SIZE)*4);
5367 return 0;
5368 }
5369 return 1;
5370}
5371
NeilBrown50ac1682009-06-18 08:47:55 +10005372static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005373{
NeilBrown070ec552009-06-16 16:54:21 +10005374 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005375
NeilBrown88ce4932009-03-31 15:24:23 +11005376 if (mddev->delta_disks == 0 &&
5377 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005378 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005379 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005380 if (mddev->bitmap)
5381 /* Cannot grow a bitmap yet */
5382 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005383 if (mddev->degraded > conf->max_degraded)
5384 return -EINVAL;
5385 if (mddev->delta_disks < 0) {
5386 /* We might be able to shrink, but the devices must
5387 * be made bigger first.
5388 * For raid6, 4 is the minimum size.
5389 * Otherwise 2 is the minimum
5390 */
5391 int min = 2;
5392 if (mddev->level == 6)
5393 min = 4;
5394 if (mddev->raid_disks + mddev->delta_disks < min)
5395 return -EINVAL;
5396 }
NeilBrown29269552006-03-27 01:18:10 -08005397
NeilBrown01ee22b2009-06-18 08:47:20 +10005398 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005399 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005400
NeilBrownec32a2b2009-03-31 15:17:38 +11005401 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005402}
5403
5404static int raid5_start_reshape(mddev_t *mddev)
5405{
NeilBrown070ec552009-06-16 16:54:21 +10005406 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005407 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005408 int spares = 0;
5409 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005410 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005411
NeilBrownf4168852007-02-28 20:11:53 -08005412 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005413 return -EBUSY;
5414
NeilBrown01ee22b2009-06-18 08:47:20 +10005415 if (!check_stripe_cache(mddev))
5416 return -ENOSPC;
5417
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005418 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005419 if (rdev->raid_disk < 0 &&
5420 !test_bit(Faulty, &rdev->flags))
5421 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005422
NeilBrownf4168852007-02-28 20:11:53 -08005423 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005424 /* Not enough devices even to make a degraded array
5425 * of that size
5426 */
5427 return -EINVAL;
5428
NeilBrownec32a2b2009-03-31 15:17:38 +11005429 /* Refuse to reduce size of the array. Any reductions in
5430 * array size must be through explicit setting of array_size
5431 * attribute.
5432 */
5433 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5434 < mddev->array_sectors) {
5435 printk(KERN_ERR "md: %s: array size must be reduced "
5436 "before number of disks\n", mdname(mddev));
5437 return -EINVAL;
5438 }
5439
NeilBrownf6705572006-03-27 01:18:11 -08005440 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005441 spin_lock_irq(&conf->device_lock);
5442 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005443 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005444 conf->prev_chunk_sectors = conf->chunk_sectors;
5445 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005446 conf->prev_algo = conf->algorithm;
5447 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005448 if (mddev->delta_disks < 0)
5449 conf->reshape_progress = raid5_size(mddev, 0, 0);
5450 else
5451 conf->reshape_progress = 0;
5452 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005453 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005454 spin_unlock_irq(&conf->device_lock);
5455
5456 /* Add some new drives, as many as will fit.
5457 * We know there are enough to make the newly sized array work.
5458 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005459 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005460 if (rdev->raid_disk < 0 &&
5461 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005462 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005463 char nm[20];
NeilBrown9eb07c22010-02-09 12:31:47 +11005464 if (rdev->raid_disk >= conf->previous_raid_disks) {
NeilBrown7ef90142009-11-13 17:40:51 +11005465 set_bit(In_sync, &rdev->flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005466 added_devices++;
5467 } else
NeilBrown7ef90142009-11-13 17:40:51 +11005468 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005469 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005470 if (sysfs_create_link(&mddev->kobj,
5471 &rdev->kobj, nm))
5472 printk(KERN_WARNING
5473 "raid5: failed to create "
5474 " link %s for %s\n",
5475 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005476 } else
5477 break;
5478 }
5479
NeilBrown9eb07c22010-02-09 12:31:47 +11005480 /* When a reshape changes the number of devices, ->degraded
5481 * is measured against the large of the pre and post number of
5482 * devices.*/
NeilBrownec32a2b2009-03-31 15:17:38 +11005483 if (mddev->delta_disks > 0) {
5484 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown9eb07c22010-02-09 12:31:47 +11005485 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
NeilBrownec32a2b2009-03-31 15:17:38 +11005486 - added_devices;
5487 spin_unlock_irqrestore(&conf->device_lock, flags);
5488 }
NeilBrown63c70c42006-03-27 01:18:13 -08005489 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005490 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005491 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005492
NeilBrown29269552006-03-27 01:18:10 -08005493 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5494 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5495 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5496 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5497 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005498 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005499 if (!mddev->sync_thread) {
5500 mddev->recovery = 0;
5501 spin_lock_irq(&conf->device_lock);
5502 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005503 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005504 spin_unlock_irq(&conf->device_lock);
5505 return -EAGAIN;
5506 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005507 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005508 md_wakeup_thread(mddev->sync_thread);
5509 md_new_event(mddev);
5510 return 0;
5511}
NeilBrown29269552006-03-27 01:18:10 -08005512
NeilBrownec32a2b2009-03-31 15:17:38 +11005513/* This is called from the reshape thread and should make any
5514 * changes needed in 'conf'
5515 */
NeilBrown29269552006-03-27 01:18:10 -08005516static void end_reshape(raid5_conf_t *conf)
5517{
NeilBrown29269552006-03-27 01:18:10 -08005518
NeilBrownf6705572006-03-27 01:18:11 -08005519 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005520
NeilBrownf6705572006-03-27 01:18:11 -08005521 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005522 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005523 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005524 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005525 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005526
5527 /* read-ahead size must cover two whole stripes, which is
5528 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5529 */
5530 {
NeilBrowncea9c222009-03-31 15:15:05 +11005531 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005532 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005533 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005534 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5535 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5536 }
NeilBrown29269552006-03-27 01:18:10 -08005537 }
NeilBrown29269552006-03-27 01:18:10 -08005538}
5539
NeilBrownec32a2b2009-03-31 15:17:38 +11005540/* This is called from the raid5d thread with mddev_lock held.
5541 * It makes config changes to the device.
5542 */
NeilBrowncea9c222009-03-31 15:15:05 +11005543static void raid5_finish_reshape(mddev_t *mddev)
5544{
NeilBrown070ec552009-06-16 16:54:21 +10005545 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005546
5547 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5548
NeilBrownec32a2b2009-03-31 15:17:38 +11005549 if (mddev->delta_disks > 0) {
5550 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5551 set_capacity(mddev->gendisk, mddev->array_sectors);
5552 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005553 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005554 } else {
5555 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005556 mddev->degraded = conf->raid_disks;
5557 for (d = 0; d < conf->raid_disks ; d++)
5558 if (conf->disks[d].rdev &&
5559 test_bit(In_sync,
5560 &conf->disks[d].rdev->flags))
5561 mddev->degraded--;
5562 for (d = conf->raid_disks ;
5563 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005564 d++) {
5565 mdk_rdev_t *rdev = conf->disks[d].rdev;
5566 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5567 char nm[20];
5568 sprintf(nm, "rd%d", rdev->raid_disk);
5569 sysfs_remove_link(&mddev->kobj, nm);
5570 rdev->raid_disk = -1;
5571 }
5572 }
NeilBrowncea9c222009-03-31 15:15:05 +11005573 }
NeilBrown88ce4932009-03-31 15:24:23 +11005574 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005575 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005576 mddev->reshape_position = MaxSector;
5577 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005578 }
5579}
5580
NeilBrown72626682005-09-09 16:23:54 -07005581static void raid5_quiesce(mddev_t *mddev, int state)
5582{
NeilBrown070ec552009-06-16 16:54:21 +10005583 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005584
5585 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005586 case 2: /* resume for a suspend */
5587 wake_up(&conf->wait_for_overlap);
5588 break;
5589
NeilBrown72626682005-09-09 16:23:54 -07005590 case 1: /* stop all writes */
5591 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005592 /* '2' tells resync/reshape to pause so that all
5593 * active stripes can drain
5594 */
5595 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005596 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005597 atomic_read(&conf->active_stripes) == 0 &&
5598 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005599 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005600 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005601 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005602 /* allow reshape to continue */
5603 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005604 break;
5605
5606 case 0: /* re-enable writes */
5607 spin_lock_irq(&conf->device_lock);
5608 conf->quiesce = 0;
5609 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005610 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005611 spin_unlock_irq(&conf->device_lock);
5612 break;
5613 }
NeilBrown72626682005-09-09 16:23:54 -07005614}
NeilBrownb15c2e52006-01-06 00:20:16 -08005615
NeilBrownd562b0c2009-03-31 14:39:39 +11005616
Trela Maciej54071b32010-03-08 16:02:42 +11005617static void *raid5_takeover_raid0(mddev_t *mddev)
5618{
5619
5620 mddev->new_level = 5;
5621 mddev->new_layout = ALGORITHM_PARITY_N;
5622 mddev->new_chunk_sectors = mddev->chunk_sectors;
5623 mddev->raid_disks += 1;
5624 mddev->delta_disks = 1;
5625 /* make sure it will be not marked as dirty */
5626 mddev->recovery_cp = MaxSector;
5627
5628 return setup_conf(mddev);
5629}
5630
5631
NeilBrownd562b0c2009-03-31 14:39:39 +11005632static void *raid5_takeover_raid1(mddev_t *mddev)
5633{
5634 int chunksect;
5635
5636 if (mddev->raid_disks != 2 ||
5637 mddev->degraded > 1)
5638 return ERR_PTR(-EINVAL);
5639
5640 /* Should check if there are write-behind devices? */
5641
5642 chunksect = 64*2; /* 64K by default */
5643
5644 /* The array must be an exact multiple of chunksize */
5645 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5646 chunksect >>= 1;
5647
5648 if ((chunksect<<9) < STRIPE_SIZE)
5649 /* array size does not allow a suitable chunk size */
5650 return ERR_PTR(-EINVAL);
5651
5652 mddev->new_level = 5;
5653 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005654 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005655
5656 return setup_conf(mddev);
5657}
5658
NeilBrownfc9739c2009-03-31 14:57:20 +11005659static void *raid5_takeover_raid6(mddev_t *mddev)
5660{
5661 int new_layout;
5662
5663 switch (mddev->layout) {
5664 case ALGORITHM_LEFT_ASYMMETRIC_6:
5665 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5666 break;
5667 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5668 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5669 break;
5670 case ALGORITHM_LEFT_SYMMETRIC_6:
5671 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5672 break;
5673 case ALGORITHM_RIGHT_SYMMETRIC_6:
5674 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5675 break;
5676 case ALGORITHM_PARITY_0_6:
5677 new_layout = ALGORITHM_PARITY_0;
5678 break;
5679 case ALGORITHM_PARITY_N:
5680 new_layout = ALGORITHM_PARITY_N;
5681 break;
5682 default:
5683 return ERR_PTR(-EINVAL);
5684 }
5685 mddev->new_level = 5;
5686 mddev->new_layout = new_layout;
5687 mddev->delta_disks = -1;
5688 mddev->raid_disks -= 1;
5689 return setup_conf(mddev);
5690}
5691
NeilBrownd562b0c2009-03-31 14:39:39 +11005692
NeilBrown50ac1682009-06-18 08:47:55 +10005693static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005694{
NeilBrown88ce4932009-03-31 15:24:23 +11005695 /* For a 2-drive array, the layout and chunk size can be changed
5696 * immediately as not restriping is needed.
5697 * For larger arrays we record the new value - after validation
5698 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005699 */
NeilBrown070ec552009-06-16 16:54:21 +10005700 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005701 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005702
NeilBrown597a7112009-06-18 08:47:42 +10005703 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005704 return -EINVAL;
5705 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005706 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005707 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005708 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005709 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005710 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005711 /* not factor of array size */
5712 return -EINVAL;
5713 }
5714
5715 /* They look valid */
5716
NeilBrown88ce4932009-03-31 15:24:23 +11005717 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005718 /* can make the change immediately */
5719 if (mddev->new_layout >= 0) {
5720 conf->algorithm = mddev->new_layout;
5721 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005722 }
5723 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005724 conf->chunk_sectors = new_chunk ;
5725 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005726 }
5727 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5728 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005729 }
NeilBrown50ac1682009-06-18 08:47:55 +10005730 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005731}
5732
NeilBrown50ac1682009-06-18 08:47:55 +10005733static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005734{
NeilBrown597a7112009-06-18 08:47:42 +10005735 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005736
NeilBrown597a7112009-06-18 08:47:42 +10005737 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005738 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005739 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005740 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005741 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005742 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005743 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005744 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005745 /* not factor of array size */
5746 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005747 }
NeilBrown88ce4932009-03-31 15:24:23 +11005748
5749 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005750 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005751}
5752
NeilBrownd562b0c2009-03-31 14:39:39 +11005753static void *raid5_takeover(mddev_t *mddev)
5754{
5755 /* raid5 can take over:
5756 * raid0 - if all devices are the same - make it a raid4 layout
5757 * raid1 - if there are two drives. We need to know the chunk size
5758 * raid4 - trivial - just use a raid4 layout.
5759 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005760 */
Trela Maciej54071b32010-03-08 16:02:42 +11005761 if (mddev->level == 0) {
5762 /* for raid0 takeover only one zone is supported */
5763 struct raid0_private_data *raid0_priv
5764 = mddev->private;
5765 if (raid0_priv->nr_strip_zones > 1) {
5766 printk(KERN_ERR "md: cannot takeover raid 0 with more than one zone.\n");
5767 return ERR_PTR(-EINVAL);
5768 }
5769 return raid5_takeover_raid0(mddev);
5770 }
NeilBrownd562b0c2009-03-31 14:39:39 +11005771
5772 if (mddev->level == 1)
5773 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005774 if (mddev->level == 4) {
5775 mddev->new_layout = ALGORITHM_PARITY_N;
5776 mddev->new_level = 5;
5777 return setup_conf(mddev);
5778 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005779 if (mddev->level == 6)
5780 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005781
5782 return ERR_PTR(-EINVAL);
5783}
5784
NeilBrowna78d38a2010-03-22 16:53:49 +11005785static void *raid4_takeover(mddev_t *mddev)
5786{
5787 /* raid4 can take over raid5 if layout is right.
5788 */
5789 if (mddev->level == 5 &&
5790 mddev->layout == ALGORITHM_PARITY_N) {
5791 mddev->new_layout = 0;
5792 mddev->new_level = 4;
5793 return setup_conf(mddev);
5794 }
5795 return ERR_PTR(-EINVAL);
5796}
NeilBrownd562b0c2009-03-31 14:39:39 +11005797
NeilBrown245f46c2009-03-31 14:39:39 +11005798static struct mdk_personality raid5_personality;
5799
5800static void *raid6_takeover(mddev_t *mddev)
5801{
5802 /* Currently can only take over a raid5. We map the
5803 * personality to an equivalent raid6 personality
5804 * with the Q block at the end.
5805 */
5806 int new_layout;
5807
5808 if (mddev->pers != &raid5_personality)
5809 return ERR_PTR(-EINVAL);
5810 if (mddev->degraded > 1)
5811 return ERR_PTR(-EINVAL);
5812 if (mddev->raid_disks > 253)
5813 return ERR_PTR(-EINVAL);
5814 if (mddev->raid_disks < 3)
5815 return ERR_PTR(-EINVAL);
5816
5817 switch (mddev->layout) {
5818 case ALGORITHM_LEFT_ASYMMETRIC:
5819 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5820 break;
5821 case ALGORITHM_RIGHT_ASYMMETRIC:
5822 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5823 break;
5824 case ALGORITHM_LEFT_SYMMETRIC:
5825 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5826 break;
5827 case ALGORITHM_RIGHT_SYMMETRIC:
5828 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5829 break;
5830 case ALGORITHM_PARITY_0:
5831 new_layout = ALGORITHM_PARITY_0_6;
5832 break;
5833 case ALGORITHM_PARITY_N:
5834 new_layout = ALGORITHM_PARITY_N;
5835 break;
5836 default:
5837 return ERR_PTR(-EINVAL);
5838 }
5839 mddev->new_level = 6;
5840 mddev->new_layout = new_layout;
5841 mddev->delta_disks = 1;
5842 mddev->raid_disks += 1;
5843 return setup_conf(mddev);
5844}
5845
5846
NeilBrown16a53ec2006-06-26 00:27:38 -07005847static struct mdk_personality raid6_personality =
5848{
5849 .name = "raid6",
5850 .level = 6,
5851 .owner = THIS_MODULE,
5852 .make_request = make_request,
5853 .run = run,
5854 .stop = stop,
5855 .status = status,
5856 .error_handler = error,
5857 .hot_add_disk = raid5_add_disk,
5858 .hot_remove_disk= raid5_remove_disk,
5859 .spare_active = raid5_spare_active,
5860 .sync_request = sync_request,
5861 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005862 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005863 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005864 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005865 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005866 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005867 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005868};
NeilBrown2604b702006-01-06 00:20:36 -08005869static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870{
5871 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005872 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 .owner = THIS_MODULE,
5874 .make_request = make_request,
5875 .run = run,
5876 .stop = stop,
5877 .status = status,
5878 .error_handler = error,
5879 .hot_add_disk = raid5_add_disk,
5880 .hot_remove_disk= raid5_remove_disk,
5881 .spare_active = raid5_spare_active,
5882 .sync_request = sync_request,
5883 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005884 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005885 .check_reshape = raid5_check_reshape,
5886 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005887 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005888 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005889 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890};
5891
NeilBrown2604b702006-01-06 00:20:36 -08005892static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893{
NeilBrown2604b702006-01-06 00:20:36 -08005894 .name = "raid4",
5895 .level = 4,
5896 .owner = THIS_MODULE,
5897 .make_request = make_request,
5898 .run = run,
5899 .stop = stop,
5900 .status = status,
5901 .error_handler = error,
5902 .hot_add_disk = raid5_add_disk,
5903 .hot_remove_disk= raid5_remove_disk,
5904 .spare_active = raid5_spare_active,
5905 .sync_request = sync_request,
5906 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005907 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005908 .check_reshape = raid5_check_reshape,
5909 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005910 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005911 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11005912 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08005913};
5914
5915static int __init raid5_init(void)
5916{
NeilBrown16a53ec2006-06-26 00:27:38 -07005917 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005918 register_md_personality(&raid5_personality);
5919 register_md_personality(&raid4_personality);
5920 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921}
5922
NeilBrown2604b702006-01-06 00:20:36 -08005923static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
NeilBrown16a53ec2006-06-26 00:27:38 -07005925 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005926 unregister_md_personality(&raid5_personality);
5927 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
5930module_init(raid5_init);
5931module_exit(raid5_exit);
5932MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11005933MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005935MODULE_ALIAS("md-raid5");
5936MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005937MODULE_ALIAS("md-level-5");
5938MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005939MODULE_ALIAS("md-personality-8"); /* RAID6 */
5940MODULE_ALIAS("md-raid6");
5941MODULE_ALIAS("md-level-6");
5942
5943/* This used to be two separate modules, they were: */
5944MODULE_ALIAS("raid5");
5945MODULE_ALIAS("raid6");