blob: 5359236a1ec76d94910db17ee1c14d48398bc0ec [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>
NeilBrownbff61972009-03-31 14:33:13 +110050#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070051#include <linux/cpu.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110052#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110053#include "raid5.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110054#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Stripe cache
58 */
59
60#define NR_STRIPES 256
61#define STRIPE_SIZE PAGE_SIZE
62#define STRIPE_SHIFT (PAGE_SHIFT - 9)
63#define STRIPE_SECTORS (STRIPE_SIZE>>9)
64#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070065#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080066#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define HASH_MASK (NR_HASH - 1)
68
NeilBrownfccddba2006-01-06 00:20:33 -080069#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/* bio's attached to a stripe+device for I/O are linked together in bi_sector
72 * order without overlap. There may be several bio's per stripe+device, and
73 * a bio could span several devices.
74 * When walking this list for a particular stripe+device, we must never proceed
75 * beyond a bio that extends past this device, as the next bio might no longer
76 * be valid.
77 * This macro is used to determine the 'next' bio in the list, given the sector
78 * of the current stripe+device
79 */
80#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
81/*
82 * The following can be used to debug the driver
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define RAID5_PARANOIA 1
85#if RAID5_PARANOIA && defined(CONFIG_SMP)
86# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87#else
88# define CHECK_DEVLOCK()
89#endif
90
Dan Williams45b42332007-07-09 11:56:43 -070091#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define inline
93#define __inline__
94#endif
95
Bernd Schubert6be9d492008-05-23 13:04:34 -070096#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
97
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
102static inline int raid5_bi_phys_segments(struct bio *bio)
103{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200104 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200105}
106
107static inline int raid5_bi_hw_segments(struct bio *bio)
108{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200109 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200110}
111
112static inline int raid5_dec_bi_phys_segments(struct bio *bio)
113{
114 --bio->bi_phys_segments;
115 return raid5_bi_phys_segments(bio);
116}
117
118static inline int raid5_dec_bi_hw_segments(struct bio *bio)
119{
120 unsigned short val = raid5_bi_hw_segments(bio);
121
122 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200123 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200124 return val;
125}
126
127static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
128{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200129 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
NeilBrownd0dabf72009-03-31 14:39:38 +1100132/* Find first data disk in a raid6 stripe */
133static inline int raid6_d0(struct stripe_head *sh)
134{
NeilBrown67cc2b82009-03-31 14:39:38 +1100135 if (sh->ddf_layout)
136 /* ddf always start from first device */
137 return 0;
138 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100155static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
156 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100157{
158 int slot;
NeilBrown67cc2b82009-03-31 14:39:38 +1100159
NeilBrownd0dabf72009-03-31 14:39:38 +1100160 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100161 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100162 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100163 return syndrome_disks + 1;
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 slot = (*count)++;
165 return slot;
166}
167
Dan Williamsa4456852007-07-09 11:56:43 -0700168static void return_io(struct bio *return_bi)
169{
170 struct bio *bi = return_bi;
171 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700172
173 return_bi = bi->bi_next;
174 bi->bi_next = NULL;
175 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000176 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700177 bi = return_bi;
178 }
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static void print_raid5_conf (raid5_conf_t *conf);
182
Dan Williams600aa102008-06-28 08:32:05 +1000183static int stripe_operations_active(struct stripe_head *sh)
184{
185 return sh->check_state || sh->reconstruct_state ||
186 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
187 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
188}
189
Arjan van de Ven858119e2006-01-14 13:20:43 -0800190static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200193 BUG_ON(!list_empty(&sh->lru));
194 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700196 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700198 blk_plug_device(conf->mddev->queue);
199 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700200 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700201 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700202 blk_plug_device(conf->mddev->queue);
203 } else {
NeilBrown72626682005-09-09 16:23:54 -0700204 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 md_wakeup_thread(conf->mddev->thread);
208 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000209 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
211 atomic_dec(&conf->preread_active_stripes);
212 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
213 md_wakeup_thread(conf->mddev->thread);
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800216 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
217 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800219 if (conf->retry_read_aligned)
220 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 }
224}
NeilBrownd0dabf72009-03-31 14:39:38 +1100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static void release_stripe(struct stripe_head *sh)
227{
228 raid5_conf_t *conf = sh->raid_conf;
229 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 spin_lock_irqsave(&conf->device_lock, flags);
232 __release_stripe(conf, sh);
233 spin_unlock_irqrestore(&conf->device_lock, flags);
234}
235
NeilBrownfccddba2006-01-06 00:20:33 -0800236static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Dan Williams45b42332007-07-09 11:56:43 -0700238 pr_debug("remove_hash(), stripe %llu\n",
239 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrownfccddba2006-01-06 00:20:33 -0800241 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrown16a53ec2006-06-26 00:27:38 -0700244static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
NeilBrownfccddba2006-01-06 00:20:33 -0800246 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dan Williams45b42332007-07-09 11:56:43 -0700248 pr_debug("insert_hash(), stripe %llu\n",
249 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800252 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255
256/* find an idle stripe, make sure it is unhashed, and return it. */
257static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
258{
259 struct stripe_head *sh = NULL;
260 struct list_head *first;
261
262 CHECK_DEVLOCK();
263 if (list_empty(&conf->inactive_list))
264 goto out;
265 first = conf->inactive_list.next;
266 sh = list_entry(first, struct stripe_head, lru);
267 list_del_init(first);
268 remove_hash(sh);
269 atomic_inc(&conf->active_stripes);
270out:
271 return sh;
272}
273
274static void shrink_buffers(struct stripe_head *sh, int num)
275{
276 struct page *p;
277 int i;
278
279 for (i=0; i<num ; i++) {
280 p = sh->dev[i].page;
281 if (!p)
282 continue;
283 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800284 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286}
287
288static int grow_buffers(struct stripe_head *sh, int num)
289{
290 int i;
291
292 for (i=0; i<num; i++) {
293 struct page *page;
294
295 if (!(page = alloc_page(GFP_KERNEL))) {
296 return 1;
297 }
298 sh->dev[i].page = page;
299 }
300 return 0;
301}
302
NeilBrown784052e2009-03-31 15:19:07 +1100303static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100304static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
305 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrownb5663ba2009-03-31 14:39:38 +1100307static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800310 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200312 BUG_ON(atomic_read(&sh->count) != 0);
313 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000314 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700317 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (unsigned long long)sh->sector);
319
320 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700321
NeilBrown86b42c72009-03-31 15:19:03 +1100322 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100323 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100325 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 sh->state = 0;
327
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800328
329 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct r5dev *dev = &sh->dev[i];
331
Dan Williamsd84e0f12007-01-02 13:52:30 -0700332 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags));
338 BUG();
339 }
340 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100341 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 insert_hash(conf, sh);
344}
345
NeilBrown86b42c72009-03-31 15:19:03 +1100346static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
347 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800350 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700353 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800354 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100355 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700357 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return NULL;
359}
360
361static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200362static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
NeilBrownb5663ba2009-03-31 14:39:38 +1100364static struct stripe_head *
365get_active_stripe(raid5_conf_t *conf, sector_t sector,
366 int previous, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct stripe_head *sh;
369
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 spin_lock_irq(&conf->device_lock);
373
374 do {
NeilBrown72626682005-09-09 16:23:54 -0700375 wait_event_lock_irq(conf->wait_for_stripe,
376 conf->quiesce == 0,
377 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100378 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!sh) {
380 if (!conf->inactive_blocked)
381 sh = get_free_stripe(conf);
382 if (noblock && sh == NULL)
383 break;
384 if (!sh) {
385 conf->inactive_blocked = 1;
386 wait_event_lock_irq(conf->wait_for_stripe,
387 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800388 (atomic_read(&conf->active_stripes)
389 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 || !conf->inactive_blocked),
391 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700392 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 );
394 conf->inactive_blocked = 0;
395 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100396 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
398 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100399 BUG_ON(!list_empty(&sh->lru)
400 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
402 if (!test_bit(STRIPE_HANDLE, &sh->state))
403 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700404 if (list_empty(&sh->lru) &&
405 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700406 BUG();
407 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
410 } while (sh == NULL);
411
412 if (sh)
413 atomic_inc(&sh->count);
414
415 spin_unlock_irq(&conf->device_lock);
416 return sh;
417}
418
NeilBrown6712ecf2007-09-27 12:47:43 +0200419static void
420raid5_end_read_request(struct bio *bi, int error);
421static void
422raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700423
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000424static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700425{
426 raid5_conf_t *conf = sh->raid_conf;
427 int i, disks = sh->disks;
428
429 might_sleep();
430
431 for (i = disks; i--; ) {
432 int rw;
433 struct bio *bi;
434 mdk_rdev_t *rdev;
435 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
436 rw = WRITE;
437 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
438 rw = READ;
439 else
440 continue;
441
442 bi = &sh->dev[i].req;
443
444 bi->bi_rw = rw;
445 if (rw == WRITE)
446 bi->bi_end_io = raid5_end_write_request;
447 else
448 bi->bi_end_io = raid5_end_read_request;
449
450 rcu_read_lock();
451 rdev = rcu_dereference(conf->disks[i].rdev);
452 if (rdev && test_bit(Faulty, &rdev->flags))
453 rdev = NULL;
454 if (rdev)
455 atomic_inc(&rdev->nr_pending);
456 rcu_read_unlock();
457
458 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000459 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700460 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
461
Dan Williams2b7497f2008-06-28 08:31:52 +1000462 set_bit(STRIPE_IO_STARTED, &sh->state);
463
Dan Williams91c00922007-01-02 13:52:30 -0700464 bi->bi_bdev = rdev->bdev;
465 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700466 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700467 bi->bi_rw, i);
468 atomic_inc(&sh->count);
469 bi->bi_sector = sh->sector + rdev->data_offset;
470 bi->bi_flags = 1 << BIO_UPTODATE;
471 bi->bi_vcnt = 1;
472 bi->bi_max_vecs = 1;
473 bi->bi_idx = 0;
474 bi->bi_io_vec = &sh->dev[i].vec;
475 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
476 bi->bi_io_vec[0].bv_offset = 0;
477 bi->bi_size = STRIPE_SIZE;
478 bi->bi_next = NULL;
479 if (rw == WRITE &&
480 test_bit(R5_ReWrite, &sh->dev[i].flags))
481 atomic_add(STRIPE_SECTORS,
482 &rdev->corrected_errors);
483 generic_make_request(bi);
484 } else {
485 if (rw == WRITE)
486 set_bit(STRIPE_DEGRADED, &sh->state);
487 pr_debug("skip op %ld on disc %d for sector %llu\n",
488 bi->bi_rw, i, (unsigned long long)sh->sector);
489 clear_bit(R5_LOCKED, &sh->dev[i].flags);
490 set_bit(STRIPE_HANDLE, &sh->state);
491 }
492 }
493}
494
495static struct dma_async_tx_descriptor *
496async_copy_data(int frombio, struct bio *bio, struct page *page,
497 sector_t sector, struct dma_async_tx_descriptor *tx)
498{
499 struct bio_vec *bvl;
500 struct page *bio_page;
501 int i;
502 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700503 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700504
505 if (bio->bi_sector >= sector)
506 page_offset = (signed)(bio->bi_sector - sector) * 512;
507 else
508 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700509
510 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williams91c00922007-01-02 13:52:30 -0700511 bio_for_each_segment(bvl, bio, i) {
512 int len = bio_iovec_idx(bio, i)->bv_len;
513 int clen;
514 int b_offset = 0;
515
516 if (page_offset < 0) {
517 b_offset = -page_offset;
518 page_offset += b_offset;
519 len -= b_offset;
520 }
521
522 if (len > 0 && page_offset + len > STRIPE_SIZE)
523 clen = STRIPE_SIZE - page_offset;
524 else
525 clen = len;
526
527 if (clen > 0) {
528 b_offset += bio_iovec_idx(bio, i)->bv_offset;
529 bio_page = bio_iovec_idx(bio, i)->bv_page;
530 if (frombio)
531 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700532 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700533 else
534 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700535 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700536 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700537 /* chain the operations */
538 submit.depend_tx = tx;
539
Dan Williams91c00922007-01-02 13:52:30 -0700540 if (clen < len) /* hit end of page */
541 break;
542 page_offset += len;
543 }
544
545 return tx;
546}
547
548static void ops_complete_biofill(void *stripe_head_ref)
549{
550 struct stripe_head *sh = stripe_head_ref;
551 struct bio *return_bi = NULL;
552 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700553 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700554
Harvey Harrisone46b2722008-04-28 02:15:50 -0700555 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700556 (unsigned long long)sh->sector);
557
558 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000559 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700560 for (i = sh->disks; i--; ) {
561 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700562
563 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700564 /* and check if we need to reply to a read request,
565 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000566 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700567 */
568 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700569 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700570
Dan Williams91c00922007-01-02 13:52:30 -0700571 BUG_ON(!dev->read);
572 rbi = dev->read;
573 dev->read = NULL;
574 while (rbi && rbi->bi_sector <
575 dev->sector + STRIPE_SECTORS) {
576 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200577 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700578 rbi->bi_next = return_bi;
579 return_bi = rbi;
580 }
Dan Williams91c00922007-01-02 13:52:30 -0700581 rbi = rbi2;
582 }
583 }
584 }
Dan Williams83de75c2008-06-28 08:31:58 +1000585 spin_unlock_irq(&conf->device_lock);
586 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700587
588 return_io(return_bi);
589
Dan Williamse4d84902007-09-24 10:06:13 -0700590 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700591 release_stripe(sh);
592}
593
594static void ops_run_biofill(struct stripe_head *sh)
595{
596 struct dma_async_tx_descriptor *tx = NULL;
597 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700598 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700599 int i;
600
Harvey Harrisone46b2722008-04-28 02:15:50 -0700601 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700602 (unsigned long long)sh->sector);
603
604 for (i = sh->disks; i--; ) {
605 struct r5dev *dev = &sh->dev[i];
606 if (test_bit(R5_Wantfill, &dev->flags)) {
607 struct bio *rbi;
608 spin_lock_irq(&conf->device_lock);
609 dev->read = rbi = dev->toread;
610 dev->toread = NULL;
611 spin_unlock_irq(&conf->device_lock);
612 while (rbi && rbi->bi_sector <
613 dev->sector + STRIPE_SECTORS) {
614 tx = async_copy_data(0, rbi, dev->page,
615 dev->sector, tx);
616 rbi = r5_next_bio(rbi, dev->sector);
617 }
618 }
619 }
620
621 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700622 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
623 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700624}
625
626static void ops_complete_compute5(void *stripe_head_ref)
627{
628 struct stripe_head *sh = stripe_head_ref;
629 int target = sh->ops.target;
630 struct r5dev *tgt = &sh->dev[target];
631
Harvey Harrisone46b2722008-04-28 02:15:50 -0700632 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700633 (unsigned long long)sh->sector);
634
635 set_bit(R5_UPTODATE, &tgt->flags);
636 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
637 clear_bit(R5_Wantcompute, &tgt->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +1000638 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
639 if (sh->check_state == check_state_compute_run)
640 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700641 set_bit(STRIPE_HANDLE, &sh->state);
642 release_stripe(sh);
643}
644
Dan Williams7b3a8712008-06-28 08:32:09 +1000645static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700646{
647 /* kernel stack size limits the total number of disks */
648 int disks = sh->disks;
649 struct page *xor_srcs[disks];
650 int target = sh->ops.target;
651 struct r5dev *tgt = &sh->dev[target];
652 struct page *xor_dest = tgt->page;
653 int count = 0;
654 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700655 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700656 int i;
657
658 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700659 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700660 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
661
662 for (i = disks; i--; )
663 if (i != target)
664 xor_srcs[count++] = sh->dev[i].page;
665
666 atomic_inc(&sh->count);
667
Dan Williamsa08abd82009-06-03 11:43:59 -0700668 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
669 ops_complete_compute5, sh, NULL);
Dan Williams91c00922007-01-02 13:52:30 -0700670 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700671 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700672 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700673 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700674
Dan Williams91c00922007-01-02 13:52:30 -0700675 return tx;
676}
677
678static void ops_complete_prexor(void *stripe_head_ref)
679{
680 struct stripe_head *sh = stripe_head_ref;
681
Harvey Harrisone46b2722008-04-28 02:15:50 -0700682 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700683 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700684}
685
686static struct dma_async_tx_descriptor *
687ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
688{
689 /* kernel stack size limits the total number of disks */
690 int disks = sh->disks;
691 struct page *xor_srcs[disks];
692 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700693 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700694
695 /* existing parity data subtracted */
696 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
697
Harvey Harrisone46b2722008-04-28 02:15:50 -0700698 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700699 (unsigned long long)sh->sector);
700
701 for (i = disks; i--; ) {
702 struct r5dev *dev = &sh->dev[i];
703 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000704 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700705 xor_srcs[count++] = dev->page;
706 }
707
Dan Williamsa08abd82009-06-03 11:43:59 -0700708 init_async_submit(&submit, ASYNC_TX_XOR_DROP_DST, tx,
709 ops_complete_prexor, sh, NULL);
710 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700711
712 return tx;
713}
714
715static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000716ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700717{
718 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000719 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700720
Harvey Harrisone46b2722008-04-28 02:15:50 -0700721 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700722 (unsigned long long)sh->sector);
723
724 for (i = disks; i--; ) {
725 struct r5dev *dev = &sh->dev[i];
726 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700727
Dan Williamsd8ee0722008-06-28 08:32:06 +1000728 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700729 struct bio *wbi;
730
731 spin_lock(&sh->lock);
732 chosen = dev->towrite;
733 dev->towrite = NULL;
734 BUG_ON(dev->written);
735 wbi = dev->written = chosen;
736 spin_unlock(&sh->lock);
737
738 while (wbi && wbi->bi_sector <
739 dev->sector + STRIPE_SECTORS) {
740 tx = async_copy_data(1, wbi, dev->page,
741 dev->sector, tx);
742 wbi = r5_next_bio(wbi, dev->sector);
743 }
744 }
745 }
746
747 return tx;
748}
749
750static void ops_complete_postxor(void *stripe_head_ref)
751{
752 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700753 int disks = sh->disks, i, pd_idx = sh->pd_idx;
754
Harvey Harrisone46b2722008-04-28 02:15:50 -0700755 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700756 (unsigned long long)sh->sector);
757
758 for (i = disks; i--; ) {
759 struct r5dev *dev = &sh->dev[i];
760 if (dev->written || i == pd_idx)
761 set_bit(R5_UPTODATE, &dev->flags);
762 }
763
Dan Williamsd8ee0722008-06-28 08:32:06 +1000764 if (sh->reconstruct_state == reconstruct_state_drain_run)
765 sh->reconstruct_state = reconstruct_state_drain_result;
766 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
767 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
768 else {
769 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
770 sh->reconstruct_state = reconstruct_state_result;
771 }
Dan Williams91c00922007-01-02 13:52:30 -0700772
773 set_bit(STRIPE_HANDLE, &sh->state);
774 release_stripe(sh);
775}
776
777static void
Dan Williamsd8ee0722008-06-28 08:32:06 +1000778ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700779{
780 /* kernel stack size limits the total number of disks */
781 int disks = sh->disks;
782 struct page *xor_srcs[disks];
Dan Williamsa08abd82009-06-03 11:43:59 -0700783 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700784 int count = 0, pd_idx = sh->pd_idx, i;
785 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000786 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700787 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700788
Harvey Harrisone46b2722008-04-28 02:15:50 -0700789 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700790 (unsigned long long)sh->sector);
791
792 /* check if prexor is active which means only process blocks
793 * that are part of a read-modify-write (written)
794 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000795 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
796 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700797 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
798 for (i = disks; i--; ) {
799 struct r5dev *dev = &sh->dev[i];
800 if (dev->written)
801 xor_srcs[count++] = dev->page;
802 }
803 } else {
804 xor_dest = sh->dev[pd_idx].page;
805 for (i = disks; i--; ) {
806 struct r5dev *dev = &sh->dev[i];
807 if (i != pd_idx)
808 xor_srcs[count++] = dev->page;
809 }
810 }
811
Dan Williams91c00922007-01-02 13:52:30 -0700812 /* 1/ if we prexor'd then the dest is reused as a source
813 * 2/ if we did not prexor then we are redoing the parity
814 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
815 * for the synchronous xor case
816 */
Dan Williams88ba2aa2009-04-09 16:16:18 -0700817 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -0700818 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
819
820 atomic_inc(&sh->count);
821
Dan Williamsa08abd82009-06-03 11:43:59 -0700822 init_async_submit(&submit, flags, tx, ops_complete_postxor, sh, NULL);
823 if (unlikely(count == 1))
824 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
825 else
826 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700827}
828
829static void ops_complete_check(void *stripe_head_ref)
830{
831 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700832
Harvey Harrisone46b2722008-04-28 02:15:50 -0700833 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700834 (unsigned long long)sh->sector);
835
Dan Williamsecc65c92008-06-28 08:31:57 +1000836 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700837 set_bit(STRIPE_HANDLE, &sh->state);
838 release_stripe(sh);
839}
840
841static void ops_run_check(struct stripe_head *sh)
842{
843 /* kernel stack size limits the total number of disks */
844 int disks = sh->disks;
845 struct page *xor_srcs[disks];
846 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700847 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700848
849 int count = 0, pd_idx = sh->pd_idx, i;
850 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
851
Harvey Harrisone46b2722008-04-28 02:15:50 -0700852 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700853 (unsigned long long)sh->sector);
854
855 for (i = disks; i--; ) {
856 struct r5dev *dev = &sh->dev[i];
857 if (i != pd_idx)
858 xor_srcs[count++] = dev->page;
859 }
860
Dan Williamsa08abd82009-06-03 11:43:59 -0700861 init_async_submit(&submit, 0, NULL, NULL, NULL, NULL);
Dan Williams099f53c2009-04-08 14:28:37 -0700862 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -0700863 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700864
Dan Williams91c00922007-01-02 13:52:30 -0700865 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700866 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
867 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700868}
869
Dan Williams600aa102008-06-28 08:32:05 +1000870static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700871{
872 int overlap_clear = 0, i, disks = sh->disks;
873 struct dma_async_tx_descriptor *tx = NULL;
874
Dan Williams83de75c2008-06-28 08:31:58 +1000875 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700876 ops_run_biofill(sh);
877 overlap_clear++;
878 }
879
Dan Williams7b3a8712008-06-28 08:32:09 +1000880 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
881 tx = ops_run_compute5(sh);
882 /* terminate the chain if postxor is not set to be run */
883 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
884 async_tx_ack(tx);
885 }
Dan Williams91c00922007-01-02 13:52:30 -0700886
Dan Williams600aa102008-06-28 08:32:05 +1000887 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700888 tx = ops_run_prexor(sh, tx);
889
Dan Williams600aa102008-06-28 08:32:05 +1000890 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000891 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700892 overlap_clear++;
893 }
894
Dan Williams600aa102008-06-28 08:32:05 +1000895 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd8ee0722008-06-28 08:32:06 +1000896 ops_run_postxor(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700897
Dan Williamsecc65c92008-06-28 08:31:57 +1000898 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700899 ops_run_check(sh);
900
Dan Williams91c00922007-01-02 13:52:30 -0700901 if (overlap_clear)
902 for (i = disks; i--; ) {
903 struct r5dev *dev = &sh->dev[i];
904 if (test_and_clear_bit(R5_Overlap, &dev->flags))
905 wake_up(&sh->raid_conf->wait_for_overlap);
906 }
907}
908
NeilBrown3f294f42005-11-08 21:39:25 -0800909static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800912 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
913 if (!sh)
914 return 0;
915 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
916 sh->raid_conf = conf;
917 spin_lock_init(&sh->lock);
918
919 if (grow_buffers(sh, conf->raid_disks)) {
920 shrink_buffers(sh, conf->raid_disks);
921 kmem_cache_free(conf->slab_cache, sh);
922 return 0;
923 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800924 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800925 /* we just created an active stripe so... */
926 atomic_set(&sh->count, 1);
927 atomic_inc(&conf->active_stripes);
928 INIT_LIST_HEAD(&sh->lru);
929 release_stripe(sh);
930 return 1;
931}
932
933static int grow_stripes(raid5_conf_t *conf, int num)
934{
Christoph Lametere18b8902006-12-06 20:33:20 -0800935 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int devs = conf->raid_disks;
937
NeilBrown245f46c2009-03-31 14:39:39 +1100938 sprintf(conf->cache_name[0],
939 "raid%d-%s", conf->level, mdname(conf->mddev));
940 sprintf(conf->cache_name[1],
941 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800942 conf->active_name = 0;
943 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900945 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!sc)
947 return 1;
948 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800949 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700950 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800951 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
NeilBrown29269552006-03-27 01:18:10 -0800955
NeilBrownad01c9e2006-03-27 01:18:07 -0800956static int resize_stripes(raid5_conf_t *conf, int newsize)
957{
958 /* Make all the stripes able to hold 'newsize' devices.
959 * New slots in each stripe get 'page' set to a new page.
960 *
961 * This happens in stages:
962 * 1/ create a new kmem_cache and allocate the required number of
963 * stripe_heads.
964 * 2/ gather all the old stripe_heads and tranfer the pages across
965 * to the new stripe_heads. This will have the side effect of
966 * freezing the array as once all stripe_heads have been collected,
967 * no IO will be possible. Old stripe heads are freed once their
968 * pages have been transferred over, and the old kmem_cache is
969 * freed when all stripes are done.
970 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
971 * we simple return a failre status - no need to clean anything up.
972 * 4/ allocate new pages for the new slots in the new stripe_heads.
973 * If this fails, we don't bother trying the shrink the
974 * stripe_heads down again, we just leave them as they are.
975 * As each stripe_head is processed the new one is released into
976 * active service.
977 *
978 * Once step2 is started, we cannot afford to wait for a write,
979 * so we use GFP_NOIO allocations.
980 */
981 struct stripe_head *osh, *nsh;
982 LIST_HEAD(newstripes);
983 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -0700984 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -0800985 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800986 int i;
987
988 if (newsize <= conf->pool_size)
989 return 0; /* never bother to shrink */
990
Dan Williamsb5470dc2008-06-27 21:44:04 -0700991 err = md_allow_write(conf->mddev);
992 if (err)
993 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -0800994
NeilBrownad01c9e2006-03-27 01:18:07 -0800995 /* Step 1 */
996 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
997 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900998 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -0800999 if (!sc)
1000 return -ENOMEM;
1001
1002 for (i = conf->max_nr_stripes; i; i--) {
1003 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1004 if (!nsh)
1005 break;
1006
1007 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1008
1009 nsh->raid_conf = conf;
1010 spin_lock_init(&nsh->lock);
1011
1012 list_add(&nsh->lru, &newstripes);
1013 }
1014 if (i) {
1015 /* didn't get enough, give up */
1016 while (!list_empty(&newstripes)) {
1017 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1018 list_del(&nsh->lru);
1019 kmem_cache_free(sc, nsh);
1020 }
1021 kmem_cache_destroy(sc);
1022 return -ENOMEM;
1023 }
1024 /* Step 2 - Must use GFP_NOIO now.
1025 * OK, we have enough stripes, start collecting inactive
1026 * stripes and copying them over
1027 */
1028 list_for_each_entry(nsh, &newstripes, lru) {
1029 spin_lock_irq(&conf->device_lock);
1030 wait_event_lock_irq(conf->wait_for_stripe,
1031 !list_empty(&conf->inactive_list),
1032 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001033 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001034 );
1035 osh = get_free_stripe(conf);
1036 spin_unlock_irq(&conf->device_lock);
1037 atomic_set(&nsh->count, 1);
1038 for(i=0; i<conf->pool_size; i++)
1039 nsh->dev[i].page = osh->dev[i].page;
1040 for( ; i<newsize; i++)
1041 nsh->dev[i].page = NULL;
1042 kmem_cache_free(conf->slab_cache, osh);
1043 }
1044 kmem_cache_destroy(conf->slab_cache);
1045
1046 /* Step 3.
1047 * At this point, we are holding all the stripes so the array
1048 * is completely stalled, so now is a good time to resize
1049 * conf->disks.
1050 */
1051 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1052 if (ndisks) {
1053 for (i=0; i<conf->raid_disks; i++)
1054 ndisks[i] = conf->disks[i];
1055 kfree(conf->disks);
1056 conf->disks = ndisks;
1057 } else
1058 err = -ENOMEM;
1059
1060 /* Step 4, return new stripes to service */
1061 while(!list_empty(&newstripes)) {
1062 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1063 list_del_init(&nsh->lru);
1064 for (i=conf->raid_disks; i < newsize; i++)
1065 if (nsh->dev[i].page == NULL) {
1066 struct page *p = alloc_page(GFP_NOIO);
1067 nsh->dev[i].page = p;
1068 if (!p)
1069 err = -ENOMEM;
1070 }
1071 release_stripe(nsh);
1072 }
1073 /* critical section pass, GFP_NOIO no longer needed */
1074
1075 conf->slab_cache = sc;
1076 conf->active_name = 1-conf->active_name;
1077 conf->pool_size = newsize;
1078 return err;
1079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
NeilBrown3f294f42005-11-08 21:39:25 -08001081static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct stripe_head *sh;
1084
NeilBrown3f294f42005-11-08 21:39:25 -08001085 spin_lock_irq(&conf->device_lock);
1086 sh = get_free_stripe(conf);
1087 spin_unlock_irq(&conf->device_lock);
1088 if (!sh)
1089 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001090 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001091 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001092 kmem_cache_free(conf->slab_cache, sh);
1093 atomic_dec(&conf->active_stripes);
1094 return 1;
1095}
1096
1097static void shrink_stripes(raid5_conf_t *conf)
1098{
1099 while (drop_one_stripe(conf))
1100 ;
1101
NeilBrown29fc7e32006-02-03 03:03:41 -08001102 if (conf->slab_cache)
1103 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 conf->slab_cache = NULL;
1105}
1106
NeilBrown6712ecf2007-09-27 12:47:43 +02001107static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
NeilBrown99c0fb52009-03-31 14:39:38 +11001109 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001111 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001113 char b[BDEVNAME_SIZE];
1114 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 for (i=0 ; i<disks; i++)
1118 if (bi == &sh->dev[i].req)
1119 break;
1120
Dan Williams45b42332007-07-09 11:56:43 -07001121 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1122 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 uptodate);
1124 if (i == disks) {
1125 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001131 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001132 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001133 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1134 " (%lu sectors at %llu on %s)\n",
1135 mdname(conf->mddev), STRIPE_SECTORS,
1136 (unsigned long long)(sh->sector
1137 + rdev->data_offset),
1138 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001139 clear_bit(R5_ReadError, &sh->dev[i].flags);
1140 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1141 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001142 if (atomic_read(&conf->disks[i].rdev->read_errors))
1143 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001145 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001146 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001147 rdev = conf->disks[i].rdev;
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001150 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001151 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001152 printk_rl(KERN_WARNING
1153 "raid5:%s: read error not correctable "
1154 "(sector %llu on %s).\n",
1155 mdname(conf->mddev),
1156 (unsigned long long)(sh->sector
1157 + rdev->data_offset),
1158 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001159 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001160 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001161 printk_rl(KERN_WARNING
1162 "raid5:%s: read error NOT corrected!! "
1163 "(sector %llu on %s).\n",
1164 mdname(conf->mddev),
1165 (unsigned long long)(sh->sector
1166 + rdev->data_offset),
1167 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001168 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001169 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001170 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001171 "raid5:%s: Too many read errors, failing device %s.\n",
1172 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001173 else
1174 retry = 1;
1175 if (retry)
1176 set_bit(R5_ReadError, &sh->dev[i].flags);
1177 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001178 clear_bit(R5_ReadError, &sh->dev[i].flags);
1179 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001180 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1185 set_bit(STRIPE_HANDLE, &sh->state);
1186 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
NeilBrownd710e132008-10-13 11:55:12 +11001189static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
NeilBrown99c0fb52009-03-31 14:39:38 +11001191 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001193 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 for (i=0 ; i<disks; i++)
1197 if (bi == &sh->dev[i].req)
1198 break;
1199
Dan Williams45b42332007-07-09 11:56:43 -07001200 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1202 uptodate);
1203 if (i == disks) {
1204 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001205 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!uptodate)
1209 md_error(conf->mddev, conf->disks[i].rdev);
1210
1211 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1212
1213 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1214 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001215 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218
NeilBrown784052e2009-03-31 15:19:07 +11001219static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
NeilBrown784052e2009-03-31 15:19:07 +11001221static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 struct r5dev *dev = &sh->dev[i];
1224
1225 bio_init(&dev->req);
1226 dev->req.bi_io_vec = &dev->vec;
1227 dev->req.bi_vcnt++;
1228 dev->req.bi_max_vecs++;
1229 dev->vec.bv_page = dev->page;
1230 dev->vec.bv_len = STRIPE_SIZE;
1231 dev->vec.bv_offset = 0;
1232
1233 dev->req.bi_sector = sh->sector;
1234 dev->req.bi_private = sh;
1235
1236 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001237 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1241{
1242 char b[BDEVNAME_SIZE];
1243 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001244 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownb2d444d2005-11-08 21:39:31 -08001246 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001247 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001248 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1249 unsigned long flags;
1250 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001252 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * if recovery was running, make sure it aborts.
1255 */
NeilBrowndfc70642008-05-23 13:04:39 -07001256 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001258 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001259 printk(KERN_ALERT
1260 "raid5: Disk failure on %s, disabling device.\n"
1261 "raid5: Operation continuing on %d devices.\n",
1262 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266/*
1267 * Input: a 'big' sector number,
1268 * Output: index of the data and parity disk, and the sector # in them.
1269 */
NeilBrown112bf892009-03-31 14:39:38 +11001270static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001271 int previous, int *dd_idx,
1272 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 long stripe;
1275 unsigned long chunk_number;
1276 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001277 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001278 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001280 int algorithm = previous ? conf->prev_algo
1281 : conf->algorithm;
NeilBrown784052e2009-03-31 15:19:07 +11001282 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1283 : (conf->chunk_size >> 9);
NeilBrown112bf892009-03-31 14:39:38 +11001284 int raid_disks = previous ? conf->previous_raid_disks
1285 : conf->raid_disks;
1286 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* First compute the information on this sector */
1289
1290 /*
1291 * Compute the chunk number and the sector offset inside the chunk
1292 */
1293 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1294 chunk_number = r_sector;
1295 BUG_ON(r_sector != chunk_number);
1296
1297 /*
1298 * Compute the stripe number
1299 */
1300 stripe = chunk_number / data_disks;
1301
1302 /*
1303 * Compute the data disk and parity disk indexes inside the stripe
1304 */
1305 *dd_idx = chunk_number % data_disks;
1306
1307 /*
1308 * Select the parity disk based on the user selected algorithm.
1309 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001310 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001311 switch(conf->level) {
1312 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001313 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001314 break;
1315 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001316 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001318 pd_idx = data_disks - stripe % raid_disks;
1319 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 (*dd_idx)++;
1321 break;
1322 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001323 pd_idx = stripe % raid_disks;
1324 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 (*dd_idx)++;
1326 break;
1327 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001328 pd_idx = data_disks - stripe % raid_disks;
1329 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
1331 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001332 pd_idx = stripe % raid_disks;
1333 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001335 case ALGORITHM_PARITY_0:
1336 pd_idx = 0;
1337 (*dd_idx)++;
1338 break;
1339 case ALGORITHM_PARITY_N:
1340 pd_idx = data_disks;
1341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001343 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001344 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001345 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001346 }
1347 break;
1348 case 6:
1349
NeilBrowne183eae2009-03-31 15:20:22 +11001350 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001351 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001352 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1353 qd_idx = pd_idx + 1;
1354 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001355 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001356 qd_idx = 0;
1357 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001358 (*dd_idx) += 2; /* D D P Q D */
1359 break;
1360 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001361 pd_idx = stripe % raid_disks;
1362 qd_idx = pd_idx + 1;
1363 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001364 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001365 qd_idx = 0;
1366 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001367 (*dd_idx) += 2; /* D D P Q D */
1368 break;
1369 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001370 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1371 qd_idx = (pd_idx + 1) % raid_disks;
1372 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001373 break;
1374 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001375 pd_idx = stripe % raid_disks;
1376 qd_idx = (pd_idx + 1) % raid_disks;
1377 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001378 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001379
1380 case ALGORITHM_PARITY_0:
1381 pd_idx = 0;
1382 qd_idx = 1;
1383 (*dd_idx) += 2;
1384 break;
1385 case ALGORITHM_PARITY_N:
1386 pd_idx = data_disks;
1387 qd_idx = data_disks + 1;
1388 break;
1389
1390 case ALGORITHM_ROTATING_ZERO_RESTART:
1391 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1392 * of blocks for computing Q is different.
1393 */
1394 pd_idx = stripe % raid_disks;
1395 qd_idx = pd_idx + 1;
1396 if (pd_idx == raid_disks-1) {
1397 (*dd_idx)++; /* Q D D D P */
1398 qd_idx = 0;
1399 } else if (*dd_idx >= pd_idx)
1400 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001401 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001402 break;
1403
1404 case ALGORITHM_ROTATING_N_RESTART:
1405 /* Same a left_asymmetric, by first stripe is
1406 * D D D P Q rather than
1407 * Q D D D P
1408 */
1409 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1410 qd_idx = pd_idx + 1;
1411 if (pd_idx == raid_disks-1) {
1412 (*dd_idx)++; /* Q D D D P */
1413 qd_idx = 0;
1414 } else if (*dd_idx >= pd_idx)
1415 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001416 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001417 break;
1418
1419 case ALGORITHM_ROTATING_N_CONTINUE:
1420 /* Same as left_symmetric but Q is before P */
1421 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1422 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1423 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001424 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001425 break;
1426
1427 case ALGORITHM_LEFT_ASYMMETRIC_6:
1428 /* RAID5 left_asymmetric, with Q on last device */
1429 pd_idx = data_disks - stripe % (raid_disks-1);
1430 if (*dd_idx >= pd_idx)
1431 (*dd_idx)++;
1432 qd_idx = raid_disks - 1;
1433 break;
1434
1435 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1436 pd_idx = stripe % (raid_disks-1);
1437 if (*dd_idx >= pd_idx)
1438 (*dd_idx)++;
1439 qd_idx = raid_disks - 1;
1440 break;
1441
1442 case ALGORITHM_LEFT_SYMMETRIC_6:
1443 pd_idx = data_disks - stripe % (raid_disks-1);
1444 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1445 qd_idx = raid_disks - 1;
1446 break;
1447
1448 case ALGORITHM_RIGHT_SYMMETRIC_6:
1449 pd_idx = stripe % (raid_disks-1);
1450 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1451 qd_idx = raid_disks - 1;
1452 break;
1453
1454 case ALGORITHM_PARITY_0_6:
1455 pd_idx = 0;
1456 (*dd_idx)++;
1457 qd_idx = raid_disks - 1;
1458 break;
1459
1460
NeilBrown16a53ec2006-06-26 00:27:38 -07001461 default:
NeilBrownd710e132008-10-13 11:55:12 +11001462 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001463 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001464 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001465 }
1466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
NeilBrown911d4ee2009-03-31 14:39:38 +11001469 if (sh) {
1470 sh->pd_idx = pd_idx;
1471 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001472 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /*
1475 * Finally, compute the new sector number
1476 */
1477 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1478 return new_sector;
1479}
1480
1481
NeilBrown784052e2009-03-31 15:19:07 +11001482static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001485 int raid_disks = sh->disks;
1486 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sector_t new_sector = sh->sector, check;
NeilBrown784052e2009-03-31 15:19:07 +11001488 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1489 : (conf->chunk_size >> 9);
NeilBrowne183eae2009-03-31 15:20:22 +11001490 int algorithm = previous ? conf->prev_algo
1491 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 sector_t stripe;
1493 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001494 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001496 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
NeilBrown16a53ec2006-06-26 00:27:38 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1500 stripe = new_sector;
1501 BUG_ON(new_sector != stripe);
1502
NeilBrown16a53ec2006-06-26 00:27:38 -07001503 if (i == sh->pd_idx)
1504 return 0;
1505 switch(conf->level) {
1506 case 4: break;
1507 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001508 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 case ALGORITHM_LEFT_ASYMMETRIC:
1510 case ALGORITHM_RIGHT_ASYMMETRIC:
1511 if (i > sh->pd_idx)
1512 i--;
1513 break;
1514 case ALGORITHM_LEFT_SYMMETRIC:
1515 case ALGORITHM_RIGHT_SYMMETRIC:
1516 if (i < sh->pd_idx)
1517 i += raid_disks;
1518 i -= (sh->pd_idx + 1);
1519 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001520 case ALGORITHM_PARITY_0:
1521 i -= 1;
1522 break;
1523 case ALGORITHM_PARITY_N:
1524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001526 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001527 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001528 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001529 }
1530 break;
1531 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001532 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001533 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001534 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001535 case ALGORITHM_LEFT_ASYMMETRIC:
1536 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001537 case ALGORITHM_ROTATING_ZERO_RESTART:
1538 case ALGORITHM_ROTATING_N_RESTART:
1539 if (sh->pd_idx == raid_disks-1)
1540 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001541 else if (i > sh->pd_idx)
1542 i -= 2; /* D D P Q D */
1543 break;
1544 case ALGORITHM_LEFT_SYMMETRIC:
1545 case ALGORITHM_RIGHT_SYMMETRIC:
1546 if (sh->pd_idx == raid_disks-1)
1547 i--; /* Q D D D P */
1548 else {
1549 /* D D P Q D */
1550 if (i < sh->pd_idx)
1551 i += raid_disks;
1552 i -= (sh->pd_idx + 2);
1553 }
1554 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001555 case ALGORITHM_PARITY_0:
1556 i -= 2;
1557 break;
1558 case ALGORITHM_PARITY_N:
1559 break;
1560 case ALGORITHM_ROTATING_N_CONTINUE:
1561 if (sh->pd_idx == 0)
1562 i--; /* P D D D Q */
1563 else if (i > sh->pd_idx)
1564 i -= 2; /* D D Q P D */
1565 break;
1566 case ALGORITHM_LEFT_ASYMMETRIC_6:
1567 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1568 if (i > sh->pd_idx)
1569 i--;
1570 break;
1571 case ALGORITHM_LEFT_SYMMETRIC_6:
1572 case ALGORITHM_RIGHT_SYMMETRIC_6:
1573 if (i < sh->pd_idx)
1574 i += data_disks + 1;
1575 i -= (sh->pd_idx + 1);
1576 break;
1577 case ALGORITHM_PARITY_0_6:
1578 i -= 1;
1579 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001580 default:
NeilBrownd710e132008-10-13 11:55:12 +11001581 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001582 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001583 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001584 }
1585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 chunk_number = stripe * data_disks + i;
1589 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1590
NeilBrown112bf892009-03-31 14:39:38 +11001591 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001592 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001593 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1594 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001595 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return 0;
1597 }
1598 return r_sector;
1599}
1600
1601
1602
1603/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001604 * Copy data between a page in the stripe cache, and one or more bion
1605 * The page could align with the middle of the bio, or there could be
1606 * several bion, each with several bio_vecs, which cover part of the page
1607 * Multiple bion are linked together on bi_next. There may be extras
1608 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
1610static void copy_data(int frombio, struct bio *bio,
1611 struct page *page,
1612 sector_t sector)
1613{
1614 char *pa = page_address(page);
1615 struct bio_vec *bvl;
1616 int i;
1617 int page_offset;
1618
1619 if (bio->bi_sector >= sector)
1620 page_offset = (signed)(bio->bi_sector - sector) * 512;
1621 else
1622 page_offset = (signed)(sector - bio->bi_sector) * -512;
1623 bio_for_each_segment(bvl, bio, i) {
1624 int len = bio_iovec_idx(bio,i)->bv_len;
1625 int clen;
1626 int b_offset = 0;
1627
1628 if (page_offset < 0) {
1629 b_offset = -page_offset;
1630 page_offset += b_offset;
1631 len -= b_offset;
1632 }
1633
1634 if (len > 0 && page_offset + len > STRIPE_SIZE)
1635 clen = STRIPE_SIZE - page_offset;
1636 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (clen > 0) {
1639 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1640 if (frombio)
1641 memcpy(pa+page_offset, ba+b_offset, clen);
1642 else
1643 memcpy(ba+b_offset, pa+page_offset, clen);
1644 __bio_kunmap_atomic(ba, KM_USER0);
1645 }
1646 if (clen < len) /* hit end of page */
1647 break;
1648 page_offset += len;
1649 }
1650}
1651
Dan Williams9bc89cd2007-01-02 11:10:44 -07001652#define check_xor() do { \
1653 if (count == MAX_XOR_BLOCKS) { \
1654 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1655 count = 0; \
1656 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 } while(0)
1658
NeilBrown16a53ec2006-06-26 00:27:38 -07001659static void compute_parity6(struct stripe_head *sh, int method)
1660{
NeilBrownbff61972009-03-31 14:33:13 +11001661 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001662 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown67cc2b82009-03-31 14:39:38 +11001663 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
NeilBrown16a53ec2006-06-26 00:27:38 -07001664 struct bio *chosen;
1665 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001666 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001667
NeilBrownd0dabf72009-03-31 14:39:38 +11001668 pd_idx = sh->pd_idx;
1669 qd_idx = sh->qd_idx;
1670 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001671
Dan Williams45b42332007-07-09 11:56:43 -07001672 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001673 (unsigned long long)sh->sector, method);
1674
1675 switch(method) {
1676 case READ_MODIFY_WRITE:
1677 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1678 case RECONSTRUCT_WRITE:
1679 for (i= disks; i-- ;)
1680 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1681 chosen = sh->dev[i].towrite;
1682 sh->dev[i].towrite = NULL;
1683
1684 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1685 wake_up(&conf->wait_for_overlap);
1686
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001687 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001688 sh->dev[i].written = chosen;
1689 }
1690 break;
1691 case CHECK_PARITY:
1692 BUG(); /* Not implemented yet */
1693 }
1694
1695 for (i = disks; i--;)
1696 if (sh->dev[i].written) {
1697 sector_t sector = sh->dev[i].sector;
1698 struct bio *wbi = sh->dev[i].written;
1699 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1700 copy_data(1, wbi, sh->dev[i].page, sector);
1701 wbi = r5_next_bio(wbi, sector);
1702 }
1703
1704 set_bit(R5_LOCKED, &sh->dev[i].flags);
1705 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1706 }
1707
NeilBrownd0dabf72009-03-31 14:39:38 +11001708 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
NeilBrown67cc2b82009-03-31 14:39:38 +11001709
1710 for (i = 0; i < disks; i++)
1711 ptrs[i] = (void *)raid6_empty_zero_page;
1712
NeilBrownd0dabf72009-03-31 14:39:38 +11001713 count = 0;
1714 i = d0_idx;
1715 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001716 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1717
NeilBrownd0dabf72009-03-31 14:39:38 +11001718 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001719 if (slot < syndrome_disks &&
NeilBrownd0dabf72009-03-31 14:39:38 +11001720 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1721 printk(KERN_ERR "block %d/%d not uptodate "
1722 "on parity calc\n", i, count);
1723 BUG();
1724 }
NeilBrown67cc2b82009-03-31 14:39:38 +11001725
NeilBrownd0dabf72009-03-31 14:39:38 +11001726 i = raid6_next_disk(i, disks);
1727 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001728 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001729
NeilBrown67cc2b82009-03-31 14:39:38 +11001730 raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001731
1732 switch(method) {
1733 case RECONSTRUCT_WRITE:
1734 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1735 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1736 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1737 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1738 break;
1739 case UPDATE_PARITY:
1740 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1741 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1742 break;
1743 }
1744}
1745
1746
1747/* Compute one missing block */
1748static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1749{
NeilBrownf4168852007-02-28 20:11:53 -08001750 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001751 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11001752 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07001753
Dan Williams45b42332007-07-09 11:56:43 -07001754 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001755 (unsigned long long)sh->sector, dd_idx);
1756
1757 if ( dd_idx == qd_idx ) {
1758 /* We're actually computing the Q drive */
1759 compute_parity6(sh, UPDATE_PARITY);
1760 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001761 dest = page_address(sh->dev[dd_idx].page);
1762 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1763 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001764 for (i = disks ; i--; ) {
1765 if (i == dd_idx || i == qd_idx)
1766 continue;
1767 p = page_address(sh->dev[i].page);
1768 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1769 ptr[count++] = p;
1770 else
1771 printk("compute_block() %d, stripe %llu, %d"
1772 " not present\n", dd_idx,
1773 (unsigned long long)sh->sector, i);
1774
1775 check_xor();
1776 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001777 if (count)
1778 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001779 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1780 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1781 }
1782}
1783
1784/* Compute two missing blocks */
1785static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1786{
NeilBrownf4168852007-02-28 20:11:53 -08001787 int i, count, disks = sh->disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001788 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
NeilBrownd0dabf72009-03-31 14:39:38 +11001789 int d0_idx = raid6_d0(sh);
1790 int faila = -1, failb = -1;
1791 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001792 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001793
NeilBrown67cc2b82009-03-31 14:39:38 +11001794 for (i = 0; i < disks ; i++)
1795 ptrs[i] = (void *)raid6_empty_zero_page;
NeilBrownd0dabf72009-03-31 14:39:38 +11001796 count = 0;
1797 i = d0_idx;
1798 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001799 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1800
NeilBrownd0dabf72009-03-31 14:39:38 +11001801 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001802
NeilBrownd0dabf72009-03-31 14:39:38 +11001803 if (i == dd_idx1)
1804 faila = slot;
1805 if (i == dd_idx2)
1806 failb = slot;
1807 i = raid6_next_disk(i, disks);
1808 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001809 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001810
1811 BUG_ON(faila == failb);
1812 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1813
Dan Williams45b42332007-07-09 11:56:43 -07001814 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11001815 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
1816 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07001817
NeilBrown67cc2b82009-03-31 14:39:38 +11001818 if (failb == syndrome_disks+1) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001819 /* Q disk is one of the missing disks */
NeilBrown67cc2b82009-03-31 14:39:38 +11001820 if (faila == syndrome_disks) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001821 /* Missing P+Q, just recompute */
1822 compute_parity6(sh, UPDATE_PARITY);
1823 return;
1824 } else {
1825 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11001826 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
1827 dd_idx2 : dd_idx1),
1828 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07001829 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1830 return;
1831 }
1832 }
1833
NeilBrownd0dabf72009-03-31 14:39:38 +11001834 /* We're missing D+P or D+D; */
NeilBrown67cc2b82009-03-31 14:39:38 +11001835 if (failb == syndrome_disks) {
NeilBrownd0dabf72009-03-31 14:39:38 +11001836 /* We're missing D+P. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001837 raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
NeilBrownd0dabf72009-03-31 14:39:38 +11001838 } else {
1839 /* We're missing D+D. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001840 raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
1841 ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001842 }
NeilBrownd0dabf72009-03-31 14:39:38 +11001843
1844 /* Both the above update both missing blocks */
1845 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1846 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07001847}
1848
Dan Williams600aa102008-06-28 08:32:05 +10001849static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001850schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001851 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001852{
1853 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001854
Dan Williamse33129d2007-01-02 13:52:30 -07001855 if (rcw) {
1856 /* if we are not expanding this is a proper write request, and
1857 * there will be bios with new data to be drained into the
1858 * stripe cache
1859 */
1860 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001861 sh->reconstruct_state = reconstruct_state_drain_run;
1862 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1863 } else
1864 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001865
Dan Williams600aa102008-06-28 08:32:05 +10001866 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001867
1868 for (i = disks; i--; ) {
1869 struct r5dev *dev = &sh->dev[i];
1870
1871 if (dev->towrite) {
1872 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001873 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001874 if (!expand)
1875 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001876 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001877 }
1878 }
Dan Williams600aa102008-06-28 08:32:05 +10001879 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001880 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1881 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001882 } else {
1883 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1884 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1885
Dan Williamsd8ee0722008-06-28 08:32:06 +10001886 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001887 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1888 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1889 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001890
1891 for (i = disks; i--; ) {
1892 struct r5dev *dev = &sh->dev[i];
1893 if (i == pd_idx)
1894 continue;
1895
Dan Williamse33129d2007-01-02 13:52:30 -07001896 if (dev->towrite &&
1897 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001898 test_bit(R5_Wantcompute, &dev->flags))) {
1899 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001900 set_bit(R5_LOCKED, &dev->flags);
1901 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001902 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001903 }
1904 }
1905 }
1906
1907 /* keep the parity disk locked while asynchronous operations
1908 * are in flight
1909 */
1910 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1911 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001912 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001913
Dan Williams600aa102008-06-28 08:32:05 +10001914 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001915 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001916 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001917}
NeilBrown16a53ec2006-06-26 00:27:38 -07001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/*
1920 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001921 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * The bi_next chain must be in order.
1923 */
1924static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1925{
1926 struct bio **bip;
1927 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001928 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Dan Williams45b42332007-07-09 11:56:43 -07001930 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 (unsigned long long)bi->bi_sector,
1932 (unsigned long long)sh->sector);
1933
1934
1935 spin_lock(&sh->lock);
1936 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001937 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001939 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1940 firstwrite = 1;
1941 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 bip = &sh->dev[dd_idx].toread;
1943 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1944 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1945 goto overlap;
1946 bip = & (*bip)->bi_next;
1947 }
1948 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1949 goto overlap;
1950
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001951 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (*bip)
1953 bi->bi_next = *bip;
1954 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02001955 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 spin_unlock_irq(&conf->device_lock);
1957 spin_unlock(&sh->lock);
1958
Dan Williams45b42332007-07-09 11:56:43 -07001959 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 (unsigned long long)bi->bi_sector,
1961 (unsigned long long)sh->sector, dd_idx);
1962
NeilBrown72626682005-09-09 16:23:54 -07001963 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001964 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1965 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001966 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001967 set_bit(STRIPE_BIT_DELAY, &sh->state);
1968 }
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (forwrite) {
1971 /* check if page is covered */
1972 sector_t sector = sh->dev[dd_idx].sector;
1973 for (bi=sh->dev[dd_idx].towrite;
1974 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1975 bi && bi->bi_sector <= sector;
1976 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1977 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1978 sector = bi->bi_sector + (bi->bi_size>>9);
1979 }
1980 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1981 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1982 }
1983 return 1;
1984
1985 overlap:
1986 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1987 spin_unlock_irq(&conf->device_lock);
1988 spin_unlock(&sh->lock);
1989 return 0;
1990}
1991
NeilBrown29269552006-03-27 01:18:10 -08001992static void end_reshape(raid5_conf_t *conf);
1993
NeilBrown16a53ec2006-06-26 00:27:38 -07001994static int page_is_zero(struct page *p)
1995{
1996 char *a = page_address(p);
1997 return ((*(u32*)a) == 0 &&
1998 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1999}
2000
NeilBrown911d4ee2009-03-31 14:39:38 +11002001static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2002 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002003{
NeilBrown784052e2009-03-31 15:19:07 +11002004 int sectors_per_chunk =
2005 previous ? (conf->prev_chunk >> 9)
2006 : (conf->chunk_size >> 9);
NeilBrown911d4ee2009-03-31 14:39:38 +11002007 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002008 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002009 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002010
NeilBrown112bf892009-03-31 14:39:38 +11002011 raid5_compute_sector(conf,
2012 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002013 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002014 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002015 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002016}
2017
Dan Williamsa4456852007-07-09 11:56:43 -07002018static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002019handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002020 struct stripe_head_state *s, int disks,
2021 struct bio **return_bi)
2022{
2023 int i;
2024 for (i = disks; i--; ) {
2025 struct bio *bi;
2026 int bitmap_end = 0;
2027
2028 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2029 mdk_rdev_t *rdev;
2030 rcu_read_lock();
2031 rdev = rcu_dereference(conf->disks[i].rdev);
2032 if (rdev && test_bit(In_sync, &rdev->flags))
2033 /* multiple read failures in one stripe */
2034 md_error(conf->mddev, rdev);
2035 rcu_read_unlock();
2036 }
2037 spin_lock_irq(&conf->device_lock);
2038 /* fail all writes first */
2039 bi = sh->dev[i].towrite;
2040 sh->dev[i].towrite = NULL;
2041 if (bi) {
2042 s->to_write--;
2043 bitmap_end = 1;
2044 }
2045
2046 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2047 wake_up(&conf->wait_for_overlap);
2048
2049 while (bi && bi->bi_sector <
2050 sh->dev[i].sector + STRIPE_SECTORS) {
2051 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2052 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002053 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002054 md_write_end(conf->mddev);
2055 bi->bi_next = *return_bi;
2056 *return_bi = bi;
2057 }
2058 bi = nextbi;
2059 }
2060 /* and fail all 'written' */
2061 bi = sh->dev[i].written;
2062 sh->dev[i].written = NULL;
2063 if (bi) bitmap_end = 1;
2064 while (bi && bi->bi_sector <
2065 sh->dev[i].sector + STRIPE_SECTORS) {
2066 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2067 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002068 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002069 md_write_end(conf->mddev);
2070 bi->bi_next = *return_bi;
2071 *return_bi = bi;
2072 }
2073 bi = bi2;
2074 }
2075
Dan Williamsb5e98d62007-01-02 13:52:31 -07002076 /* fail any reads if this device is non-operational and
2077 * the data has not reached the cache yet.
2078 */
2079 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2080 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2081 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002082 bi = sh->dev[i].toread;
2083 sh->dev[i].toread = NULL;
2084 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2085 wake_up(&conf->wait_for_overlap);
2086 if (bi) s->to_read--;
2087 while (bi && bi->bi_sector <
2088 sh->dev[i].sector + STRIPE_SECTORS) {
2089 struct bio *nextbi =
2090 r5_next_bio(bi, sh->dev[i].sector);
2091 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002092 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002093 bi->bi_next = *return_bi;
2094 *return_bi = bi;
2095 }
2096 bi = nextbi;
2097 }
2098 }
2099 spin_unlock_irq(&conf->device_lock);
2100 if (bitmap_end)
2101 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2102 STRIPE_SECTORS, 0, 0);
2103 }
2104
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002105 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2106 if (atomic_dec_and_test(&conf->pending_full_writes))
2107 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002108}
2109
Dan Williams1fe797e2008-06-28 09:16:30 +10002110/* fetch_block5 - checks the given member device to see if its data needs
2111 * to be read or computed to satisfy a request.
2112 *
2113 * Returns 1 when no more member devices need to be checked, otherwise returns
2114 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002115 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002116static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2117 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002118{
2119 struct r5dev *dev = &sh->dev[disk_idx];
2120 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2121
Dan Williamsf38e1212007-01-02 13:52:30 -07002122 /* is the data in this block needed, and can we get it? */
2123 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002124 !test_bit(R5_UPTODATE, &dev->flags) &&
2125 (dev->toread ||
2126 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2127 s->syncing || s->expanding ||
2128 (s->failed &&
2129 (failed_dev->toread ||
2130 (failed_dev->towrite &&
2131 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002132 /* We would like to get this block, possibly by computing it,
2133 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002134 */
2135 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002136 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002137 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2138 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002139 set_bit(R5_Wantcompute, &dev->flags);
2140 sh->ops.target = disk_idx;
2141 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002142 /* Careful: from this point on 'uptodate' is in the eye
2143 * of raid5_run_ops which services 'compute' operations
2144 * before writes. R5_Wantcompute flags a block that will
2145 * be R5_UPTODATE by the time it is needed for a
2146 * subsequent operation.
2147 */
2148 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002149 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002150 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002151 set_bit(R5_LOCKED, &dev->flags);
2152 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002153 s->locked++;
2154 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2155 s->syncing);
2156 }
2157 }
2158
Dan Williams1fe797e2008-06-28 09:16:30 +10002159 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002160}
2161
Dan Williams1fe797e2008-06-28 09:16:30 +10002162/**
2163 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2164 */
2165static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002166 struct stripe_head_state *s, int disks)
2167{
2168 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002169
Dan Williamsf38e1212007-01-02 13:52:30 -07002170 /* look for blocks to read/compute, skip this if a compute
2171 * is already in flight, or if the stripe contents are in the
2172 * midst of changing due to a write
2173 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002174 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002175 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002176 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002177 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002178 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002179 set_bit(STRIPE_HANDLE, &sh->state);
2180}
2181
Dan Williams1fe797e2008-06-28 09:16:30 +10002182static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002183 struct stripe_head_state *s, struct r6_state *r6s,
2184 int disks)
2185{
2186 int i;
2187 for (i = disks; i--; ) {
2188 struct r5dev *dev = &sh->dev[i];
2189 if (!test_bit(R5_LOCKED, &dev->flags) &&
2190 !test_bit(R5_UPTODATE, &dev->flags) &&
2191 (dev->toread || (dev->towrite &&
2192 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2193 s->syncing || s->expanding ||
2194 (s->failed >= 1 &&
2195 (sh->dev[r6s->failed_num[0]].toread ||
2196 s->to_write)) ||
2197 (s->failed >= 2 &&
2198 (sh->dev[r6s->failed_num[1]].toread ||
2199 s->to_write)))) {
2200 /* we would like to get this block, possibly
2201 * by computing it, but we might not be able to
2202 */
Dan Williamsc3378692008-06-05 22:45:54 -07002203 if ((s->uptodate == disks - 1) &&
2204 (s->failed && (i == r6s->failed_num[0] ||
2205 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07002206 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002207 (unsigned long long)sh->sector, i);
2208 compute_block_1(sh, i, 0);
2209 s->uptodate++;
2210 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2211 /* Computing 2-failure is *very* expensive; only
2212 * do it if failed >= 2
2213 */
2214 int other;
2215 for (other = disks; other--; ) {
2216 if (other == i)
2217 continue;
2218 if (!test_bit(R5_UPTODATE,
2219 &sh->dev[other].flags))
2220 break;
2221 }
2222 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07002223 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002224 (unsigned long long)sh->sector,
2225 i, other);
2226 compute_block_2(sh, i, other);
2227 s->uptodate += 2;
2228 } else if (test_bit(R5_Insync, &dev->flags)) {
2229 set_bit(R5_LOCKED, &dev->flags);
2230 set_bit(R5_Wantread, &dev->flags);
2231 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002232 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002233 i, s->syncing);
2234 }
2235 }
2236 }
2237 set_bit(STRIPE_HANDLE, &sh->state);
2238}
2239
2240
Dan Williams1fe797e2008-06-28 09:16:30 +10002241/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002242 * any written block on an uptodate or failed drive can be returned.
2243 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2244 * never LOCKED, so we don't need to test 'failed' directly.
2245 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002246static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002247 struct stripe_head *sh, int disks, struct bio **return_bi)
2248{
2249 int i;
2250 struct r5dev *dev;
2251
2252 for (i = disks; i--; )
2253 if (sh->dev[i].written) {
2254 dev = &sh->dev[i];
2255 if (!test_bit(R5_LOCKED, &dev->flags) &&
2256 test_bit(R5_UPTODATE, &dev->flags)) {
2257 /* We can return any write requests */
2258 struct bio *wbi, *wbi2;
2259 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002260 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002261 spin_lock_irq(&conf->device_lock);
2262 wbi = dev->written;
2263 dev->written = NULL;
2264 while (wbi && wbi->bi_sector <
2265 dev->sector + STRIPE_SECTORS) {
2266 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002267 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002268 md_write_end(conf->mddev);
2269 wbi->bi_next = *return_bi;
2270 *return_bi = wbi;
2271 }
2272 wbi = wbi2;
2273 }
2274 if (dev->towrite == NULL)
2275 bitmap_end = 1;
2276 spin_unlock_irq(&conf->device_lock);
2277 if (bitmap_end)
2278 bitmap_endwrite(conf->mddev->bitmap,
2279 sh->sector,
2280 STRIPE_SECTORS,
2281 !test_bit(STRIPE_DEGRADED, &sh->state),
2282 0);
2283 }
2284 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002285
2286 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2287 if (atomic_dec_and_test(&conf->pending_full_writes))
2288 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002289}
2290
Dan Williams1fe797e2008-06-28 09:16:30 +10002291static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002292 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2293{
2294 int rmw = 0, rcw = 0, i;
2295 for (i = disks; i--; ) {
2296 /* would I have to read this buffer for read_modify_write */
2297 struct r5dev *dev = &sh->dev[i];
2298 if ((dev->towrite || i == sh->pd_idx) &&
2299 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002300 !(test_bit(R5_UPTODATE, &dev->flags) ||
2301 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002302 if (test_bit(R5_Insync, &dev->flags))
2303 rmw++;
2304 else
2305 rmw += 2*disks; /* cannot read it */
2306 }
2307 /* Would I have to read this buffer for reconstruct_write */
2308 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2309 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002310 !(test_bit(R5_UPTODATE, &dev->flags) ||
2311 test_bit(R5_Wantcompute, &dev->flags))) {
2312 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002313 else
2314 rcw += 2*disks;
2315 }
2316 }
Dan Williams45b42332007-07-09 11:56:43 -07002317 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002318 (unsigned long long)sh->sector, rmw, rcw);
2319 set_bit(STRIPE_HANDLE, &sh->state);
2320 if (rmw < rcw && rmw > 0)
2321 /* prefer read-modify-write, but need to get some data */
2322 for (i = disks; i--; ) {
2323 struct r5dev *dev = &sh->dev[i];
2324 if ((dev->towrite || i == sh->pd_idx) &&
2325 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002326 !(test_bit(R5_UPTODATE, &dev->flags) ||
2327 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002328 test_bit(R5_Insync, &dev->flags)) {
2329 if (
2330 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002331 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002332 "%d for r-m-w\n", i);
2333 set_bit(R5_LOCKED, &dev->flags);
2334 set_bit(R5_Wantread, &dev->flags);
2335 s->locked++;
2336 } else {
2337 set_bit(STRIPE_DELAYED, &sh->state);
2338 set_bit(STRIPE_HANDLE, &sh->state);
2339 }
2340 }
2341 }
2342 if (rcw <= rmw && rcw > 0)
2343 /* want reconstruct write, but need to get some data */
2344 for (i = disks; i--; ) {
2345 struct r5dev *dev = &sh->dev[i];
2346 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2347 i != sh->pd_idx &&
2348 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002349 !(test_bit(R5_UPTODATE, &dev->flags) ||
2350 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002351 test_bit(R5_Insync, &dev->flags)) {
2352 if (
2353 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002354 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002355 "%d for Reconstruct\n", i);
2356 set_bit(R5_LOCKED, &dev->flags);
2357 set_bit(R5_Wantread, &dev->flags);
2358 s->locked++;
2359 } else {
2360 set_bit(STRIPE_DELAYED, &sh->state);
2361 set_bit(STRIPE_HANDLE, &sh->state);
2362 }
2363 }
2364 }
2365 /* now if nothing is locked, and if we have enough data,
2366 * we can start a write request
2367 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002368 /* since handle_stripe can be called at any time we need to handle the
2369 * case where a compute block operation has been submitted and then a
2370 * subsequent call wants to start a write request. raid5_run_ops only
2371 * handles the case where compute block and postxor are requested
2372 * simultaneously. If this is not the case then new writes need to be
2373 * held off until the compute completes.
2374 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002375 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2376 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2377 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002378 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002379}
2380
Dan Williams1fe797e2008-06-28 09:16:30 +10002381static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002382 struct stripe_head *sh, struct stripe_head_state *s,
2383 struct r6_state *r6s, int disks)
2384{
2385 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002386 int qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07002387 for (i = disks; i--; ) {
2388 struct r5dev *dev = &sh->dev[i];
2389 /* Would I have to read this buffer for reconstruct_write */
2390 if (!test_bit(R5_OVERWRITE, &dev->flags)
2391 && i != pd_idx && i != qd_idx
2392 && (!test_bit(R5_LOCKED, &dev->flags)
2393 ) &&
2394 !test_bit(R5_UPTODATE, &dev->flags)) {
2395 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2396 else {
Dan Williams45b42332007-07-09 11:56:43 -07002397 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002398 "disk %d flags=%#lx\n", i, dev->flags);
2399 must_compute++;
2400 }
2401 }
2402 }
Dan Williams45b42332007-07-09 11:56:43 -07002403 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002404 (unsigned long long)sh->sector, rcw, must_compute);
2405 set_bit(STRIPE_HANDLE, &sh->state);
2406
2407 if (rcw > 0)
2408 /* want reconstruct write, but need to get some data */
2409 for (i = disks; i--; ) {
2410 struct r5dev *dev = &sh->dev[i];
2411 if (!test_bit(R5_OVERWRITE, &dev->flags)
2412 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2413 && !test_bit(R5_LOCKED, &dev->flags) &&
2414 !test_bit(R5_UPTODATE, &dev->flags) &&
2415 test_bit(R5_Insync, &dev->flags)) {
2416 if (
2417 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002418 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002419 "block %d for Reconstruct\n",
2420 (unsigned long long)sh->sector, i);
2421 set_bit(R5_LOCKED, &dev->flags);
2422 set_bit(R5_Wantread, &dev->flags);
2423 s->locked++;
2424 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002425 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002426 "block %d for Reconstruct\n",
2427 (unsigned long long)sh->sector, i);
2428 set_bit(STRIPE_DELAYED, &sh->state);
2429 set_bit(STRIPE_HANDLE, &sh->state);
2430 }
2431 }
2432 }
2433 /* now if nothing is locked, and if we have enough data, we can start a
2434 * write request
2435 */
2436 if (s->locked == 0 && rcw == 0 &&
2437 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2438 if (must_compute > 0) {
2439 /* We have failed blocks and need to compute them */
2440 switch (s->failed) {
2441 case 0:
2442 BUG();
2443 case 1:
2444 compute_block_1(sh, r6s->failed_num[0], 0);
2445 break;
2446 case 2:
2447 compute_block_2(sh, r6s->failed_num[0],
2448 r6s->failed_num[1]);
2449 break;
2450 default: /* This request should have been failed? */
2451 BUG();
2452 }
2453 }
2454
Dan Williams45b42332007-07-09 11:56:43 -07002455 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002456 (unsigned long long)sh->sector);
2457 compute_parity6(sh, RECONSTRUCT_WRITE);
2458 /* now every locked buffer is ready to be written */
2459 for (i = disks; i--; )
2460 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002461 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002462 (unsigned long long)sh->sector, i);
2463 s->locked++;
2464 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2465 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002466 if (s->locked == disks)
2467 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2468 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002469 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2470 set_bit(STRIPE_INSYNC, &sh->state);
2471
2472 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2473 atomic_dec(&conf->preread_active_stripes);
2474 if (atomic_read(&conf->preread_active_stripes) <
2475 IO_THRESHOLD)
2476 md_wakeup_thread(conf->mddev->thread);
2477 }
2478 }
2479}
2480
2481static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2482 struct stripe_head_state *s, int disks)
2483{
Dan Williamsecc65c92008-06-28 08:31:57 +10002484 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002485
Dan Williamsbd2ab672008-04-10 21:29:27 -07002486 set_bit(STRIPE_HANDLE, &sh->state);
2487
Dan Williamsecc65c92008-06-28 08:31:57 +10002488 switch (sh->check_state) {
2489 case check_state_idle:
2490 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002491 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002492 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002493 sh->check_state = check_state_run;
2494 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002495 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002496 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002497 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002498 }
Dan Williamsa4456852007-07-09 11:56:43 -07002499 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002500 /* fall through */
2501 case check_state_compute_result:
2502 sh->check_state = check_state_idle;
2503 if (!dev)
2504 dev = &sh->dev[sh->pd_idx];
2505
2506 /* check that a write has not made the stripe insync */
2507 if (test_bit(STRIPE_INSYNC, &sh->state))
2508 break;
2509
2510 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002511 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2512 BUG_ON(s->uptodate != disks);
2513
2514 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002515 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002516 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002517
Dan Williamsa4456852007-07-09 11:56:43 -07002518 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002519 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002520 break;
2521 case check_state_run:
2522 break; /* we will be called again upon completion */
2523 case check_state_check_result:
2524 sh->check_state = check_state_idle;
2525
2526 /* if a failure occurred during the check operation, leave
2527 * STRIPE_INSYNC not set and let the stripe be handled again
2528 */
2529 if (s->failed)
2530 break;
2531
2532 /* handle a successful check operation, if parity is correct
2533 * we are done. Otherwise update the mismatch count and repair
2534 * parity if !MD_RECOVERY_CHECK
2535 */
2536 if (sh->ops.zero_sum_result == 0)
2537 /* parity is correct (on disc,
2538 * not in buffer any more)
2539 */
2540 set_bit(STRIPE_INSYNC, &sh->state);
2541 else {
2542 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2543 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2544 /* don't try to repair!! */
2545 set_bit(STRIPE_INSYNC, &sh->state);
2546 else {
2547 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002548 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002549 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2550 set_bit(R5_Wantcompute,
2551 &sh->dev[sh->pd_idx].flags);
2552 sh->ops.target = sh->pd_idx;
2553 s->uptodate++;
2554 }
2555 }
2556 break;
2557 case check_state_compute_run:
2558 break;
2559 default:
2560 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2561 __func__, sh->check_state,
2562 (unsigned long long) sh->sector);
2563 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002564 }
2565}
2566
2567
2568static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002569 struct stripe_head_state *s,
2570 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002571{
2572 int update_p = 0, update_q = 0;
2573 struct r5dev *dev;
2574 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002575 int qd_idx = sh->qd_idx;
Dan Williams36d1c642009-07-14 11:48:22 -07002576 unsigned long cpu;
2577 struct page *tmp_page;
Dan Williamsa4456852007-07-09 11:56:43 -07002578
2579 set_bit(STRIPE_HANDLE, &sh->state);
2580
2581 BUG_ON(s->failed > 2);
2582 BUG_ON(s->uptodate < disks);
2583 /* Want to check and possibly repair P and Q.
2584 * However there could be one 'failed' device, in which
2585 * case we can only check one of them, possibly using the
2586 * other to generate missing data
2587 */
Dan Williams36d1c642009-07-14 11:48:22 -07002588 cpu = get_cpu();
2589 tmp_page = per_cpu_ptr(conf->percpu, cpu)->spare_page;
2590 if (s->failed == r6s->q_failed) {
2591 /* The only possible failed device holds 'Q', so it
2592 * makes sense to check P (If anything else were failed,
2593 * we would have used P to recreate it).
Dan Williamsa4456852007-07-09 11:56:43 -07002594 */
Dan Williams36d1c642009-07-14 11:48:22 -07002595 compute_block_1(sh, pd_idx, 1);
2596 if (!page_is_zero(sh->dev[pd_idx].page)) {
2597 compute_block_1(sh, pd_idx, 0);
2598 update_p = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002599 }
Dan Williamsa4456852007-07-09 11:56:43 -07002600 }
Dan Williams36d1c642009-07-14 11:48:22 -07002601 if (!r6s->q_failed && s->failed < 2) {
2602 /* q is not failed, and we didn't use it to generate
2603 * anything, so it makes sense to check it
2604 */
2605 memcpy(page_address(tmp_page),
2606 page_address(sh->dev[qd_idx].page),
2607 STRIPE_SIZE);
2608 compute_parity6(sh, UPDATE_PARITY);
2609 if (memcmp(page_address(tmp_page),
2610 page_address(sh->dev[qd_idx].page),
2611 STRIPE_SIZE) != 0) {
2612 clear_bit(STRIPE_INSYNC, &sh->state);
2613 update_q = 1;
2614 }
2615 }
2616 put_cpu();
2617
2618 if (update_p || update_q) {
2619 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2620 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2621 /* don't try to repair!! */
2622 update_p = update_q = 0;
2623 }
2624
2625 /* now write out any block on a failed drive,
2626 * or P or Q if they need it
2627 */
2628
2629 if (s->failed == 2) {
2630 dev = &sh->dev[r6s->failed_num[1]];
2631 s->locked++;
2632 set_bit(R5_LOCKED, &dev->flags);
2633 set_bit(R5_Wantwrite, &dev->flags);
2634 }
2635 if (s->failed >= 1) {
2636 dev = &sh->dev[r6s->failed_num[0]];
2637 s->locked++;
2638 set_bit(R5_LOCKED, &dev->flags);
2639 set_bit(R5_Wantwrite, &dev->flags);
2640 }
2641
2642 if (update_p) {
2643 dev = &sh->dev[pd_idx];
2644 s->locked++;
2645 set_bit(R5_LOCKED, &dev->flags);
2646 set_bit(R5_Wantwrite, &dev->flags);
2647 }
2648 if (update_q) {
2649 dev = &sh->dev[qd_idx];
2650 s->locked++;
2651 set_bit(R5_LOCKED, &dev->flags);
2652 set_bit(R5_Wantwrite, &dev->flags);
2653 }
2654 clear_bit(STRIPE_DEGRADED, &sh->state);
2655
2656 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002657}
2658
2659static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2660 struct r6_state *r6s)
2661{
2662 int i;
2663
2664 /* We have read all the blocks in this stripe and now we need to
2665 * copy some of them into a target stripe for expand.
2666 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002667 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002668 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2669 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002670 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002671 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002672 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002673 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002674
NeilBrown784052e2009-03-31 15:19:07 +11002675 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002676 sector_t s = raid5_compute_sector(conf, bn, 0,
2677 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11002678 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002679 if (sh2 == NULL)
2680 /* so far only the early blocks of this stripe
2681 * have been requested. When later blocks
2682 * get requested, we will try again
2683 */
2684 continue;
2685 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2686 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2687 /* must have already done this block */
2688 release_stripe(sh2);
2689 continue;
2690 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002691
2692 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002693 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002694 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002695 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002696 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002697
Dan Williamsa4456852007-07-09 11:56:43 -07002698 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2699 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2700 for (j = 0; j < conf->raid_disks; j++)
2701 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002702 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002703 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2704 break;
2705 if (j == conf->raid_disks) {
2706 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2707 set_bit(STRIPE_HANDLE, &sh2->state);
2708 }
2709 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002710
Dan Williamsa4456852007-07-09 11:56:43 -07002711 }
NeilBrowna2e08552007-09-11 15:23:36 -07002712 /* done submitting copies, wait for them to complete */
2713 if (tx) {
2714 async_tx_ack(tx);
2715 dma_wait_for_async_tx(tx);
2716 }
Dan Williamsa4456852007-07-09 11:56:43 -07002717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Dan Williams6bfe0b42008-04-30 00:52:32 -07002719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720/*
2721 * handle_stripe - do things to a stripe.
2722 *
2723 * We lock the stripe and then examine the state of various bits
2724 * to see what needs to be done.
2725 * Possible results:
2726 * return some read request which now have data
2727 * return some write requests which are safely on disc
2728 * schedule a read on some buffers
2729 * schedule a write of some buffers
2730 * return confirmation of parity correctness
2731 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 * buffers are taken off read_list or write_list, and bh_cache buffers
2733 * get BH_Lock set before the stripe lock is released.
2734 *
2735 */
Dan Williamsa4456852007-07-09 11:56:43 -07002736
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002737static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002740 int disks = sh->disks, i;
2741 struct bio *return_bi = NULL;
2742 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002744 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002745 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Dan Williamsa4456852007-07-09 11:56:43 -07002747 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002748 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2749 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2750 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2751 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 spin_lock(&sh->lock);
2754 clear_bit(STRIPE_HANDLE, &sh->state);
2755 clear_bit(STRIPE_DELAYED, &sh->state);
2756
Dan Williamsa4456852007-07-09 11:56:43 -07002757 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2758 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2759 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002762 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 for (i=disks; i--; ) {
2764 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002765 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Dan Williamsb5e98d62007-01-02 13:52:31 -07002768 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2769 "written %p\n", i, dev->flags, dev->toread, dev->read,
2770 dev->towrite, dev->written);
2771
2772 /* maybe we can request a biofill operation
2773 *
2774 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002775 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002776 */
2777 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002778 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002779 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002782 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2783 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002784 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Dan Williamsb5e98d62007-01-02 13:52:31 -07002786 if (test_bit(R5_Wantfill, &dev->flags))
2787 s.to_fill++;
2788 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002789 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002791 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002793 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Dan Williamsa4456852007-07-09 11:56:43 -07002795 if (dev->written)
2796 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002797 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002798 if (blocked_rdev == NULL &&
2799 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002800 blocked_rdev = rdev;
2801 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002802 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002803 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002804 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002805 clear_bit(R5_ReadError, &dev->flags);
2806 clear_bit(R5_ReWrite, &dev->flags);
2807 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002808 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002809 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002810 s.failed++;
2811 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 } else
2813 set_bit(R5_Insync, &dev->flags);
2814 }
NeilBrown9910f162006-01-06 00:20:24 -08002815 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002816
Dan Williams6bfe0b42008-04-30 00:52:32 -07002817 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002818 if (s.syncing || s.expanding || s.expanded ||
2819 s.to_write || s.written) {
2820 set_bit(STRIPE_HANDLE, &sh->state);
2821 goto unlock;
2822 }
2823 /* There is nothing for the blocked_rdev to block */
2824 rdev_dec_pending(blocked_rdev, conf->mddev);
2825 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002826 }
2827
Dan Williams83de75c2008-06-28 08:31:58 +10002828 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2829 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2830 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2831 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002832
Dan Williams45b42332007-07-09 11:56:43 -07002833 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002835 s.locked, s.uptodate, s.to_read, s.to_write,
2836 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 /* check if the array has lost two devices and, if so, some requests might
2838 * need to be failed
2839 */
Dan Williamsa4456852007-07-09 11:56:43 -07002840 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002841 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002842 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2844 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002845 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
2848 /* might be able to return some write requests if the parity block
2849 * is safe, or on a failed drive
2850 */
2851 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002852 if ( s.written &&
2853 ((test_bit(R5_Insync, &dev->flags) &&
2854 !test_bit(R5_LOCKED, &dev->flags) &&
2855 test_bit(R5_UPTODATE, &dev->flags)) ||
2856 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002857 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /* Now we might consider reading some blocks, either to check/generate
2860 * parity, or to satisfy requests
2861 * or to load a block that is being partially written.
2862 */
Dan Williamsa4456852007-07-09 11:56:43 -07002863 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002864 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002865 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Dan Williamse33129d2007-01-02 13:52:30 -07002867 /* Now we check to see if any write operations have recently
2868 * completed
2869 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002870 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002871 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07002872 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002873 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2874 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002875 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002876
2877 /* All the 'written' buffers and the parity block are ready to
2878 * be written back to disk
2879 */
2880 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2881 for (i = disks; i--; ) {
2882 dev = &sh->dev[i];
2883 if (test_bit(R5_LOCKED, &dev->flags) &&
2884 (i == sh->pd_idx || dev->written)) {
2885 pr_debug("Writing block %d\n", i);
2886 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002887 if (prexor)
2888 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002889 if (!test_bit(R5_Insync, &dev->flags) ||
2890 (i == sh->pd_idx && s.failed == 0))
2891 set_bit(STRIPE_INSYNC, &sh->state);
2892 }
2893 }
2894 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2895 atomic_dec(&conf->preread_active_stripes);
2896 if (atomic_read(&conf->preread_active_stripes) <
2897 IO_THRESHOLD)
2898 md_wakeup_thread(conf->mddev->thread);
2899 }
2900 }
2901
2902 /* Now to consider new write requests and what else, if anything
2903 * should be read. We do not handle new writes when:
2904 * 1/ A 'write' operation (copy+xor) is already in flight.
2905 * 2/ A 'check' operation is in flight, as it may clobber the parity
2906 * block.
2907 */
Dan Williams600aa102008-06-28 08:32:05 +10002908 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002909 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002912 * Any reads will already have been scheduled, so we just see if enough
2913 * data is available. The parity check is held off while parity
2914 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002916 if (sh->check_state ||
2917 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002918 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002919 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002920 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002921
Dan Williamsa4456852007-07-09 11:56:43 -07002922 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2924 clear_bit(STRIPE_SYNCING, &sh->state);
2925 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002926
2927 /* If the failed drive is just a ReadError, then we might need to progress
2928 * the repair/check process
2929 */
Dan Williamsa4456852007-07-09 11:56:43 -07002930 if (s.failed == 1 && !conf->mddev->ro &&
2931 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2932 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2933 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002934 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002935 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08002936 if (!test_bit(R5_ReWrite, &dev->flags)) {
2937 set_bit(R5_Wantwrite, &dev->flags);
2938 set_bit(R5_ReWrite, &dev->flags);
2939 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002940 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002941 } else {
2942 /* let's read it back */
2943 set_bit(R5_Wantread, &dev->flags);
2944 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002945 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002946 }
2947 }
2948
Dan Williams600aa102008-06-28 08:32:05 +10002949 /* Finish reconstruct operations initiated by the expansion process */
2950 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11002951 struct stripe_head *sh2
2952 = get_active_stripe(conf, sh->sector, 1, 1);
2953 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
2954 /* sh cannot be written until sh2 has been read.
2955 * so arrange for sh to be delayed a little
2956 */
2957 set_bit(STRIPE_DELAYED, &sh->state);
2958 set_bit(STRIPE_HANDLE, &sh->state);
2959 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
2960 &sh2->state))
2961 atomic_inc(&conf->preread_active_stripes);
2962 release_stripe(sh2);
2963 goto unlock;
2964 }
2965 if (sh2)
2966 release_stripe(sh2);
2967
Dan Williams600aa102008-06-28 08:32:05 +10002968 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002969 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07002970 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07002971 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07002972 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10002973 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07002974 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002975 }
2976
2977 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10002978 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002979 /* Need to write out all blocks after computing parity */
2980 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11002981 stripe_set_idx(sh->sector, conf, 0, sh);
Dan Williams1fe797e2008-06-28 09:16:30 +10002982 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10002983 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002984 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08002985 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002986 wake_up(&conf->wait_for_overlap);
2987 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2988 }
2989
Dan Williams0f94e872008-01-08 15:32:53 -08002990 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002991 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002992 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002993
Dan Williams6bfe0b42008-04-30 00:52:32 -07002994 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 spin_unlock(&sh->lock);
2996
Dan Williams6bfe0b42008-04-30 00:52:32 -07002997 /* wait for this device to become unblocked */
2998 if (unlikely(blocked_rdev))
2999 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3000
Dan Williams600aa102008-06-28 08:32:05 +10003001 if (s.ops_request)
3002 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003003
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003004 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Dan Williamsa4456852007-07-09 11:56:43 -07003006 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003007
3008 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Dan Williams36d1c642009-07-14 11:48:22 -07003011static bool handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003012{
NeilBrownbff61972009-03-31 14:33:13 +11003013 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003014 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003015 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003016 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003017 struct stripe_head_state s;
3018 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003019 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003020 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003021
Dan Williams45b42332007-07-09 11:56:43 -07003022 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07003023 "pd_idx=%d, qd_idx=%d\n",
3024 (unsigned long long)sh->sector, sh->state,
NeilBrown34e04e82009-03-31 15:10:16 +11003025 atomic_read(&sh->count), pd_idx, qd_idx);
Dan Williamsa4456852007-07-09 11:56:43 -07003026 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003027
3028 spin_lock(&sh->lock);
3029 clear_bit(STRIPE_HANDLE, &sh->state);
3030 clear_bit(STRIPE_DELAYED, &sh->state);
3031
Dan Williamsa4456852007-07-09 11:56:43 -07003032 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3033 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3034 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003035 /* Now to look around and see what can be done */
3036
3037 rcu_read_lock();
3038 for (i=disks; i--; ) {
3039 mdk_rdev_t *rdev;
3040 dev = &sh->dev[i];
3041 clear_bit(R5_Insync, &dev->flags);
3042
Dan Williams45b42332007-07-09 11:56:43 -07003043 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003044 i, dev->flags, dev->toread, dev->towrite, dev->written);
3045 /* maybe we can reply to a read */
3046 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3047 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003048 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003049 spin_lock_irq(&conf->device_lock);
3050 rbi = dev->toread;
3051 dev->toread = NULL;
3052 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3053 wake_up(&conf->wait_for_overlap);
3054 spin_unlock_irq(&conf->device_lock);
3055 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3056 copy_data(0, rbi, dev->page, dev->sector);
3057 rbi2 = r5_next_bio(rbi, dev->sector);
3058 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003059 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003060 rbi->bi_next = return_bi;
3061 return_bi = rbi;
3062 }
3063 spin_unlock_irq(&conf->device_lock);
3064 rbi = rbi2;
3065 }
3066 }
3067
3068 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003069 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3070 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003071
3072
Dan Williamsa4456852007-07-09 11:56:43 -07003073 if (dev->toread)
3074 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003075 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003076 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003077 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003078 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003079 }
Dan Williamsa4456852007-07-09 11:56:43 -07003080 if (dev->written)
3081 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003082 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003083 if (blocked_rdev == NULL &&
3084 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003085 blocked_rdev = rdev;
3086 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003087 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003088 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3089 /* The ReadError flag will just be confusing now */
3090 clear_bit(R5_ReadError, &dev->flags);
3091 clear_bit(R5_ReWrite, &dev->flags);
3092 }
3093 if (!rdev || !test_bit(In_sync, &rdev->flags)
3094 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003095 if (s.failed < 2)
3096 r6s.failed_num[s.failed] = i;
3097 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003098 } else
3099 set_bit(R5_Insync, &dev->flags);
3100 }
3101 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003102
3103 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003104 if (s.syncing || s.expanding || s.expanded ||
3105 s.to_write || s.written) {
3106 set_bit(STRIPE_HANDLE, &sh->state);
3107 goto unlock;
3108 }
3109 /* There is nothing for the blocked_rdev to block */
3110 rdev_dec_pending(blocked_rdev, conf->mddev);
3111 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003112 }
NeilBrownac4090d2008-08-05 15:54:13 +10003113
Dan Williams45b42332007-07-09 11:56:43 -07003114 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003115 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003116 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3117 r6s.failed_num[0], r6s.failed_num[1]);
3118 /* check if the array has lost >2 devices and, if so, some requests
3119 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003120 */
Dan Williamsa4456852007-07-09 11:56:43 -07003121 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003122 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003123 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003124 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3125 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003126 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003127 }
3128
3129 /*
3130 * might be able to return some write requests if the parity blocks
3131 * are safe, or on a failed drive
3132 */
3133 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003134 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3135 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003136 qdev = &sh->dev[qd_idx];
3137 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3138 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003139
Dan Williamsa4456852007-07-09 11:56:43 -07003140 if ( s.written &&
3141 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003142 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003143 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3144 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003145 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003146 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003147 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003148
3149 /* Now we might consider reading some blocks, either to check/generate
3150 * parity, or to satisfy requests
3151 * or to load a block that is being partially written.
3152 */
Dan Williamsa4456852007-07-09 11:56:43 -07003153 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3154 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003155 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003156
3157 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07003158 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10003159 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003160
3161 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003162 * Any reads will already have been scheduled, so we just see if enough
3163 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003164 */
Dan Williamsa4456852007-07-09 11:56:43 -07003165 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
Dan Williams36d1c642009-07-14 11:48:22 -07003166 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003167
Dan Williamsa4456852007-07-09 11:56:43 -07003168 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003169 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3170 clear_bit(STRIPE_SYNCING, &sh->state);
3171 }
3172
3173 /* If the failed drives are just a ReadError, then we might need
3174 * to progress the repair/check process
3175 */
Dan Williamsa4456852007-07-09 11:56:43 -07003176 if (s.failed <= 2 && !conf->mddev->ro)
3177 for (i = 0; i < s.failed; i++) {
3178 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003179 if (test_bit(R5_ReadError, &dev->flags)
3180 && !test_bit(R5_LOCKED, &dev->flags)
3181 && test_bit(R5_UPTODATE, &dev->flags)
3182 ) {
3183 if (!test_bit(R5_ReWrite, &dev->flags)) {
3184 set_bit(R5_Wantwrite, &dev->flags);
3185 set_bit(R5_ReWrite, &dev->flags);
3186 set_bit(R5_LOCKED, &dev->flags);
3187 } else {
3188 /* let's read it back */
3189 set_bit(R5_Wantread, &dev->flags);
3190 set_bit(R5_LOCKED, &dev->flags);
3191 }
3192 }
3193 }
NeilBrownf4168852007-02-28 20:11:53 -08003194
Dan Williamsa4456852007-07-09 11:56:43 -07003195 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownab69ae12009-03-31 15:26:47 +11003196 struct stripe_head *sh2
3197 = get_active_stripe(conf, sh->sector, 1, 1);
3198 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3199 /* sh cannot be written until sh2 has been read.
3200 * so arrange for sh to be delayed a little
3201 */
3202 set_bit(STRIPE_DELAYED, &sh->state);
3203 set_bit(STRIPE_HANDLE, &sh->state);
3204 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3205 &sh2->state))
3206 atomic_inc(&conf->preread_active_stripes);
3207 release_stripe(sh2);
3208 goto unlock;
3209 }
3210 if (sh2)
3211 release_stripe(sh2);
3212
NeilBrownf4168852007-02-28 20:11:53 -08003213 /* Need to write out all blocks after computing P&Q */
3214 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003215 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003216 compute_parity6(sh, RECONSTRUCT_WRITE);
3217 for (i = conf->raid_disks ; i-- ; ) {
3218 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003219 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003220 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3221 }
3222 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003223 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003224 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3225 atomic_dec(&conf->reshape_stripes);
3226 wake_up(&conf->wait_for_overlap);
3227 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3228 }
3229
Dan Williams0f94e872008-01-08 15:32:53 -08003230 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003231 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003232 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003233
Dan Williams6bfe0b42008-04-30 00:52:32 -07003234 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003235 spin_unlock(&sh->lock);
3236
Dan Williams6bfe0b42008-04-30 00:52:32 -07003237 /* wait for this device to become unblocked */
3238 if (unlikely(blocked_rdev))
3239 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3240
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003241 ops_run_io(sh, &s);
3242
Dan Williamsa4456852007-07-09 11:56:43 -07003243 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003244
3245 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003246}
3247
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003248/* returns true if the stripe was handled */
Dan Williams36d1c642009-07-14 11:48:22 -07003249static bool handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003250{
3251 if (sh->raid_conf->level == 6)
Dan Williams36d1c642009-07-14 11:48:22 -07003252 return handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003253 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003254 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003255}
3256
Arjan van de Ven858119e2006-01-14 13:20:43 -08003257static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
3259 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3260 while (!list_empty(&conf->delayed_list)) {
3261 struct list_head *l = conf->delayed_list.next;
3262 struct stripe_head *sh;
3263 sh = list_entry(l, struct stripe_head, lru);
3264 list_del_init(l);
3265 clear_bit(STRIPE_DELAYED, &sh->state);
3266 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3267 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003268 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
NeilBrown6ed30032008-02-06 01:40:00 -08003270 } else
3271 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Arjan van de Ven858119e2006-01-14 13:20:43 -08003274static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003275{
3276 /* device_lock is held */
3277 struct list_head head;
3278 list_add(&head, &conf->bitmap_list);
3279 list_del_init(&conf->bitmap_list);
3280 while (!list_empty(&head)) {
3281 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3282 list_del_init(&sh->lru);
3283 atomic_inc(&sh->count);
3284 __release_stripe(conf, sh);
3285 }
3286}
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288static void unplug_slaves(mddev_t *mddev)
3289{
3290 raid5_conf_t *conf = mddev_to_conf(mddev);
3291 int i;
3292
3293 rcu_read_lock();
3294 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003295 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003296 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003297 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 atomic_inc(&rdev->nr_pending);
3300 rcu_read_unlock();
3301
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003302 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 rdev_dec_pending(rdev, mddev);
3305 rcu_read_lock();
3306 }
3307 }
3308 rcu_read_unlock();
3309}
3310
Jens Axboe165125e2007-07-24 09:28:11 +02003311static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 mddev_t *mddev = q->queuedata;
3314 raid5_conf_t *conf = mddev_to_conf(mddev);
3315 unsigned long flags;
3316
3317 spin_lock_irqsave(&conf->device_lock, flags);
3318
NeilBrown72626682005-09-09 16:23:54 -07003319 if (blk_remove_plug(q)) {
3320 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 md_wakeup_thread(mddev->thread);
3324
3325 spin_unlock_irqrestore(&conf->device_lock, flags);
3326
3327 unplug_slaves(mddev);
3328}
3329
NeilBrownf022b2f2006-10-03 01:15:56 -07003330static int raid5_congested(void *data, int bits)
3331{
3332 mddev_t *mddev = data;
3333 raid5_conf_t *conf = mddev_to_conf(mddev);
3334
3335 /* No difference between reads and writes. Just check
3336 * how busy the stripe_cache is
3337 */
3338 if (conf->inactive_blocked)
3339 return 1;
3340 if (conf->quiesce)
3341 return 1;
3342 if (list_empty_careful(&conf->inactive_list))
3343 return 1;
3344
3345 return 0;
3346}
3347
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003348/* We want read requests to align with chunks where possible,
3349 * but write requests don't need to.
3350 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003351static int raid5_mergeable_bvec(struct request_queue *q,
3352 struct bvec_merge_data *bvm,
3353 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003354{
3355 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003356 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003357 int max;
3358 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003359 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003360
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003361 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003362 return biovec->bv_len; /* always allow writes to be mergeable */
3363
NeilBrown784052e2009-03-31 15:19:07 +11003364 if (mddev->new_chunk < mddev->chunk_size)
3365 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003366 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3367 if (max < 0) max = 0;
3368 if (max <= biovec->bv_len && bio_sectors == 0)
3369 return biovec->bv_len;
3370 else
3371 return max;
3372}
3373
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003374
3375static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3376{
3377 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3378 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3379 unsigned int bio_sectors = bio->bi_size >> 9;
3380
NeilBrown784052e2009-03-31 15:19:07 +11003381 if (mddev->new_chunk < mddev->chunk_size)
3382 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003383 return chunk_sectors >=
3384 ((sector & (chunk_sectors - 1)) + bio_sectors);
3385}
3386
3387/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003388 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3389 * later sampled by raid5d.
3390 */
3391static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3392{
3393 unsigned long flags;
3394
3395 spin_lock_irqsave(&conf->device_lock, flags);
3396
3397 bi->bi_next = conf->retry_read_aligned_list;
3398 conf->retry_read_aligned_list = bi;
3399
3400 spin_unlock_irqrestore(&conf->device_lock, flags);
3401 md_wakeup_thread(conf->mddev->thread);
3402}
3403
3404
3405static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3406{
3407 struct bio *bi;
3408
3409 bi = conf->retry_read_aligned;
3410 if (bi) {
3411 conf->retry_read_aligned = NULL;
3412 return bi;
3413 }
3414 bi = conf->retry_read_aligned_list;
3415 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003416 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003417 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003418 /*
3419 * this sets the active strip count to 1 and the processed
3420 * strip count to zero (upper 8 bits)
3421 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003422 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003423 }
3424
3425 return bi;
3426}
3427
3428
3429/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003430 * The "raid5_align_endio" should check if the read succeeded and if it
3431 * did, call bio_endio on the original bio (having bio_put the new bio
3432 * first).
3433 * If the read failed..
3434 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003435static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003436{
3437 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003438 mddev_t *mddev;
3439 raid5_conf_t *conf;
3440 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3441 mdk_rdev_t *rdev;
3442
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003443 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003444
3445 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3446 conf = mddev_to_conf(mddev);
3447 rdev = (void*)raid_bi->bi_next;
3448 raid_bi->bi_next = NULL;
3449
3450 rdev_dec_pending(rdev, conf->mddev);
3451
3452 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003453 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003454 if (atomic_dec_and_test(&conf->active_aligned_reads))
3455 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003456 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003457 }
3458
3459
Dan Williams45b42332007-07-09 11:56:43 -07003460 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003461
3462 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003463}
3464
Neil Brown387bb172007-02-08 14:20:29 -08003465static int bio_fits_rdev(struct bio *bi)
3466{
Jens Axboe165125e2007-07-24 09:28:11 +02003467 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003468
3469 if ((bi->bi_size>>9) > q->max_sectors)
3470 return 0;
3471 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003472 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003473 return 0;
3474
3475 if (q->merge_bvec_fn)
3476 /* it's too hard to apply the merge_bvec_fn at this stage,
3477 * just just give up
3478 */
3479 return 0;
3480
3481 return 1;
3482}
3483
3484
Jens Axboe165125e2007-07-24 09:28:11 +02003485static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003486{
3487 mddev_t *mddev = q->queuedata;
3488 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003489 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003490 struct bio* align_bi;
3491 mdk_rdev_t *rdev;
3492
3493 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003494 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003495 return 0;
3496 }
3497 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003498 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003499 */
3500 align_bi = bio_clone(raid_bio, GFP_NOIO);
3501 if (!align_bi)
3502 return 0;
3503 /*
3504 * set bi_end_io to a new function, and set bi_private to the
3505 * original bio.
3506 */
3507 align_bi->bi_end_io = raid5_align_endio;
3508 align_bi->bi_private = raid_bio;
3509 /*
3510 * compute position
3511 */
NeilBrown112bf892009-03-31 14:39:38 +11003512 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3513 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003514 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003515
3516 rcu_read_lock();
3517 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3518 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003519 atomic_inc(&rdev->nr_pending);
3520 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003521 raid_bio->bi_next = (void*)rdev;
3522 align_bi->bi_bdev = rdev->bdev;
3523 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3524 align_bi->bi_sector += rdev->data_offset;
3525
Neil Brown387bb172007-02-08 14:20:29 -08003526 if (!bio_fits_rdev(align_bi)) {
3527 /* too big in some way */
3528 bio_put(align_bi);
3529 rdev_dec_pending(rdev, mddev);
3530 return 0;
3531 }
3532
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003533 spin_lock_irq(&conf->device_lock);
3534 wait_event_lock_irq(conf->wait_for_stripe,
3535 conf->quiesce == 0,
3536 conf->device_lock, /* nothing */);
3537 atomic_inc(&conf->active_aligned_reads);
3538 spin_unlock_irq(&conf->device_lock);
3539
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003540 generic_make_request(align_bi);
3541 return 1;
3542 } else {
3543 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003544 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003545 return 0;
3546 }
3547}
3548
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003549/* __get_priority_stripe - get the next stripe to process
3550 *
3551 * Full stripe writes are allowed to pass preread active stripes up until
3552 * the bypass_threshold is exceeded. In general the bypass_count
3553 * increments when the handle_list is handled before the hold_list; however, it
3554 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3555 * stripe with in flight i/o. The bypass_count will be reset when the
3556 * head of the hold_list has changed, i.e. the head was promoted to the
3557 * handle_list.
3558 */
3559static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3560{
3561 struct stripe_head *sh;
3562
3563 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3564 __func__,
3565 list_empty(&conf->handle_list) ? "empty" : "busy",
3566 list_empty(&conf->hold_list) ? "empty" : "busy",
3567 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3568
3569 if (!list_empty(&conf->handle_list)) {
3570 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3571
3572 if (list_empty(&conf->hold_list))
3573 conf->bypass_count = 0;
3574 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3575 if (conf->hold_list.next == conf->last_hold)
3576 conf->bypass_count++;
3577 else {
3578 conf->last_hold = conf->hold_list.next;
3579 conf->bypass_count -= conf->bypass_threshold;
3580 if (conf->bypass_count < 0)
3581 conf->bypass_count = 0;
3582 }
3583 }
3584 } else if (!list_empty(&conf->hold_list) &&
3585 ((conf->bypass_threshold &&
3586 conf->bypass_count > conf->bypass_threshold) ||
3587 atomic_read(&conf->pending_full_writes) == 0)) {
3588 sh = list_entry(conf->hold_list.next,
3589 typeof(*sh), lru);
3590 conf->bypass_count -= conf->bypass_threshold;
3591 if (conf->bypass_count < 0)
3592 conf->bypass_count = 0;
3593 } else
3594 return NULL;
3595
3596 list_del_init(&sh->lru);
3597 atomic_inc(&sh->count);
3598 BUG_ON(atomic_read(&sh->count) != 1);
3599 return sh;
3600}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003601
Jens Axboe165125e2007-07-24 09:28:11 +02003602static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 mddev_t *mddev = q->queuedata;
3605 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003606 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 sector_t new_sector;
3608 sector_t logical_sector, last_sector;
3609 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003610 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003611 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
NeilBrowne5dcdd82005-09-09 16:23:41 -07003613 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003614 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003615 return 0;
3616 }
3617
NeilBrown3d310eb2005-06-21 17:17:26 -07003618 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003619
Tejun Heo074a7ac2008-08-25 19:56:14 +09003620 cpu = part_stat_lock();
3621 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3622 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3623 bio_sectors(bi));
3624 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
NeilBrown802ba062006-12-13 00:34:13 -08003626 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003627 mddev->reshape_position == MaxSector &&
3628 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003629 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3632 last_sector = bi->bi_sector + (bi->bi_size>>9);
3633 bi->bi_next = NULL;
3634 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3637 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003638 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003639 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003640
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003641 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003642 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003643 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003644 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003645 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003646 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003647 * 64bit on a 32bit platform, and so it might be
3648 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003649 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003650 * the lock is dropped, so once we get a reference
3651 * to the stripe that we think it is, we will have
3652 * to check again.
3653 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003654 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003655 if (mddev->delta_disks < 0
3656 ? logical_sector < conf->reshape_progress
3657 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003658 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003659 previous = 1;
3660 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003661 if (mddev->delta_disks < 0
3662 ? logical_sector < conf->reshape_safe
3663 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003664 spin_unlock_irq(&conf->device_lock);
3665 schedule();
3666 goto retry;
3667 }
3668 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003669 spin_unlock_irq(&conf->device_lock);
3670 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003671 data_disks = disks - conf->max_degraded;
3672
NeilBrown112bf892009-03-31 14:39:38 +11003673 new_sector = raid5_compute_sector(conf, logical_sector,
3674 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003675 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003676 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 (unsigned long long)new_sector,
3678 (unsigned long long)logical_sector);
3679
NeilBrownb5663ba2009-03-31 14:39:38 +11003680 sh = get_active_stripe(conf, new_sector, previous,
3681 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003683 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003684 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003685 * stripe, so we must do the range check again.
3686 * Expansion could still move past after this
3687 * test, but as we are holding a reference to
3688 * 'sh', we know that if that happens,
3689 * STRIPE_EXPANDING will get set and the expansion
3690 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003691 */
3692 int must_retry = 0;
3693 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003694 if (mddev->delta_disks < 0
3695 ? logical_sector >= conf->reshape_progress
3696 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003697 /* mismatch, need to try again */
3698 must_retry = 1;
3699 spin_unlock_irq(&conf->device_lock);
3700 if (must_retry) {
3701 release_stripe(sh);
3702 goto retry;
3703 }
3704 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003705 /* FIXME what if we get a false positive because these
3706 * are being updated.
3707 */
3708 if (logical_sector >= mddev->suspend_lo &&
3709 logical_sector < mddev->suspend_hi) {
3710 release_stripe(sh);
3711 schedule();
3712 goto retry;
3713 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003714
3715 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3716 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3717 /* Stripe is busy expanding or
3718 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 * and wait a while
3720 */
3721 raid5_unplug_device(mddev->queue);
3722 release_stripe(sh);
3723 schedule();
3724 goto retry;
3725 }
3726 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003727 set_bit(STRIPE_HANDLE, &sh->state);
3728 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 } else {
3731 /* cannot get stripe for read-ahead, just give-up */
3732 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3733 finish_wait(&conf->wait_for_overlap, &w);
3734 break;
3735 }
3736
3737 }
3738 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003739 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003740 spin_unlock_irq(&conf->device_lock);
3741 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
NeilBrown16a53ec2006-06-26 00:27:38 -07003743 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003745
Neil Brown0e13fe232008-06-28 08:31:20 +10003746 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return 0;
3749}
3750
Dan Williamsb522adc2009-03-31 15:00:31 +11003751static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3752
NeilBrown52c03292006-06-26 00:27:43 -07003753static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
NeilBrown52c03292006-06-26 00:27:43 -07003755 /* reshaping is quite different to recovery/resync so it is
3756 * handled quite separately ... here.
3757 *
3758 * On each call to sync_request, we gather one chunk worth of
3759 * destination stripes and flag them as expanding.
3760 * Then we find all the source stripes and request reads.
3761 * As the reads complete, handle_stripe will copy the data
3762 * into the destination stripe and release that stripe.
3763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3765 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003766 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003767 int raid_disks = conf->previous_raid_disks;
3768 int data_disks = raid_disks - conf->max_degraded;
3769 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003770 int i;
3771 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003772 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003773 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003774 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003775 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003776
NeilBrownfef9c612009-03-31 15:16:46 +11003777 if (sector_nr == 0) {
3778 /* If restarting in the middle, skip the initial sectors */
3779 if (mddev->delta_disks < 0 &&
3780 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3781 sector_nr = raid5_size(mddev, 0, 0)
3782 - conf->reshape_progress;
3783 } else if (mddev->delta_disks > 0 &&
3784 conf->reshape_progress > 0)
3785 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003786 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003787 if (sector_nr) {
3788 *skipped = 1;
3789 return sector_nr;
3790 }
NeilBrown52c03292006-06-26 00:27:43 -07003791 }
3792
NeilBrown7a661382009-03-31 15:21:40 +11003793 /* We need to process a full chunk at a time.
3794 * If old and new chunk sizes differ, we need to process the
3795 * largest of these
3796 */
3797 if (mddev->new_chunk > mddev->chunk_size)
3798 reshape_sectors = mddev->new_chunk / 512;
3799 else
3800 reshape_sectors = mddev->chunk_size / 512;
3801
NeilBrown52c03292006-06-26 00:27:43 -07003802 /* we update the metadata when there is more than 3Meg
3803 * in the block range (that is rather arbitrary, should
3804 * probably be time based) or when the data about to be
3805 * copied would over-write the source of the data at
3806 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003807 * i.e. one new_stripe along from reshape_progress new_maps
3808 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003809 */
NeilBrownfef9c612009-03-31 15:16:46 +11003810 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003811 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003812 readpos = conf->reshape_progress;
3813 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003814 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003815 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003816 if (mddev->delta_disks < 0) {
NeilBrown7a661382009-03-31 15:21:40 +11003817 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11003818 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003819 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003820 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003821 writepos += reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11003822 readpos -= reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003823 safepos -= reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003824 }
NeilBrown52c03292006-06-26 00:27:43 -07003825
NeilBrownc8f517c2009-03-31 15:28:40 +11003826 /* 'writepos' is the most advanced device address we might write.
3827 * 'readpos' is the least advanced device address we might read.
3828 * 'safepos' is the least address recorded in the metadata as having
3829 * been reshaped.
3830 * If 'readpos' is behind 'writepos', then there is no way that we can
3831 * ensure safety in the face of a crash - that must be done by userspace
3832 * making a backup of the data. So in that case there is no particular
3833 * rush to update metadata.
3834 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3835 * update the metadata to advance 'safepos' to match 'readpos' so that
3836 * we can be safe in the event of a crash.
3837 * So we insist on updating metadata if safepos is behind writepos and
3838 * readpos is beyond writepos.
3839 * In any case, update the metadata every 10 seconds.
3840 * Maybe that number should be configurable, but I'm not sure it is
3841 * worth it.... maybe it could be a multiple of safemode_delay???
3842 */
NeilBrownfef9c612009-03-31 15:16:46 +11003843 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003844 ? (safepos > writepos && readpos < writepos)
3845 : (safepos < writepos && readpos > writepos)) ||
3846 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003847 /* Cannot proceed until we've updated the superblock... */
3848 wait_event(conf->wait_for_overlap,
3849 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003850 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11003851 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003852 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003853 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003854 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003855 kthread_should_stop());
3856 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003857 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003858 spin_unlock_irq(&conf->device_lock);
3859 wake_up(&conf->wait_for_overlap);
3860 }
3861
NeilBrownec32a2b2009-03-31 15:17:38 +11003862 if (mddev->delta_disks < 0) {
3863 BUG_ON(conf->reshape_progress == 0);
3864 stripe_addr = writepos;
3865 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003866 ~((sector_t)reshape_sectors - 1))
3867 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003868 != sector_nr);
3869 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003870 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003871 stripe_addr = sector_nr;
3872 }
NeilBrownab69ae12009-03-31 15:26:47 +11003873 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003874 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003875 int j;
3876 int skipped = 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11003877 sh = get_active_stripe(conf, stripe_addr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003878 set_bit(STRIPE_EXPANDING, &sh->state);
3879 atomic_inc(&conf->reshape_stripes);
3880 /* If any of this stripe is beyond the end of the old
3881 * array, then we need to zero those blocks
3882 */
3883 for (j=sh->disks; j--;) {
3884 sector_t s;
3885 if (j == sh->pd_idx)
3886 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003887 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003888 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003889 continue;
NeilBrown784052e2009-03-31 15:19:07 +11003890 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11003891 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07003892 skipped = 1;
3893 continue;
3894 }
3895 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3896 set_bit(R5_Expanded, &sh->dev[j].flags);
3897 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3898 }
3899 if (!skipped) {
3900 set_bit(STRIPE_EXPAND_READY, &sh->state);
3901 set_bit(STRIPE_HANDLE, &sh->state);
3902 }
NeilBrownab69ae12009-03-31 15:26:47 +11003903 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07003904 }
3905 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003906 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11003907 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11003908 else
NeilBrown7a661382009-03-31 15:21:40 +11003909 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003910 spin_unlock_irq(&conf->device_lock);
3911 /* Ok, those stripe are ready. We can start scheduling
3912 * reads on the source stripes.
3913 * The source stripes are determined by mapping the first and last
3914 * block on the destination stripes.
3915 */
NeilBrown52c03292006-06-26 00:27:43 -07003916 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003917 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11003918 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07003919 last_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003920 raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512)
NeilBrown112bf892009-03-31 14:39:38 +11003921 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11003922 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11003923 if (last_sector >= mddev->dev_sectors)
3924 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07003925 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003926 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003927 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3928 set_bit(STRIPE_HANDLE, &sh->state);
3929 release_stripe(sh);
3930 first_sector += STRIPE_SECTORS;
3931 }
NeilBrownab69ae12009-03-31 15:26:47 +11003932 /* Now that the sources are clearly marked, we can release
3933 * the destination stripes
3934 */
3935 while (!list_empty(&stripes)) {
3936 sh = list_entry(stripes.next, struct stripe_head, lru);
3937 list_del_init(&sh->lru);
3938 release_stripe(sh);
3939 }
NeilBrownc6207272008-02-06 01:39:52 -08003940 /* If this takes us to the resync_max point where we have to pause,
3941 * then we need to write out the superblock.
3942 */
NeilBrown7a661382009-03-31 15:21:40 +11003943 sector_nr += reshape_sectors;
NeilBrownc6207272008-02-06 01:39:52 -08003944 if (sector_nr >= mddev->resync_max) {
3945 /* Cannot proceed until we've updated the superblock... */
3946 wait_event(conf->wait_for_overlap,
3947 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11003948 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11003949 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08003950 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3951 md_wakeup_thread(mddev->thread);
3952 wait_event(mddev->sb_wait,
3953 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3954 || kthread_should_stop());
3955 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003956 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08003957 spin_unlock_irq(&conf->device_lock);
3958 wake_up(&conf->wait_for_overlap);
3959 }
NeilBrown7a661382009-03-31 15:21:40 +11003960 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07003961}
3962
3963/* FIXME go_faster isn't used */
3964static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3965{
3966 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3967 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11003968 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07003969 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003970 int still_degraded = 0;
3971 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
NeilBrown72626682005-09-09 16:23:54 -07003973 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 /* just being told to finish up .. nothing much to do */
3975 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11003976
NeilBrown29269552006-03-27 01:18:10 -08003977 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3978 end_reshape(conf);
3979 return 0;
3980 }
NeilBrown72626682005-09-09 16:23:54 -07003981
3982 if (mddev->curr_resync < max_sector) /* aborted */
3983 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3984 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003985 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003986 conf->fullsync = 0;
3987 bitmap_close_sync(mddev->bitmap);
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return 0;
3990 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003991
NeilBrown52c03292006-06-26 00:27:43 -07003992 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3993 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003994
NeilBrownc6207272008-02-06 01:39:52 -08003995 /* No need to check resync_max as we never do more than one
3996 * stripe, and as resync_max will always be on a chunk boundary,
3997 * if the check in md_do_sync didn't fire, there is no chance
3998 * of overstepping resync_max here
3999 */
4000
NeilBrown16a53ec2006-06-26 00:27:38 -07004001 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 * to resync, then assert that we are finished, because there is
4003 * nothing we can do.
4004 */
NeilBrown3285edf2006-06-26 00:27:55 -07004005 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004006 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004007 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004008 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return rv;
4010 }
NeilBrown72626682005-09-09 16:23:54 -07004011 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004012 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004013 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4014 /* we can skip this block, and probably more */
4015 sync_blocks /= STRIPE_SECTORS;
4016 *skipped = 1;
4017 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
NeilBrownb47490c2008-02-06 01:39:50 -08004020
4021 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4022
NeilBrownb5663ba2009-03-31 14:39:38 +11004023 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004025 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004027 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004029 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004031 /* Need to check if array will still be degraded after recovery/resync
4032 * We don't need to check the 'failed' flag as when that gets set,
4033 * recovery aborts.
4034 */
4035 for (i=0; i<mddev->raid_disks; i++)
4036 if (conf->disks[i].rdev == NULL)
4037 still_degraded = 1;
4038
4039 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4040
4041 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 set_bit(STRIPE_SYNCING, &sh->state);
4043 clear_bit(STRIPE_INSYNC, &sh->state);
4044 spin_unlock(&sh->lock);
4045
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004046 /* wait for any blocked device to be handled */
Dan Williams36d1c642009-07-14 11:48:22 -07004047 while (unlikely(!handle_stripe(sh)))
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004048 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 release_stripe(sh);
4050
4051 return STRIPE_SECTORS;
4052}
4053
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004054static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4055{
4056 /* We may not be able to submit a whole bio at once as there
4057 * may not be enough stripe_heads available.
4058 * We cannot pre-allocate enough stripe_heads as we may need
4059 * more than exist in the cache (if we allow ever large chunks).
4060 * So we do one stripe head at a time and record in
4061 * ->bi_hw_segments how many have been done.
4062 *
4063 * We *know* that this entire raid_bio is in one chunk, so
4064 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4065 */
4066 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004067 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004068 sector_t sector, logical_sector, last_sector;
4069 int scnt = 0;
4070 int remaining;
4071 int handled = 0;
4072
4073 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004074 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004075 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004076 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4077
4078 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004079 logical_sector += STRIPE_SECTORS,
4080 sector += STRIPE_SECTORS,
4081 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004082
Jens Axboe960e7392008-08-15 10:41:18 +02004083 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004084 /* already done this stripe */
4085 continue;
4086
NeilBrownb5663ba2009-03-31 14:39:38 +11004087 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004088
4089 if (!sh) {
4090 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004091 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004092 conf->retry_read_aligned = raid_bio;
4093 return handled;
4094 }
4095
4096 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004097 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4098 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004099 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004100 conf->retry_read_aligned = raid_bio;
4101 return handled;
4102 }
4103
Dan Williams36d1c642009-07-14 11:48:22 -07004104 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004105 release_stripe(sh);
4106 handled++;
4107 }
4108 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004109 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004110 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004111 if (remaining == 0)
4112 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004113 if (atomic_dec_and_test(&conf->active_aligned_reads))
4114 wake_up(&conf->wait_for_stripe);
4115 return handled;
4116}
4117
4118
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120/*
4121 * This is our raid5 kernel thread.
4122 *
4123 * We scan the hash table for stripes which can be handled now.
4124 * During the scan, completed stripes are saved for us by the interrupt
4125 * handler, so that they will not have to wait for our next wakeup.
4126 */
NeilBrown6ed30032008-02-06 01:40:00 -08004127static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 struct stripe_head *sh;
4130 raid5_conf_t *conf = mddev_to_conf(mddev);
4131 int handled;
4132
Dan Williams45b42332007-07-09 11:56:43 -07004133 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 handled = 0;
4138 spin_lock_irq(&conf->device_lock);
4139 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004140 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
NeilBrownae3c20c2006-07-10 04:44:17 -07004142 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004143 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004144 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004145 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004146 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004147 conf->seq_write = seq;
4148 activate_bit_delay(conf);
4149 }
4150
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004151 while ((bio = remove_bio_from_retry(conf))) {
4152 int ok;
4153 spin_unlock_irq(&conf->device_lock);
4154 ok = retry_aligned_read(conf, bio);
4155 spin_lock_irq(&conf->device_lock);
4156 if (!ok)
4157 break;
4158 handled++;
4159 }
4160
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004161 sh = __get_priority_stripe(conf);
4162
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004163 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 spin_unlock_irq(&conf->device_lock);
4166
4167 handled++;
Dan Williams36d1c642009-07-14 11:48:22 -07004168 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 release_stripe(sh);
4170
4171 spin_lock_irq(&conf->device_lock);
4172 }
Dan Williams45b42332007-07-09 11:56:43 -07004173 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 spin_unlock_irq(&conf->device_lock);
4176
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004177 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 unplug_slaves(mddev);
4179
Dan Williams45b42332007-07-09 11:56:43 -07004180 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
NeilBrown3f294f42005-11-08 21:39:25 -08004183static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004184raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004185{
NeilBrown007583c2005-11-08 21:39:30 -08004186 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004187 if (conf)
4188 return sprintf(page, "%d\n", conf->max_nr_stripes);
4189 else
4190 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004191}
4192
4193static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004194raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004195{
NeilBrown007583c2005-11-08 21:39:30 -08004196 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004197 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004198 int err;
4199
NeilBrown3f294f42005-11-08 21:39:25 -08004200 if (len >= PAGE_SIZE)
4201 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004202 if (!conf)
4203 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004204
Dan Williams4ef197d82008-04-28 02:15:54 -07004205 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004206 return -EINVAL;
4207 if (new <= 16 || new > 32768)
4208 return -EINVAL;
4209 while (new < conf->max_nr_stripes) {
4210 if (drop_one_stripe(conf))
4211 conf->max_nr_stripes--;
4212 else
4213 break;
4214 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004215 err = md_allow_write(mddev);
4216 if (err)
4217 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004218 while (new > conf->max_nr_stripes) {
4219 if (grow_one_stripe(conf))
4220 conf->max_nr_stripes++;
4221 else break;
4222 }
4223 return len;
4224}
NeilBrown007583c2005-11-08 21:39:30 -08004225
NeilBrown96de1e62005-11-08 21:39:39 -08004226static struct md_sysfs_entry
4227raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4228 raid5_show_stripe_cache_size,
4229 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004230
4231static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004232raid5_show_preread_threshold(mddev_t *mddev, char *page)
4233{
4234 raid5_conf_t *conf = mddev_to_conf(mddev);
4235 if (conf)
4236 return sprintf(page, "%d\n", conf->bypass_threshold);
4237 else
4238 return 0;
4239}
4240
4241static ssize_t
4242raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4243{
4244 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004245 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004246 if (len >= PAGE_SIZE)
4247 return -EINVAL;
4248 if (!conf)
4249 return -ENODEV;
4250
Dan Williams4ef197d82008-04-28 02:15:54 -07004251 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004252 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004253 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004254 return -EINVAL;
4255 conf->bypass_threshold = new;
4256 return len;
4257}
4258
4259static struct md_sysfs_entry
4260raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4261 S_IRUGO | S_IWUSR,
4262 raid5_show_preread_threshold,
4263 raid5_store_preread_threshold);
4264
4265static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004266stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004267{
NeilBrown007583c2005-11-08 21:39:30 -08004268 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004269 if (conf)
4270 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4271 else
4272 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004273}
4274
NeilBrown96de1e62005-11-08 21:39:39 -08004275static struct md_sysfs_entry
4276raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004277
NeilBrown007583c2005-11-08 21:39:30 -08004278static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004279 &raid5_stripecache_size.attr,
4280 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004281 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004282 NULL,
4283};
NeilBrown007583c2005-11-08 21:39:30 -08004284static struct attribute_group raid5_attrs_group = {
4285 .name = NULL,
4286 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004287};
4288
Dan Williams80c3a6c2009-03-17 18:10:40 -07004289static sector_t
4290raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4291{
4292 raid5_conf_t *conf = mddev_to_conf(mddev);
4293
4294 if (!sectors)
4295 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004296 if (!raid_disks) {
4297 /* size is defined by the smallest of previous and new size */
4298 if (conf->raid_disks < conf->previous_raid_disks)
4299 raid_disks = conf->raid_disks;
4300 else
4301 raid_disks = conf->previous_raid_disks;
4302 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004303
4304 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown784052e2009-03-31 15:19:07 +11004305 sectors &= ~((sector_t)mddev->new_chunk/512 - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004306 return sectors * (raid_disks - conf->max_degraded);
4307}
4308
Dan Williams36d1c642009-07-14 11:48:22 -07004309static void raid5_free_percpu(raid5_conf_t *conf)
4310{
4311 struct raid5_percpu *percpu;
4312 unsigned long cpu;
4313
4314 if (!conf->percpu)
4315 return;
4316
4317 get_online_cpus();
4318 for_each_possible_cpu(cpu) {
4319 percpu = per_cpu_ptr(conf->percpu, cpu);
4320 safe_put_page(percpu->spare_page);
4321 }
4322#ifdef CONFIG_HOTPLUG_CPU
4323 unregister_cpu_notifier(&conf->cpu_notify);
4324#endif
4325 put_online_cpus();
4326
4327 free_percpu(conf->percpu);
4328}
4329
Dan Williamsa11034b2009-07-14 11:48:16 -07004330static void free_conf(raid5_conf_t *conf)
4331{
4332 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004333 raid5_free_percpu(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004334 kfree(conf->disks);
4335 kfree(conf->stripe_hashtbl);
4336 kfree(conf);
4337}
4338
Dan Williams36d1c642009-07-14 11:48:22 -07004339#ifdef CONFIG_HOTPLUG_CPU
4340static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4341 void *hcpu)
4342{
4343 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4344 long cpu = (long)hcpu;
4345 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4346
4347 switch (action) {
4348 case CPU_UP_PREPARE:
4349 case CPU_UP_PREPARE_FROZEN:
4350 if (!percpu->spare_page)
4351 percpu->spare_page = alloc_page(GFP_KERNEL);
4352 if (!percpu->spare_page) {
4353 pr_err("%s: failed memory allocation for cpu%ld\n",
4354 __func__, cpu);
4355 return NOTIFY_BAD;
4356 }
4357 break;
4358 case CPU_DEAD:
4359 case CPU_DEAD_FROZEN:
4360 safe_put_page(percpu->spare_page);
4361 percpu->spare_page = NULL;
4362 break;
4363 default:
4364 break;
4365 }
4366 return NOTIFY_OK;
4367}
4368#endif
4369
4370static int raid5_alloc_percpu(raid5_conf_t *conf)
4371{
4372 unsigned long cpu;
4373 struct page *spare_page;
4374 struct raid5_percpu *allcpus;
4375 int err;
4376
4377 /* the only percpu data is the raid6 spare page */
4378 if (conf->level != 6)
4379 return 0;
4380
4381 allcpus = alloc_percpu(struct raid5_percpu);
4382 if (!allcpus)
4383 return -ENOMEM;
4384 conf->percpu = allcpus;
4385
4386 get_online_cpus();
4387 err = 0;
4388 for_each_present_cpu(cpu) {
4389 spare_page = alloc_page(GFP_KERNEL);
4390 if (!spare_page) {
4391 err = -ENOMEM;
4392 break;
4393 }
4394 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4395 }
4396#ifdef CONFIG_HOTPLUG_CPU
4397 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4398 conf->cpu_notify.priority = 0;
4399 if (err == 0)
4400 err = register_cpu_notifier(&conf->cpu_notify);
4401#endif
4402 put_online_cpus();
4403
4404 return err;
4405}
4406
NeilBrown91adb562009-03-31 14:39:39 +11004407static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
4409 raid5_conf_t *conf;
4410 int raid_disk, memory;
4411 mdk_rdev_t *rdev;
4412 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
NeilBrown91adb562009-03-31 14:39:39 +11004414 if (mddev->new_level != 5
4415 && mddev->new_level != 4
4416 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004417 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004418 mdname(mddev), mddev->new_level);
4419 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 }
NeilBrown91adb562009-03-31 14:39:39 +11004421 if ((mddev->new_level == 5
4422 && !algorithm_valid_raid5(mddev->new_layout)) ||
4423 (mddev->new_level == 6
4424 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004425 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004426 mdname(mddev), mddev->new_layout);
4427 return ERR_PTR(-EIO);
4428 }
4429 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4430 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4431 mdname(mddev), mddev->raid_disks);
4432 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
NeilBrown91adb562009-03-31 14:39:39 +11004435 if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
4436 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4437 mddev->new_chunk, mdname(mddev));
4438 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004439 }
4440
NeilBrown91adb562009-03-31 14:39:39 +11004441 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4442 if (conf == NULL)
4443 goto abort;
4444
4445 conf->raid_disks = mddev->raid_disks;
4446 if (mddev->reshape_position == MaxSector)
4447 conf->previous_raid_disks = mddev->raid_disks;
4448 else
4449 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4450
4451 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4452 GFP_KERNEL);
4453 if (!conf->disks)
4454 goto abort;
4455
4456 conf->mddev = mddev;
4457
4458 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4459 goto abort;
4460
Dan Williams36d1c642009-07-14 11:48:22 -07004461 conf->level = mddev->new_level;
4462 if (raid5_alloc_percpu(conf) != 0)
4463 goto abort;
4464
NeilBrown91adb562009-03-31 14:39:39 +11004465 spin_lock_init(&conf->device_lock);
4466 init_waitqueue_head(&conf->wait_for_stripe);
4467 init_waitqueue_head(&conf->wait_for_overlap);
4468 INIT_LIST_HEAD(&conf->handle_list);
4469 INIT_LIST_HEAD(&conf->hold_list);
4470 INIT_LIST_HEAD(&conf->delayed_list);
4471 INIT_LIST_HEAD(&conf->bitmap_list);
4472 INIT_LIST_HEAD(&conf->inactive_list);
4473 atomic_set(&conf->active_stripes, 0);
4474 atomic_set(&conf->preread_active_stripes, 0);
4475 atomic_set(&conf->active_aligned_reads, 0);
4476 conf->bypass_threshold = BYPASS_THRESHOLD;
4477
4478 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4479
4480 list_for_each_entry(rdev, &mddev->disks, same_set) {
4481 raid_disk = rdev->raid_disk;
4482 if (raid_disk >= conf->raid_disks
4483 || raid_disk < 0)
4484 continue;
4485 disk = conf->disks + raid_disk;
4486
4487 disk->rdev = rdev;
4488
4489 if (test_bit(In_sync, &rdev->flags)) {
4490 char b[BDEVNAME_SIZE];
4491 printk(KERN_INFO "raid5: device %s operational as raid"
4492 " disk %d\n", bdevname(rdev->bdev,b),
4493 raid_disk);
4494 } else
4495 /* Cannot rely on bitmap to complete recovery */
4496 conf->fullsync = 1;
4497 }
4498
4499 conf->chunk_size = mddev->new_chunk;
NeilBrown91adb562009-03-31 14:39:39 +11004500 if (conf->level == 6)
4501 conf->max_degraded = 2;
4502 else
4503 conf->max_degraded = 1;
4504 conf->algorithm = mddev->new_layout;
4505 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004506 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004507 if (conf->reshape_progress != MaxSector) {
NeilBrown784052e2009-03-31 15:19:07 +11004508 conf->prev_chunk = mddev->chunk_size;
NeilBrowne183eae2009-03-31 15:20:22 +11004509 conf->prev_algo = mddev->layout;
4510 }
NeilBrown91adb562009-03-31 14:39:39 +11004511
4512 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4513 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4514 if (grow_stripes(conf, conf->max_nr_stripes)) {
4515 printk(KERN_ERR
4516 "raid5: couldn't allocate %dkB for buffers\n", memory);
4517 goto abort;
4518 } else
4519 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4520 memory, mdname(mddev));
4521
4522 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4523 if (!conf->thread) {
4524 printk(KERN_ERR
4525 "raid5: couldn't allocate thread for %s\n",
4526 mdname(mddev));
4527 goto abort;
4528 }
4529
4530 return conf;
4531
4532 abort:
4533 if (conf) {
Dan Williamsa11034b2009-07-14 11:48:16 -07004534 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004535 return ERR_PTR(-EIO);
4536 } else
4537 return ERR_PTR(-ENOMEM);
4538}
4539
4540static int run(mddev_t *mddev)
4541{
4542 raid5_conf_t *conf;
4543 int working_disks = 0;
4544 mdk_rdev_t *rdev;
4545
NeilBrownf6705572006-03-27 01:18:11 -08004546 if (mddev->reshape_position != MaxSector) {
4547 /* Check that we can continue the reshape.
4548 * Currently only disks can change, it must
4549 * increase, and we must be past the point where
4550 * a stripe over-writes itself
4551 */
4552 sector_t here_new, here_old;
4553 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004554 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004555
NeilBrown88ce4932009-03-31 15:24:23 +11004556 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004557 printk(KERN_ERR "raid5: %s: unsupported reshape "
4558 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004559 mdname(mddev));
4560 return -EINVAL;
4561 }
NeilBrownf6705572006-03-27 01:18:11 -08004562 old_disks = mddev->raid_disks - mddev->delta_disks;
4563 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004564 * further up in new geometry must map after here in old
4565 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004566 */
4567 here_new = mddev->reshape_position;
NeilBrown784052e2009-03-31 15:19:07 +11004568 if (sector_div(here_new, (mddev->new_chunk>>9)*
NeilBrownf4168852007-02-28 20:11:53 -08004569 (mddev->raid_disks - max_degraded))) {
4570 printk(KERN_ERR "raid5: reshape_position not "
4571 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004572 return -EINVAL;
4573 }
4574 /* here_new is the stripe we will write to */
4575 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004576 sector_div(here_old, (mddev->chunk_size>>9)*
4577 (old_disks-max_degraded));
4578 /* here_old is the first stripe that we might need to read
4579 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004580 if (here_new >= here_old) {
4581 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004582 printk(KERN_ERR "raid5: reshape_position too early for "
4583 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004584 return -EINVAL;
4585 }
4586 printk(KERN_INFO "raid5: reshape will continue\n");
4587 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004588 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004589 BUG_ON(mddev->level != mddev->new_level);
4590 BUG_ON(mddev->layout != mddev->new_layout);
4591 BUG_ON(mddev->chunk_size != mddev->new_chunk);
4592 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004593 }
4594
NeilBrown245f46c2009-03-31 14:39:39 +11004595 if (mddev->private == NULL)
4596 conf = setup_conf(mddev);
4597 else
4598 conf = mddev->private;
4599
NeilBrown91adb562009-03-31 14:39:39 +11004600 if (IS_ERR(conf))
4601 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004602
NeilBrown91adb562009-03-31 14:39:39 +11004603 mddev->thread = conf->thread;
4604 conf->thread = NULL;
4605 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004608 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 */
NeilBrown91adb562009-03-31 14:39:39 +11004610 list_for_each_entry(rdev, &mddev->disks, same_set)
4611 if (rdev->raid_disk >= 0 &&
4612 test_bit(In_sync, &rdev->flags))
4613 working_disks++;
4614
NeilBrown02c2de82006-10-03 01:15:47 -07004615 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
NeilBrown16a53ec2006-06-26 00:27:38 -07004617 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 printk(KERN_ERR "raid5: not enough operational devices for %s"
4619 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004620 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 goto abort;
4622 }
4623
NeilBrown91adb562009-03-31 14:39:39 +11004624 /* device size must be a multiple of chunk size */
4625 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4626 mddev->resync_max_sectors = mddev->dev_sectors;
4627
NeilBrown16a53ec2006-06-26 00:27:38 -07004628 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004630 if (mddev->ok_start_degraded)
4631 printk(KERN_WARNING
4632 "raid5: starting dirty degraded array: %s"
4633 "- data corruption possible.\n",
4634 mdname(mddev));
4635 else {
4636 printk(KERN_ERR
4637 "raid5: cannot start dirty degraded array for %s\n",
4638 mdname(mddev));
4639 goto abort;
4640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 if (mddev->degraded == 0)
4644 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004645 " devices, algorithm %d\n", conf->level, mdname(mddev),
4646 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4647 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 else
4649 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4650 " out of %d devices, algorithm %d\n", conf->level,
4651 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004652 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 print_raid5_conf(conf);
4655
NeilBrownfef9c612009-03-31 15:16:46 +11004656 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004657 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004658 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004659 atomic_set(&conf->reshape_stripes, 0);
4660 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4661 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4662 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4663 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4664 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4665 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004666 }
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004669 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 */
4671 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004672 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4673 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004674 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4676 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4677 }
4678
4679 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004680 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4681 printk(KERN_WARNING
4682 "raid5: failed to create sysfs attributes for %s\n",
4683 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004684
NeilBrown91adb562009-03-31 14:39:39 +11004685 mddev->queue->queue_lock = &conf->device_lock;
4686
NeilBrown7a5febe2005-05-16 21:53:16 -07004687 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004688 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004689 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004690
Dan Williams1f403622009-03-31 14:59:03 +11004691 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004692
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004693 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return 0;
4696abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11004697 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11004698 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (conf) {
4700 print_raid5_conf(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004701 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703 mddev->private = NULL;
4704 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4705 return -EIO;
4706}
4707
4708
4709
NeilBrown3f294f42005-11-08 21:39:25 -08004710static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
4712 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4713
4714 md_unregister_thread(mddev->thread);
4715 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004716 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004718 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williamsa11034b2009-07-14 11:48:16 -07004719 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 mddev->private = NULL;
4721 return 0;
4722}
4723
Dan Williams45b42332007-07-09 11:56:43 -07004724#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004725static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 int i;
4728
NeilBrown16a53ec2006-06-26 00:27:38 -07004729 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4730 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4731 seq_printf(seq, "sh %llu, count %d.\n",
4732 (unsigned long long)sh->sector, atomic_read(&sh->count));
4733 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004734 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004735 seq_printf(seq, "(cache%d: %p %ld) ",
4736 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004738 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
NeilBrownd710e132008-10-13 11:55:12 +11004741static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
4743 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004744 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 int i;
4746
4747 spin_lock_irq(&conf->device_lock);
4748 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004749 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 if (sh->raid_conf != conf)
4751 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004752 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
4754 }
4755 spin_unlock_irq(&conf->device_lock);
4756}
4757#endif
4758
NeilBrownd710e132008-10-13 11:55:12 +11004759static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
4761 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4762 int i;
4763
4764 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004765 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 for (i = 0; i < conf->raid_disks; i++)
4767 seq_printf (seq, "%s",
4768 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004769 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004771#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004772 seq_printf (seq, "\n");
4773 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774#endif
4775}
4776
4777static void print_raid5_conf (raid5_conf_t *conf)
4778{
4779 int i;
4780 struct disk_info *tmp;
4781
4782 printk("RAID5 conf printout:\n");
4783 if (!conf) {
4784 printk("(conf==NULL)\n");
4785 return;
4786 }
NeilBrown02c2de82006-10-03 01:15:47 -07004787 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4788 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 for (i = 0; i < conf->raid_disks; i++) {
4791 char b[BDEVNAME_SIZE];
4792 tmp = conf->disks + i;
4793 if (tmp->rdev)
4794 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004795 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 bdevname(tmp->rdev->bdev,b));
4797 }
4798}
4799
4800static int raid5_spare_active(mddev_t *mddev)
4801{
4802 int i;
4803 raid5_conf_t *conf = mddev->private;
4804 struct disk_info *tmp;
4805
4806 for (i = 0; i < conf->raid_disks; i++) {
4807 tmp = conf->disks + i;
4808 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004809 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004810 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4811 unsigned long flags;
4812 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004814 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
4816 }
4817 print_raid5_conf(conf);
4818 return 0;
4819}
4820
4821static int raid5_remove_disk(mddev_t *mddev, int number)
4822{
4823 raid5_conf_t *conf = mddev->private;
4824 int err = 0;
4825 mdk_rdev_t *rdev;
4826 struct disk_info *p = conf->disks + number;
4827
4828 print_raid5_conf(conf);
4829 rdev = p->rdev;
4830 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11004831 if (number >= conf->raid_disks &&
4832 conf->reshape_progress == MaxSector)
4833 clear_bit(In_sync, &rdev->flags);
4834
NeilBrownb2d444d2005-11-08 21:39:31 -08004835 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 atomic_read(&rdev->nr_pending)) {
4837 err = -EBUSY;
4838 goto abort;
4839 }
NeilBrowndfc70642008-05-23 13:04:39 -07004840 /* Only remove non-faulty devices if recovery
4841 * isn't possible.
4842 */
4843 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11004844 mddev->degraded <= conf->max_degraded &&
4845 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07004846 err = -EBUSY;
4847 goto abort;
4848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004850 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 if (atomic_read(&rdev->nr_pending)) {
4852 /* lost the race, try later */
4853 err = -EBUSY;
4854 p->rdev = rdev;
4855 }
4856 }
4857abort:
4858
4859 print_raid5_conf(conf);
4860 return err;
4861}
4862
4863static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4864{
4865 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004866 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 int disk;
4868 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004869 int first = 0;
4870 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
NeilBrown16a53ec2006-06-26 00:27:38 -07004872 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Neil Brown6c2fce22008-06-28 08:31:31 +10004876 if (rdev->raid_disk >= 0)
4877 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
4879 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004880 * find the disk ... but prefer rdev->saved_raid_disk
4881 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004883 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004884 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004885 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4886 disk = rdev->saved_raid_disk;
4887 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004888 disk = first;
4889 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004891 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004893 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004894 if (rdev->saved_raid_disk != disk)
4895 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004896 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 break;
4898 }
4899 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
4903static int raid5_resize(mddev_t *mddev, sector_t sectors)
4904{
4905 /* no resync is happening, and there is enough space
4906 * on all devices, so we can resize.
4907 * We need to make sure resync covers any new space.
4908 * If the array is shrinking we should possibly wait until
4909 * any io in the removed space completes, but it hardly seems
4910 * worth it.
4911 */
4912 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Dan Williams1f403622009-03-31 14:59:03 +11004913 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
4914 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11004915 if (mddev->array_sectors >
4916 raid5_size(mddev, sectors, mddev->raid_disks))
4917 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10004918 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004919 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11004920 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
4921 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4923 }
Andre Noll58c0fed2009-03-31 14:33:13 +11004924 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07004925 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return 0;
4927}
4928
NeilBrown63c70c42006-03-27 01:18:13 -08004929static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08004930{
4931 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown29269552006-03-27 01:18:10 -08004932
NeilBrown88ce4932009-03-31 15:24:23 +11004933 if (mddev->delta_disks == 0 &&
4934 mddev->new_layout == mddev->layout &&
4935 mddev->new_chunk == mddev->chunk_size)
4936 return -EINVAL; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10004937 if (mddev->bitmap)
4938 /* Cannot grow a bitmap yet */
4939 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11004940 if (mddev->degraded > conf->max_degraded)
4941 return -EINVAL;
4942 if (mddev->delta_disks < 0) {
4943 /* We might be able to shrink, but the devices must
4944 * be made bigger first.
4945 * For raid6, 4 is the minimum size.
4946 * Otherwise 2 is the minimum
4947 */
4948 int min = 2;
4949 if (mddev->level == 6)
4950 min = 4;
4951 if (mddev->raid_disks + mddev->delta_disks < min)
4952 return -EINVAL;
4953 }
NeilBrown29269552006-03-27 01:18:10 -08004954
4955 /* Can only proceed if there are plenty of stripe_heads.
4956 * We need a minimum of one full stripe,, and for sensible progress
4957 * it is best to have about 4 times that.
4958 * If we require 4 times, then the default 256 4K stripe_heads will
4959 * allow for chunk sizes up to 256K, which is probably OK.
4960 * If the chunk size is greater, user-space should request more
4961 * stripe_heads first.
4962 */
NeilBrown63c70c42006-03-27 01:18:13 -08004963 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4964 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08004965 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
NeilBrown784052e2009-03-31 15:19:07 +11004966 (max(mddev->chunk_size, mddev->new_chunk)
4967 / STRIPE_SIZE)*4);
NeilBrown29269552006-03-27 01:18:10 -08004968 return -ENOSPC;
4969 }
4970
NeilBrownec32a2b2009-03-31 15:17:38 +11004971 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08004972}
4973
4974static int raid5_start_reshape(mddev_t *mddev)
4975{
4976 raid5_conf_t *conf = mddev_to_conf(mddev);
4977 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08004978 int spares = 0;
4979 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07004980 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08004981
NeilBrownf4168852007-02-28 20:11:53 -08004982 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08004983 return -EBUSY;
4984
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004985 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004986 if (rdev->raid_disk < 0 &&
4987 !test_bit(Faulty, &rdev->flags))
4988 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08004989
NeilBrownf4168852007-02-28 20:11:53 -08004990 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08004991 /* Not enough devices even to make a degraded array
4992 * of that size
4993 */
4994 return -EINVAL;
4995
NeilBrownec32a2b2009-03-31 15:17:38 +11004996 /* Refuse to reduce size of the array. Any reductions in
4997 * array size must be through explicit setting of array_size
4998 * attribute.
4999 */
5000 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5001 < mddev->array_sectors) {
5002 printk(KERN_ERR "md: %s: array size must be reduced "
5003 "before number of disks\n", mdname(mddev));
5004 return -EINVAL;
5005 }
5006
NeilBrownf6705572006-03-27 01:18:11 -08005007 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005008 spin_lock_irq(&conf->device_lock);
5009 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005010 conf->raid_disks += mddev->delta_disks;
NeilBrown88ce4932009-03-31 15:24:23 +11005011 conf->prev_chunk = conf->chunk_size;
5012 conf->chunk_size = mddev->new_chunk;
5013 conf->prev_algo = conf->algorithm;
5014 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005015 if (mddev->delta_disks < 0)
5016 conf->reshape_progress = raid5_size(mddev, 0, 0);
5017 else
5018 conf->reshape_progress = 0;
5019 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005020 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005021 spin_unlock_irq(&conf->device_lock);
5022
5023 /* Add some new drives, as many as will fit.
5024 * We know there are enough to make the newly sized array work.
5025 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005026 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005027 if (rdev->raid_disk < 0 &&
5028 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005029 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005030 char nm[20];
5031 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005032 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005033 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005034 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005035 if (sysfs_create_link(&mddev->kobj,
5036 &rdev->kobj, nm))
5037 printk(KERN_WARNING
5038 "raid5: failed to create "
5039 " link %s for %s\n",
5040 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005041 } else
5042 break;
5043 }
5044
NeilBrownec32a2b2009-03-31 15:17:38 +11005045 if (mddev->delta_disks > 0) {
5046 spin_lock_irqsave(&conf->device_lock, flags);
5047 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5048 - added_devices;
5049 spin_unlock_irqrestore(&conf->device_lock, flags);
5050 }
NeilBrown63c70c42006-03-27 01:18:13 -08005051 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08005052 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005053 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005054
NeilBrown29269552006-03-27 01:18:10 -08005055 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5056 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5057 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5058 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5059 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5060 "%s_reshape");
5061 if (!mddev->sync_thread) {
5062 mddev->recovery = 0;
5063 spin_lock_irq(&conf->device_lock);
5064 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005065 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005066 spin_unlock_irq(&conf->device_lock);
5067 return -EAGAIN;
5068 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005069 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005070 md_wakeup_thread(mddev->sync_thread);
5071 md_new_event(mddev);
5072 return 0;
5073}
NeilBrown29269552006-03-27 01:18:10 -08005074
NeilBrownec32a2b2009-03-31 15:17:38 +11005075/* This is called from the reshape thread and should make any
5076 * changes needed in 'conf'
5077 */
NeilBrown29269552006-03-27 01:18:10 -08005078static void end_reshape(raid5_conf_t *conf)
5079{
NeilBrown29269552006-03-27 01:18:10 -08005080
NeilBrownf6705572006-03-27 01:18:11 -08005081 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005082
NeilBrownf6705572006-03-27 01:18:11 -08005083 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005084 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005085 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005086 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005087 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005088
5089 /* read-ahead size must cover two whole stripes, which is
5090 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5091 */
5092 {
NeilBrowncea9c222009-03-31 15:15:05 +11005093 int data_disks = conf->raid_disks - conf->max_degraded;
5094 int stripe = data_disks * (conf->chunk_size
5095 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005096 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5097 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5098 }
NeilBrown29269552006-03-27 01:18:10 -08005099 }
NeilBrown29269552006-03-27 01:18:10 -08005100}
5101
NeilBrownec32a2b2009-03-31 15:17:38 +11005102/* This is called from the raid5d thread with mddev_lock held.
5103 * It makes config changes to the device.
5104 */
NeilBrowncea9c222009-03-31 15:15:05 +11005105static void raid5_finish_reshape(mddev_t *mddev)
5106{
5107 struct block_device *bdev;
NeilBrown88ce4932009-03-31 15:24:23 +11005108 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11005109
5110 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5111
NeilBrownec32a2b2009-03-31 15:17:38 +11005112 if (mddev->delta_disks > 0) {
5113 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5114 set_capacity(mddev->gendisk, mddev->array_sectors);
5115 mddev->changed = 1;
NeilBrowncea9c222009-03-31 15:15:05 +11005116
NeilBrownec32a2b2009-03-31 15:17:38 +11005117 bdev = bdget_disk(mddev->gendisk, 0);
5118 if (bdev) {
5119 mutex_lock(&bdev->bd_inode->i_mutex);
5120 i_size_write(bdev->bd_inode,
5121 (loff_t)mddev->array_sectors << 9);
5122 mutex_unlock(&bdev->bd_inode->i_mutex);
5123 bdput(bdev);
5124 }
5125 } else {
5126 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005127 mddev->degraded = conf->raid_disks;
5128 for (d = 0; d < conf->raid_disks ; d++)
5129 if (conf->disks[d].rdev &&
5130 test_bit(In_sync,
5131 &conf->disks[d].rdev->flags))
5132 mddev->degraded--;
5133 for (d = conf->raid_disks ;
5134 d < conf->raid_disks - mddev->delta_disks;
5135 d++)
5136 raid5_remove_disk(mddev, d);
NeilBrowncea9c222009-03-31 15:15:05 +11005137 }
NeilBrown88ce4932009-03-31 15:24:23 +11005138 mddev->layout = conf->algorithm;
5139 mddev->chunk_size = conf->chunk_size;
NeilBrownec32a2b2009-03-31 15:17:38 +11005140 mddev->reshape_position = MaxSector;
5141 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005142 }
5143}
5144
NeilBrown72626682005-09-09 16:23:54 -07005145static void raid5_quiesce(mddev_t *mddev, int state)
5146{
5147 raid5_conf_t *conf = mddev_to_conf(mddev);
5148
5149 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005150 case 2: /* resume for a suspend */
5151 wake_up(&conf->wait_for_overlap);
5152 break;
5153
NeilBrown72626682005-09-09 16:23:54 -07005154 case 1: /* stop all writes */
5155 spin_lock_irq(&conf->device_lock);
5156 conf->quiesce = 1;
5157 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005158 atomic_read(&conf->active_stripes) == 0 &&
5159 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005160 conf->device_lock, /* nothing */);
5161 spin_unlock_irq(&conf->device_lock);
5162 break;
5163
5164 case 0: /* re-enable writes */
5165 spin_lock_irq(&conf->device_lock);
5166 conf->quiesce = 0;
5167 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005168 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005169 spin_unlock_irq(&conf->device_lock);
5170 break;
5171 }
NeilBrown72626682005-09-09 16:23:54 -07005172}
NeilBrownb15c2e52006-01-06 00:20:16 -08005173
NeilBrownd562b0c2009-03-31 14:39:39 +11005174
5175static void *raid5_takeover_raid1(mddev_t *mddev)
5176{
5177 int chunksect;
5178
5179 if (mddev->raid_disks != 2 ||
5180 mddev->degraded > 1)
5181 return ERR_PTR(-EINVAL);
5182
5183 /* Should check if there are write-behind devices? */
5184
5185 chunksect = 64*2; /* 64K by default */
5186
5187 /* The array must be an exact multiple of chunksize */
5188 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5189 chunksect >>= 1;
5190
5191 if ((chunksect<<9) < STRIPE_SIZE)
5192 /* array size does not allow a suitable chunk size */
5193 return ERR_PTR(-EINVAL);
5194
5195 mddev->new_level = 5;
5196 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5197 mddev->new_chunk = chunksect << 9;
5198
5199 return setup_conf(mddev);
5200}
5201
NeilBrownfc9739c2009-03-31 14:57:20 +11005202static void *raid5_takeover_raid6(mddev_t *mddev)
5203{
5204 int new_layout;
5205
5206 switch (mddev->layout) {
5207 case ALGORITHM_LEFT_ASYMMETRIC_6:
5208 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5209 break;
5210 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5211 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5212 break;
5213 case ALGORITHM_LEFT_SYMMETRIC_6:
5214 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5215 break;
5216 case ALGORITHM_RIGHT_SYMMETRIC_6:
5217 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5218 break;
5219 case ALGORITHM_PARITY_0_6:
5220 new_layout = ALGORITHM_PARITY_0;
5221 break;
5222 case ALGORITHM_PARITY_N:
5223 new_layout = ALGORITHM_PARITY_N;
5224 break;
5225 default:
5226 return ERR_PTR(-EINVAL);
5227 }
5228 mddev->new_level = 5;
5229 mddev->new_layout = new_layout;
5230 mddev->delta_disks = -1;
5231 mddev->raid_disks -= 1;
5232 return setup_conf(mddev);
5233}
5234
NeilBrownd562b0c2009-03-31 14:39:39 +11005235
NeilBrownb3546032009-03-31 14:56:41 +11005236static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5237{
NeilBrown88ce4932009-03-31 15:24:23 +11005238 /* For a 2-drive array, the layout and chunk size can be changed
5239 * immediately as not restriping is needed.
5240 * For larger arrays we record the new value - after validation
5241 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005242 */
5243 raid5_conf_t *conf = mddev_to_conf(mddev);
5244
5245 if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
5246 return -EINVAL;
5247 if (new_chunk > 0) {
5248 if (new_chunk & (new_chunk-1))
5249 /* not a power of 2 */
5250 return -EINVAL;
5251 if (new_chunk < PAGE_SIZE)
5252 return -EINVAL;
5253 if (mddev->array_sectors & ((new_chunk>>9)-1))
5254 /* not factor of array size */
5255 return -EINVAL;
5256 }
5257
5258 /* They look valid */
5259
NeilBrown88ce4932009-03-31 15:24:23 +11005260 if (mddev->raid_disks == 2) {
NeilBrownb3546032009-03-31 14:56:41 +11005261
NeilBrown88ce4932009-03-31 15:24:23 +11005262 if (new_layout >= 0) {
5263 conf->algorithm = new_layout;
5264 mddev->layout = mddev->new_layout = new_layout;
5265 }
5266 if (new_chunk > 0) {
5267 conf->chunk_size = new_chunk;
5268 mddev->chunk_size = mddev->new_chunk = new_chunk;
5269 }
5270 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5271 md_wakeup_thread(mddev->thread);
5272 } else {
5273 if (new_layout >= 0)
5274 mddev->new_layout = new_layout;
5275 if (new_chunk > 0)
5276 mddev->new_chunk = new_chunk;
NeilBrownb3546032009-03-31 14:56:41 +11005277 }
NeilBrown88ce4932009-03-31 15:24:23 +11005278 return 0;
5279}
5280
5281static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5282{
5283 if (new_layout >= 0 && !algorithm_valid_raid6(new_layout))
5284 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005285 if (new_chunk > 0) {
NeilBrown88ce4932009-03-31 15:24:23 +11005286 if (new_chunk & (new_chunk-1))
5287 /* not a power of 2 */
5288 return -EINVAL;
5289 if (new_chunk < PAGE_SIZE)
5290 return -EINVAL;
5291 if (mddev->array_sectors & ((new_chunk>>9)-1))
5292 /* not factor of array size */
5293 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005294 }
NeilBrown88ce4932009-03-31 15:24:23 +11005295
5296 /* They look valid */
5297
5298 if (new_layout >= 0)
5299 mddev->new_layout = new_layout;
5300 if (new_chunk > 0)
5301 mddev->new_chunk = new_chunk;
5302
NeilBrownb3546032009-03-31 14:56:41 +11005303 return 0;
5304}
5305
NeilBrownd562b0c2009-03-31 14:39:39 +11005306static void *raid5_takeover(mddev_t *mddev)
5307{
5308 /* raid5 can take over:
5309 * raid0 - if all devices are the same - make it a raid4 layout
5310 * raid1 - if there are two drives. We need to know the chunk size
5311 * raid4 - trivial - just use a raid4 layout.
5312 * raid6 - Providing it is a *_6 layout
5313 *
5314 * For now, just do raid1
5315 */
5316
5317 if (mddev->level == 1)
5318 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005319 if (mddev->level == 4) {
5320 mddev->new_layout = ALGORITHM_PARITY_N;
5321 mddev->new_level = 5;
5322 return setup_conf(mddev);
5323 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005324 if (mddev->level == 6)
5325 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005326
5327 return ERR_PTR(-EINVAL);
5328}
5329
5330
NeilBrown245f46c2009-03-31 14:39:39 +11005331static struct mdk_personality raid5_personality;
5332
5333static void *raid6_takeover(mddev_t *mddev)
5334{
5335 /* Currently can only take over a raid5. We map the
5336 * personality to an equivalent raid6 personality
5337 * with the Q block at the end.
5338 */
5339 int new_layout;
5340
5341 if (mddev->pers != &raid5_personality)
5342 return ERR_PTR(-EINVAL);
5343 if (mddev->degraded > 1)
5344 return ERR_PTR(-EINVAL);
5345 if (mddev->raid_disks > 253)
5346 return ERR_PTR(-EINVAL);
5347 if (mddev->raid_disks < 3)
5348 return ERR_PTR(-EINVAL);
5349
5350 switch (mddev->layout) {
5351 case ALGORITHM_LEFT_ASYMMETRIC:
5352 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5353 break;
5354 case ALGORITHM_RIGHT_ASYMMETRIC:
5355 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5356 break;
5357 case ALGORITHM_LEFT_SYMMETRIC:
5358 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5359 break;
5360 case ALGORITHM_RIGHT_SYMMETRIC:
5361 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5362 break;
5363 case ALGORITHM_PARITY_0:
5364 new_layout = ALGORITHM_PARITY_0_6;
5365 break;
5366 case ALGORITHM_PARITY_N:
5367 new_layout = ALGORITHM_PARITY_N;
5368 break;
5369 default:
5370 return ERR_PTR(-EINVAL);
5371 }
5372 mddev->new_level = 6;
5373 mddev->new_layout = new_layout;
5374 mddev->delta_disks = 1;
5375 mddev->raid_disks += 1;
5376 return setup_conf(mddev);
5377}
5378
5379
NeilBrown16a53ec2006-06-26 00:27:38 -07005380static struct mdk_personality raid6_personality =
5381{
5382 .name = "raid6",
5383 .level = 6,
5384 .owner = THIS_MODULE,
5385 .make_request = make_request,
5386 .run = run,
5387 .stop = stop,
5388 .status = status,
5389 .error_handler = error,
5390 .hot_add_disk = raid5_add_disk,
5391 .hot_remove_disk= raid5_remove_disk,
5392 .spare_active = raid5_spare_active,
5393 .sync_request = sync_request,
5394 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005395 .size = raid5_size,
NeilBrownf4168852007-02-28 20:11:53 -08005396 .check_reshape = raid5_check_reshape,
5397 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005398 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005399 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005400 .takeover = raid6_takeover,
NeilBrown88ce4932009-03-31 15:24:23 +11005401 .reconfig = raid6_reconfig,
NeilBrown16a53ec2006-06-26 00:27:38 -07005402};
NeilBrown2604b702006-01-06 00:20:36 -08005403static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
5405 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005406 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 .owner = THIS_MODULE,
5408 .make_request = make_request,
5409 .run = run,
5410 .stop = stop,
5411 .status = status,
5412 .error_handler = error,
5413 .hot_add_disk = raid5_add_disk,
5414 .hot_remove_disk= raid5_remove_disk,
5415 .spare_active = raid5_spare_active,
5416 .sync_request = sync_request,
5417 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005418 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005419 .check_reshape = raid5_check_reshape,
5420 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005421 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005422 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005423 .takeover = raid5_takeover,
NeilBrownb3546032009-03-31 14:56:41 +11005424 .reconfig = raid5_reconfig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425};
5426
NeilBrown2604b702006-01-06 00:20:36 -08005427static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
NeilBrown2604b702006-01-06 00:20:36 -08005429 .name = "raid4",
5430 .level = 4,
5431 .owner = THIS_MODULE,
5432 .make_request = make_request,
5433 .run = run,
5434 .stop = stop,
5435 .status = status,
5436 .error_handler = error,
5437 .hot_add_disk = raid5_add_disk,
5438 .hot_remove_disk= raid5_remove_disk,
5439 .spare_active = raid5_spare_active,
5440 .sync_request = sync_request,
5441 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005442 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005443 .check_reshape = raid5_check_reshape,
5444 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005445 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005446 .quiesce = raid5_quiesce,
5447};
5448
5449static int __init raid5_init(void)
5450{
NeilBrown16a53ec2006-06-26 00:27:38 -07005451 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005452 register_md_personality(&raid5_personality);
5453 register_md_personality(&raid4_personality);
5454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455}
5456
NeilBrown2604b702006-01-06 00:20:36 -08005457static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
NeilBrown16a53ec2006-06-26 00:27:38 -07005459 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005460 unregister_md_personality(&raid5_personality);
5461 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
5464module_init(raid5_init);
5465module_exit(raid5_exit);
5466MODULE_LICENSE("GPL");
5467MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005468MODULE_ALIAS("md-raid5");
5469MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005470MODULE_ALIAS("md-level-5");
5471MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005472MODULE_ALIAS("md-personality-8"); /* RAID6 */
5473MODULE_ALIAS("md-raid6");
5474MODULE_ALIAS("md-level-6");
5475
5476/* This used to be two separate modules, they were: */
5477MODULE_ALIAS("raid5");
5478MODULE_ALIAS("raid6");