blob: 08f806379b074e06cf2372078b77ca3f1fc23ae0 [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
Dan Williams4e7d2c02009-08-29 19:13:11 -0700626static void mark_target_uptodate(struct stripe_head *sh, int target)
627{
628 struct r5dev *tgt;
629
630 if (target < 0)
631 return;
632
633 tgt = &sh->dev[target];
634 set_bit(R5_UPTODATE, &tgt->flags);
635 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
636 clear_bit(R5_Wantcompute, &tgt->flags);
637}
638
Dan Williamsac6b53b2009-07-14 13:40:19 -0700639static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700640{
641 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700642
Harvey Harrisone46b2722008-04-28 02:15:50 -0700643 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700644 (unsigned long long)sh->sector);
645
Dan Williamsac6b53b2009-07-14 13:40:19 -0700646 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700647 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700648 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700649
Dan Williamsecc65c92008-06-28 08:31:57 +1000650 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
651 if (sh->check_state == check_state_compute_run)
652 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700653 set_bit(STRIPE_HANDLE, &sh->state);
654 release_stripe(sh);
655}
656
Dan Williamsd6f38f32009-07-14 11:50:52 -0700657/* return a pointer to the address conversion region of the scribble buffer */
658static addr_conv_t *to_addr_conv(struct stripe_head *sh,
659 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700660{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700661 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
662}
663
664static struct dma_async_tx_descriptor *
665ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
666{
Dan Williams91c00922007-01-02 13:52:30 -0700667 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700668 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700669 int target = sh->ops.target;
670 struct r5dev *tgt = &sh->dev[target];
671 struct page *xor_dest = tgt->page;
672 int count = 0;
673 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700674 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700675 int i;
676
677 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700678 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700679 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
680
681 for (i = disks; i--; )
682 if (i != target)
683 xor_srcs[count++] = sh->dev[i].page;
684
685 atomic_inc(&sh->count);
686
Dan Williamsa08abd82009-06-03 11:43:59 -0700687 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700688 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700689 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700690 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700691 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700692 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700693
Dan Williams91c00922007-01-02 13:52:30 -0700694 return tx;
695}
696
Dan Williamsac6b53b2009-07-14 13:40:19 -0700697/* set_syndrome_sources - populate source buffers for gen_syndrome
698 * @srcs - (struct page *) array of size sh->disks
699 * @sh - stripe_head to parse
700 *
701 * Populates srcs in proper layout order for the stripe and returns the
702 * 'count' of sources to be used in a call to async_gen_syndrome. The P
703 * destination buffer is recorded in srcs[count] and the Q destination
704 * is recorded in srcs[count+1]].
705 */
706static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
707{
708 int disks = sh->disks;
709 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
710 int d0_idx = raid6_d0(sh);
711 int count;
712 int i;
713
714 for (i = 0; i < disks; i++)
715 srcs[i] = (void *)raid6_empty_zero_page;
716
717 count = 0;
718 i = d0_idx;
719 do {
720 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
721
722 srcs[slot] = sh->dev[i].page;
723 i = raid6_next_disk(i, disks);
724 } while (i != d0_idx);
725 BUG_ON(count != syndrome_disks);
726
727 return count;
728}
729
730static struct dma_async_tx_descriptor *
731ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
732{
733 int disks = sh->disks;
734 struct page **blocks = percpu->scribble;
735 int target;
736 int qd_idx = sh->qd_idx;
737 struct dma_async_tx_descriptor *tx;
738 struct async_submit_ctl submit;
739 struct r5dev *tgt;
740 struct page *dest;
741 int i;
742 int count;
743
744 if (sh->ops.target < 0)
745 target = sh->ops.target2;
746 else if (sh->ops.target2 < 0)
747 target = sh->ops.target;
748 else
749 /* we should only have one valid target */
750 BUG();
751 BUG_ON(target < 0);
752 pr_debug("%s: stripe %llu block: %d\n",
753 __func__, (unsigned long long)sh->sector, target);
754
755 tgt = &sh->dev[target];
756 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
757 dest = tgt->page;
758
759 atomic_inc(&sh->count);
760
761 if (target == qd_idx) {
762 count = set_syndrome_sources(blocks, sh);
763 blocks[count] = NULL; /* regenerating p is not necessary */
764 BUG_ON(blocks[count+1] != dest); /* q should already be set */
765 init_async_submit(&submit, 0, NULL, ops_complete_compute, sh,
766 to_addr_conv(sh, percpu));
767 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
768 } else {
769 /* Compute any data- or p-drive using XOR */
770 count = 0;
771 for (i = disks; i-- ; ) {
772 if (i == target || i == qd_idx)
773 continue;
774 blocks[count++] = sh->dev[i].page;
775 }
776
777 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
778 ops_complete_compute, sh,
779 to_addr_conv(sh, percpu));
780 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
781 }
782
783 return tx;
784}
785
786static struct dma_async_tx_descriptor *
787ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
788{
789 int i, count, disks = sh->disks;
790 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
791 int d0_idx = raid6_d0(sh);
792 int faila = -1, failb = -1;
793 int target = sh->ops.target;
794 int target2 = sh->ops.target2;
795 struct r5dev *tgt = &sh->dev[target];
796 struct r5dev *tgt2 = &sh->dev[target2];
797 struct dma_async_tx_descriptor *tx;
798 struct page **blocks = percpu->scribble;
799 struct async_submit_ctl submit;
800
801 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
802 __func__, (unsigned long long)sh->sector, target, target2);
803 BUG_ON(target < 0 || target2 < 0);
804 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
805 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
806
807 /* we need to open-code set_syndrome_sources to handle to the
808 * slot number conversion for 'faila' and 'failb'
809 */
810 for (i = 0; i < disks ; i++)
811 blocks[i] = (void *)raid6_empty_zero_page;
812 count = 0;
813 i = d0_idx;
814 do {
815 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
816
817 blocks[slot] = sh->dev[i].page;
818
819 if (i == target)
820 faila = slot;
821 if (i == target2)
822 failb = slot;
823 i = raid6_next_disk(i, disks);
824 } while (i != d0_idx);
825 BUG_ON(count != syndrome_disks);
826
827 BUG_ON(faila == failb);
828 if (failb < faila)
829 swap(faila, failb);
830 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
831 __func__, (unsigned long long)sh->sector, faila, failb);
832
833 atomic_inc(&sh->count);
834
835 if (failb == syndrome_disks+1) {
836 /* Q disk is one of the missing disks */
837 if (faila == syndrome_disks) {
838 /* Missing P+Q, just recompute */
839 init_async_submit(&submit, 0, NULL, ops_complete_compute,
840 sh, to_addr_conv(sh, percpu));
841 return async_gen_syndrome(blocks, 0, count+2,
842 STRIPE_SIZE, &submit);
843 } else {
844 struct page *dest;
845 int data_target;
846 int qd_idx = sh->qd_idx;
847
848 /* Missing D+Q: recompute D from P, then recompute Q */
849 if (target == qd_idx)
850 data_target = target2;
851 else
852 data_target = target;
853
854 count = 0;
855 for (i = disks; i-- ; ) {
856 if (i == data_target || i == qd_idx)
857 continue;
858 blocks[count++] = sh->dev[i].page;
859 }
860 dest = sh->dev[data_target].page;
861 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
862 NULL, NULL, to_addr_conv(sh, percpu));
863 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
864 &submit);
865
866 count = set_syndrome_sources(blocks, sh);
867 init_async_submit(&submit, 0, tx, ops_complete_compute,
868 sh, to_addr_conv(sh, percpu));
869 return async_gen_syndrome(blocks, 0, count+2,
870 STRIPE_SIZE, &submit);
871 }
872 }
873
874 init_async_submit(&submit, 0, NULL, ops_complete_compute, sh,
875 to_addr_conv(sh, percpu));
876 if (failb == syndrome_disks) {
877 /* We're missing D+P. */
878 return async_raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE,
879 faila, blocks, &submit);
880 } else {
881 /* We're missing D+D. */
882 return async_raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE,
883 faila, failb, blocks, &submit);
884 }
885}
886
887
Dan Williams91c00922007-01-02 13:52:30 -0700888static void ops_complete_prexor(void *stripe_head_ref)
889{
890 struct stripe_head *sh = stripe_head_ref;
891
Harvey Harrisone46b2722008-04-28 02:15:50 -0700892 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700893 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700894}
895
896static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700897ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
898 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700899{
Dan Williams91c00922007-01-02 13:52:30 -0700900 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700901 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700902 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700903 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700904
905 /* existing parity data subtracted */
906 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
907
Harvey Harrisone46b2722008-04-28 02:15:50 -0700908 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700909 (unsigned long long)sh->sector);
910
911 for (i = disks; i--; ) {
912 struct r5dev *dev = &sh->dev[i];
913 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000914 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700915 xor_srcs[count++] = dev->page;
916 }
917
Dan Williamsa08abd82009-06-03 11:43:59 -0700918 init_async_submit(&submit, ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700919 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700920 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700921
922 return tx;
923}
924
925static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000926ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700927{
928 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000929 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700930
Harvey Harrisone46b2722008-04-28 02:15:50 -0700931 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700932 (unsigned long long)sh->sector);
933
934 for (i = disks; i--; ) {
935 struct r5dev *dev = &sh->dev[i];
936 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700937
Dan Williamsd8ee0722008-06-28 08:32:06 +1000938 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700939 struct bio *wbi;
940
941 spin_lock(&sh->lock);
942 chosen = dev->towrite;
943 dev->towrite = NULL;
944 BUG_ON(dev->written);
945 wbi = dev->written = chosen;
946 spin_unlock(&sh->lock);
947
948 while (wbi && wbi->bi_sector <
949 dev->sector + STRIPE_SECTORS) {
950 tx = async_copy_data(1, wbi, dev->page,
951 dev->sector, tx);
952 wbi = r5_next_bio(wbi, dev->sector);
953 }
954 }
955 }
956
957 return tx;
958}
959
Dan Williamsac6b53b2009-07-14 13:40:19 -0700960static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700961{
962 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700963 int disks = sh->disks;
964 int pd_idx = sh->pd_idx;
965 int qd_idx = sh->qd_idx;
966 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700967
Harvey Harrisone46b2722008-04-28 02:15:50 -0700968 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700969 (unsigned long long)sh->sector);
970
971 for (i = disks; i--; ) {
972 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973
974 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700975 set_bit(R5_UPTODATE, &dev->flags);
976 }
977
Dan Williamsd8ee0722008-06-28 08:32:06 +1000978 if (sh->reconstruct_state == reconstruct_state_drain_run)
979 sh->reconstruct_state = reconstruct_state_drain_result;
980 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
981 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
982 else {
983 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
984 sh->reconstruct_state = reconstruct_state_result;
985 }
Dan Williams91c00922007-01-02 13:52:30 -0700986
987 set_bit(STRIPE_HANDLE, &sh->state);
988 release_stripe(sh);
989}
990
991static void
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
993 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700994{
Dan Williams91c00922007-01-02 13:52:30 -0700995 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700996 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -0700997 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700998 int count = 0, pd_idx = sh->pd_idx, i;
999 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001000 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001001 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001002
Harvey Harrisone46b2722008-04-28 02:15:50 -07001003 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001004 (unsigned long long)sh->sector);
1005
1006 /* check if prexor is active which means only process blocks
1007 * that are part of a read-modify-write (written)
1008 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001009 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1010 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001011 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1012 for (i = disks; i--; ) {
1013 struct r5dev *dev = &sh->dev[i];
1014 if (dev->written)
1015 xor_srcs[count++] = dev->page;
1016 }
1017 } else {
1018 xor_dest = sh->dev[pd_idx].page;
1019 for (i = disks; i--; ) {
1020 struct r5dev *dev = &sh->dev[i];
1021 if (i != pd_idx)
1022 xor_srcs[count++] = dev->page;
1023 }
1024 }
1025
Dan Williams91c00922007-01-02 13:52:30 -07001026 /* 1/ if we prexor'd then the dest is reused as a source
1027 * 2/ if we did not prexor then we are redoing the parity
1028 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1029 * for the synchronous xor case
1030 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001031 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001032 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1033
1034 atomic_inc(&sh->count);
1035
Dan Williamsac6b53b2009-07-14 13:40:19 -07001036 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001037 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001038 if (unlikely(count == 1))
1039 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1040 else
1041 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001042}
1043
Dan Williamsac6b53b2009-07-14 13:40:19 -07001044static void
1045ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1046 struct dma_async_tx_descriptor *tx)
1047{
1048 struct async_submit_ctl submit;
1049 struct page **blocks = percpu->scribble;
1050 int count;
1051
1052 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1053
1054 count = set_syndrome_sources(blocks, sh);
1055
1056 atomic_inc(&sh->count);
1057
1058 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1059 sh, to_addr_conv(sh, percpu));
1060 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1061}
1062
Dan Williams91c00922007-01-02 13:52:30 -07001063static void ops_complete_check(void *stripe_head_ref)
1064{
1065 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001066
Harvey Harrisone46b2722008-04-28 02:15:50 -07001067 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001068 (unsigned long long)sh->sector);
1069
Dan Williamsecc65c92008-06-28 08:31:57 +10001070 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001071 set_bit(STRIPE_HANDLE, &sh->state);
1072 release_stripe(sh);
1073}
1074
Dan Williamsac6b53b2009-07-14 13:40:19 -07001075static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001076{
Dan Williams91c00922007-01-02 13:52:30 -07001077 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001078 int pd_idx = sh->pd_idx;
1079 int qd_idx = sh->qd_idx;
1080 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001081 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001082 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001083 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001084 int count;
1085 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001086
Harvey Harrisone46b2722008-04-28 02:15:50 -07001087 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001088 (unsigned long long)sh->sector);
1089
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 count = 0;
1091 xor_dest = sh->dev[pd_idx].page;
1092 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001093 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094 if (i == pd_idx || i == qd_idx)
1095 continue;
1096 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001097 }
1098
Dan Williamsd6f38f32009-07-14 11:50:52 -07001099 init_async_submit(&submit, 0, NULL, NULL, NULL,
1100 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001101 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001102 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001103
Dan Williams91c00922007-01-02 13:52:30 -07001104 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001105 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1106 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001107}
1108
Dan Williamsac6b53b2009-07-14 13:40:19 -07001109static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1110{
1111 struct page **srcs = percpu->scribble;
1112 struct async_submit_ctl submit;
1113 int count;
1114
1115 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1116 (unsigned long long)sh->sector, checkp);
1117
1118 count = set_syndrome_sources(srcs, sh);
1119 if (!checkp)
1120 srcs[count] = NULL;
1121
1122 atomic_inc(&sh->count);
1123 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1124 sh, to_addr_conv(sh, percpu));
1125 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1126 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1127}
1128
1129static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001130{
1131 int overlap_clear = 0, i, disks = sh->disks;
1132 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001133 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001134 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001135 struct raid5_percpu *percpu;
1136 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001137
Dan Williamsd6f38f32009-07-14 11:50:52 -07001138 cpu = get_cpu();
1139 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001140 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001141 ops_run_biofill(sh);
1142 overlap_clear++;
1143 }
1144
Dan Williams7b3a8712008-06-28 08:32:09 +10001145 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001146 if (level < 6)
1147 tx = ops_run_compute5(sh, percpu);
1148 else {
1149 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1150 tx = ops_run_compute6_1(sh, percpu);
1151 else
1152 tx = ops_run_compute6_2(sh, percpu);
1153 }
1154 /* terminate the chain if reconstruct is not set to be run */
1155 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001156 async_tx_ack(tx);
1157 }
Dan Williams91c00922007-01-02 13:52:30 -07001158
Dan Williams600aa102008-06-28 08:32:05 +10001159 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001160 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001161
Dan Williams600aa102008-06-28 08:32:05 +10001162 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001163 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001164 overlap_clear++;
1165 }
1166
Dan Williamsac6b53b2009-07-14 13:40:19 -07001167 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1168 if (level < 6)
1169 ops_run_reconstruct5(sh, percpu, tx);
1170 else
1171 ops_run_reconstruct6(sh, percpu, tx);
1172 }
Dan Williams91c00922007-01-02 13:52:30 -07001173
Dan Williamsac6b53b2009-07-14 13:40:19 -07001174 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1175 if (sh->check_state == check_state_run)
1176 ops_run_check_p(sh, percpu);
1177 else if (sh->check_state == check_state_run_q)
1178 ops_run_check_pq(sh, percpu, 0);
1179 else if (sh->check_state == check_state_run_pq)
1180 ops_run_check_pq(sh, percpu, 1);
1181 else
1182 BUG();
1183 }
Dan Williams91c00922007-01-02 13:52:30 -07001184
Dan Williams91c00922007-01-02 13:52:30 -07001185 if (overlap_clear)
1186 for (i = disks; i--; ) {
1187 struct r5dev *dev = &sh->dev[i];
1188 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1189 wake_up(&sh->raid_conf->wait_for_overlap);
1190 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001191 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001192}
1193
NeilBrown3f294f42005-11-08 21:39:25 -08001194static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001197 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1198 if (!sh)
1199 return 0;
1200 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1201 sh->raid_conf = conf;
1202 spin_lock_init(&sh->lock);
1203
1204 if (grow_buffers(sh, conf->raid_disks)) {
1205 shrink_buffers(sh, conf->raid_disks);
1206 kmem_cache_free(conf->slab_cache, sh);
1207 return 0;
1208 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001209 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -08001210 /* we just created an active stripe so... */
1211 atomic_set(&sh->count, 1);
1212 atomic_inc(&conf->active_stripes);
1213 INIT_LIST_HEAD(&sh->lru);
1214 release_stripe(sh);
1215 return 1;
1216}
1217
1218static int grow_stripes(raid5_conf_t *conf, int num)
1219{
Christoph Lametere18b8902006-12-06 20:33:20 -08001220 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int devs = conf->raid_disks;
1222
NeilBrown245f46c2009-03-31 14:39:39 +11001223 sprintf(conf->cache_name[0],
1224 "raid%d-%s", conf->level, mdname(conf->mddev));
1225 sprintf(conf->cache_name[1],
1226 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001227 conf->active_name = 0;
1228 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001230 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (!sc)
1232 return 1;
1233 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001234 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001235 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001236 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
NeilBrown29269552006-03-27 01:18:10 -08001240
Dan Williamsd6f38f32009-07-14 11:50:52 -07001241/**
1242 * scribble_len - return the required size of the scribble region
1243 * @num - total number of disks in the array
1244 *
1245 * The size must be enough to contain:
1246 * 1/ a struct page pointer for each device in the array +2
1247 * 2/ room to convert each entry in (1) to its corresponding dma
1248 * (dma_map_page()) or page (page_address()) address.
1249 *
1250 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1251 * calculate over all devices (not just the data blocks), using zeros in place
1252 * of the P and Q blocks.
1253 */
1254static size_t scribble_len(int num)
1255{
1256 size_t len;
1257
1258 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1259
1260 return len;
1261}
1262
NeilBrownad01c9e2006-03-27 01:18:07 -08001263static int resize_stripes(raid5_conf_t *conf, int newsize)
1264{
1265 /* Make all the stripes able to hold 'newsize' devices.
1266 * New slots in each stripe get 'page' set to a new page.
1267 *
1268 * This happens in stages:
1269 * 1/ create a new kmem_cache and allocate the required number of
1270 * stripe_heads.
1271 * 2/ gather all the old stripe_heads and tranfer the pages across
1272 * to the new stripe_heads. This will have the side effect of
1273 * freezing the array as once all stripe_heads have been collected,
1274 * no IO will be possible. Old stripe heads are freed once their
1275 * pages have been transferred over, and the old kmem_cache is
1276 * freed when all stripes are done.
1277 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1278 * we simple return a failre status - no need to clean anything up.
1279 * 4/ allocate new pages for the new slots in the new stripe_heads.
1280 * If this fails, we don't bother trying the shrink the
1281 * stripe_heads down again, we just leave them as they are.
1282 * As each stripe_head is processed the new one is released into
1283 * active service.
1284 *
1285 * Once step2 is started, we cannot afford to wait for a write,
1286 * so we use GFP_NOIO allocations.
1287 */
1288 struct stripe_head *osh, *nsh;
1289 LIST_HEAD(newstripes);
1290 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001291 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001292 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001293 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001294 int i;
1295
1296 if (newsize <= conf->pool_size)
1297 return 0; /* never bother to shrink */
1298
Dan Williamsb5470dc2008-06-27 21:44:04 -07001299 err = md_allow_write(conf->mddev);
1300 if (err)
1301 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001302
NeilBrownad01c9e2006-03-27 01:18:07 -08001303 /* Step 1 */
1304 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1305 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001306 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001307 if (!sc)
1308 return -ENOMEM;
1309
1310 for (i = conf->max_nr_stripes; i; i--) {
1311 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1312 if (!nsh)
1313 break;
1314
1315 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1316
1317 nsh->raid_conf = conf;
1318 spin_lock_init(&nsh->lock);
1319
1320 list_add(&nsh->lru, &newstripes);
1321 }
1322 if (i) {
1323 /* didn't get enough, give up */
1324 while (!list_empty(&newstripes)) {
1325 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1326 list_del(&nsh->lru);
1327 kmem_cache_free(sc, nsh);
1328 }
1329 kmem_cache_destroy(sc);
1330 return -ENOMEM;
1331 }
1332 /* Step 2 - Must use GFP_NOIO now.
1333 * OK, we have enough stripes, start collecting inactive
1334 * stripes and copying them over
1335 */
1336 list_for_each_entry(nsh, &newstripes, lru) {
1337 spin_lock_irq(&conf->device_lock);
1338 wait_event_lock_irq(conf->wait_for_stripe,
1339 !list_empty(&conf->inactive_list),
1340 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001341 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001342 );
1343 osh = get_free_stripe(conf);
1344 spin_unlock_irq(&conf->device_lock);
1345 atomic_set(&nsh->count, 1);
1346 for(i=0; i<conf->pool_size; i++)
1347 nsh->dev[i].page = osh->dev[i].page;
1348 for( ; i<newsize; i++)
1349 nsh->dev[i].page = NULL;
1350 kmem_cache_free(conf->slab_cache, osh);
1351 }
1352 kmem_cache_destroy(conf->slab_cache);
1353
1354 /* Step 3.
1355 * At this point, we are holding all the stripes so the array
1356 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001357 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001358 */
1359 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1360 if (ndisks) {
1361 for (i=0; i<conf->raid_disks; i++)
1362 ndisks[i] = conf->disks[i];
1363 kfree(conf->disks);
1364 conf->disks = ndisks;
1365 } else
1366 err = -ENOMEM;
1367
Dan Williamsd6f38f32009-07-14 11:50:52 -07001368 get_online_cpus();
1369 conf->scribble_len = scribble_len(newsize);
1370 for_each_present_cpu(cpu) {
1371 struct raid5_percpu *percpu;
1372 void *scribble;
1373
1374 percpu = per_cpu_ptr(conf->percpu, cpu);
1375 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1376
1377 if (scribble) {
1378 kfree(percpu->scribble);
1379 percpu->scribble = scribble;
1380 } else {
1381 err = -ENOMEM;
1382 break;
1383 }
1384 }
1385 put_online_cpus();
1386
NeilBrownad01c9e2006-03-27 01:18:07 -08001387 /* Step 4, return new stripes to service */
1388 while(!list_empty(&newstripes)) {
1389 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1390 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001391
NeilBrownad01c9e2006-03-27 01:18:07 -08001392 for (i=conf->raid_disks; i < newsize; i++)
1393 if (nsh->dev[i].page == NULL) {
1394 struct page *p = alloc_page(GFP_NOIO);
1395 nsh->dev[i].page = p;
1396 if (!p)
1397 err = -ENOMEM;
1398 }
1399 release_stripe(nsh);
1400 }
1401 /* critical section pass, GFP_NOIO no longer needed */
1402
1403 conf->slab_cache = sc;
1404 conf->active_name = 1-conf->active_name;
1405 conf->pool_size = newsize;
1406 return err;
1407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
NeilBrown3f294f42005-11-08 21:39:25 -08001409static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 struct stripe_head *sh;
1412
NeilBrown3f294f42005-11-08 21:39:25 -08001413 spin_lock_irq(&conf->device_lock);
1414 sh = get_free_stripe(conf);
1415 spin_unlock_irq(&conf->device_lock);
1416 if (!sh)
1417 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001418 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001419 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001420 kmem_cache_free(conf->slab_cache, sh);
1421 atomic_dec(&conf->active_stripes);
1422 return 1;
1423}
1424
1425static void shrink_stripes(raid5_conf_t *conf)
1426{
1427 while (drop_one_stripe(conf))
1428 ;
1429
NeilBrown29fc7e32006-02-03 03:03:41 -08001430 if (conf->slab_cache)
1431 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 conf->slab_cache = NULL;
1433}
1434
NeilBrown6712ecf2007-09-27 12:47:43 +02001435static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
NeilBrown99c0fb52009-03-31 14:39:38 +11001437 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001439 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001441 char b[BDEVNAME_SIZE];
1442 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 for (i=0 ; i<disks; i++)
1446 if (bi == &sh->dev[i].req)
1447 break;
1448
Dan Williams45b42332007-07-09 11:56:43 -07001449 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1450 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 uptodate);
1452 if (i == disks) {
1453 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
1457 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001459 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001460 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001461 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1462 " (%lu sectors at %llu on %s)\n",
1463 mdname(conf->mddev), STRIPE_SECTORS,
1464 (unsigned long long)(sh->sector
1465 + rdev->data_offset),
1466 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001467 clear_bit(R5_ReadError, &sh->dev[i].flags);
1468 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1469 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001470 if (atomic_read(&conf->disks[i].rdev->read_errors))
1471 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001473 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001474 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001475 rdev = conf->disks[i].rdev;
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001478 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001479 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001480 printk_rl(KERN_WARNING
1481 "raid5:%s: read error not correctable "
1482 "(sector %llu on %s).\n",
1483 mdname(conf->mddev),
1484 (unsigned long long)(sh->sector
1485 + rdev->data_offset),
1486 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001487 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001488 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001489 printk_rl(KERN_WARNING
1490 "raid5:%s: read error NOT corrected!! "
1491 "(sector %llu on %s).\n",
1492 mdname(conf->mddev),
1493 (unsigned long long)(sh->sector
1494 + rdev->data_offset),
1495 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001496 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001497 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001498 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001499 "raid5:%s: Too many read errors, failing device %s.\n",
1500 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001501 else
1502 retry = 1;
1503 if (retry)
1504 set_bit(R5_ReadError, &sh->dev[i].flags);
1505 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001506 clear_bit(R5_ReadError, &sh->dev[i].flags);
1507 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001508 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1513 set_bit(STRIPE_HANDLE, &sh->state);
1514 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
NeilBrownd710e132008-10-13 11:55:12 +11001517static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
NeilBrown99c0fb52009-03-31 14:39:38 +11001519 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001521 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 for (i=0 ; i<disks; i++)
1525 if (bi == &sh->dev[i].req)
1526 break;
1527
Dan Williams45b42332007-07-09 11:56:43 -07001528 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1530 uptodate);
1531 if (i == disks) {
1532 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001533 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (!uptodate)
1537 md_error(conf->mddev, conf->disks[i].rdev);
1538
1539 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1540
1541 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1542 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001543 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546
NeilBrown784052e2009-03-31 15:19:07 +11001547static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
NeilBrown784052e2009-03-31 15:19:07 +11001549static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 struct r5dev *dev = &sh->dev[i];
1552
1553 bio_init(&dev->req);
1554 dev->req.bi_io_vec = &dev->vec;
1555 dev->req.bi_vcnt++;
1556 dev->req.bi_max_vecs++;
1557 dev->vec.bv_page = dev->page;
1558 dev->vec.bv_len = STRIPE_SIZE;
1559 dev->vec.bv_offset = 0;
1560
1561 dev->req.bi_sector = sh->sector;
1562 dev->req.bi_private = sh;
1563
1564 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001565 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1569{
1570 char b[BDEVNAME_SIZE];
1571 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001572 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
NeilBrownb2d444d2005-11-08 21:39:31 -08001574 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001575 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001576 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1577 unsigned long flags;
1578 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001580 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * if recovery was running, make sure it aborts.
1583 */
NeilBrowndfc70642008-05-23 13:04:39 -07001584 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001586 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001587 printk(KERN_ALERT
1588 "raid5: Disk failure on %s, disabling device.\n"
1589 "raid5: Operation continuing on %d devices.\n",
1590 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594/*
1595 * Input: a 'big' sector number,
1596 * Output: index of the data and parity disk, and the sector # in them.
1597 */
NeilBrown112bf892009-03-31 14:39:38 +11001598static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001599 int previous, int *dd_idx,
1600 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 long stripe;
1603 unsigned long chunk_number;
1604 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001605 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001606 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001608 int algorithm = previous ? conf->prev_algo
1609 : conf->algorithm;
NeilBrown784052e2009-03-31 15:19:07 +11001610 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1611 : (conf->chunk_size >> 9);
NeilBrown112bf892009-03-31 14:39:38 +11001612 int raid_disks = previous ? conf->previous_raid_disks
1613 : conf->raid_disks;
1614 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /* First compute the information on this sector */
1617
1618 /*
1619 * Compute the chunk number and the sector offset inside the chunk
1620 */
1621 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1622 chunk_number = r_sector;
1623 BUG_ON(r_sector != chunk_number);
1624
1625 /*
1626 * Compute the stripe number
1627 */
1628 stripe = chunk_number / data_disks;
1629
1630 /*
1631 * Compute the data disk and parity disk indexes inside the stripe
1632 */
1633 *dd_idx = chunk_number % data_disks;
1634
1635 /*
1636 * Select the parity disk based on the user selected algorithm.
1637 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001638 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001639 switch(conf->level) {
1640 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001641 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001642 break;
1643 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001644 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001646 pd_idx = data_disks - stripe % raid_disks;
1647 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 (*dd_idx)++;
1649 break;
1650 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001651 pd_idx = stripe % raid_disks;
1652 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 (*dd_idx)++;
1654 break;
1655 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001656 pd_idx = data_disks - stripe % raid_disks;
1657 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001660 pd_idx = stripe % raid_disks;
1661 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001663 case ALGORITHM_PARITY_0:
1664 pd_idx = 0;
1665 (*dd_idx)++;
1666 break;
1667 case ALGORITHM_PARITY_N:
1668 pd_idx = data_disks;
1669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001671 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001672 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001673 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001674 }
1675 break;
1676 case 6:
1677
NeilBrowne183eae2009-03-31 15:20:22 +11001678 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001679 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001680 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1681 qd_idx = pd_idx + 1;
1682 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001683 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001684 qd_idx = 0;
1685 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001686 (*dd_idx) += 2; /* D D P Q D */
1687 break;
1688 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001689 pd_idx = stripe % raid_disks;
1690 qd_idx = pd_idx + 1;
1691 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001692 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001693 qd_idx = 0;
1694 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001695 (*dd_idx) += 2; /* D D P Q D */
1696 break;
1697 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001698 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1699 qd_idx = (pd_idx + 1) % raid_disks;
1700 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001701 break;
1702 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001703 pd_idx = stripe % raid_disks;
1704 qd_idx = (pd_idx + 1) % raid_disks;
1705 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001706 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001707
1708 case ALGORITHM_PARITY_0:
1709 pd_idx = 0;
1710 qd_idx = 1;
1711 (*dd_idx) += 2;
1712 break;
1713 case ALGORITHM_PARITY_N:
1714 pd_idx = data_disks;
1715 qd_idx = data_disks + 1;
1716 break;
1717
1718 case ALGORITHM_ROTATING_ZERO_RESTART:
1719 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1720 * of blocks for computing Q is different.
1721 */
1722 pd_idx = stripe % raid_disks;
1723 qd_idx = pd_idx + 1;
1724 if (pd_idx == raid_disks-1) {
1725 (*dd_idx)++; /* Q D D D P */
1726 qd_idx = 0;
1727 } else if (*dd_idx >= pd_idx)
1728 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001729 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001730 break;
1731
1732 case ALGORITHM_ROTATING_N_RESTART:
1733 /* Same a left_asymmetric, by first stripe is
1734 * D D D P Q rather than
1735 * Q D D D P
1736 */
1737 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1738 qd_idx = pd_idx + 1;
1739 if (pd_idx == raid_disks-1) {
1740 (*dd_idx)++; /* Q D D D P */
1741 qd_idx = 0;
1742 } else if (*dd_idx >= pd_idx)
1743 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001744 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001745 break;
1746
1747 case ALGORITHM_ROTATING_N_CONTINUE:
1748 /* Same as left_symmetric but Q is before P */
1749 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1750 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1751 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001752 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001753 break;
1754
1755 case ALGORITHM_LEFT_ASYMMETRIC_6:
1756 /* RAID5 left_asymmetric, with Q on last device */
1757 pd_idx = data_disks - stripe % (raid_disks-1);
1758 if (*dd_idx >= pd_idx)
1759 (*dd_idx)++;
1760 qd_idx = raid_disks - 1;
1761 break;
1762
1763 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1764 pd_idx = stripe % (raid_disks-1);
1765 if (*dd_idx >= pd_idx)
1766 (*dd_idx)++;
1767 qd_idx = raid_disks - 1;
1768 break;
1769
1770 case ALGORITHM_LEFT_SYMMETRIC_6:
1771 pd_idx = data_disks - stripe % (raid_disks-1);
1772 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1773 qd_idx = raid_disks - 1;
1774 break;
1775
1776 case ALGORITHM_RIGHT_SYMMETRIC_6:
1777 pd_idx = stripe % (raid_disks-1);
1778 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1779 qd_idx = raid_disks - 1;
1780 break;
1781
1782 case ALGORITHM_PARITY_0_6:
1783 pd_idx = 0;
1784 (*dd_idx)++;
1785 qd_idx = raid_disks - 1;
1786 break;
1787
1788
NeilBrown16a53ec2006-06-26 00:27:38 -07001789 default:
NeilBrownd710e132008-10-13 11:55:12 +11001790 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001791 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001792 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001793 }
1794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
NeilBrown911d4ee2009-03-31 14:39:38 +11001797 if (sh) {
1798 sh->pd_idx = pd_idx;
1799 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001800 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * Finally, compute the new sector number
1804 */
1805 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1806 return new_sector;
1807}
1808
1809
NeilBrown784052e2009-03-31 15:19:07 +11001810static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001813 int raid_disks = sh->disks;
1814 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sector_t new_sector = sh->sector, check;
NeilBrown784052e2009-03-31 15:19:07 +11001816 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1817 : (conf->chunk_size >> 9);
NeilBrowne183eae2009-03-31 15:20:22 +11001818 int algorithm = previous ? conf->prev_algo
1819 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sector_t stripe;
1821 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001822 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001824 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
NeilBrown16a53ec2006-06-26 00:27:38 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1828 stripe = new_sector;
1829 BUG_ON(new_sector != stripe);
1830
NeilBrown16a53ec2006-06-26 00:27:38 -07001831 if (i == sh->pd_idx)
1832 return 0;
1833 switch(conf->level) {
1834 case 4: break;
1835 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001836 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 case ALGORITHM_LEFT_ASYMMETRIC:
1838 case ALGORITHM_RIGHT_ASYMMETRIC:
1839 if (i > sh->pd_idx)
1840 i--;
1841 break;
1842 case ALGORITHM_LEFT_SYMMETRIC:
1843 case ALGORITHM_RIGHT_SYMMETRIC:
1844 if (i < sh->pd_idx)
1845 i += raid_disks;
1846 i -= (sh->pd_idx + 1);
1847 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001848 case ALGORITHM_PARITY_0:
1849 i -= 1;
1850 break;
1851 case ALGORITHM_PARITY_N:
1852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001854 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001855 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001856 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001857 }
1858 break;
1859 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001860 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001861 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001862 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001863 case ALGORITHM_LEFT_ASYMMETRIC:
1864 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001865 case ALGORITHM_ROTATING_ZERO_RESTART:
1866 case ALGORITHM_ROTATING_N_RESTART:
1867 if (sh->pd_idx == raid_disks-1)
1868 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001869 else if (i > sh->pd_idx)
1870 i -= 2; /* D D P Q D */
1871 break;
1872 case ALGORITHM_LEFT_SYMMETRIC:
1873 case ALGORITHM_RIGHT_SYMMETRIC:
1874 if (sh->pd_idx == raid_disks-1)
1875 i--; /* Q D D D P */
1876 else {
1877 /* D D P Q D */
1878 if (i < sh->pd_idx)
1879 i += raid_disks;
1880 i -= (sh->pd_idx + 2);
1881 }
1882 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001883 case ALGORITHM_PARITY_0:
1884 i -= 2;
1885 break;
1886 case ALGORITHM_PARITY_N:
1887 break;
1888 case ALGORITHM_ROTATING_N_CONTINUE:
1889 if (sh->pd_idx == 0)
1890 i--; /* P D D D Q */
1891 else if (i > sh->pd_idx)
1892 i -= 2; /* D D Q P D */
1893 break;
1894 case ALGORITHM_LEFT_ASYMMETRIC_6:
1895 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1896 if (i > sh->pd_idx)
1897 i--;
1898 break;
1899 case ALGORITHM_LEFT_SYMMETRIC_6:
1900 case ALGORITHM_RIGHT_SYMMETRIC_6:
1901 if (i < sh->pd_idx)
1902 i += data_disks + 1;
1903 i -= (sh->pd_idx + 1);
1904 break;
1905 case ALGORITHM_PARITY_0_6:
1906 i -= 1;
1907 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001908 default:
NeilBrownd710e132008-10-13 11:55:12 +11001909 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001910 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001911 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 }
1913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915
1916 chunk_number = stripe * data_disks + i;
1917 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1918
NeilBrown112bf892009-03-31 14:39:38 +11001919 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001920 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001921 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1922 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001923 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925 }
1926 return r_sector;
1927}
1928
1929
1930
1931/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001932 * Copy data between a page in the stripe cache, and one or more bion
1933 * The page could align with the middle of the bio, or there could be
1934 * several bion, each with several bio_vecs, which cover part of the page
1935 * Multiple bion are linked together on bi_next. There may be extras
1936 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 */
1938static void copy_data(int frombio, struct bio *bio,
1939 struct page *page,
1940 sector_t sector)
1941{
1942 char *pa = page_address(page);
1943 struct bio_vec *bvl;
1944 int i;
1945 int page_offset;
1946
1947 if (bio->bi_sector >= sector)
1948 page_offset = (signed)(bio->bi_sector - sector) * 512;
1949 else
1950 page_offset = (signed)(sector - bio->bi_sector) * -512;
1951 bio_for_each_segment(bvl, bio, i) {
1952 int len = bio_iovec_idx(bio,i)->bv_len;
1953 int clen;
1954 int b_offset = 0;
1955
1956 if (page_offset < 0) {
1957 b_offset = -page_offset;
1958 page_offset += b_offset;
1959 len -= b_offset;
1960 }
1961
1962 if (len > 0 && page_offset + len > STRIPE_SIZE)
1963 clen = STRIPE_SIZE - page_offset;
1964 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (clen > 0) {
1967 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1968 if (frombio)
1969 memcpy(pa+page_offset, ba+b_offset, clen);
1970 else
1971 memcpy(ba+b_offset, pa+page_offset, clen);
1972 __bio_kunmap_atomic(ba, KM_USER0);
1973 }
1974 if (clen < len) /* hit end of page */
1975 break;
1976 page_offset += len;
1977 }
1978}
1979
Dan Williams9bc89cd2007-01-02 11:10:44 -07001980#define check_xor() do { \
1981 if (count == MAX_XOR_BLOCKS) { \
1982 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1983 count = 0; \
1984 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 } while(0)
1986
NeilBrown16a53ec2006-06-26 00:27:38 -07001987static void compute_parity6(struct stripe_head *sh, int method)
1988{
NeilBrownbff61972009-03-31 14:33:13 +11001989 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001990 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown67cc2b82009-03-31 14:39:38 +11001991 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
NeilBrown16a53ec2006-06-26 00:27:38 -07001992 struct bio *chosen;
1993 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001994 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001995
NeilBrownd0dabf72009-03-31 14:39:38 +11001996 pd_idx = sh->pd_idx;
1997 qd_idx = sh->qd_idx;
1998 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001999
Dan Williams45b42332007-07-09 11:56:43 -07002000 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07002001 (unsigned long long)sh->sector, method);
2002
2003 switch(method) {
2004 case READ_MODIFY_WRITE:
2005 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
2006 case RECONSTRUCT_WRITE:
2007 for (i= disks; i-- ;)
2008 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
2009 chosen = sh->dev[i].towrite;
2010 sh->dev[i].towrite = NULL;
2011
2012 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2013 wake_up(&conf->wait_for_overlap);
2014
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02002015 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07002016 sh->dev[i].written = chosen;
2017 }
2018 break;
2019 case CHECK_PARITY:
2020 BUG(); /* Not implemented yet */
2021 }
2022
2023 for (i = disks; i--;)
2024 if (sh->dev[i].written) {
2025 sector_t sector = sh->dev[i].sector;
2026 struct bio *wbi = sh->dev[i].written;
2027 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
2028 copy_data(1, wbi, sh->dev[i].page, sector);
2029 wbi = r5_next_bio(wbi, sector);
2030 }
2031
2032 set_bit(R5_LOCKED, &sh->dev[i].flags);
2033 set_bit(R5_UPTODATE, &sh->dev[i].flags);
2034 }
2035
NeilBrownd0dabf72009-03-31 14:39:38 +11002036 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
NeilBrown67cc2b82009-03-31 14:39:38 +11002037
2038 for (i = 0; i < disks; i++)
2039 ptrs[i] = (void *)raid6_empty_zero_page;
2040
NeilBrownd0dabf72009-03-31 14:39:38 +11002041 count = 0;
2042 i = d0_idx;
2043 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11002044 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
2045
NeilBrownd0dabf72009-03-31 14:39:38 +11002046 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11002047 if (slot < syndrome_disks &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002048 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
2049 printk(KERN_ERR "block %d/%d not uptodate "
2050 "on parity calc\n", i, count);
2051 BUG();
2052 }
NeilBrown67cc2b82009-03-31 14:39:38 +11002053
NeilBrownd0dabf72009-03-31 14:39:38 +11002054 i = raid6_next_disk(i, disks);
2055 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11002056 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07002057
NeilBrown67cc2b82009-03-31 14:39:38 +11002058 raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07002059
2060 switch(method) {
2061 case RECONSTRUCT_WRITE:
2062 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2063 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
2064 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2065 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
2066 break;
2067 case UPDATE_PARITY:
2068 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2069 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
2070 break;
2071 }
2072}
2073
2074
2075/* Compute one missing block */
2076static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
2077{
NeilBrownf4168852007-02-28 20:11:53 -08002078 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07002079 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11002080 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07002081
Dan Williams45b42332007-07-09 11:56:43 -07002082 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07002083 (unsigned long long)sh->sector, dd_idx);
2084
2085 if ( dd_idx == qd_idx ) {
2086 /* We're actually computing the Q drive */
2087 compute_parity6(sh, UPDATE_PARITY);
2088 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07002089 dest = page_address(sh->dev[dd_idx].page);
2090 if (!nozero) memset(dest, 0, STRIPE_SIZE);
2091 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07002092 for (i = disks ; i--; ) {
2093 if (i == dd_idx || i == qd_idx)
2094 continue;
2095 p = page_address(sh->dev[i].page);
2096 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
2097 ptr[count++] = p;
2098 else
2099 printk("compute_block() %d, stripe %llu, %d"
2100 " not present\n", dd_idx,
2101 (unsigned long long)sh->sector, i);
2102
2103 check_xor();
2104 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07002105 if (count)
2106 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07002107 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
2108 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
2109 }
2110}
2111
2112/* Compute two missing blocks */
2113static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
2114{
NeilBrownf4168852007-02-28 20:11:53 -08002115 int i, count, disks = sh->disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002116 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
NeilBrownd0dabf72009-03-31 14:39:38 +11002117 int d0_idx = raid6_d0(sh);
2118 int faila = -1, failb = -1;
2119 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11002120 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07002121
NeilBrown67cc2b82009-03-31 14:39:38 +11002122 for (i = 0; i < disks ; i++)
2123 ptrs[i] = (void *)raid6_empty_zero_page;
NeilBrownd0dabf72009-03-31 14:39:38 +11002124 count = 0;
2125 i = d0_idx;
2126 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11002127 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
2128
NeilBrownd0dabf72009-03-31 14:39:38 +11002129 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11002130
NeilBrownd0dabf72009-03-31 14:39:38 +11002131 if (i == dd_idx1)
2132 faila = slot;
2133 if (i == dd_idx2)
2134 failb = slot;
2135 i = raid6_next_disk(i, disks);
2136 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11002137 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07002138
2139 BUG_ON(faila == failb);
2140 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
2141
Dan Williams45b42332007-07-09 11:56:43 -07002142 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11002143 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
2144 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07002145
NeilBrown67cc2b82009-03-31 14:39:38 +11002146 if (failb == syndrome_disks+1) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002147 /* Q disk is one of the missing disks */
NeilBrown67cc2b82009-03-31 14:39:38 +11002148 if (faila == syndrome_disks) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002149 /* Missing P+Q, just recompute */
2150 compute_parity6(sh, UPDATE_PARITY);
2151 return;
2152 } else {
2153 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11002154 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
2155 dd_idx2 : dd_idx1),
2156 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07002157 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
2158 return;
2159 }
2160 }
2161
NeilBrownd0dabf72009-03-31 14:39:38 +11002162 /* We're missing D+P or D+D; */
NeilBrown67cc2b82009-03-31 14:39:38 +11002163 if (failb == syndrome_disks) {
NeilBrownd0dabf72009-03-31 14:39:38 +11002164 /* We're missing D+P. */
NeilBrown67cc2b82009-03-31 14:39:38 +11002165 raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
NeilBrownd0dabf72009-03-31 14:39:38 +11002166 } else {
2167 /* We're missing D+D. */
NeilBrown67cc2b82009-03-31 14:39:38 +11002168 raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
2169 ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07002170 }
NeilBrownd0dabf72009-03-31 14:39:38 +11002171
2172 /* Both the above update both missing blocks */
2173 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
2174 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07002175}
2176
Dan Williams600aa102008-06-28 08:32:05 +10002177static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002178schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002179 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002180{
2181 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002182 raid5_conf_t *conf = sh->raid_conf;
2183 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002184
Dan Williamse33129d2007-01-02 13:52:30 -07002185 if (rcw) {
2186 /* if we are not expanding this is a proper write request, and
2187 * there will be bios with new data to be drained into the
2188 * stripe cache
2189 */
2190 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002191 sh->reconstruct_state = reconstruct_state_drain_run;
2192 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2193 } else
2194 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002195
Dan Williamsac6b53b2009-07-14 13:40:19 -07002196 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002197
2198 for (i = disks; i--; ) {
2199 struct r5dev *dev = &sh->dev[i];
2200
2201 if (dev->towrite) {
2202 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002203 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002204 if (!expand)
2205 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002206 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002207 }
2208 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002209 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002210 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002211 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002212 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002213 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002214 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2215 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2216
Dan Williamsd8ee0722008-06-28 08:32:06 +10002217 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002218 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2219 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002220 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002221
2222 for (i = disks; i--; ) {
2223 struct r5dev *dev = &sh->dev[i];
2224 if (i == pd_idx)
2225 continue;
2226
Dan Williamse33129d2007-01-02 13:52:30 -07002227 if (dev->towrite &&
2228 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002229 test_bit(R5_Wantcompute, &dev->flags))) {
2230 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002231 set_bit(R5_LOCKED, &dev->flags);
2232 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002233 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002234 }
2235 }
2236 }
2237
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002238 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002239 * are in flight
2240 */
2241 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2242 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002243 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002244
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002245 if (level == 6) {
2246 int qd_idx = sh->qd_idx;
2247 struct r5dev *dev = &sh->dev[qd_idx];
2248
2249 set_bit(R5_LOCKED, &dev->flags);
2250 clear_bit(R5_UPTODATE, &dev->flags);
2251 s->locked++;
2252 }
2253
Dan Williams600aa102008-06-28 08:32:05 +10002254 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002255 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002256 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002257}
NeilBrown16a53ec2006-06-26 00:27:38 -07002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
2260 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002261 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 * The bi_next chain must be in order.
2263 */
2264static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2265{
2266 struct bio **bip;
2267 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002268 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Dan Williams45b42332007-07-09 11:56:43 -07002270 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 (unsigned long long)bi->bi_sector,
2272 (unsigned long long)sh->sector);
2273
2274
2275 spin_lock(&sh->lock);
2276 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002277 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002279 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2280 firstwrite = 1;
2281 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 bip = &sh->dev[dd_idx].toread;
2283 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2284 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2285 goto overlap;
2286 bip = & (*bip)->bi_next;
2287 }
2288 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2289 goto overlap;
2290
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002291 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (*bip)
2293 bi->bi_next = *bip;
2294 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002295 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 spin_unlock_irq(&conf->device_lock);
2297 spin_unlock(&sh->lock);
2298
Dan Williams45b42332007-07-09 11:56:43 -07002299 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 (unsigned long long)bi->bi_sector,
2301 (unsigned long long)sh->sector, dd_idx);
2302
NeilBrown72626682005-09-09 16:23:54 -07002303 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002304 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2305 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002306 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002307 set_bit(STRIPE_BIT_DELAY, &sh->state);
2308 }
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (forwrite) {
2311 /* check if page is covered */
2312 sector_t sector = sh->dev[dd_idx].sector;
2313 for (bi=sh->dev[dd_idx].towrite;
2314 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2315 bi && bi->bi_sector <= sector;
2316 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2317 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2318 sector = bi->bi_sector + (bi->bi_size>>9);
2319 }
2320 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2321 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2322 }
2323 return 1;
2324
2325 overlap:
2326 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2327 spin_unlock_irq(&conf->device_lock);
2328 spin_unlock(&sh->lock);
2329 return 0;
2330}
2331
NeilBrown29269552006-03-27 01:18:10 -08002332static void end_reshape(raid5_conf_t *conf);
2333
NeilBrown16a53ec2006-06-26 00:27:38 -07002334static int page_is_zero(struct page *p)
2335{
2336 char *a = page_address(p);
2337 return ((*(u32*)a) == 0 &&
2338 memcmp(a, a+4, STRIPE_SIZE-4)==0);
2339}
2340
NeilBrown911d4ee2009-03-31 14:39:38 +11002341static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2342 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002343{
NeilBrown784052e2009-03-31 15:19:07 +11002344 int sectors_per_chunk =
2345 previous ? (conf->prev_chunk >> 9)
2346 : (conf->chunk_size >> 9);
NeilBrown911d4ee2009-03-31 14:39:38 +11002347 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002348 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002349 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002350
NeilBrown112bf892009-03-31 14:39:38 +11002351 raid5_compute_sector(conf,
2352 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002353 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002354 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002355 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002356}
2357
Dan Williamsa4456852007-07-09 11:56:43 -07002358static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002359handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002360 struct stripe_head_state *s, int disks,
2361 struct bio **return_bi)
2362{
2363 int i;
2364 for (i = disks; i--; ) {
2365 struct bio *bi;
2366 int bitmap_end = 0;
2367
2368 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2369 mdk_rdev_t *rdev;
2370 rcu_read_lock();
2371 rdev = rcu_dereference(conf->disks[i].rdev);
2372 if (rdev && test_bit(In_sync, &rdev->flags))
2373 /* multiple read failures in one stripe */
2374 md_error(conf->mddev, rdev);
2375 rcu_read_unlock();
2376 }
2377 spin_lock_irq(&conf->device_lock);
2378 /* fail all writes first */
2379 bi = sh->dev[i].towrite;
2380 sh->dev[i].towrite = NULL;
2381 if (bi) {
2382 s->to_write--;
2383 bitmap_end = 1;
2384 }
2385
2386 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2387 wake_up(&conf->wait_for_overlap);
2388
2389 while (bi && bi->bi_sector <
2390 sh->dev[i].sector + STRIPE_SECTORS) {
2391 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2392 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002393 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002394 md_write_end(conf->mddev);
2395 bi->bi_next = *return_bi;
2396 *return_bi = bi;
2397 }
2398 bi = nextbi;
2399 }
2400 /* and fail all 'written' */
2401 bi = sh->dev[i].written;
2402 sh->dev[i].written = NULL;
2403 if (bi) bitmap_end = 1;
2404 while (bi && bi->bi_sector <
2405 sh->dev[i].sector + STRIPE_SECTORS) {
2406 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2407 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002408 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002409 md_write_end(conf->mddev);
2410 bi->bi_next = *return_bi;
2411 *return_bi = bi;
2412 }
2413 bi = bi2;
2414 }
2415
Dan Williamsb5e98d62007-01-02 13:52:31 -07002416 /* fail any reads if this device is non-operational and
2417 * the data has not reached the cache yet.
2418 */
2419 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2420 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2421 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002422 bi = sh->dev[i].toread;
2423 sh->dev[i].toread = NULL;
2424 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2425 wake_up(&conf->wait_for_overlap);
2426 if (bi) s->to_read--;
2427 while (bi && bi->bi_sector <
2428 sh->dev[i].sector + STRIPE_SECTORS) {
2429 struct bio *nextbi =
2430 r5_next_bio(bi, sh->dev[i].sector);
2431 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002432 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002433 bi->bi_next = *return_bi;
2434 *return_bi = bi;
2435 }
2436 bi = nextbi;
2437 }
2438 }
2439 spin_unlock_irq(&conf->device_lock);
2440 if (bitmap_end)
2441 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2442 STRIPE_SECTORS, 0, 0);
2443 }
2444
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002445 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2446 if (atomic_dec_and_test(&conf->pending_full_writes))
2447 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002448}
2449
Dan Williams1fe797e2008-06-28 09:16:30 +10002450/* fetch_block5 - checks the given member device to see if its data needs
2451 * to be read or computed to satisfy a request.
2452 *
2453 * Returns 1 when no more member devices need to be checked, otherwise returns
2454 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002455 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002456static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2457 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002458{
2459 struct r5dev *dev = &sh->dev[disk_idx];
2460 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2461
Dan Williamsf38e1212007-01-02 13:52:30 -07002462 /* is the data in this block needed, and can we get it? */
2463 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002464 !test_bit(R5_UPTODATE, &dev->flags) &&
2465 (dev->toread ||
2466 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2467 s->syncing || s->expanding ||
2468 (s->failed &&
2469 (failed_dev->toread ||
2470 (failed_dev->towrite &&
2471 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002472 /* We would like to get this block, possibly by computing it,
2473 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002474 */
2475 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002476 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002477 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2478 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002479 set_bit(R5_Wantcompute, &dev->flags);
2480 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002481 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002482 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002483 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002484 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002485 * before writes. R5_Wantcompute flags a block that will
2486 * be R5_UPTODATE by the time it is needed for a
2487 * subsequent operation.
2488 */
2489 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002490 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002491 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002492 set_bit(R5_LOCKED, &dev->flags);
2493 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002494 s->locked++;
2495 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2496 s->syncing);
2497 }
2498 }
2499
Dan Williams1fe797e2008-06-28 09:16:30 +10002500 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002501}
2502
Dan Williams1fe797e2008-06-28 09:16:30 +10002503/**
2504 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2505 */
2506static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002507 struct stripe_head_state *s, int disks)
2508{
2509 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002510
Dan Williamsf38e1212007-01-02 13:52:30 -07002511 /* look for blocks to read/compute, skip this if a compute
2512 * is already in flight, or if the stripe contents are in the
2513 * midst of changing due to a write
2514 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002515 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002516 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002517 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002518 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002519 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002520 set_bit(STRIPE_HANDLE, &sh->state);
2521}
2522
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002523/* fetch_block6 - checks the given member device to see if its data needs
2524 * to be read or computed to satisfy a request.
2525 *
2526 * Returns 1 when no more member devices need to be checked, otherwise returns
2527 * 0 to tell the loop in handle_stripe_fill6 to continue
2528 */
2529static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2530 struct r6_state *r6s, int disk_idx, int disks)
2531{
2532 struct r5dev *dev = &sh->dev[disk_idx];
2533 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2534 &sh->dev[r6s->failed_num[1]] };
2535
2536 if (!test_bit(R5_LOCKED, &dev->flags) &&
2537 !test_bit(R5_UPTODATE, &dev->flags) &&
2538 (dev->toread ||
2539 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2540 s->syncing || s->expanding ||
2541 (s->failed >= 1 &&
2542 (fdev[0]->toread || s->to_write)) ||
2543 (s->failed >= 2 &&
2544 (fdev[1]->toread || s->to_write)))) {
2545 /* we would like to get this block, possibly by computing it,
2546 * otherwise read it if the backing disk is insync
2547 */
2548 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2549 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2550 if ((s->uptodate == disks - 1) &&
2551 (s->failed && (disk_idx == r6s->failed_num[0] ||
2552 disk_idx == r6s->failed_num[1]))) {
2553 /* have disk failed, and we're requested to fetch it;
2554 * do compute it
2555 */
2556 pr_debug("Computing stripe %llu block %d\n",
2557 (unsigned long long)sh->sector, disk_idx);
2558 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2559 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2560 set_bit(R5_Wantcompute, &dev->flags);
2561 sh->ops.target = disk_idx;
2562 sh->ops.target2 = -1; /* no 2nd target */
2563 s->req_compute = 1;
2564 s->uptodate++;
2565 return 1;
2566 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2567 /* Computing 2-failure is *very* expensive; only
2568 * do it if failed >= 2
2569 */
2570 int other;
2571 for (other = disks; other--; ) {
2572 if (other == disk_idx)
2573 continue;
2574 if (!test_bit(R5_UPTODATE,
2575 &sh->dev[other].flags))
2576 break;
2577 }
2578 BUG_ON(other < 0);
2579 pr_debug("Computing stripe %llu blocks %d,%d\n",
2580 (unsigned long long)sh->sector,
2581 disk_idx, other);
2582 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2583 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2584 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2585 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2586 sh->ops.target = disk_idx;
2587 sh->ops.target2 = other;
2588 s->uptodate += 2;
2589 s->req_compute = 1;
2590 return 1;
2591 } else if (test_bit(R5_Insync, &dev->flags)) {
2592 set_bit(R5_LOCKED, &dev->flags);
2593 set_bit(R5_Wantread, &dev->flags);
2594 s->locked++;
2595 pr_debug("Reading block %d (sync=%d)\n",
2596 disk_idx, s->syncing);
2597 }
2598 }
2599
2600 return 0;
2601}
2602
2603/**
2604 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2605 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002606static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002607 struct stripe_head_state *s, struct r6_state *r6s,
2608 int disks)
2609{
2610 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002611
2612 /* look for blocks to read/compute, skip this if a compute
2613 * is already in flight, or if the stripe contents are in the
2614 * midst of changing due to a write
2615 */
2616 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2617 !sh->reconstruct_state)
2618 for (i = disks; i--; )
2619 if (fetch_block6(sh, s, r6s, i, disks))
2620 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002621 set_bit(STRIPE_HANDLE, &sh->state);
2622}
2623
2624
Dan Williams1fe797e2008-06-28 09:16:30 +10002625/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002626 * any written block on an uptodate or failed drive can be returned.
2627 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2628 * never LOCKED, so we don't need to test 'failed' directly.
2629 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002630static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002631 struct stripe_head *sh, int disks, struct bio **return_bi)
2632{
2633 int i;
2634 struct r5dev *dev;
2635
2636 for (i = disks; i--; )
2637 if (sh->dev[i].written) {
2638 dev = &sh->dev[i];
2639 if (!test_bit(R5_LOCKED, &dev->flags) &&
2640 test_bit(R5_UPTODATE, &dev->flags)) {
2641 /* We can return any write requests */
2642 struct bio *wbi, *wbi2;
2643 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002644 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002645 spin_lock_irq(&conf->device_lock);
2646 wbi = dev->written;
2647 dev->written = NULL;
2648 while (wbi && wbi->bi_sector <
2649 dev->sector + STRIPE_SECTORS) {
2650 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002651 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002652 md_write_end(conf->mddev);
2653 wbi->bi_next = *return_bi;
2654 *return_bi = wbi;
2655 }
2656 wbi = wbi2;
2657 }
2658 if (dev->towrite == NULL)
2659 bitmap_end = 1;
2660 spin_unlock_irq(&conf->device_lock);
2661 if (bitmap_end)
2662 bitmap_endwrite(conf->mddev->bitmap,
2663 sh->sector,
2664 STRIPE_SECTORS,
2665 !test_bit(STRIPE_DEGRADED, &sh->state),
2666 0);
2667 }
2668 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002669
2670 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2671 if (atomic_dec_and_test(&conf->pending_full_writes))
2672 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002673}
2674
Dan Williams1fe797e2008-06-28 09:16:30 +10002675static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002676 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2677{
2678 int rmw = 0, rcw = 0, i;
2679 for (i = disks; i--; ) {
2680 /* would I have to read this buffer for read_modify_write */
2681 struct r5dev *dev = &sh->dev[i];
2682 if ((dev->towrite || i == sh->pd_idx) &&
2683 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002684 !(test_bit(R5_UPTODATE, &dev->flags) ||
2685 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002686 if (test_bit(R5_Insync, &dev->flags))
2687 rmw++;
2688 else
2689 rmw += 2*disks; /* cannot read it */
2690 }
2691 /* Would I have to read this buffer for reconstruct_write */
2692 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2693 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002694 !(test_bit(R5_UPTODATE, &dev->flags) ||
2695 test_bit(R5_Wantcompute, &dev->flags))) {
2696 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002697 else
2698 rcw += 2*disks;
2699 }
2700 }
Dan Williams45b42332007-07-09 11:56:43 -07002701 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002702 (unsigned long long)sh->sector, rmw, rcw);
2703 set_bit(STRIPE_HANDLE, &sh->state);
2704 if (rmw < rcw && rmw > 0)
2705 /* prefer read-modify-write, but need to get some data */
2706 for (i = disks; i--; ) {
2707 struct r5dev *dev = &sh->dev[i];
2708 if ((dev->towrite || i == sh->pd_idx) &&
2709 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002710 !(test_bit(R5_UPTODATE, &dev->flags) ||
2711 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002712 test_bit(R5_Insync, &dev->flags)) {
2713 if (
2714 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002715 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002716 "%d for r-m-w\n", i);
2717 set_bit(R5_LOCKED, &dev->flags);
2718 set_bit(R5_Wantread, &dev->flags);
2719 s->locked++;
2720 } else {
2721 set_bit(STRIPE_DELAYED, &sh->state);
2722 set_bit(STRIPE_HANDLE, &sh->state);
2723 }
2724 }
2725 }
2726 if (rcw <= rmw && rcw > 0)
2727 /* want reconstruct write, but need to get some data */
2728 for (i = disks; i--; ) {
2729 struct r5dev *dev = &sh->dev[i];
2730 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2731 i != sh->pd_idx &&
2732 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002733 !(test_bit(R5_UPTODATE, &dev->flags) ||
2734 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002735 test_bit(R5_Insync, &dev->flags)) {
2736 if (
2737 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002738 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002739 "%d for Reconstruct\n", i);
2740 set_bit(R5_LOCKED, &dev->flags);
2741 set_bit(R5_Wantread, &dev->flags);
2742 s->locked++;
2743 } else {
2744 set_bit(STRIPE_DELAYED, &sh->state);
2745 set_bit(STRIPE_HANDLE, &sh->state);
2746 }
2747 }
2748 }
2749 /* now if nothing is locked, and if we have enough data,
2750 * we can start a write request
2751 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002752 /* since handle_stripe can be called at any time we need to handle the
2753 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002754 * subsequent call wants to start a write request. raid_run_ops only
2755 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002756 * simultaneously. If this is not the case then new writes need to be
2757 * held off until the compute completes.
2758 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002759 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2760 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2761 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002762 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002763}
2764
Dan Williams1fe797e2008-06-28 09:16:30 +10002765static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002766 struct stripe_head *sh, struct stripe_head_state *s,
2767 struct r6_state *r6s, int disks)
2768{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002769 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002770 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002771
2772 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002773 for (i = disks; i--; ) {
2774 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002775 /* check if we haven't enough data */
2776 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2777 i != pd_idx && i != qd_idx &&
2778 !test_bit(R5_LOCKED, &dev->flags) &&
2779 !(test_bit(R5_UPTODATE, &dev->flags) ||
2780 test_bit(R5_Wantcompute, &dev->flags))) {
2781 rcw++;
2782 if (!test_bit(R5_Insync, &dev->flags))
2783 continue; /* it's a failed drive */
2784
2785 if (
2786 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2787 pr_debug("Read_old stripe %llu "
2788 "block %d for Reconstruct\n",
2789 (unsigned long long)sh->sector, i);
2790 set_bit(R5_LOCKED, &dev->flags);
2791 set_bit(R5_Wantread, &dev->flags);
2792 s->locked++;
2793 } else {
2794 pr_debug("Request delayed stripe %llu "
2795 "block %d for Reconstruct\n",
2796 (unsigned long long)sh->sector, i);
2797 set_bit(STRIPE_DELAYED, &sh->state);
2798 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002799 }
2800 }
2801 }
Dan Williamsa4456852007-07-09 11:56:43 -07002802 /* now if nothing is locked, and if we have enough data, we can start a
2803 * write request
2804 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002805 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2806 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002807 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002808 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002809 }
2810}
2811
2812static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2813 struct stripe_head_state *s, int disks)
2814{
Dan Williamsecc65c92008-06-28 08:31:57 +10002815 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002816
Dan Williamsbd2ab672008-04-10 21:29:27 -07002817 set_bit(STRIPE_HANDLE, &sh->state);
2818
Dan Williamsecc65c92008-06-28 08:31:57 +10002819 switch (sh->check_state) {
2820 case check_state_idle:
2821 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002822 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002823 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002824 sh->check_state = check_state_run;
2825 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002826 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002827 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002828 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002829 }
Dan Williamsa4456852007-07-09 11:56:43 -07002830 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002831 /* fall through */
2832 case check_state_compute_result:
2833 sh->check_state = check_state_idle;
2834 if (!dev)
2835 dev = &sh->dev[sh->pd_idx];
2836
2837 /* check that a write has not made the stripe insync */
2838 if (test_bit(STRIPE_INSYNC, &sh->state))
2839 break;
2840
2841 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002842 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2843 BUG_ON(s->uptodate != disks);
2844
2845 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002846 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002847 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002848
Dan Williamsa4456852007-07-09 11:56:43 -07002849 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002850 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002851 break;
2852 case check_state_run:
2853 break; /* we will be called again upon completion */
2854 case check_state_check_result:
2855 sh->check_state = check_state_idle;
2856
2857 /* if a failure occurred during the check operation, leave
2858 * STRIPE_INSYNC not set and let the stripe be handled again
2859 */
2860 if (s->failed)
2861 break;
2862
2863 /* handle a successful check operation, if parity is correct
2864 * we are done. Otherwise update the mismatch count and repair
2865 * parity if !MD_RECOVERY_CHECK
2866 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002867 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002868 /* parity is correct (on disc,
2869 * not in buffer any more)
2870 */
2871 set_bit(STRIPE_INSYNC, &sh->state);
2872 else {
2873 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2874 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2875 /* don't try to repair!! */
2876 set_bit(STRIPE_INSYNC, &sh->state);
2877 else {
2878 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002879 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002880 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2881 set_bit(R5_Wantcompute,
2882 &sh->dev[sh->pd_idx].flags);
2883 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002884 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 s->uptodate++;
2886 }
2887 }
2888 break;
2889 case check_state_compute_run:
2890 break;
2891 default:
2892 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2893 __func__, sh->check_state,
2894 (unsigned long long) sh->sector);
2895 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002896 }
2897}
2898
2899
2900static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002901 struct stripe_head_state *s,
2902 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002903{
2904 int update_p = 0, update_q = 0;
2905 struct r5dev *dev;
2906 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002907 int qd_idx = sh->qd_idx;
Dan Williams36d1c642009-07-14 11:48:22 -07002908 unsigned long cpu;
2909 struct page *tmp_page;
Dan Williamsa4456852007-07-09 11:56:43 -07002910
2911 set_bit(STRIPE_HANDLE, &sh->state);
2912
2913 BUG_ON(s->failed > 2);
2914 BUG_ON(s->uptodate < disks);
2915 /* Want to check and possibly repair P and Q.
2916 * However there could be one 'failed' device, in which
2917 * case we can only check one of them, possibly using the
2918 * other to generate missing data
2919 */
Dan Williams36d1c642009-07-14 11:48:22 -07002920 cpu = get_cpu();
2921 tmp_page = per_cpu_ptr(conf->percpu, cpu)->spare_page;
2922 if (s->failed == r6s->q_failed) {
2923 /* The only possible failed device holds 'Q', so it
2924 * makes sense to check P (If anything else were failed,
2925 * we would have used P to recreate it).
Dan Williamsa4456852007-07-09 11:56:43 -07002926 */
Dan Williams36d1c642009-07-14 11:48:22 -07002927 compute_block_1(sh, pd_idx, 1);
2928 if (!page_is_zero(sh->dev[pd_idx].page)) {
2929 compute_block_1(sh, pd_idx, 0);
2930 update_p = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002931 }
Dan Williamsa4456852007-07-09 11:56:43 -07002932 }
Dan Williams36d1c642009-07-14 11:48:22 -07002933 if (!r6s->q_failed && s->failed < 2) {
2934 /* q is not failed, and we didn't use it to generate
2935 * anything, so it makes sense to check it
2936 */
2937 memcpy(page_address(tmp_page),
2938 page_address(sh->dev[qd_idx].page),
2939 STRIPE_SIZE);
2940 compute_parity6(sh, UPDATE_PARITY);
2941 if (memcmp(page_address(tmp_page),
2942 page_address(sh->dev[qd_idx].page),
2943 STRIPE_SIZE) != 0) {
2944 clear_bit(STRIPE_INSYNC, &sh->state);
2945 update_q = 1;
2946 }
2947 }
2948 put_cpu();
2949
2950 if (update_p || update_q) {
2951 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2952 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2953 /* don't try to repair!! */
2954 update_p = update_q = 0;
2955 }
2956
2957 /* now write out any block on a failed drive,
2958 * or P or Q if they need it
2959 */
2960
2961 if (s->failed == 2) {
2962 dev = &sh->dev[r6s->failed_num[1]];
2963 s->locked++;
2964 set_bit(R5_LOCKED, &dev->flags);
2965 set_bit(R5_Wantwrite, &dev->flags);
2966 }
2967 if (s->failed >= 1) {
2968 dev = &sh->dev[r6s->failed_num[0]];
2969 s->locked++;
2970 set_bit(R5_LOCKED, &dev->flags);
2971 set_bit(R5_Wantwrite, &dev->flags);
2972 }
2973
2974 if (update_p) {
2975 dev = &sh->dev[pd_idx];
2976 s->locked++;
2977 set_bit(R5_LOCKED, &dev->flags);
2978 set_bit(R5_Wantwrite, &dev->flags);
2979 }
2980 if (update_q) {
2981 dev = &sh->dev[qd_idx];
2982 s->locked++;
2983 set_bit(R5_LOCKED, &dev->flags);
2984 set_bit(R5_Wantwrite, &dev->flags);
2985 }
2986 clear_bit(STRIPE_DEGRADED, &sh->state);
2987
2988 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002989}
2990
2991static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2992 struct r6_state *r6s)
2993{
2994 int i;
2995
2996 /* We have read all the blocks in this stripe and now we need to
2997 * copy some of them into a target stripe for expand.
2998 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002999 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003000 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3001 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003002 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003003 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003004 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003005 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003006
NeilBrown784052e2009-03-31 15:19:07 +11003007 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003008 sector_t s = raid5_compute_sector(conf, bn, 0,
3009 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11003010 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003011 if (sh2 == NULL)
3012 /* so far only the early blocks of this stripe
3013 * have been requested. When later blocks
3014 * get requested, we will try again
3015 */
3016 continue;
3017 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3018 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3019 /* must have already done this block */
3020 release_stripe(sh2);
3021 continue;
3022 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003023
3024 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003025 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003026 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003027 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003028 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003029
Dan Williamsa4456852007-07-09 11:56:43 -07003030 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3031 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3032 for (j = 0; j < conf->raid_disks; j++)
3033 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003034 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003035 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3036 break;
3037 if (j == conf->raid_disks) {
3038 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3039 set_bit(STRIPE_HANDLE, &sh2->state);
3040 }
3041 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003042
Dan Williamsa4456852007-07-09 11:56:43 -07003043 }
NeilBrowna2e08552007-09-11 15:23:36 -07003044 /* done submitting copies, wait for them to complete */
3045 if (tx) {
3046 async_tx_ack(tx);
3047 dma_wait_for_async_tx(tx);
3048 }
Dan Williamsa4456852007-07-09 11:56:43 -07003049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Dan Williams6bfe0b42008-04-30 00:52:32 -07003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052/*
3053 * handle_stripe - do things to a stripe.
3054 *
3055 * We lock the stripe and then examine the state of various bits
3056 * to see what needs to be done.
3057 * Possible results:
3058 * return some read request which now have data
3059 * return some write requests which are safely on disc
3060 * schedule a read on some buffers
3061 * schedule a write of some buffers
3062 * return confirmation of parity correctness
3063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 * buffers are taken off read_list or write_list, and bh_cache buffers
3065 * get BH_Lock set before the stripe lock is released.
3066 *
3067 */
Dan Williamsa4456852007-07-09 11:56:43 -07003068
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003069static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
3071 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07003072 int disks = sh->disks, i;
3073 struct bio *return_bi = NULL;
3074 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003076 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07003077 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Dan Williamsa4456852007-07-09 11:56:43 -07003079 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10003080 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
3081 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
3082 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
3083 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 spin_lock(&sh->lock);
3086 clear_bit(STRIPE_HANDLE, &sh->state);
3087 clear_bit(STRIPE_DELAYED, &sh->state);
3088
Dan Williamsa4456852007-07-09 11:56:43 -07003089 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3090 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3091 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08003094 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 for (i=disks; i--; ) {
3096 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003097 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Dan Williamsb5e98d62007-01-02 13:52:31 -07003100 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
3101 "written %p\n", i, dev->flags, dev->toread, dev->read,
3102 dev->towrite, dev->written);
3103
3104 /* maybe we can request a biofill operation
3105 *
3106 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10003107 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07003108 */
3109 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10003110 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07003111 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
3113 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003114 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3115 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07003116 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Dan Williamsb5e98d62007-01-02 13:52:31 -07003118 if (test_bit(R5_Wantfill, &dev->flags))
3119 s.to_fill++;
3120 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003121 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003123 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003125 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
Dan Williamsa4456852007-07-09 11:56:43 -07003127 if (dev->written)
3128 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08003129 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003130 if (blocked_rdev == NULL &&
3131 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003132 blocked_rdev = rdev;
3133 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003134 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003135 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08003136 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08003137 clear_bit(R5_ReadError, &dev->flags);
3138 clear_bit(R5_ReWrite, &dev->flags);
3139 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003140 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003141 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003142 s.failed++;
3143 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 } else
3145 set_bit(R5_Insync, &dev->flags);
3146 }
NeilBrown9910f162006-01-06 00:20:24 -08003147 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07003148
Dan Williams6bfe0b42008-04-30 00:52:32 -07003149 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003150 if (s.syncing || s.expanding || s.expanded ||
3151 s.to_write || s.written) {
3152 set_bit(STRIPE_HANDLE, &sh->state);
3153 goto unlock;
3154 }
3155 /* There is nothing for the blocked_rdev to block */
3156 rdev_dec_pending(blocked_rdev, conf->mddev);
3157 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003158 }
3159
Dan Williams83de75c2008-06-28 08:31:58 +10003160 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3161 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3162 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3163 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07003164
Dan Williams45b42332007-07-09 11:56:43 -07003165 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003167 s.locked, s.uptodate, s.to_read, s.to_write,
3168 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 /* check if the array has lost two devices and, if so, some requests might
3170 * need to be failed
3171 */
Dan Williamsa4456852007-07-09 11:56:43 -07003172 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003173 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003174 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3176 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003177 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179
3180 /* might be able to return some write requests if the parity block
3181 * is safe, or on a failed drive
3182 */
3183 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003184 if ( s.written &&
3185 ((test_bit(R5_Insync, &dev->flags) &&
3186 !test_bit(R5_LOCKED, &dev->flags) &&
3187 test_bit(R5_UPTODATE, &dev->flags)) ||
3188 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003189 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 /* Now we might consider reading some blocks, either to check/generate
3192 * parity, or to satisfy requests
3193 * or to load a block that is being partially written.
3194 */
Dan Williamsa4456852007-07-09 11:56:43 -07003195 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003196 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003197 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Dan Williamse33129d2007-01-02 13:52:30 -07003199 /* Now we check to see if any write operations have recently
3200 * completed
3201 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003202 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003203 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003204 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003205 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3206 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003207 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003208
3209 /* All the 'written' buffers and the parity block are ready to
3210 * be written back to disk
3211 */
3212 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3213 for (i = disks; i--; ) {
3214 dev = &sh->dev[i];
3215 if (test_bit(R5_LOCKED, &dev->flags) &&
3216 (i == sh->pd_idx || dev->written)) {
3217 pr_debug("Writing block %d\n", i);
3218 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003219 if (prexor)
3220 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003221 if (!test_bit(R5_Insync, &dev->flags) ||
3222 (i == sh->pd_idx && s.failed == 0))
3223 set_bit(STRIPE_INSYNC, &sh->state);
3224 }
3225 }
3226 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3227 atomic_dec(&conf->preread_active_stripes);
3228 if (atomic_read(&conf->preread_active_stripes) <
3229 IO_THRESHOLD)
3230 md_wakeup_thread(conf->mddev->thread);
3231 }
3232 }
3233
3234 /* Now to consider new write requests and what else, if anything
3235 * should be read. We do not handle new writes when:
3236 * 1/ A 'write' operation (copy+xor) is already in flight.
3237 * 2/ A 'check' operation is in flight, as it may clobber the parity
3238 * block.
3239 */
Dan Williams600aa102008-06-28 08:32:05 +10003240 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003241 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003244 * Any reads will already have been scheduled, so we just see if enough
3245 * data is available. The parity check is held off while parity
3246 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003248 if (sh->check_state ||
3249 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003250 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003251 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003252 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003253
Dan Williamsa4456852007-07-09 11:56:43 -07003254 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3256 clear_bit(STRIPE_SYNCING, &sh->state);
3257 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003258
3259 /* If the failed drive is just a ReadError, then we might need to progress
3260 * the repair/check process
3261 */
Dan Williamsa4456852007-07-09 11:56:43 -07003262 if (s.failed == 1 && !conf->mddev->ro &&
3263 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3264 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3265 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003266 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003267 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003268 if (!test_bit(R5_ReWrite, &dev->flags)) {
3269 set_bit(R5_Wantwrite, &dev->flags);
3270 set_bit(R5_ReWrite, &dev->flags);
3271 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003272 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003273 } else {
3274 /* let's read it back */
3275 set_bit(R5_Wantread, &dev->flags);
3276 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003277 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003278 }
3279 }
3280
Dan Williams600aa102008-06-28 08:32:05 +10003281 /* Finish reconstruct operations initiated by the expansion process */
3282 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003283 struct stripe_head *sh2
3284 = get_active_stripe(conf, sh->sector, 1, 1);
3285 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3286 /* sh cannot be written until sh2 has been read.
3287 * so arrange for sh to be delayed a little
3288 */
3289 set_bit(STRIPE_DELAYED, &sh->state);
3290 set_bit(STRIPE_HANDLE, &sh->state);
3291 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3292 &sh2->state))
3293 atomic_inc(&conf->preread_active_stripes);
3294 release_stripe(sh2);
3295 goto unlock;
3296 }
3297 if (sh2)
3298 release_stripe(sh2);
3299
Dan Williams600aa102008-06-28 08:32:05 +10003300 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003301 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003302 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003303 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003304 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003305 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003306 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003307 }
3308
3309 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003310 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003311 /* Need to write out all blocks after computing parity */
3312 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003313 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003314 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003315 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003316 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003317 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003318 wake_up(&conf->wait_for_overlap);
3319 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3320 }
3321
Dan Williams0f94e872008-01-08 15:32:53 -08003322 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003323 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003324 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003325
Dan Williams6bfe0b42008-04-30 00:52:32 -07003326 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 spin_unlock(&sh->lock);
3328
Dan Williams6bfe0b42008-04-30 00:52:32 -07003329 /* wait for this device to become unblocked */
3330 if (unlikely(blocked_rdev))
3331 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3332
Dan Williams600aa102008-06-28 08:32:05 +10003333 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003334 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003335
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003336 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Dan Williamsa4456852007-07-09 11:56:43 -07003338 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003339
3340 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Dan Williams36d1c642009-07-14 11:48:22 -07003343static bool handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003344{
NeilBrownbff61972009-03-31 14:33:13 +11003345 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003346 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003347 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003348 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003349 struct stripe_head_state s;
3350 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003351 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003352 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003353
Dan Williams45b42332007-07-09 11:56:43 -07003354 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07003355 "pd_idx=%d, qd_idx=%d\n",
3356 (unsigned long long)sh->sector, sh->state,
NeilBrown34e04e82009-03-31 15:10:16 +11003357 atomic_read(&sh->count), pd_idx, qd_idx);
Dan Williamsa4456852007-07-09 11:56:43 -07003358 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003359
3360 spin_lock(&sh->lock);
3361 clear_bit(STRIPE_HANDLE, &sh->state);
3362 clear_bit(STRIPE_DELAYED, &sh->state);
3363
Dan Williamsa4456852007-07-09 11:56:43 -07003364 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3365 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3366 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003367 /* Now to look around and see what can be done */
3368
3369 rcu_read_lock();
3370 for (i=disks; i--; ) {
3371 mdk_rdev_t *rdev;
3372 dev = &sh->dev[i];
3373 clear_bit(R5_Insync, &dev->flags);
3374
Dan Williams45b42332007-07-09 11:56:43 -07003375 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003376 i, dev->flags, dev->toread, dev->towrite, dev->written);
3377 /* maybe we can reply to a read */
3378 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3379 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003380 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003381 spin_lock_irq(&conf->device_lock);
3382 rbi = dev->toread;
3383 dev->toread = NULL;
3384 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3385 wake_up(&conf->wait_for_overlap);
3386 spin_unlock_irq(&conf->device_lock);
3387 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3388 copy_data(0, rbi, dev->page, dev->sector);
3389 rbi2 = r5_next_bio(rbi, dev->sector);
3390 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003391 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003392 rbi->bi_next = return_bi;
3393 return_bi = rbi;
3394 }
3395 spin_unlock_irq(&conf->device_lock);
3396 rbi = rbi2;
3397 }
3398 }
3399
3400 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003401 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3402 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003403
3404
Dan Williamsa4456852007-07-09 11:56:43 -07003405 if (dev->toread)
3406 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003407 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003408 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003409 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003410 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003411 }
Dan Williamsa4456852007-07-09 11:56:43 -07003412 if (dev->written)
3413 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003414 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003415 if (blocked_rdev == NULL &&
3416 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003417 blocked_rdev = rdev;
3418 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003419 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003420 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3421 /* The ReadError flag will just be confusing now */
3422 clear_bit(R5_ReadError, &dev->flags);
3423 clear_bit(R5_ReWrite, &dev->flags);
3424 }
3425 if (!rdev || !test_bit(In_sync, &rdev->flags)
3426 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003427 if (s.failed < 2)
3428 r6s.failed_num[s.failed] = i;
3429 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003430 } else
3431 set_bit(R5_Insync, &dev->flags);
3432 }
3433 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003434
3435 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003436 if (s.syncing || s.expanding || s.expanded ||
3437 s.to_write || s.written) {
3438 set_bit(STRIPE_HANDLE, &sh->state);
3439 goto unlock;
3440 }
3441 /* There is nothing for the blocked_rdev to block */
3442 rdev_dec_pending(blocked_rdev, conf->mddev);
3443 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003444 }
NeilBrownac4090d2008-08-05 15:54:13 +10003445
Dan Williams45b42332007-07-09 11:56:43 -07003446 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003447 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003448 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3449 r6s.failed_num[0], r6s.failed_num[1]);
3450 /* check if the array has lost >2 devices and, if so, some requests
3451 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003452 */
Dan Williamsa4456852007-07-09 11:56:43 -07003453 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003454 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003455 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003456 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3457 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003458 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003459 }
3460
3461 /*
3462 * might be able to return some write requests if the parity blocks
3463 * are safe, or on a failed drive
3464 */
3465 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003466 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3467 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003468 qdev = &sh->dev[qd_idx];
3469 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3470 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003471
Dan Williamsa4456852007-07-09 11:56:43 -07003472 if ( s.written &&
3473 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003474 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003475 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3476 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003477 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003478 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003479 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003480
3481 /* Now we might consider reading some blocks, either to check/generate
3482 * parity, or to satisfy requests
3483 * or to load a block that is being partially written.
3484 */
Dan Williamsa4456852007-07-09 11:56:43 -07003485 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3486 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003487 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003488
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003489 /* Now to consider new write requests and what else, if anything
3490 * should be read. We do not handle new writes when:
3491 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3492 * 2/ A 'check' operation is in flight, as it may clobber the parity
3493 * block.
3494 */
3495 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003496 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003497
3498 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003499 * Any reads will already have been scheduled, so we just see if enough
3500 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003501 */
Dan Williamsa4456852007-07-09 11:56:43 -07003502 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
Dan Williams36d1c642009-07-14 11:48:22 -07003503 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003504
Dan Williamsa4456852007-07-09 11:56:43 -07003505 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003506 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3507 clear_bit(STRIPE_SYNCING, &sh->state);
3508 }
3509
3510 /* If the failed drives are just a ReadError, then we might need
3511 * to progress the repair/check process
3512 */
Dan Williamsa4456852007-07-09 11:56:43 -07003513 if (s.failed <= 2 && !conf->mddev->ro)
3514 for (i = 0; i < s.failed; i++) {
3515 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003516 if (test_bit(R5_ReadError, &dev->flags)
3517 && !test_bit(R5_LOCKED, &dev->flags)
3518 && test_bit(R5_UPTODATE, &dev->flags)
3519 ) {
3520 if (!test_bit(R5_ReWrite, &dev->flags)) {
3521 set_bit(R5_Wantwrite, &dev->flags);
3522 set_bit(R5_ReWrite, &dev->flags);
3523 set_bit(R5_LOCKED, &dev->flags);
3524 } else {
3525 /* let's read it back */
3526 set_bit(R5_Wantread, &dev->flags);
3527 set_bit(R5_LOCKED, &dev->flags);
3528 }
3529 }
3530 }
NeilBrownf4168852007-02-28 20:11:53 -08003531
Dan Williamsa4456852007-07-09 11:56:43 -07003532 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownab69ae12009-03-31 15:26:47 +11003533 struct stripe_head *sh2
3534 = get_active_stripe(conf, sh->sector, 1, 1);
3535 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3536 /* sh cannot be written until sh2 has been read.
3537 * so arrange for sh to be delayed a little
3538 */
3539 set_bit(STRIPE_DELAYED, &sh->state);
3540 set_bit(STRIPE_HANDLE, &sh->state);
3541 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3542 &sh2->state))
3543 atomic_inc(&conf->preread_active_stripes);
3544 release_stripe(sh2);
3545 goto unlock;
3546 }
3547 if (sh2)
3548 release_stripe(sh2);
3549
NeilBrownf4168852007-02-28 20:11:53 -08003550 /* Need to write out all blocks after computing P&Q */
3551 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003552 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003553 compute_parity6(sh, RECONSTRUCT_WRITE);
3554 for (i = conf->raid_disks ; i-- ; ) {
3555 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003556 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003557 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3558 }
3559 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003560 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003561 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3562 atomic_dec(&conf->reshape_stripes);
3563 wake_up(&conf->wait_for_overlap);
3564 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3565 }
3566
Dan Williams0f94e872008-01-08 15:32:53 -08003567 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003568 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003569 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003570
Dan Williams6bfe0b42008-04-30 00:52:32 -07003571 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003572 spin_unlock(&sh->lock);
3573
Dan Williams6bfe0b42008-04-30 00:52:32 -07003574 /* wait for this device to become unblocked */
3575 if (unlikely(blocked_rdev))
3576 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3577
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003578 ops_run_io(sh, &s);
3579
Dan Williamsa4456852007-07-09 11:56:43 -07003580 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003581
3582 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003583}
3584
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003585/* returns true if the stripe was handled */
Dan Williams36d1c642009-07-14 11:48:22 -07003586static bool handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003587{
3588 if (sh->raid_conf->level == 6)
Dan Williams36d1c642009-07-14 11:48:22 -07003589 return handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003590 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003591 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003592}
3593
Arjan van de Ven858119e2006-01-14 13:20:43 -08003594static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3597 while (!list_empty(&conf->delayed_list)) {
3598 struct list_head *l = conf->delayed_list.next;
3599 struct stripe_head *sh;
3600 sh = list_entry(l, struct stripe_head, lru);
3601 list_del_init(l);
3602 clear_bit(STRIPE_DELAYED, &sh->state);
3603 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3604 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003605 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
NeilBrown6ed30032008-02-06 01:40:00 -08003607 } else
3608 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
Arjan van de Ven858119e2006-01-14 13:20:43 -08003611static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003612{
3613 /* device_lock is held */
3614 struct list_head head;
3615 list_add(&head, &conf->bitmap_list);
3616 list_del_init(&conf->bitmap_list);
3617 while (!list_empty(&head)) {
3618 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3619 list_del_init(&sh->lru);
3620 atomic_inc(&sh->count);
3621 __release_stripe(conf, sh);
3622 }
3623}
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625static void unplug_slaves(mddev_t *mddev)
3626{
3627 raid5_conf_t *conf = mddev_to_conf(mddev);
3628 int i;
3629
3630 rcu_read_lock();
3631 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003632 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003633 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003634 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 atomic_inc(&rdev->nr_pending);
3637 rcu_read_unlock();
3638
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003639 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 rdev_dec_pending(rdev, mddev);
3642 rcu_read_lock();
3643 }
3644 }
3645 rcu_read_unlock();
3646}
3647
Jens Axboe165125e2007-07-24 09:28:11 +02003648static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 mddev_t *mddev = q->queuedata;
3651 raid5_conf_t *conf = mddev_to_conf(mddev);
3652 unsigned long flags;
3653
3654 spin_lock_irqsave(&conf->device_lock, flags);
3655
NeilBrown72626682005-09-09 16:23:54 -07003656 if (blk_remove_plug(q)) {
3657 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 md_wakeup_thread(mddev->thread);
3661
3662 spin_unlock_irqrestore(&conf->device_lock, flags);
3663
3664 unplug_slaves(mddev);
3665}
3666
NeilBrownf022b2f2006-10-03 01:15:56 -07003667static int raid5_congested(void *data, int bits)
3668{
3669 mddev_t *mddev = data;
3670 raid5_conf_t *conf = mddev_to_conf(mddev);
3671
3672 /* No difference between reads and writes. Just check
3673 * how busy the stripe_cache is
3674 */
3675 if (conf->inactive_blocked)
3676 return 1;
3677 if (conf->quiesce)
3678 return 1;
3679 if (list_empty_careful(&conf->inactive_list))
3680 return 1;
3681
3682 return 0;
3683}
3684
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003685/* We want read requests to align with chunks where possible,
3686 * but write requests don't need to.
3687 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003688static int raid5_mergeable_bvec(struct request_queue *q,
3689 struct bvec_merge_data *bvm,
3690 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003691{
3692 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003693 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003694 int max;
3695 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003696 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003697
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003698 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003699 return biovec->bv_len; /* always allow writes to be mergeable */
3700
NeilBrown784052e2009-03-31 15:19:07 +11003701 if (mddev->new_chunk < mddev->chunk_size)
3702 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003703 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3704 if (max < 0) max = 0;
3705 if (max <= biovec->bv_len && bio_sectors == 0)
3706 return biovec->bv_len;
3707 else
3708 return max;
3709}
3710
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003711
3712static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3713{
3714 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3715 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3716 unsigned int bio_sectors = bio->bi_size >> 9;
3717
NeilBrown784052e2009-03-31 15:19:07 +11003718 if (mddev->new_chunk < mddev->chunk_size)
3719 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003720 return chunk_sectors >=
3721 ((sector & (chunk_sectors - 1)) + bio_sectors);
3722}
3723
3724/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003725 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3726 * later sampled by raid5d.
3727 */
3728static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3729{
3730 unsigned long flags;
3731
3732 spin_lock_irqsave(&conf->device_lock, flags);
3733
3734 bi->bi_next = conf->retry_read_aligned_list;
3735 conf->retry_read_aligned_list = bi;
3736
3737 spin_unlock_irqrestore(&conf->device_lock, flags);
3738 md_wakeup_thread(conf->mddev->thread);
3739}
3740
3741
3742static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3743{
3744 struct bio *bi;
3745
3746 bi = conf->retry_read_aligned;
3747 if (bi) {
3748 conf->retry_read_aligned = NULL;
3749 return bi;
3750 }
3751 bi = conf->retry_read_aligned_list;
3752 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003753 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003754 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003755 /*
3756 * this sets the active strip count to 1 and the processed
3757 * strip count to zero (upper 8 bits)
3758 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003759 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003760 }
3761
3762 return bi;
3763}
3764
3765
3766/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003767 * The "raid5_align_endio" should check if the read succeeded and if it
3768 * did, call bio_endio on the original bio (having bio_put the new bio
3769 * first).
3770 * If the read failed..
3771 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003772static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003773{
3774 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003775 mddev_t *mddev;
3776 raid5_conf_t *conf;
3777 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3778 mdk_rdev_t *rdev;
3779
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003780 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003781
3782 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3783 conf = mddev_to_conf(mddev);
3784 rdev = (void*)raid_bi->bi_next;
3785 raid_bi->bi_next = NULL;
3786
3787 rdev_dec_pending(rdev, conf->mddev);
3788
3789 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003790 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003791 if (atomic_dec_and_test(&conf->active_aligned_reads))
3792 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003793 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003794 }
3795
3796
Dan Williams45b42332007-07-09 11:56:43 -07003797 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003798
3799 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003800}
3801
Neil Brown387bb172007-02-08 14:20:29 -08003802static int bio_fits_rdev(struct bio *bi)
3803{
Jens Axboe165125e2007-07-24 09:28:11 +02003804 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003805
3806 if ((bi->bi_size>>9) > q->max_sectors)
3807 return 0;
3808 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003809 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003810 return 0;
3811
3812 if (q->merge_bvec_fn)
3813 /* it's too hard to apply the merge_bvec_fn at this stage,
3814 * just just give up
3815 */
3816 return 0;
3817
3818 return 1;
3819}
3820
3821
Jens Axboe165125e2007-07-24 09:28:11 +02003822static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003823{
3824 mddev_t *mddev = q->queuedata;
3825 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003826 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003827 struct bio* align_bi;
3828 mdk_rdev_t *rdev;
3829
3830 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003831 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003832 return 0;
3833 }
3834 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003835 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003836 */
3837 align_bi = bio_clone(raid_bio, GFP_NOIO);
3838 if (!align_bi)
3839 return 0;
3840 /*
3841 * set bi_end_io to a new function, and set bi_private to the
3842 * original bio.
3843 */
3844 align_bi->bi_end_io = raid5_align_endio;
3845 align_bi->bi_private = raid_bio;
3846 /*
3847 * compute position
3848 */
NeilBrown112bf892009-03-31 14:39:38 +11003849 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3850 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003851 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003852
3853 rcu_read_lock();
3854 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3855 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003856 atomic_inc(&rdev->nr_pending);
3857 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003858 raid_bio->bi_next = (void*)rdev;
3859 align_bi->bi_bdev = rdev->bdev;
3860 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3861 align_bi->bi_sector += rdev->data_offset;
3862
Neil Brown387bb172007-02-08 14:20:29 -08003863 if (!bio_fits_rdev(align_bi)) {
3864 /* too big in some way */
3865 bio_put(align_bi);
3866 rdev_dec_pending(rdev, mddev);
3867 return 0;
3868 }
3869
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003870 spin_lock_irq(&conf->device_lock);
3871 wait_event_lock_irq(conf->wait_for_stripe,
3872 conf->quiesce == 0,
3873 conf->device_lock, /* nothing */);
3874 atomic_inc(&conf->active_aligned_reads);
3875 spin_unlock_irq(&conf->device_lock);
3876
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003877 generic_make_request(align_bi);
3878 return 1;
3879 } else {
3880 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003881 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003882 return 0;
3883 }
3884}
3885
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003886/* __get_priority_stripe - get the next stripe to process
3887 *
3888 * Full stripe writes are allowed to pass preread active stripes up until
3889 * the bypass_threshold is exceeded. In general the bypass_count
3890 * increments when the handle_list is handled before the hold_list; however, it
3891 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3892 * stripe with in flight i/o. The bypass_count will be reset when the
3893 * head of the hold_list has changed, i.e. the head was promoted to the
3894 * handle_list.
3895 */
3896static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3897{
3898 struct stripe_head *sh;
3899
3900 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3901 __func__,
3902 list_empty(&conf->handle_list) ? "empty" : "busy",
3903 list_empty(&conf->hold_list) ? "empty" : "busy",
3904 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3905
3906 if (!list_empty(&conf->handle_list)) {
3907 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3908
3909 if (list_empty(&conf->hold_list))
3910 conf->bypass_count = 0;
3911 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3912 if (conf->hold_list.next == conf->last_hold)
3913 conf->bypass_count++;
3914 else {
3915 conf->last_hold = conf->hold_list.next;
3916 conf->bypass_count -= conf->bypass_threshold;
3917 if (conf->bypass_count < 0)
3918 conf->bypass_count = 0;
3919 }
3920 }
3921 } else if (!list_empty(&conf->hold_list) &&
3922 ((conf->bypass_threshold &&
3923 conf->bypass_count > conf->bypass_threshold) ||
3924 atomic_read(&conf->pending_full_writes) == 0)) {
3925 sh = list_entry(conf->hold_list.next,
3926 typeof(*sh), lru);
3927 conf->bypass_count -= conf->bypass_threshold;
3928 if (conf->bypass_count < 0)
3929 conf->bypass_count = 0;
3930 } else
3931 return NULL;
3932
3933 list_del_init(&sh->lru);
3934 atomic_inc(&sh->count);
3935 BUG_ON(atomic_read(&sh->count) != 1);
3936 return sh;
3937}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003938
Jens Axboe165125e2007-07-24 09:28:11 +02003939static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
3941 mddev_t *mddev = q->queuedata;
3942 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003943 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 sector_t new_sector;
3945 sector_t logical_sector, last_sector;
3946 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003947 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003948 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
NeilBrowne5dcdd82005-09-09 16:23:41 -07003950 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003951 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003952 return 0;
3953 }
3954
NeilBrown3d310eb2005-06-21 17:17:26 -07003955 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003956
Tejun Heo074a7ac2008-08-25 19:56:14 +09003957 cpu = part_stat_lock();
3958 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3959 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3960 bio_sectors(bi));
3961 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
NeilBrown802ba062006-12-13 00:34:13 -08003963 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003964 mddev->reshape_position == MaxSector &&
3965 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003966 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3969 last_sector = bi->bi_sector + (bi->bi_size>>9);
3970 bi->bi_next = NULL;
3971 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3974 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003975 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003976 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003977
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003978 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003979 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003980 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003981 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003982 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003983 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003984 * 64bit on a 32bit platform, and so it might be
3985 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003986 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003987 * the lock is dropped, so once we get a reference
3988 * to the stripe that we think it is, we will have
3989 * to check again.
3990 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003991 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003992 if (mddev->delta_disks < 0
3993 ? logical_sector < conf->reshape_progress
3994 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003995 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003996 previous = 1;
3997 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003998 if (mddev->delta_disks < 0
3999 ? logical_sector < conf->reshape_safe
4000 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004001 spin_unlock_irq(&conf->device_lock);
4002 schedule();
4003 goto retry;
4004 }
4005 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004006 spin_unlock_irq(&conf->device_lock);
4007 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004008 data_disks = disks - conf->max_degraded;
4009
NeilBrown112bf892009-03-31 14:39:38 +11004010 new_sector = raid5_compute_sector(conf, logical_sector,
4011 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004012 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07004013 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 (unsigned long long)new_sector,
4015 (unsigned long long)logical_sector);
4016
NeilBrownb5663ba2009-03-31 14:39:38 +11004017 sh = get_active_stripe(conf, new_sector, previous,
4018 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004020 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004021 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004022 * stripe, so we must do the range check again.
4023 * Expansion could still move past after this
4024 * test, but as we are holding a reference to
4025 * 'sh', we know that if that happens,
4026 * STRIPE_EXPANDING will get set and the expansion
4027 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004028 */
4029 int must_retry = 0;
4030 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004031 if (mddev->delta_disks < 0
4032 ? logical_sector >= conf->reshape_progress
4033 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004034 /* mismatch, need to try again */
4035 must_retry = 1;
4036 spin_unlock_irq(&conf->device_lock);
4037 if (must_retry) {
4038 release_stripe(sh);
4039 goto retry;
4040 }
4041 }
NeilBrowne464eaf2006-03-27 01:18:14 -08004042 /* FIXME what if we get a false positive because these
4043 * are being updated.
4044 */
4045 if (logical_sector >= mddev->suspend_lo &&
4046 logical_sector < mddev->suspend_hi) {
4047 release_stripe(sh);
4048 schedule();
4049 goto retry;
4050 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004051
4052 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
4053 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
4054 /* Stripe is busy expanding or
4055 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 * and wait a while
4057 */
4058 raid5_unplug_device(mddev->queue);
4059 release_stripe(sh);
4060 schedule();
4061 goto retry;
4062 }
4063 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004064 set_bit(STRIPE_HANDLE, &sh->state);
4065 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 } else {
4068 /* cannot get stripe for read-ahead, just give-up */
4069 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4070 finish_wait(&conf->wait_for_overlap, &w);
4071 break;
4072 }
4073
4074 }
4075 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004076 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004077 spin_unlock_irq(&conf->device_lock);
4078 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
NeilBrown16a53ec2006-06-26 00:27:38 -07004080 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004082
Neil Brown0e13fe232008-06-28 08:31:20 +10004083 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 return 0;
4086}
4087
Dan Williamsb522adc2009-03-31 15:00:31 +11004088static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
4089
NeilBrown52c03292006-06-26 00:27:43 -07004090static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
NeilBrown52c03292006-06-26 00:27:43 -07004092 /* reshaping is quite different to recovery/resync so it is
4093 * handled quite separately ... here.
4094 *
4095 * On each call to sync_request, we gather one chunk worth of
4096 * destination stripes and flag them as expanding.
4097 * Then we find all the source stripes and request reads.
4098 * As the reads complete, handle_stripe will copy the data
4099 * into the destination stripe and release that stripe.
4100 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4102 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004103 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004104 int raid_disks = conf->previous_raid_disks;
4105 int data_disks = raid_disks - conf->max_degraded;
4106 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004107 int i;
4108 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004109 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004110 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004111 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004112 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004113
NeilBrownfef9c612009-03-31 15:16:46 +11004114 if (sector_nr == 0) {
4115 /* If restarting in the middle, skip the initial sectors */
4116 if (mddev->delta_disks < 0 &&
4117 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4118 sector_nr = raid5_size(mddev, 0, 0)
4119 - conf->reshape_progress;
4120 } else if (mddev->delta_disks > 0 &&
4121 conf->reshape_progress > 0)
4122 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004123 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004124 if (sector_nr) {
4125 *skipped = 1;
4126 return sector_nr;
4127 }
NeilBrown52c03292006-06-26 00:27:43 -07004128 }
4129
NeilBrown7a661382009-03-31 15:21:40 +11004130 /* We need to process a full chunk at a time.
4131 * If old and new chunk sizes differ, we need to process the
4132 * largest of these
4133 */
4134 if (mddev->new_chunk > mddev->chunk_size)
4135 reshape_sectors = mddev->new_chunk / 512;
4136 else
4137 reshape_sectors = mddev->chunk_size / 512;
4138
NeilBrown52c03292006-06-26 00:27:43 -07004139 /* we update the metadata when there is more than 3Meg
4140 * in the block range (that is rather arbitrary, should
4141 * probably be time based) or when the data about to be
4142 * copied would over-write the source of the data at
4143 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004144 * i.e. one new_stripe along from reshape_progress new_maps
4145 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004146 */
NeilBrownfef9c612009-03-31 15:16:46 +11004147 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004148 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004149 readpos = conf->reshape_progress;
4150 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004151 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004152 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004153 if (mddev->delta_disks < 0) {
NeilBrown7a661382009-03-31 15:21:40 +11004154 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004155 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004156 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004157 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004158 writepos += reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004159 readpos -= reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004160 safepos -= reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004161 }
NeilBrown52c03292006-06-26 00:27:43 -07004162
NeilBrownc8f517c2009-03-31 15:28:40 +11004163 /* 'writepos' is the most advanced device address we might write.
4164 * 'readpos' is the least advanced device address we might read.
4165 * 'safepos' is the least address recorded in the metadata as having
4166 * been reshaped.
4167 * If 'readpos' is behind 'writepos', then there is no way that we can
4168 * ensure safety in the face of a crash - that must be done by userspace
4169 * making a backup of the data. So in that case there is no particular
4170 * rush to update metadata.
4171 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4172 * update the metadata to advance 'safepos' to match 'readpos' so that
4173 * we can be safe in the event of a crash.
4174 * So we insist on updating metadata if safepos is behind writepos and
4175 * readpos is beyond writepos.
4176 * In any case, update the metadata every 10 seconds.
4177 * Maybe that number should be configurable, but I'm not sure it is
4178 * worth it.... maybe it could be a multiple of safemode_delay???
4179 */
NeilBrownfef9c612009-03-31 15:16:46 +11004180 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004181 ? (safepos > writepos && readpos < writepos)
4182 : (safepos < writepos && readpos > writepos)) ||
4183 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004184 /* Cannot proceed until we've updated the superblock... */
4185 wait_event(conf->wait_for_overlap,
4186 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004187 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11004188 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004189 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004190 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004191 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004192 kthread_should_stop());
4193 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004194 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004195 spin_unlock_irq(&conf->device_lock);
4196 wake_up(&conf->wait_for_overlap);
4197 }
4198
NeilBrownec32a2b2009-03-31 15:17:38 +11004199 if (mddev->delta_disks < 0) {
4200 BUG_ON(conf->reshape_progress == 0);
4201 stripe_addr = writepos;
4202 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004203 ~((sector_t)reshape_sectors - 1))
4204 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004205 != sector_nr);
4206 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004207 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004208 stripe_addr = sector_nr;
4209 }
NeilBrownab69ae12009-03-31 15:26:47 +11004210 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004211 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004212 int j;
4213 int skipped = 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11004214 sh = get_active_stripe(conf, stripe_addr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07004215 set_bit(STRIPE_EXPANDING, &sh->state);
4216 atomic_inc(&conf->reshape_stripes);
4217 /* If any of this stripe is beyond the end of the old
4218 * array, then we need to zero those blocks
4219 */
4220 for (j=sh->disks; j--;) {
4221 sector_t s;
4222 if (j == sh->pd_idx)
4223 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004224 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004225 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004226 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004227 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004228 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07004229 skipped = 1;
4230 continue;
4231 }
4232 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4233 set_bit(R5_Expanded, &sh->dev[j].flags);
4234 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4235 }
4236 if (!skipped) {
4237 set_bit(STRIPE_EXPAND_READY, &sh->state);
4238 set_bit(STRIPE_HANDLE, &sh->state);
4239 }
NeilBrownab69ae12009-03-31 15:26:47 +11004240 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004241 }
4242 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004243 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004244 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004245 else
NeilBrown7a661382009-03-31 15:21:40 +11004246 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004247 spin_unlock_irq(&conf->device_lock);
4248 /* Ok, those stripe are ready. We can start scheduling
4249 * reads on the source stripes.
4250 * The source stripes are determined by mapping the first and last
4251 * block on the destination stripes.
4252 */
NeilBrown52c03292006-06-26 00:27:43 -07004253 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004254 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004255 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004256 last_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004257 raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512)
NeilBrown112bf892009-03-31 14:39:38 +11004258 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004259 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004260 if (last_sector >= mddev->dev_sectors)
4261 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004262 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004263 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07004264 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4265 set_bit(STRIPE_HANDLE, &sh->state);
4266 release_stripe(sh);
4267 first_sector += STRIPE_SECTORS;
4268 }
NeilBrownab69ae12009-03-31 15:26:47 +11004269 /* Now that the sources are clearly marked, we can release
4270 * the destination stripes
4271 */
4272 while (!list_empty(&stripes)) {
4273 sh = list_entry(stripes.next, struct stripe_head, lru);
4274 list_del_init(&sh->lru);
4275 release_stripe(sh);
4276 }
NeilBrownc6207272008-02-06 01:39:52 -08004277 /* If this takes us to the resync_max point where we have to pause,
4278 * then we need to write out the superblock.
4279 */
NeilBrown7a661382009-03-31 15:21:40 +11004280 sector_nr += reshape_sectors;
NeilBrownc6207272008-02-06 01:39:52 -08004281 if (sector_nr >= mddev->resync_max) {
4282 /* Cannot proceed until we've updated the superblock... */
4283 wait_event(conf->wait_for_overlap,
4284 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004285 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11004286 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004287 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4288 md_wakeup_thread(mddev->thread);
4289 wait_event(mddev->sb_wait,
4290 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4291 || kthread_should_stop());
4292 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004293 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004294 spin_unlock_irq(&conf->device_lock);
4295 wake_up(&conf->wait_for_overlap);
4296 }
NeilBrown7a661382009-03-31 15:21:40 +11004297 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004298}
4299
4300/* FIXME go_faster isn't used */
4301static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4302{
4303 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4304 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004305 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004306 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004307 int still_degraded = 0;
4308 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
NeilBrown72626682005-09-09 16:23:54 -07004310 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 /* just being told to finish up .. nothing much to do */
4312 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004313
NeilBrown29269552006-03-27 01:18:10 -08004314 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4315 end_reshape(conf);
4316 return 0;
4317 }
NeilBrown72626682005-09-09 16:23:54 -07004318
4319 if (mddev->curr_resync < max_sector) /* aborted */
4320 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4321 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004322 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004323 conf->fullsync = 0;
4324 bitmap_close_sync(mddev->bitmap);
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return 0;
4327 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004328
NeilBrown52c03292006-06-26 00:27:43 -07004329 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4330 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004331
NeilBrownc6207272008-02-06 01:39:52 -08004332 /* No need to check resync_max as we never do more than one
4333 * stripe, and as resync_max will always be on a chunk boundary,
4334 * if the check in md_do_sync didn't fire, there is no chance
4335 * of overstepping resync_max here
4336 */
4337
NeilBrown16a53ec2006-06-26 00:27:38 -07004338 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 * to resync, then assert that we are finished, because there is
4340 * nothing we can do.
4341 */
NeilBrown3285edf2006-06-26 00:27:55 -07004342 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004343 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004344 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004345 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return rv;
4347 }
NeilBrown72626682005-09-09 16:23:54 -07004348 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004349 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004350 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4351 /* we can skip this block, and probably more */
4352 sync_blocks /= STRIPE_SECTORS;
4353 *skipped = 1;
4354 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
NeilBrownb47490c2008-02-06 01:39:50 -08004357
4358 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4359
NeilBrownb5663ba2009-03-31 14:39:38 +11004360 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004362 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004364 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004366 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004368 /* Need to check if array will still be degraded after recovery/resync
4369 * We don't need to check the 'failed' flag as when that gets set,
4370 * recovery aborts.
4371 */
4372 for (i=0; i<mddev->raid_disks; i++)
4373 if (conf->disks[i].rdev == NULL)
4374 still_degraded = 1;
4375
4376 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4377
4378 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 set_bit(STRIPE_SYNCING, &sh->state);
4380 clear_bit(STRIPE_INSYNC, &sh->state);
4381 spin_unlock(&sh->lock);
4382
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004383 /* wait for any blocked device to be handled */
Dan Williams36d1c642009-07-14 11:48:22 -07004384 while (unlikely(!handle_stripe(sh)))
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004385 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 release_stripe(sh);
4387
4388 return STRIPE_SECTORS;
4389}
4390
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004391static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4392{
4393 /* We may not be able to submit a whole bio at once as there
4394 * may not be enough stripe_heads available.
4395 * We cannot pre-allocate enough stripe_heads as we may need
4396 * more than exist in the cache (if we allow ever large chunks).
4397 * So we do one stripe head at a time and record in
4398 * ->bi_hw_segments how many have been done.
4399 *
4400 * We *know* that this entire raid_bio is in one chunk, so
4401 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4402 */
4403 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004404 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004405 sector_t sector, logical_sector, last_sector;
4406 int scnt = 0;
4407 int remaining;
4408 int handled = 0;
4409
4410 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004411 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004412 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004413 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4414
4415 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004416 logical_sector += STRIPE_SECTORS,
4417 sector += STRIPE_SECTORS,
4418 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004419
Jens Axboe960e7392008-08-15 10:41:18 +02004420 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004421 /* already done this stripe */
4422 continue;
4423
NeilBrownb5663ba2009-03-31 14:39:38 +11004424 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004425
4426 if (!sh) {
4427 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004428 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004429 conf->retry_read_aligned = raid_bio;
4430 return handled;
4431 }
4432
4433 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004434 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4435 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004436 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004437 conf->retry_read_aligned = raid_bio;
4438 return handled;
4439 }
4440
Dan Williams36d1c642009-07-14 11:48:22 -07004441 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004442 release_stripe(sh);
4443 handled++;
4444 }
4445 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004446 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004447 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004448 if (remaining == 0)
4449 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004450 if (atomic_dec_and_test(&conf->active_aligned_reads))
4451 wake_up(&conf->wait_for_stripe);
4452 return handled;
4453}
4454
4455
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457/*
4458 * This is our raid5 kernel thread.
4459 *
4460 * We scan the hash table for stripes which can be handled now.
4461 * During the scan, completed stripes are saved for us by the interrupt
4462 * handler, so that they will not have to wait for our next wakeup.
4463 */
NeilBrown6ed30032008-02-06 01:40:00 -08004464static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
4466 struct stripe_head *sh;
4467 raid5_conf_t *conf = mddev_to_conf(mddev);
4468 int handled;
4469
Dan Williams45b42332007-07-09 11:56:43 -07004470 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
4472 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 handled = 0;
4475 spin_lock_irq(&conf->device_lock);
4476 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004477 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
NeilBrownae3c20c2006-07-10 04:44:17 -07004479 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004480 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004481 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004482 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004483 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004484 conf->seq_write = seq;
4485 activate_bit_delay(conf);
4486 }
4487
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004488 while ((bio = remove_bio_from_retry(conf))) {
4489 int ok;
4490 spin_unlock_irq(&conf->device_lock);
4491 ok = retry_aligned_read(conf, bio);
4492 spin_lock_irq(&conf->device_lock);
4493 if (!ok)
4494 break;
4495 handled++;
4496 }
4497
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004498 sh = __get_priority_stripe(conf);
4499
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004500 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 spin_unlock_irq(&conf->device_lock);
4503
4504 handled++;
Dan Williams36d1c642009-07-14 11:48:22 -07004505 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 release_stripe(sh);
4507
4508 spin_lock_irq(&conf->device_lock);
4509 }
Dan Williams45b42332007-07-09 11:56:43 -07004510 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 spin_unlock_irq(&conf->device_lock);
4513
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004514 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 unplug_slaves(mddev);
4516
Dan Williams45b42332007-07-09 11:56:43 -07004517 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518}
4519
NeilBrown3f294f42005-11-08 21:39:25 -08004520static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004521raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004522{
NeilBrown007583c2005-11-08 21:39:30 -08004523 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004524 if (conf)
4525 return sprintf(page, "%d\n", conf->max_nr_stripes);
4526 else
4527 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004528}
4529
4530static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004531raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004532{
NeilBrown007583c2005-11-08 21:39:30 -08004533 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004534 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004535 int err;
4536
NeilBrown3f294f42005-11-08 21:39:25 -08004537 if (len >= PAGE_SIZE)
4538 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004539 if (!conf)
4540 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004541
Dan Williams4ef197d82008-04-28 02:15:54 -07004542 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004543 return -EINVAL;
4544 if (new <= 16 || new > 32768)
4545 return -EINVAL;
4546 while (new < conf->max_nr_stripes) {
4547 if (drop_one_stripe(conf))
4548 conf->max_nr_stripes--;
4549 else
4550 break;
4551 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004552 err = md_allow_write(mddev);
4553 if (err)
4554 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004555 while (new > conf->max_nr_stripes) {
4556 if (grow_one_stripe(conf))
4557 conf->max_nr_stripes++;
4558 else break;
4559 }
4560 return len;
4561}
NeilBrown007583c2005-11-08 21:39:30 -08004562
NeilBrown96de1e62005-11-08 21:39:39 -08004563static struct md_sysfs_entry
4564raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4565 raid5_show_stripe_cache_size,
4566 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004567
4568static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004569raid5_show_preread_threshold(mddev_t *mddev, char *page)
4570{
4571 raid5_conf_t *conf = mddev_to_conf(mddev);
4572 if (conf)
4573 return sprintf(page, "%d\n", conf->bypass_threshold);
4574 else
4575 return 0;
4576}
4577
4578static ssize_t
4579raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4580{
4581 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004582 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004583 if (len >= PAGE_SIZE)
4584 return -EINVAL;
4585 if (!conf)
4586 return -ENODEV;
4587
Dan Williams4ef197d82008-04-28 02:15:54 -07004588 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004589 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004590 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004591 return -EINVAL;
4592 conf->bypass_threshold = new;
4593 return len;
4594}
4595
4596static struct md_sysfs_entry
4597raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4598 S_IRUGO | S_IWUSR,
4599 raid5_show_preread_threshold,
4600 raid5_store_preread_threshold);
4601
4602static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004603stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004604{
NeilBrown007583c2005-11-08 21:39:30 -08004605 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004606 if (conf)
4607 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4608 else
4609 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004610}
4611
NeilBrown96de1e62005-11-08 21:39:39 -08004612static struct md_sysfs_entry
4613raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004614
NeilBrown007583c2005-11-08 21:39:30 -08004615static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004616 &raid5_stripecache_size.attr,
4617 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004618 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004619 NULL,
4620};
NeilBrown007583c2005-11-08 21:39:30 -08004621static struct attribute_group raid5_attrs_group = {
4622 .name = NULL,
4623 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004624};
4625
Dan Williams80c3a6c2009-03-17 18:10:40 -07004626static sector_t
4627raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4628{
4629 raid5_conf_t *conf = mddev_to_conf(mddev);
4630
4631 if (!sectors)
4632 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004633 if (!raid_disks) {
4634 /* size is defined by the smallest of previous and new size */
4635 if (conf->raid_disks < conf->previous_raid_disks)
4636 raid_disks = conf->raid_disks;
4637 else
4638 raid_disks = conf->previous_raid_disks;
4639 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004640
4641 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown784052e2009-03-31 15:19:07 +11004642 sectors &= ~((sector_t)mddev->new_chunk/512 - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004643 return sectors * (raid_disks - conf->max_degraded);
4644}
4645
Dan Williams36d1c642009-07-14 11:48:22 -07004646static void raid5_free_percpu(raid5_conf_t *conf)
4647{
4648 struct raid5_percpu *percpu;
4649 unsigned long cpu;
4650
4651 if (!conf->percpu)
4652 return;
4653
4654 get_online_cpus();
4655 for_each_possible_cpu(cpu) {
4656 percpu = per_cpu_ptr(conf->percpu, cpu);
4657 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004658 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004659 }
4660#ifdef CONFIG_HOTPLUG_CPU
4661 unregister_cpu_notifier(&conf->cpu_notify);
4662#endif
4663 put_online_cpus();
4664
4665 free_percpu(conf->percpu);
4666}
4667
Dan Williamsa11034b2009-07-14 11:48:16 -07004668static void free_conf(raid5_conf_t *conf)
4669{
4670 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004671 raid5_free_percpu(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004672 kfree(conf->disks);
4673 kfree(conf->stripe_hashtbl);
4674 kfree(conf);
4675}
4676
Dan Williams36d1c642009-07-14 11:48:22 -07004677#ifdef CONFIG_HOTPLUG_CPU
4678static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4679 void *hcpu)
4680{
4681 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4682 long cpu = (long)hcpu;
4683 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4684
4685 switch (action) {
4686 case CPU_UP_PREPARE:
4687 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004688 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004689 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004690 if (!percpu->scribble)
4691 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4692
4693 if (!percpu->scribble ||
4694 (conf->level == 6 && !percpu->spare_page)) {
4695 safe_put_page(percpu->spare_page);
4696 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004697 pr_err("%s: failed memory allocation for cpu%ld\n",
4698 __func__, cpu);
4699 return NOTIFY_BAD;
4700 }
4701 break;
4702 case CPU_DEAD:
4703 case CPU_DEAD_FROZEN:
4704 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004705 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004706 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004707 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004708 break;
4709 default:
4710 break;
4711 }
4712 return NOTIFY_OK;
4713}
4714#endif
4715
4716static int raid5_alloc_percpu(raid5_conf_t *conf)
4717{
4718 unsigned long cpu;
4719 struct page *spare_page;
4720 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004721 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004722 int err;
4723
Dan Williams36d1c642009-07-14 11:48:22 -07004724 allcpus = alloc_percpu(struct raid5_percpu);
4725 if (!allcpus)
4726 return -ENOMEM;
4727 conf->percpu = allcpus;
4728
4729 get_online_cpus();
4730 err = 0;
4731 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004732 if (conf->level == 6) {
4733 spare_page = alloc_page(GFP_KERNEL);
4734 if (!spare_page) {
4735 err = -ENOMEM;
4736 break;
4737 }
4738 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4739 }
4740 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4741 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004742 err = -ENOMEM;
4743 break;
4744 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004745 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004746 }
4747#ifdef CONFIG_HOTPLUG_CPU
4748 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4749 conf->cpu_notify.priority = 0;
4750 if (err == 0)
4751 err = register_cpu_notifier(&conf->cpu_notify);
4752#endif
4753 put_online_cpus();
4754
4755 return err;
4756}
4757
NeilBrown91adb562009-03-31 14:39:39 +11004758static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
4760 raid5_conf_t *conf;
4761 int raid_disk, memory;
4762 mdk_rdev_t *rdev;
4763 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
NeilBrown91adb562009-03-31 14:39:39 +11004765 if (mddev->new_level != 5
4766 && mddev->new_level != 4
4767 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004768 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004769 mdname(mddev), mddev->new_level);
4770 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 }
NeilBrown91adb562009-03-31 14:39:39 +11004772 if ((mddev->new_level == 5
4773 && !algorithm_valid_raid5(mddev->new_layout)) ||
4774 (mddev->new_level == 6
4775 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004776 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004777 mdname(mddev), mddev->new_layout);
4778 return ERR_PTR(-EIO);
4779 }
4780 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4781 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4782 mdname(mddev), mddev->raid_disks);
4783 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
NeilBrown91adb562009-03-31 14:39:39 +11004786 if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
4787 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4788 mddev->new_chunk, mdname(mddev));
4789 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004790 }
4791
NeilBrown91adb562009-03-31 14:39:39 +11004792 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4793 if (conf == NULL)
4794 goto abort;
4795
4796 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004797 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004798 if (mddev->reshape_position == MaxSector)
4799 conf->previous_raid_disks = mddev->raid_disks;
4800 else
4801 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4802
4803 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4804 GFP_KERNEL);
4805 if (!conf->disks)
4806 goto abort;
4807
4808 conf->mddev = mddev;
4809
4810 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4811 goto abort;
4812
Dan Williams36d1c642009-07-14 11:48:22 -07004813 conf->level = mddev->new_level;
4814 if (raid5_alloc_percpu(conf) != 0)
4815 goto abort;
4816
NeilBrown91adb562009-03-31 14:39:39 +11004817 spin_lock_init(&conf->device_lock);
4818 init_waitqueue_head(&conf->wait_for_stripe);
4819 init_waitqueue_head(&conf->wait_for_overlap);
4820 INIT_LIST_HEAD(&conf->handle_list);
4821 INIT_LIST_HEAD(&conf->hold_list);
4822 INIT_LIST_HEAD(&conf->delayed_list);
4823 INIT_LIST_HEAD(&conf->bitmap_list);
4824 INIT_LIST_HEAD(&conf->inactive_list);
4825 atomic_set(&conf->active_stripes, 0);
4826 atomic_set(&conf->preread_active_stripes, 0);
4827 atomic_set(&conf->active_aligned_reads, 0);
4828 conf->bypass_threshold = BYPASS_THRESHOLD;
4829
4830 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4831
4832 list_for_each_entry(rdev, &mddev->disks, same_set) {
4833 raid_disk = rdev->raid_disk;
4834 if (raid_disk >= conf->raid_disks
4835 || raid_disk < 0)
4836 continue;
4837 disk = conf->disks + raid_disk;
4838
4839 disk->rdev = rdev;
4840
4841 if (test_bit(In_sync, &rdev->flags)) {
4842 char b[BDEVNAME_SIZE];
4843 printk(KERN_INFO "raid5: device %s operational as raid"
4844 " disk %d\n", bdevname(rdev->bdev,b),
4845 raid_disk);
4846 } else
4847 /* Cannot rely on bitmap to complete recovery */
4848 conf->fullsync = 1;
4849 }
4850
4851 conf->chunk_size = mddev->new_chunk;
NeilBrown91adb562009-03-31 14:39:39 +11004852 if (conf->level == 6)
4853 conf->max_degraded = 2;
4854 else
4855 conf->max_degraded = 1;
4856 conf->algorithm = mddev->new_layout;
4857 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004858 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004859 if (conf->reshape_progress != MaxSector) {
NeilBrown784052e2009-03-31 15:19:07 +11004860 conf->prev_chunk = mddev->chunk_size;
NeilBrowne183eae2009-03-31 15:20:22 +11004861 conf->prev_algo = mddev->layout;
4862 }
NeilBrown91adb562009-03-31 14:39:39 +11004863
4864 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4865 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4866 if (grow_stripes(conf, conf->max_nr_stripes)) {
4867 printk(KERN_ERR
4868 "raid5: couldn't allocate %dkB for buffers\n", memory);
4869 goto abort;
4870 } else
4871 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4872 memory, mdname(mddev));
4873
4874 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4875 if (!conf->thread) {
4876 printk(KERN_ERR
4877 "raid5: couldn't allocate thread for %s\n",
4878 mdname(mddev));
4879 goto abort;
4880 }
4881
4882 return conf;
4883
4884 abort:
4885 if (conf) {
Dan Williamsa11034b2009-07-14 11:48:16 -07004886 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004887 return ERR_PTR(-EIO);
4888 } else
4889 return ERR_PTR(-ENOMEM);
4890}
4891
4892static int run(mddev_t *mddev)
4893{
4894 raid5_conf_t *conf;
4895 int working_disks = 0;
4896 mdk_rdev_t *rdev;
4897
NeilBrownf6705572006-03-27 01:18:11 -08004898 if (mddev->reshape_position != MaxSector) {
4899 /* Check that we can continue the reshape.
4900 * Currently only disks can change, it must
4901 * increase, and we must be past the point where
4902 * a stripe over-writes itself
4903 */
4904 sector_t here_new, here_old;
4905 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004906 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004907
NeilBrown88ce4932009-03-31 15:24:23 +11004908 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004909 printk(KERN_ERR "raid5: %s: unsupported reshape "
4910 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004911 mdname(mddev));
4912 return -EINVAL;
4913 }
NeilBrownf6705572006-03-27 01:18:11 -08004914 old_disks = mddev->raid_disks - mddev->delta_disks;
4915 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004916 * further up in new geometry must map after here in old
4917 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004918 */
4919 here_new = mddev->reshape_position;
NeilBrown784052e2009-03-31 15:19:07 +11004920 if (sector_div(here_new, (mddev->new_chunk>>9)*
NeilBrownf4168852007-02-28 20:11:53 -08004921 (mddev->raid_disks - max_degraded))) {
4922 printk(KERN_ERR "raid5: reshape_position not "
4923 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004924 return -EINVAL;
4925 }
4926 /* here_new is the stripe we will write to */
4927 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004928 sector_div(here_old, (mddev->chunk_size>>9)*
4929 (old_disks-max_degraded));
4930 /* here_old is the first stripe that we might need to read
4931 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004932 if (here_new >= here_old) {
4933 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004934 printk(KERN_ERR "raid5: reshape_position too early for "
4935 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004936 return -EINVAL;
4937 }
4938 printk(KERN_INFO "raid5: reshape will continue\n");
4939 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004940 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004941 BUG_ON(mddev->level != mddev->new_level);
4942 BUG_ON(mddev->layout != mddev->new_layout);
4943 BUG_ON(mddev->chunk_size != mddev->new_chunk);
4944 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004945 }
4946
NeilBrown245f46c2009-03-31 14:39:39 +11004947 if (mddev->private == NULL)
4948 conf = setup_conf(mddev);
4949 else
4950 conf = mddev->private;
4951
NeilBrown91adb562009-03-31 14:39:39 +11004952 if (IS_ERR(conf))
4953 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004954
NeilBrown91adb562009-03-31 14:39:39 +11004955 mddev->thread = conf->thread;
4956 conf->thread = NULL;
4957 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004960 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 */
NeilBrown91adb562009-03-31 14:39:39 +11004962 list_for_each_entry(rdev, &mddev->disks, same_set)
4963 if (rdev->raid_disk >= 0 &&
4964 test_bit(In_sync, &rdev->flags))
4965 working_disks++;
4966
NeilBrown02c2de82006-10-03 01:15:47 -07004967 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
NeilBrown16a53ec2006-06-26 00:27:38 -07004969 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 printk(KERN_ERR "raid5: not enough operational devices for %s"
4971 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004972 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 goto abort;
4974 }
4975
NeilBrown91adb562009-03-31 14:39:39 +11004976 /* device size must be a multiple of chunk size */
4977 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4978 mddev->resync_max_sectors = mddev->dev_sectors;
4979
NeilBrown16a53ec2006-06-26 00:27:38 -07004980 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004982 if (mddev->ok_start_degraded)
4983 printk(KERN_WARNING
4984 "raid5: starting dirty degraded array: %s"
4985 "- data corruption possible.\n",
4986 mdname(mddev));
4987 else {
4988 printk(KERN_ERR
4989 "raid5: cannot start dirty degraded array for %s\n",
4990 mdname(mddev));
4991 goto abort;
4992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 }
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (mddev->degraded == 0)
4996 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004997 " devices, algorithm %d\n", conf->level, mdname(mddev),
4998 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4999 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 else
5001 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
5002 " out of %d devices, algorithm %d\n", conf->level,
5003 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11005004 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 print_raid5_conf(conf);
5007
NeilBrownfef9c612009-03-31 15:16:46 +11005008 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08005009 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11005010 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005011 atomic_set(&conf->reshape_stripes, 0);
5012 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5013 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5014 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5015 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5016 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5017 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005018 }
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07005021 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 */
5023 {
NeilBrown16a53ec2006-06-26 00:27:38 -07005024 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5025 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07005026 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5028 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5029 }
5030
5031 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08005032 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
5033 printk(KERN_WARNING
5034 "raid5: failed to create sysfs attributes for %s\n",
5035 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07005036
NeilBrown91adb562009-03-31 14:39:39 +11005037 mddev->queue->queue_lock = &conf->device_lock;
5038
NeilBrown7a5febe2005-05-16 21:53:16 -07005039 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07005040 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08005041 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07005042
Dan Williams1f403622009-03-31 14:59:03 +11005043 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07005044
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005045 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 return 0;
5048abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005049 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005050 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 if (conf) {
5052 print_raid5_conf(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07005053 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
5055 mddev->private = NULL;
5056 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5057 return -EIO;
5058}
5059
5060
5061
NeilBrown3f294f42005-11-08 21:39:25 -08005062static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
5064 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5065
5066 md_unregister_thread(mddev->thread);
5067 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005068 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08005070 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williamsa11034b2009-07-14 11:48:16 -07005071 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 mddev->private = NULL;
5073 return 0;
5074}
5075
Dan Williams45b42332007-07-09 11:56:43 -07005076#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005077static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
5079 int i;
5080
NeilBrown16a53ec2006-06-26 00:27:38 -07005081 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5082 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5083 seq_printf(seq, "sh %llu, count %d.\n",
5084 (unsigned long long)sh->sector, atomic_read(&sh->count));
5085 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005086 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005087 seq_printf(seq, "(cache%d: %p %ld) ",
5088 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005090 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091}
5092
NeilBrownd710e132008-10-13 11:55:12 +11005093static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
5095 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005096 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 int i;
5098
5099 spin_lock_irq(&conf->device_lock);
5100 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005101 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (sh->raid_conf != conf)
5103 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005104 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 }
5106 }
5107 spin_unlock_irq(&conf->device_lock);
5108}
5109#endif
5110
NeilBrownd710e132008-10-13 11:55:12 +11005111static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
5113 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5114 int i;
5115
5116 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005117 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 for (i = 0; i < conf->raid_disks; i++)
5119 seq_printf (seq, "%s",
5120 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005121 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005123#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005124 seq_printf (seq, "\n");
5125 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126#endif
5127}
5128
5129static void print_raid5_conf (raid5_conf_t *conf)
5130{
5131 int i;
5132 struct disk_info *tmp;
5133
5134 printk("RAID5 conf printout:\n");
5135 if (!conf) {
5136 printk("(conf==NULL)\n");
5137 return;
5138 }
NeilBrown02c2de82006-10-03 01:15:47 -07005139 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5140 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 for (i = 0; i < conf->raid_disks; i++) {
5143 char b[BDEVNAME_SIZE];
5144 tmp = conf->disks + i;
5145 if (tmp->rdev)
5146 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005147 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 bdevname(tmp->rdev->bdev,b));
5149 }
5150}
5151
5152static int raid5_spare_active(mddev_t *mddev)
5153{
5154 int i;
5155 raid5_conf_t *conf = mddev->private;
5156 struct disk_info *tmp;
5157
5158 for (i = 0; i < conf->raid_disks; i++) {
5159 tmp = conf->disks + i;
5160 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005161 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005162 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5163 unsigned long flags;
5164 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005166 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 }
5168 }
5169 print_raid5_conf(conf);
5170 return 0;
5171}
5172
5173static int raid5_remove_disk(mddev_t *mddev, int number)
5174{
5175 raid5_conf_t *conf = mddev->private;
5176 int err = 0;
5177 mdk_rdev_t *rdev;
5178 struct disk_info *p = conf->disks + number;
5179
5180 print_raid5_conf(conf);
5181 rdev = p->rdev;
5182 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005183 if (number >= conf->raid_disks &&
5184 conf->reshape_progress == MaxSector)
5185 clear_bit(In_sync, &rdev->flags);
5186
NeilBrownb2d444d2005-11-08 21:39:31 -08005187 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 atomic_read(&rdev->nr_pending)) {
5189 err = -EBUSY;
5190 goto abort;
5191 }
NeilBrowndfc70642008-05-23 13:04:39 -07005192 /* Only remove non-faulty devices if recovery
5193 * isn't possible.
5194 */
5195 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005196 mddev->degraded <= conf->max_degraded &&
5197 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005198 err = -EBUSY;
5199 goto abort;
5200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005202 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 if (atomic_read(&rdev->nr_pending)) {
5204 /* lost the race, try later */
5205 err = -EBUSY;
5206 p->rdev = rdev;
5207 }
5208 }
5209abort:
5210
5211 print_raid5_conf(conf);
5212 return err;
5213}
5214
5215static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5216{
5217 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005218 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 int disk;
5220 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005221 int first = 0;
5222 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
NeilBrown16a53ec2006-06-26 00:27:38 -07005224 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
Neil Brown6c2fce22008-06-28 08:31:31 +10005228 if (rdev->raid_disk >= 0)
5229 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005232 * find the disk ... but prefer rdev->saved_raid_disk
5233 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005235 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005236 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005237 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5238 disk = rdev->saved_raid_disk;
5239 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005240 disk = first;
5241 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005243 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005245 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005246 if (rdev->saved_raid_disk != disk)
5247 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005248 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 break;
5250 }
5251 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
5255static int raid5_resize(mddev_t *mddev, sector_t sectors)
5256{
5257 /* no resync is happening, and there is enough space
5258 * on all devices, so we can resize.
5259 * We need to make sure resync covers any new space.
5260 * If the array is shrinking we should possibly wait until
5261 * any io in the removed space completes, but it hardly seems
5262 * worth it.
5263 */
5264 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005265 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5266 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005267 if (mddev->array_sectors >
5268 raid5_size(mddev, sectors, mddev->raid_disks))
5269 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005270 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce6292007-05-09 18:51:36 -07005271 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11005272 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5273 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5275 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005276 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005277 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 return 0;
5279}
5280
NeilBrown63c70c42006-03-27 01:18:13 -08005281static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005282{
5283 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown29269552006-03-27 01:18:10 -08005284
NeilBrown88ce4932009-03-31 15:24:23 +11005285 if (mddev->delta_disks == 0 &&
5286 mddev->new_layout == mddev->layout &&
5287 mddev->new_chunk == mddev->chunk_size)
5288 return -EINVAL; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005289 if (mddev->bitmap)
5290 /* Cannot grow a bitmap yet */
5291 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005292 if (mddev->degraded > conf->max_degraded)
5293 return -EINVAL;
5294 if (mddev->delta_disks < 0) {
5295 /* We might be able to shrink, but the devices must
5296 * be made bigger first.
5297 * For raid6, 4 is the minimum size.
5298 * Otherwise 2 is the minimum
5299 */
5300 int min = 2;
5301 if (mddev->level == 6)
5302 min = 4;
5303 if (mddev->raid_disks + mddev->delta_disks < min)
5304 return -EINVAL;
5305 }
NeilBrown29269552006-03-27 01:18:10 -08005306
5307 /* Can only proceed if there are plenty of stripe_heads.
5308 * We need a minimum of one full stripe,, and for sensible progress
5309 * it is best to have about 4 times that.
5310 * If we require 4 times, then the default 256 4K stripe_heads will
5311 * allow for chunk sizes up to 256K, which is probably OK.
5312 * If the chunk size is greater, user-space should request more
5313 * stripe_heads first.
5314 */
NeilBrown63c70c42006-03-27 01:18:13 -08005315 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
5316 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08005317 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
NeilBrown784052e2009-03-31 15:19:07 +11005318 (max(mddev->chunk_size, mddev->new_chunk)
5319 / STRIPE_SIZE)*4);
NeilBrown29269552006-03-27 01:18:10 -08005320 return -ENOSPC;
5321 }
5322
NeilBrownec32a2b2009-03-31 15:17:38 +11005323 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005324}
5325
5326static int raid5_start_reshape(mddev_t *mddev)
5327{
5328 raid5_conf_t *conf = mddev_to_conf(mddev);
5329 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005330 int spares = 0;
5331 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005332 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005333
NeilBrownf4168852007-02-28 20:11:53 -08005334 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005335 return -EBUSY;
5336
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005337 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005338 if (rdev->raid_disk < 0 &&
5339 !test_bit(Faulty, &rdev->flags))
5340 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005341
NeilBrownf4168852007-02-28 20:11:53 -08005342 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005343 /* Not enough devices even to make a degraded array
5344 * of that size
5345 */
5346 return -EINVAL;
5347
NeilBrownec32a2b2009-03-31 15:17:38 +11005348 /* Refuse to reduce size of the array. Any reductions in
5349 * array size must be through explicit setting of array_size
5350 * attribute.
5351 */
5352 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5353 < mddev->array_sectors) {
5354 printk(KERN_ERR "md: %s: array size must be reduced "
5355 "before number of disks\n", mdname(mddev));
5356 return -EINVAL;
5357 }
5358
NeilBrownf6705572006-03-27 01:18:11 -08005359 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005360 spin_lock_irq(&conf->device_lock);
5361 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005362 conf->raid_disks += mddev->delta_disks;
NeilBrown88ce4932009-03-31 15:24:23 +11005363 conf->prev_chunk = conf->chunk_size;
5364 conf->chunk_size = mddev->new_chunk;
5365 conf->prev_algo = conf->algorithm;
5366 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005367 if (mddev->delta_disks < 0)
5368 conf->reshape_progress = raid5_size(mddev, 0, 0);
5369 else
5370 conf->reshape_progress = 0;
5371 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005372 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005373 spin_unlock_irq(&conf->device_lock);
5374
5375 /* Add some new drives, as many as will fit.
5376 * We know there are enough to make the newly sized array work.
5377 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005378 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005379 if (rdev->raid_disk < 0 &&
5380 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005381 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005382 char nm[20];
5383 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005384 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005385 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005386 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005387 if (sysfs_create_link(&mddev->kobj,
5388 &rdev->kobj, nm))
5389 printk(KERN_WARNING
5390 "raid5: failed to create "
5391 " link %s for %s\n",
5392 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005393 } else
5394 break;
5395 }
5396
NeilBrownec32a2b2009-03-31 15:17:38 +11005397 if (mddev->delta_disks > 0) {
5398 spin_lock_irqsave(&conf->device_lock, flags);
5399 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5400 - added_devices;
5401 spin_unlock_irqrestore(&conf->device_lock, flags);
5402 }
NeilBrown63c70c42006-03-27 01:18:13 -08005403 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08005404 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005405 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005406
NeilBrown29269552006-03-27 01:18:10 -08005407 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5408 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5409 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5410 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5411 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5412 "%s_reshape");
5413 if (!mddev->sync_thread) {
5414 mddev->recovery = 0;
5415 spin_lock_irq(&conf->device_lock);
5416 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005417 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005418 spin_unlock_irq(&conf->device_lock);
5419 return -EAGAIN;
5420 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005421 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005422 md_wakeup_thread(mddev->sync_thread);
5423 md_new_event(mddev);
5424 return 0;
5425}
NeilBrown29269552006-03-27 01:18:10 -08005426
NeilBrownec32a2b2009-03-31 15:17:38 +11005427/* This is called from the reshape thread and should make any
5428 * changes needed in 'conf'
5429 */
NeilBrown29269552006-03-27 01:18:10 -08005430static void end_reshape(raid5_conf_t *conf)
5431{
NeilBrown29269552006-03-27 01:18:10 -08005432
NeilBrownf6705572006-03-27 01:18:11 -08005433 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005434
NeilBrownf6705572006-03-27 01:18:11 -08005435 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005436 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005437 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005438 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005439 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005440
5441 /* read-ahead size must cover two whole stripes, which is
5442 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5443 */
5444 {
NeilBrowncea9c222009-03-31 15:15:05 +11005445 int data_disks = conf->raid_disks - conf->max_degraded;
5446 int stripe = data_disks * (conf->chunk_size
5447 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005448 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5449 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5450 }
NeilBrown29269552006-03-27 01:18:10 -08005451 }
NeilBrown29269552006-03-27 01:18:10 -08005452}
5453
NeilBrownec32a2b2009-03-31 15:17:38 +11005454/* This is called from the raid5d thread with mddev_lock held.
5455 * It makes config changes to the device.
5456 */
NeilBrowncea9c222009-03-31 15:15:05 +11005457static void raid5_finish_reshape(mddev_t *mddev)
5458{
5459 struct block_device *bdev;
NeilBrown88ce4932009-03-31 15:24:23 +11005460 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11005461
5462 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5463
NeilBrownec32a2b2009-03-31 15:17:38 +11005464 if (mddev->delta_disks > 0) {
5465 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5466 set_capacity(mddev->gendisk, mddev->array_sectors);
5467 mddev->changed = 1;
NeilBrowncea9c222009-03-31 15:15:05 +11005468
NeilBrownec32a2b2009-03-31 15:17:38 +11005469 bdev = bdget_disk(mddev->gendisk, 0);
5470 if (bdev) {
5471 mutex_lock(&bdev->bd_inode->i_mutex);
5472 i_size_write(bdev->bd_inode,
5473 (loff_t)mddev->array_sectors << 9);
5474 mutex_unlock(&bdev->bd_inode->i_mutex);
5475 bdput(bdev);
5476 }
5477 } else {
5478 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005479 mddev->degraded = conf->raid_disks;
5480 for (d = 0; d < conf->raid_disks ; d++)
5481 if (conf->disks[d].rdev &&
5482 test_bit(In_sync,
5483 &conf->disks[d].rdev->flags))
5484 mddev->degraded--;
5485 for (d = conf->raid_disks ;
5486 d < conf->raid_disks - mddev->delta_disks;
5487 d++)
5488 raid5_remove_disk(mddev, d);
NeilBrowncea9c222009-03-31 15:15:05 +11005489 }
NeilBrown88ce4932009-03-31 15:24:23 +11005490 mddev->layout = conf->algorithm;
5491 mddev->chunk_size = conf->chunk_size;
NeilBrownec32a2b2009-03-31 15:17:38 +11005492 mddev->reshape_position = MaxSector;
5493 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005494 }
5495}
5496
NeilBrown72626682005-09-09 16:23:54 -07005497static void raid5_quiesce(mddev_t *mddev, int state)
5498{
5499 raid5_conf_t *conf = mddev_to_conf(mddev);
5500
5501 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005502 case 2: /* resume for a suspend */
5503 wake_up(&conf->wait_for_overlap);
5504 break;
5505
NeilBrown72626682005-09-09 16:23:54 -07005506 case 1: /* stop all writes */
5507 spin_lock_irq(&conf->device_lock);
5508 conf->quiesce = 1;
5509 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005510 atomic_read(&conf->active_stripes) == 0 &&
5511 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005512 conf->device_lock, /* nothing */);
5513 spin_unlock_irq(&conf->device_lock);
5514 break;
5515
5516 case 0: /* re-enable writes */
5517 spin_lock_irq(&conf->device_lock);
5518 conf->quiesce = 0;
5519 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005520 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005521 spin_unlock_irq(&conf->device_lock);
5522 break;
5523 }
NeilBrown72626682005-09-09 16:23:54 -07005524}
NeilBrownb15c2e52006-01-06 00:20:16 -08005525
NeilBrownd562b0c2009-03-31 14:39:39 +11005526
5527static void *raid5_takeover_raid1(mddev_t *mddev)
5528{
5529 int chunksect;
5530
5531 if (mddev->raid_disks != 2 ||
5532 mddev->degraded > 1)
5533 return ERR_PTR(-EINVAL);
5534
5535 /* Should check if there are write-behind devices? */
5536
5537 chunksect = 64*2; /* 64K by default */
5538
5539 /* The array must be an exact multiple of chunksize */
5540 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5541 chunksect >>= 1;
5542
5543 if ((chunksect<<9) < STRIPE_SIZE)
5544 /* array size does not allow a suitable chunk size */
5545 return ERR_PTR(-EINVAL);
5546
5547 mddev->new_level = 5;
5548 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5549 mddev->new_chunk = chunksect << 9;
5550
5551 return setup_conf(mddev);
5552}
5553
NeilBrownfc9739c2009-03-31 14:57:20 +11005554static void *raid5_takeover_raid6(mddev_t *mddev)
5555{
5556 int new_layout;
5557
5558 switch (mddev->layout) {
5559 case ALGORITHM_LEFT_ASYMMETRIC_6:
5560 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5561 break;
5562 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5563 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5564 break;
5565 case ALGORITHM_LEFT_SYMMETRIC_6:
5566 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5567 break;
5568 case ALGORITHM_RIGHT_SYMMETRIC_6:
5569 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5570 break;
5571 case ALGORITHM_PARITY_0_6:
5572 new_layout = ALGORITHM_PARITY_0;
5573 break;
5574 case ALGORITHM_PARITY_N:
5575 new_layout = ALGORITHM_PARITY_N;
5576 break;
5577 default:
5578 return ERR_PTR(-EINVAL);
5579 }
5580 mddev->new_level = 5;
5581 mddev->new_layout = new_layout;
5582 mddev->delta_disks = -1;
5583 mddev->raid_disks -= 1;
5584 return setup_conf(mddev);
5585}
5586
NeilBrownd562b0c2009-03-31 14:39:39 +11005587
NeilBrownb3546032009-03-31 14:56:41 +11005588static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5589{
NeilBrown88ce4932009-03-31 15:24:23 +11005590 /* For a 2-drive array, the layout and chunk size can be changed
5591 * immediately as not restriping is needed.
5592 * For larger arrays we record the new value - after validation
5593 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005594 */
5595 raid5_conf_t *conf = mddev_to_conf(mddev);
5596
5597 if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
5598 return -EINVAL;
5599 if (new_chunk > 0) {
5600 if (new_chunk & (new_chunk-1))
5601 /* not a power of 2 */
5602 return -EINVAL;
5603 if (new_chunk < PAGE_SIZE)
5604 return -EINVAL;
5605 if (mddev->array_sectors & ((new_chunk>>9)-1))
5606 /* not factor of array size */
5607 return -EINVAL;
5608 }
5609
5610 /* They look valid */
5611
NeilBrown88ce4932009-03-31 15:24:23 +11005612 if (mddev->raid_disks == 2) {
NeilBrownb3546032009-03-31 14:56:41 +11005613
NeilBrown88ce4932009-03-31 15:24:23 +11005614 if (new_layout >= 0) {
5615 conf->algorithm = new_layout;
5616 mddev->layout = mddev->new_layout = new_layout;
5617 }
5618 if (new_chunk > 0) {
5619 conf->chunk_size = new_chunk;
5620 mddev->chunk_size = mddev->new_chunk = new_chunk;
5621 }
5622 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5623 md_wakeup_thread(mddev->thread);
5624 } else {
5625 if (new_layout >= 0)
5626 mddev->new_layout = new_layout;
5627 if (new_chunk > 0)
5628 mddev->new_chunk = new_chunk;
NeilBrownb3546032009-03-31 14:56:41 +11005629 }
NeilBrown88ce4932009-03-31 15:24:23 +11005630 return 0;
5631}
5632
5633static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5634{
5635 if (new_layout >= 0 && !algorithm_valid_raid6(new_layout))
5636 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005637 if (new_chunk > 0) {
NeilBrown88ce4932009-03-31 15:24:23 +11005638 if (new_chunk & (new_chunk-1))
5639 /* not a power of 2 */
5640 return -EINVAL;
5641 if (new_chunk < PAGE_SIZE)
5642 return -EINVAL;
5643 if (mddev->array_sectors & ((new_chunk>>9)-1))
5644 /* not factor of array size */
5645 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005646 }
NeilBrown88ce4932009-03-31 15:24:23 +11005647
5648 /* They look valid */
5649
5650 if (new_layout >= 0)
5651 mddev->new_layout = new_layout;
5652 if (new_chunk > 0)
5653 mddev->new_chunk = new_chunk;
5654
NeilBrownb3546032009-03-31 14:56:41 +11005655 return 0;
5656}
5657
NeilBrownd562b0c2009-03-31 14:39:39 +11005658static void *raid5_takeover(mddev_t *mddev)
5659{
5660 /* raid5 can take over:
5661 * raid0 - if all devices are the same - make it a raid4 layout
5662 * raid1 - if there are two drives. We need to know the chunk size
5663 * raid4 - trivial - just use a raid4 layout.
5664 * raid6 - Providing it is a *_6 layout
5665 *
5666 * For now, just do raid1
5667 */
5668
5669 if (mddev->level == 1)
5670 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005671 if (mddev->level == 4) {
5672 mddev->new_layout = ALGORITHM_PARITY_N;
5673 mddev->new_level = 5;
5674 return setup_conf(mddev);
5675 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005676 if (mddev->level == 6)
5677 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005678
5679 return ERR_PTR(-EINVAL);
5680}
5681
5682
NeilBrown245f46c2009-03-31 14:39:39 +11005683static struct mdk_personality raid5_personality;
5684
5685static void *raid6_takeover(mddev_t *mddev)
5686{
5687 /* Currently can only take over a raid5. We map the
5688 * personality to an equivalent raid6 personality
5689 * with the Q block at the end.
5690 */
5691 int new_layout;
5692
5693 if (mddev->pers != &raid5_personality)
5694 return ERR_PTR(-EINVAL);
5695 if (mddev->degraded > 1)
5696 return ERR_PTR(-EINVAL);
5697 if (mddev->raid_disks > 253)
5698 return ERR_PTR(-EINVAL);
5699 if (mddev->raid_disks < 3)
5700 return ERR_PTR(-EINVAL);
5701
5702 switch (mddev->layout) {
5703 case ALGORITHM_LEFT_ASYMMETRIC:
5704 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5705 break;
5706 case ALGORITHM_RIGHT_ASYMMETRIC:
5707 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5708 break;
5709 case ALGORITHM_LEFT_SYMMETRIC:
5710 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5711 break;
5712 case ALGORITHM_RIGHT_SYMMETRIC:
5713 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5714 break;
5715 case ALGORITHM_PARITY_0:
5716 new_layout = ALGORITHM_PARITY_0_6;
5717 break;
5718 case ALGORITHM_PARITY_N:
5719 new_layout = ALGORITHM_PARITY_N;
5720 break;
5721 default:
5722 return ERR_PTR(-EINVAL);
5723 }
5724 mddev->new_level = 6;
5725 mddev->new_layout = new_layout;
5726 mddev->delta_disks = 1;
5727 mddev->raid_disks += 1;
5728 return setup_conf(mddev);
5729}
5730
5731
NeilBrown16a53ec2006-06-26 00:27:38 -07005732static struct mdk_personality raid6_personality =
5733{
5734 .name = "raid6",
5735 .level = 6,
5736 .owner = THIS_MODULE,
5737 .make_request = make_request,
5738 .run = run,
5739 .stop = stop,
5740 .status = status,
5741 .error_handler = error,
5742 .hot_add_disk = raid5_add_disk,
5743 .hot_remove_disk= raid5_remove_disk,
5744 .spare_active = raid5_spare_active,
5745 .sync_request = sync_request,
5746 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005747 .size = raid5_size,
NeilBrownf4168852007-02-28 20:11:53 -08005748 .check_reshape = raid5_check_reshape,
5749 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005750 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005751 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005752 .takeover = raid6_takeover,
NeilBrown88ce4932009-03-31 15:24:23 +11005753 .reconfig = raid6_reconfig,
NeilBrown16a53ec2006-06-26 00:27:38 -07005754};
NeilBrown2604b702006-01-06 00:20:36 -08005755static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756{
5757 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005758 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 .owner = THIS_MODULE,
5760 .make_request = make_request,
5761 .run = run,
5762 .stop = stop,
5763 .status = status,
5764 .error_handler = error,
5765 .hot_add_disk = raid5_add_disk,
5766 .hot_remove_disk= raid5_remove_disk,
5767 .spare_active = raid5_spare_active,
5768 .sync_request = sync_request,
5769 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005770 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005771 .check_reshape = raid5_check_reshape,
5772 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005773 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005774 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005775 .takeover = raid5_takeover,
NeilBrownb3546032009-03-31 14:56:41 +11005776 .reconfig = raid5_reconfig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777};
5778
NeilBrown2604b702006-01-06 00:20:36 -08005779static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780{
NeilBrown2604b702006-01-06 00:20:36 -08005781 .name = "raid4",
5782 .level = 4,
5783 .owner = THIS_MODULE,
5784 .make_request = make_request,
5785 .run = run,
5786 .stop = stop,
5787 .status = status,
5788 .error_handler = error,
5789 .hot_add_disk = raid5_add_disk,
5790 .hot_remove_disk= raid5_remove_disk,
5791 .spare_active = raid5_spare_active,
5792 .sync_request = sync_request,
5793 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005794 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005795 .check_reshape = raid5_check_reshape,
5796 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005797 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005798 .quiesce = raid5_quiesce,
5799};
5800
5801static int __init raid5_init(void)
5802{
NeilBrown16a53ec2006-06-26 00:27:38 -07005803 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005804 register_md_personality(&raid5_personality);
5805 register_md_personality(&raid4_personality);
5806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
NeilBrown2604b702006-01-06 00:20:36 -08005809static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
NeilBrown16a53ec2006-06-26 00:27:38 -07005811 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005812 unregister_md_personality(&raid5_personality);
5813 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814}
5815
5816module_init(raid5_init);
5817module_exit(raid5_exit);
5818MODULE_LICENSE("GPL");
5819MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005820MODULE_ALIAS("md-raid5");
5821MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005822MODULE_ALIAS("md-level-5");
5823MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005824MODULE_ALIAS("md-personality-8"); /* RAID6 */
5825MODULE_ALIAS("md-raid6");
5826MODULE_ALIAS("md-level-6");
5827
5828/* This used to be two separate modules, they were: */
5829MODULE_ALIAS("raid5");
5830MODULE_ALIAS("raid6");