blob: e3a2990bdc7c3dbae5a701ef5a07ab6978d410b1 [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 Williams91c00922007-01-02 13:52:30 -0700639static void ops_complete_compute5(void *stripe_head_ref)
640{
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 Williams4e7d2c02009-08-29 19:13:11 -0700646 /* mark the computed target as uptodate */
647 mark_target_uptodate(sh, sh->ops.target);
648
Dan Williamsecc65c92008-06-28 08:31:57 +1000649 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
650 if (sh->check_state == check_state_compute_run)
651 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700652 set_bit(STRIPE_HANDLE, &sh->state);
653 release_stripe(sh);
654}
655
Dan Williamsd6f38f32009-07-14 11:50:52 -0700656/* return a pointer to the address conversion region of the scribble buffer */
657static addr_conv_t *to_addr_conv(struct stripe_head *sh,
658 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700659{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700660 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
661}
662
663static struct dma_async_tx_descriptor *
664ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
665{
Dan Williams91c00922007-01-02 13:52:30 -0700666 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700667 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700668 int target = sh->ops.target;
669 struct r5dev *tgt = &sh->dev[target];
670 struct page *xor_dest = tgt->page;
671 int count = 0;
672 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700673 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700674 int i;
675
676 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700677 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700678 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
679
680 for (i = disks; i--; )
681 if (i != target)
682 xor_srcs[count++] = sh->dev[i].page;
683
684 atomic_inc(&sh->count);
685
Dan Williamsa08abd82009-06-03 11:43:59 -0700686 init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700687 ops_complete_compute5, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700688 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700689 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700690 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700691 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700692
Dan Williams91c00922007-01-02 13:52:30 -0700693 return tx;
694}
695
696static void ops_complete_prexor(void *stripe_head_ref)
697{
698 struct stripe_head *sh = stripe_head_ref;
699
Harvey Harrisone46b2722008-04-28 02:15:50 -0700700 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700701 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700702}
703
704static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700705ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
706 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700707{
Dan Williams91c00922007-01-02 13:52:30 -0700708 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700709 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700710 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700711 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700712
713 /* existing parity data subtracted */
714 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
715
Harvey Harrisone46b2722008-04-28 02:15:50 -0700716 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700717 (unsigned long long)sh->sector);
718
719 for (i = disks; i--; ) {
720 struct r5dev *dev = &sh->dev[i];
721 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000722 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700723 xor_srcs[count++] = dev->page;
724 }
725
Dan Williamsa08abd82009-06-03 11:43:59 -0700726 init_async_submit(&submit, ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700727 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700728 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700729
730 return tx;
731}
732
733static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000734ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700735{
736 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000737 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700738
Harvey Harrisone46b2722008-04-28 02:15:50 -0700739 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700740 (unsigned long long)sh->sector);
741
742 for (i = disks; i--; ) {
743 struct r5dev *dev = &sh->dev[i];
744 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700745
Dan Williamsd8ee0722008-06-28 08:32:06 +1000746 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700747 struct bio *wbi;
748
749 spin_lock(&sh->lock);
750 chosen = dev->towrite;
751 dev->towrite = NULL;
752 BUG_ON(dev->written);
753 wbi = dev->written = chosen;
754 spin_unlock(&sh->lock);
755
756 while (wbi && wbi->bi_sector <
757 dev->sector + STRIPE_SECTORS) {
758 tx = async_copy_data(1, wbi, dev->page,
759 dev->sector, tx);
760 wbi = r5_next_bio(wbi, dev->sector);
761 }
762 }
763 }
764
765 return tx;
766}
767
768static void ops_complete_postxor(void *stripe_head_ref)
769{
770 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700771 int disks = sh->disks, i, pd_idx = sh->pd_idx;
772
Harvey Harrisone46b2722008-04-28 02:15:50 -0700773 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700774 (unsigned long long)sh->sector);
775
776 for (i = disks; i--; ) {
777 struct r5dev *dev = &sh->dev[i];
778 if (dev->written || i == pd_idx)
779 set_bit(R5_UPTODATE, &dev->flags);
780 }
781
Dan Williamsd8ee0722008-06-28 08:32:06 +1000782 if (sh->reconstruct_state == reconstruct_state_drain_run)
783 sh->reconstruct_state = reconstruct_state_drain_result;
784 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
785 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
786 else {
787 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
788 sh->reconstruct_state = reconstruct_state_result;
789 }
Dan Williams91c00922007-01-02 13:52:30 -0700790
791 set_bit(STRIPE_HANDLE, &sh->state);
792 release_stripe(sh);
793}
794
795static void
Dan Williamsd6f38f32009-07-14 11:50:52 -0700796ops_run_postxor(struct stripe_head *sh, struct raid5_percpu *percpu,
797 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700798{
Dan Williams91c00922007-01-02 13:52:30 -0700799 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700800 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -0700801 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700802 int count = 0, pd_idx = sh->pd_idx, i;
803 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000804 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700805 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700806
Harvey Harrisone46b2722008-04-28 02:15:50 -0700807 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700808 (unsigned long long)sh->sector);
809
810 /* check if prexor is active which means only process blocks
811 * that are part of a read-modify-write (written)
812 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000813 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
814 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700815 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
816 for (i = disks; i--; ) {
817 struct r5dev *dev = &sh->dev[i];
818 if (dev->written)
819 xor_srcs[count++] = dev->page;
820 }
821 } else {
822 xor_dest = sh->dev[pd_idx].page;
823 for (i = disks; i--; ) {
824 struct r5dev *dev = &sh->dev[i];
825 if (i != pd_idx)
826 xor_srcs[count++] = dev->page;
827 }
828 }
829
Dan Williams91c00922007-01-02 13:52:30 -0700830 /* 1/ if we prexor'd then the dest is reused as a source
831 * 2/ if we did not prexor then we are redoing the parity
832 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
833 * for the synchronous xor case
834 */
Dan Williams88ba2aa2009-04-09 16:16:18 -0700835 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -0700836 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
837
838 atomic_inc(&sh->count);
839
Dan Williamsd6f38f32009-07-14 11:50:52 -0700840 init_async_submit(&submit, flags, tx, ops_complete_postxor, sh,
841 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700842 if (unlikely(count == 1))
843 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
844 else
845 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700846}
847
848static void ops_complete_check(void *stripe_head_ref)
849{
850 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700851
Harvey Harrisone46b2722008-04-28 02:15:50 -0700852 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700853 (unsigned long long)sh->sector);
854
Dan Williamsecc65c92008-06-28 08:31:57 +1000855 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700856 set_bit(STRIPE_HANDLE, &sh->state);
857 release_stripe(sh);
858}
859
Dan Williamsd6f38f32009-07-14 11:50:52 -0700860static void ops_run_check(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700861{
Dan Williams91c00922007-01-02 13:52:30 -0700862 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700863 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700864 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700865 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700866
867 int count = 0, pd_idx = sh->pd_idx, i;
868 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
869
Harvey Harrisone46b2722008-04-28 02:15:50 -0700870 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700871 (unsigned long long)sh->sector);
872
873 for (i = disks; i--; ) {
874 struct r5dev *dev = &sh->dev[i];
875 if (i != pd_idx)
876 xor_srcs[count++] = dev->page;
877 }
878
Dan Williamsd6f38f32009-07-14 11:50:52 -0700879 init_async_submit(&submit, 0, NULL, NULL, NULL,
880 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -0700881 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -0700882 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700883
Dan Williams91c00922007-01-02 13:52:30 -0700884 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700885 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
886 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700887}
888
Dan Williams600aa102008-06-28 08:32:05 +1000889static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700890{
891 int overlap_clear = 0, i, disks = sh->disks;
892 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700893 raid5_conf_t *conf = sh->raid_conf;
894 struct raid5_percpu *percpu;
895 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -0700896
Dan Williamsd6f38f32009-07-14 11:50:52 -0700897 cpu = get_cpu();
898 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +1000899 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700900 ops_run_biofill(sh);
901 overlap_clear++;
902 }
903
Dan Williams7b3a8712008-06-28 08:32:09 +1000904 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsd6f38f32009-07-14 11:50:52 -0700905 tx = ops_run_compute5(sh, percpu);
Dan Williams7b3a8712008-06-28 08:32:09 +1000906 /* terminate the chain if postxor is not set to be run */
907 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
908 async_tx_ack(tx);
909 }
Dan Williams91c00922007-01-02 13:52:30 -0700910
Dan Williams600aa102008-06-28 08:32:05 +1000911 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -0700912 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700913
Dan Williams600aa102008-06-28 08:32:05 +1000914 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000915 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700916 overlap_clear++;
917 }
918
Dan Williams600aa102008-06-28 08:32:05 +1000919 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -0700920 ops_run_postxor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700921
Dan Williamsecc65c92008-06-28 08:31:57 +1000922 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -0700923 ops_run_check(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -0700924
Dan Williams91c00922007-01-02 13:52:30 -0700925 if (overlap_clear)
926 for (i = disks; i--; ) {
927 struct r5dev *dev = &sh->dev[i];
928 if (test_and_clear_bit(R5_Overlap, &dev->flags))
929 wake_up(&sh->raid_conf->wait_for_overlap);
930 }
Dan Williamsd6f38f32009-07-14 11:50:52 -0700931 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -0700932}
933
NeilBrown3f294f42005-11-08 21:39:25 -0800934static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800937 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
938 if (!sh)
939 return 0;
940 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
941 sh->raid_conf = conf;
942 spin_lock_init(&sh->lock);
943
944 if (grow_buffers(sh, conf->raid_disks)) {
945 shrink_buffers(sh, conf->raid_disks);
946 kmem_cache_free(conf->slab_cache, sh);
947 return 0;
948 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800949 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800950 /* we just created an active stripe so... */
951 atomic_set(&sh->count, 1);
952 atomic_inc(&conf->active_stripes);
953 INIT_LIST_HEAD(&sh->lru);
954 release_stripe(sh);
955 return 1;
956}
957
958static int grow_stripes(raid5_conf_t *conf, int num)
959{
Christoph Lametere18b8902006-12-06 20:33:20 -0800960 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int devs = conf->raid_disks;
962
NeilBrown245f46c2009-03-31 14:39:39 +1100963 sprintf(conf->cache_name[0],
964 "raid%d-%s", conf->level, mdname(conf->mddev));
965 sprintf(conf->cache_name[1],
966 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800967 conf->active_name = 0;
968 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900970 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!sc)
972 return 1;
973 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800974 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700975 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800976 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979}
NeilBrown29269552006-03-27 01:18:10 -0800980
Dan Williamsd6f38f32009-07-14 11:50:52 -0700981/**
982 * scribble_len - return the required size of the scribble region
983 * @num - total number of disks in the array
984 *
985 * The size must be enough to contain:
986 * 1/ a struct page pointer for each device in the array +2
987 * 2/ room to convert each entry in (1) to its corresponding dma
988 * (dma_map_page()) or page (page_address()) address.
989 *
990 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
991 * calculate over all devices (not just the data blocks), using zeros in place
992 * of the P and Q blocks.
993 */
994static size_t scribble_len(int num)
995{
996 size_t len;
997
998 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
999
1000 return len;
1001}
1002
NeilBrownad01c9e2006-03-27 01:18:07 -08001003static int resize_stripes(raid5_conf_t *conf, int newsize)
1004{
1005 /* Make all the stripes able to hold 'newsize' devices.
1006 * New slots in each stripe get 'page' set to a new page.
1007 *
1008 * This happens in stages:
1009 * 1/ create a new kmem_cache and allocate the required number of
1010 * stripe_heads.
1011 * 2/ gather all the old stripe_heads and tranfer the pages across
1012 * to the new stripe_heads. This will have the side effect of
1013 * freezing the array as once all stripe_heads have been collected,
1014 * no IO will be possible. Old stripe heads are freed once their
1015 * pages have been transferred over, and the old kmem_cache is
1016 * freed when all stripes are done.
1017 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1018 * we simple return a failre status - no need to clean anything up.
1019 * 4/ allocate new pages for the new slots in the new stripe_heads.
1020 * If this fails, we don't bother trying the shrink the
1021 * stripe_heads down again, we just leave them as they are.
1022 * As each stripe_head is processed the new one is released into
1023 * active service.
1024 *
1025 * Once step2 is started, we cannot afford to wait for a write,
1026 * so we use GFP_NOIO allocations.
1027 */
1028 struct stripe_head *osh, *nsh;
1029 LIST_HEAD(newstripes);
1030 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001031 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001032 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001033 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001034 int i;
1035
1036 if (newsize <= conf->pool_size)
1037 return 0; /* never bother to shrink */
1038
Dan Williamsb5470dc2008-06-27 21:44:04 -07001039 err = md_allow_write(conf->mddev);
1040 if (err)
1041 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001042
NeilBrownad01c9e2006-03-27 01:18:07 -08001043 /* Step 1 */
1044 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1045 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001046 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001047 if (!sc)
1048 return -ENOMEM;
1049
1050 for (i = conf->max_nr_stripes; i; i--) {
1051 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1052 if (!nsh)
1053 break;
1054
1055 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1056
1057 nsh->raid_conf = conf;
1058 spin_lock_init(&nsh->lock);
1059
1060 list_add(&nsh->lru, &newstripes);
1061 }
1062 if (i) {
1063 /* didn't get enough, give up */
1064 while (!list_empty(&newstripes)) {
1065 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1066 list_del(&nsh->lru);
1067 kmem_cache_free(sc, nsh);
1068 }
1069 kmem_cache_destroy(sc);
1070 return -ENOMEM;
1071 }
1072 /* Step 2 - Must use GFP_NOIO now.
1073 * OK, we have enough stripes, start collecting inactive
1074 * stripes and copying them over
1075 */
1076 list_for_each_entry(nsh, &newstripes, lru) {
1077 spin_lock_irq(&conf->device_lock);
1078 wait_event_lock_irq(conf->wait_for_stripe,
1079 !list_empty(&conf->inactive_list),
1080 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001081 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001082 );
1083 osh = get_free_stripe(conf);
1084 spin_unlock_irq(&conf->device_lock);
1085 atomic_set(&nsh->count, 1);
1086 for(i=0; i<conf->pool_size; i++)
1087 nsh->dev[i].page = osh->dev[i].page;
1088 for( ; i<newsize; i++)
1089 nsh->dev[i].page = NULL;
1090 kmem_cache_free(conf->slab_cache, osh);
1091 }
1092 kmem_cache_destroy(conf->slab_cache);
1093
1094 /* Step 3.
1095 * At this point, we are holding all the stripes so the array
1096 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001097 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001098 */
1099 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1100 if (ndisks) {
1101 for (i=0; i<conf->raid_disks; i++)
1102 ndisks[i] = conf->disks[i];
1103 kfree(conf->disks);
1104 conf->disks = ndisks;
1105 } else
1106 err = -ENOMEM;
1107
Dan Williamsd6f38f32009-07-14 11:50:52 -07001108 get_online_cpus();
1109 conf->scribble_len = scribble_len(newsize);
1110 for_each_present_cpu(cpu) {
1111 struct raid5_percpu *percpu;
1112 void *scribble;
1113
1114 percpu = per_cpu_ptr(conf->percpu, cpu);
1115 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1116
1117 if (scribble) {
1118 kfree(percpu->scribble);
1119 percpu->scribble = scribble;
1120 } else {
1121 err = -ENOMEM;
1122 break;
1123 }
1124 }
1125 put_online_cpus();
1126
NeilBrownad01c9e2006-03-27 01:18:07 -08001127 /* Step 4, return new stripes to service */
1128 while(!list_empty(&newstripes)) {
1129 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1130 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001131
NeilBrownad01c9e2006-03-27 01:18:07 -08001132 for (i=conf->raid_disks; i < newsize; i++)
1133 if (nsh->dev[i].page == NULL) {
1134 struct page *p = alloc_page(GFP_NOIO);
1135 nsh->dev[i].page = p;
1136 if (!p)
1137 err = -ENOMEM;
1138 }
1139 release_stripe(nsh);
1140 }
1141 /* critical section pass, GFP_NOIO no longer needed */
1142
1143 conf->slab_cache = sc;
1144 conf->active_name = 1-conf->active_name;
1145 conf->pool_size = newsize;
1146 return err;
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
NeilBrown3f294f42005-11-08 21:39:25 -08001149static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct stripe_head *sh;
1152
NeilBrown3f294f42005-11-08 21:39:25 -08001153 spin_lock_irq(&conf->device_lock);
1154 sh = get_free_stripe(conf);
1155 spin_unlock_irq(&conf->device_lock);
1156 if (!sh)
1157 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001158 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001159 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001160 kmem_cache_free(conf->slab_cache, sh);
1161 atomic_dec(&conf->active_stripes);
1162 return 1;
1163}
1164
1165static void shrink_stripes(raid5_conf_t *conf)
1166{
1167 while (drop_one_stripe(conf))
1168 ;
1169
NeilBrown29fc7e32006-02-03 03:03:41 -08001170 if (conf->slab_cache)
1171 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 conf->slab_cache = NULL;
1173}
1174
NeilBrown6712ecf2007-09-27 12:47:43 +02001175static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
NeilBrown99c0fb52009-03-31 14:39:38 +11001177 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001179 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001181 char b[BDEVNAME_SIZE];
1182 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 for (i=0 ; i<disks; i++)
1186 if (bi == &sh->dev[i].req)
1187 break;
1188
Dan Williams45b42332007-07-09 11:56:43 -07001189 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1190 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 uptodate);
1192 if (i == disks) {
1193 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001199 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001200 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001201 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1202 " (%lu sectors at %llu on %s)\n",
1203 mdname(conf->mddev), STRIPE_SECTORS,
1204 (unsigned long long)(sh->sector
1205 + rdev->data_offset),
1206 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001207 clear_bit(R5_ReadError, &sh->dev[i].flags);
1208 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1209 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001210 if (atomic_read(&conf->disks[i].rdev->read_errors))
1211 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001213 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001214 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001215 rdev = conf->disks[i].rdev;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001218 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001219 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001220 printk_rl(KERN_WARNING
1221 "raid5:%s: read error not correctable "
1222 "(sector %llu on %s).\n",
1223 mdname(conf->mddev),
1224 (unsigned long long)(sh->sector
1225 + rdev->data_offset),
1226 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001227 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001228 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001229 printk_rl(KERN_WARNING
1230 "raid5:%s: read error NOT corrected!! "
1231 "(sector %llu on %s).\n",
1232 mdname(conf->mddev),
1233 (unsigned long long)(sh->sector
1234 + rdev->data_offset),
1235 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001236 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001237 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001238 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001239 "raid5:%s: Too many read errors, failing device %s.\n",
1240 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001241 else
1242 retry = 1;
1243 if (retry)
1244 set_bit(R5_ReadError, &sh->dev[i].flags);
1245 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001246 clear_bit(R5_ReadError, &sh->dev[i].flags);
1247 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001248 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1253 set_bit(STRIPE_HANDLE, &sh->state);
1254 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
NeilBrownd710e132008-10-13 11:55:12 +11001257static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
NeilBrown99c0fb52009-03-31 14:39:38 +11001259 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001261 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 for (i=0 ; i<disks; i++)
1265 if (bi == &sh->dev[i].req)
1266 break;
1267
Dan Williams45b42332007-07-09 11:56:43 -07001268 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1270 uptodate);
1271 if (i == disks) {
1272 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!uptodate)
1277 md_error(conf->mddev, conf->disks[i].rdev);
1278
1279 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1280
1281 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1282 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001283 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286
NeilBrown784052e2009-03-31 15:19:07 +11001287static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
NeilBrown784052e2009-03-31 15:19:07 +11001289static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct r5dev *dev = &sh->dev[i];
1292
1293 bio_init(&dev->req);
1294 dev->req.bi_io_vec = &dev->vec;
1295 dev->req.bi_vcnt++;
1296 dev->req.bi_max_vecs++;
1297 dev->vec.bv_page = dev->page;
1298 dev->vec.bv_len = STRIPE_SIZE;
1299 dev->vec.bv_offset = 0;
1300
1301 dev->req.bi_sector = sh->sector;
1302 dev->req.bi_private = sh;
1303
1304 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001305 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1309{
1310 char b[BDEVNAME_SIZE];
1311 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001312 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
NeilBrownb2d444d2005-11-08 21:39:31 -08001314 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001315 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001316 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1317 unsigned long flags;
1318 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001320 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * if recovery was running, make sure it aborts.
1323 */
NeilBrowndfc70642008-05-23 13:04:39 -07001324 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001326 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001327 printk(KERN_ALERT
1328 "raid5: Disk failure on %s, disabling device.\n"
1329 "raid5: Operation continuing on %d devices.\n",
1330 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334/*
1335 * Input: a 'big' sector number,
1336 * Output: index of the data and parity disk, and the sector # in them.
1337 */
NeilBrown112bf892009-03-31 14:39:38 +11001338static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001339 int previous, int *dd_idx,
1340 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 long stripe;
1343 unsigned long chunk_number;
1344 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001345 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001346 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001348 int algorithm = previous ? conf->prev_algo
1349 : conf->algorithm;
NeilBrown784052e2009-03-31 15:19:07 +11001350 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1351 : (conf->chunk_size >> 9);
NeilBrown112bf892009-03-31 14:39:38 +11001352 int raid_disks = previous ? conf->previous_raid_disks
1353 : conf->raid_disks;
1354 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* First compute the information on this sector */
1357
1358 /*
1359 * Compute the chunk number and the sector offset inside the chunk
1360 */
1361 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1362 chunk_number = r_sector;
1363 BUG_ON(r_sector != chunk_number);
1364
1365 /*
1366 * Compute the stripe number
1367 */
1368 stripe = chunk_number / data_disks;
1369
1370 /*
1371 * Compute the data disk and parity disk indexes inside the stripe
1372 */
1373 *dd_idx = chunk_number % data_disks;
1374
1375 /*
1376 * Select the parity disk based on the user selected algorithm.
1377 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001378 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001379 switch(conf->level) {
1380 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001381 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001382 break;
1383 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001384 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001386 pd_idx = data_disks - stripe % raid_disks;
1387 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 (*dd_idx)++;
1389 break;
1390 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001391 pd_idx = stripe % raid_disks;
1392 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 (*dd_idx)++;
1394 break;
1395 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001396 pd_idx = data_disks - stripe % raid_disks;
1397 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001400 pd_idx = stripe % raid_disks;
1401 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001403 case ALGORITHM_PARITY_0:
1404 pd_idx = 0;
1405 (*dd_idx)++;
1406 break;
1407 case ALGORITHM_PARITY_N:
1408 pd_idx = data_disks;
1409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001411 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001412 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001413 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001414 }
1415 break;
1416 case 6:
1417
NeilBrowne183eae2009-03-31 15:20:22 +11001418 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001419 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001420 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1421 qd_idx = pd_idx + 1;
1422 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001423 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001424 qd_idx = 0;
1425 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001426 (*dd_idx) += 2; /* D D P Q D */
1427 break;
1428 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001429 pd_idx = stripe % raid_disks;
1430 qd_idx = pd_idx + 1;
1431 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001432 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001433 qd_idx = 0;
1434 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001435 (*dd_idx) += 2; /* D D P Q D */
1436 break;
1437 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001438 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1439 qd_idx = (pd_idx + 1) % raid_disks;
1440 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001441 break;
1442 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001443 pd_idx = stripe % raid_disks;
1444 qd_idx = (pd_idx + 1) % raid_disks;
1445 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001446 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001447
1448 case ALGORITHM_PARITY_0:
1449 pd_idx = 0;
1450 qd_idx = 1;
1451 (*dd_idx) += 2;
1452 break;
1453 case ALGORITHM_PARITY_N:
1454 pd_idx = data_disks;
1455 qd_idx = data_disks + 1;
1456 break;
1457
1458 case ALGORITHM_ROTATING_ZERO_RESTART:
1459 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1460 * of blocks for computing Q is different.
1461 */
1462 pd_idx = stripe % raid_disks;
1463 qd_idx = pd_idx + 1;
1464 if (pd_idx == raid_disks-1) {
1465 (*dd_idx)++; /* Q D D D P */
1466 qd_idx = 0;
1467 } else if (*dd_idx >= pd_idx)
1468 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001469 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001470 break;
1471
1472 case ALGORITHM_ROTATING_N_RESTART:
1473 /* Same a left_asymmetric, by first stripe is
1474 * D D D P Q rather than
1475 * Q D D D P
1476 */
1477 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1478 qd_idx = pd_idx + 1;
1479 if (pd_idx == raid_disks-1) {
1480 (*dd_idx)++; /* Q D D D P */
1481 qd_idx = 0;
1482 } else if (*dd_idx >= pd_idx)
1483 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001484 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001485 break;
1486
1487 case ALGORITHM_ROTATING_N_CONTINUE:
1488 /* Same as left_symmetric but Q is before P */
1489 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1490 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1491 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001492 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001493 break;
1494
1495 case ALGORITHM_LEFT_ASYMMETRIC_6:
1496 /* RAID5 left_asymmetric, with Q on last device */
1497 pd_idx = data_disks - stripe % (raid_disks-1);
1498 if (*dd_idx >= pd_idx)
1499 (*dd_idx)++;
1500 qd_idx = raid_disks - 1;
1501 break;
1502
1503 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1504 pd_idx = stripe % (raid_disks-1);
1505 if (*dd_idx >= pd_idx)
1506 (*dd_idx)++;
1507 qd_idx = raid_disks - 1;
1508 break;
1509
1510 case ALGORITHM_LEFT_SYMMETRIC_6:
1511 pd_idx = data_disks - stripe % (raid_disks-1);
1512 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1513 qd_idx = raid_disks - 1;
1514 break;
1515
1516 case ALGORITHM_RIGHT_SYMMETRIC_6:
1517 pd_idx = stripe % (raid_disks-1);
1518 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1519 qd_idx = raid_disks - 1;
1520 break;
1521
1522 case ALGORITHM_PARITY_0_6:
1523 pd_idx = 0;
1524 (*dd_idx)++;
1525 qd_idx = raid_disks - 1;
1526 break;
1527
1528
NeilBrown16a53ec2006-06-26 00:27:38 -07001529 default:
NeilBrownd710e132008-10-13 11:55:12 +11001530 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001531 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001532 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001533 }
1534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
NeilBrown911d4ee2009-03-31 14:39:38 +11001537 if (sh) {
1538 sh->pd_idx = pd_idx;
1539 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001540 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Finally, compute the new sector number
1544 */
1545 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1546 return new_sector;
1547}
1548
1549
NeilBrown784052e2009-03-31 15:19:07 +11001550static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001553 int raid_disks = sh->disks;
1554 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sector_t new_sector = sh->sector, check;
NeilBrown784052e2009-03-31 15:19:07 +11001556 int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
1557 : (conf->chunk_size >> 9);
NeilBrowne183eae2009-03-31 15:20:22 +11001558 int algorithm = previous ? conf->prev_algo
1559 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 sector_t stripe;
1561 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001562 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001564 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
NeilBrown16a53ec2006-06-26 00:27:38 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1568 stripe = new_sector;
1569 BUG_ON(new_sector != stripe);
1570
NeilBrown16a53ec2006-06-26 00:27:38 -07001571 if (i == sh->pd_idx)
1572 return 0;
1573 switch(conf->level) {
1574 case 4: break;
1575 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001576 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 case ALGORITHM_LEFT_ASYMMETRIC:
1578 case ALGORITHM_RIGHT_ASYMMETRIC:
1579 if (i > sh->pd_idx)
1580 i--;
1581 break;
1582 case ALGORITHM_LEFT_SYMMETRIC:
1583 case ALGORITHM_RIGHT_SYMMETRIC:
1584 if (i < sh->pd_idx)
1585 i += raid_disks;
1586 i -= (sh->pd_idx + 1);
1587 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001588 case ALGORITHM_PARITY_0:
1589 i -= 1;
1590 break;
1591 case ALGORITHM_PARITY_N:
1592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001594 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001595 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001596 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001597 }
1598 break;
1599 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001600 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001601 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001602 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001603 case ALGORITHM_LEFT_ASYMMETRIC:
1604 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001605 case ALGORITHM_ROTATING_ZERO_RESTART:
1606 case ALGORITHM_ROTATING_N_RESTART:
1607 if (sh->pd_idx == raid_disks-1)
1608 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001609 else if (i > sh->pd_idx)
1610 i -= 2; /* D D P Q D */
1611 break;
1612 case ALGORITHM_LEFT_SYMMETRIC:
1613 case ALGORITHM_RIGHT_SYMMETRIC:
1614 if (sh->pd_idx == raid_disks-1)
1615 i--; /* Q D D D P */
1616 else {
1617 /* D D P Q D */
1618 if (i < sh->pd_idx)
1619 i += raid_disks;
1620 i -= (sh->pd_idx + 2);
1621 }
1622 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001623 case ALGORITHM_PARITY_0:
1624 i -= 2;
1625 break;
1626 case ALGORITHM_PARITY_N:
1627 break;
1628 case ALGORITHM_ROTATING_N_CONTINUE:
1629 if (sh->pd_idx == 0)
1630 i--; /* P D D D Q */
1631 else if (i > sh->pd_idx)
1632 i -= 2; /* D D Q P D */
1633 break;
1634 case ALGORITHM_LEFT_ASYMMETRIC_6:
1635 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1636 if (i > sh->pd_idx)
1637 i--;
1638 break;
1639 case ALGORITHM_LEFT_SYMMETRIC_6:
1640 case ALGORITHM_RIGHT_SYMMETRIC_6:
1641 if (i < sh->pd_idx)
1642 i += data_disks + 1;
1643 i -= (sh->pd_idx + 1);
1644 break;
1645 case ALGORITHM_PARITY_0_6:
1646 i -= 1;
1647 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001648 default:
NeilBrownd710e132008-10-13 11:55:12 +11001649 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001650 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001651 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001652 }
1653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
1656 chunk_number = stripe * data_disks + i;
1657 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1658
NeilBrown112bf892009-03-31 14:39:38 +11001659 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001660 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001661 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1662 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001663 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return 0;
1665 }
1666 return r_sector;
1667}
1668
1669
1670
1671/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001672 * Copy data between a page in the stripe cache, and one or more bion
1673 * The page could align with the middle of the bio, or there could be
1674 * several bion, each with several bio_vecs, which cover part of the page
1675 * Multiple bion are linked together on bi_next. There may be extras
1676 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 */
1678static void copy_data(int frombio, struct bio *bio,
1679 struct page *page,
1680 sector_t sector)
1681{
1682 char *pa = page_address(page);
1683 struct bio_vec *bvl;
1684 int i;
1685 int page_offset;
1686
1687 if (bio->bi_sector >= sector)
1688 page_offset = (signed)(bio->bi_sector - sector) * 512;
1689 else
1690 page_offset = (signed)(sector - bio->bi_sector) * -512;
1691 bio_for_each_segment(bvl, bio, i) {
1692 int len = bio_iovec_idx(bio,i)->bv_len;
1693 int clen;
1694 int b_offset = 0;
1695
1696 if (page_offset < 0) {
1697 b_offset = -page_offset;
1698 page_offset += b_offset;
1699 len -= b_offset;
1700 }
1701
1702 if (len > 0 && page_offset + len > STRIPE_SIZE)
1703 clen = STRIPE_SIZE - page_offset;
1704 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (clen > 0) {
1707 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1708 if (frombio)
1709 memcpy(pa+page_offset, ba+b_offset, clen);
1710 else
1711 memcpy(ba+b_offset, pa+page_offset, clen);
1712 __bio_kunmap_atomic(ba, KM_USER0);
1713 }
1714 if (clen < len) /* hit end of page */
1715 break;
1716 page_offset += len;
1717 }
1718}
1719
Dan Williams9bc89cd2007-01-02 11:10:44 -07001720#define check_xor() do { \
1721 if (count == MAX_XOR_BLOCKS) { \
1722 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1723 count = 0; \
1724 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 } while(0)
1726
NeilBrown16a53ec2006-06-26 00:27:38 -07001727static void compute_parity6(struct stripe_head *sh, int method)
1728{
NeilBrownbff61972009-03-31 14:33:13 +11001729 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001730 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown67cc2b82009-03-31 14:39:38 +11001731 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
NeilBrown16a53ec2006-06-26 00:27:38 -07001732 struct bio *chosen;
1733 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001734 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001735
NeilBrownd0dabf72009-03-31 14:39:38 +11001736 pd_idx = sh->pd_idx;
1737 qd_idx = sh->qd_idx;
1738 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001739
Dan Williams45b42332007-07-09 11:56:43 -07001740 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001741 (unsigned long long)sh->sector, method);
1742
1743 switch(method) {
1744 case READ_MODIFY_WRITE:
1745 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1746 case RECONSTRUCT_WRITE:
1747 for (i= disks; i-- ;)
1748 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1749 chosen = sh->dev[i].towrite;
1750 sh->dev[i].towrite = NULL;
1751
1752 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1753 wake_up(&conf->wait_for_overlap);
1754
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001755 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001756 sh->dev[i].written = chosen;
1757 }
1758 break;
1759 case CHECK_PARITY:
1760 BUG(); /* Not implemented yet */
1761 }
1762
1763 for (i = disks; i--;)
1764 if (sh->dev[i].written) {
1765 sector_t sector = sh->dev[i].sector;
1766 struct bio *wbi = sh->dev[i].written;
1767 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1768 copy_data(1, wbi, sh->dev[i].page, sector);
1769 wbi = r5_next_bio(wbi, sector);
1770 }
1771
1772 set_bit(R5_LOCKED, &sh->dev[i].flags);
1773 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1774 }
1775
NeilBrownd0dabf72009-03-31 14:39:38 +11001776 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
NeilBrown67cc2b82009-03-31 14:39:38 +11001777
1778 for (i = 0; i < disks; i++)
1779 ptrs[i] = (void *)raid6_empty_zero_page;
1780
NeilBrownd0dabf72009-03-31 14:39:38 +11001781 count = 0;
1782 i = d0_idx;
1783 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001784 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1785
NeilBrownd0dabf72009-03-31 14:39:38 +11001786 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001787 if (slot < syndrome_disks &&
NeilBrownd0dabf72009-03-31 14:39:38 +11001788 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1789 printk(KERN_ERR "block %d/%d not uptodate "
1790 "on parity calc\n", i, count);
1791 BUG();
1792 }
NeilBrown67cc2b82009-03-31 14:39:38 +11001793
NeilBrownd0dabf72009-03-31 14:39:38 +11001794 i = raid6_next_disk(i, disks);
1795 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001796 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001797
NeilBrown67cc2b82009-03-31 14:39:38 +11001798 raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001799
1800 switch(method) {
1801 case RECONSTRUCT_WRITE:
1802 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1803 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1804 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1805 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1806 break;
1807 case UPDATE_PARITY:
1808 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1809 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1810 break;
1811 }
1812}
1813
1814
1815/* Compute one missing block */
1816static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1817{
NeilBrownf4168852007-02-28 20:11:53 -08001818 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001819 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11001820 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07001821
Dan Williams45b42332007-07-09 11:56:43 -07001822 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001823 (unsigned long long)sh->sector, dd_idx);
1824
1825 if ( dd_idx == qd_idx ) {
1826 /* We're actually computing the Q drive */
1827 compute_parity6(sh, UPDATE_PARITY);
1828 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001829 dest = page_address(sh->dev[dd_idx].page);
1830 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1831 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001832 for (i = disks ; i--; ) {
1833 if (i == dd_idx || i == qd_idx)
1834 continue;
1835 p = page_address(sh->dev[i].page);
1836 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1837 ptr[count++] = p;
1838 else
1839 printk("compute_block() %d, stripe %llu, %d"
1840 " not present\n", dd_idx,
1841 (unsigned long long)sh->sector, i);
1842
1843 check_xor();
1844 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001845 if (count)
1846 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001847 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1848 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1849 }
1850}
1851
1852/* Compute two missing blocks */
1853static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1854{
NeilBrownf4168852007-02-28 20:11:53 -08001855 int i, count, disks = sh->disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001856 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
NeilBrownd0dabf72009-03-31 14:39:38 +11001857 int d0_idx = raid6_d0(sh);
1858 int faila = -1, failb = -1;
1859 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001860 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001861
NeilBrown67cc2b82009-03-31 14:39:38 +11001862 for (i = 0; i < disks ; i++)
1863 ptrs[i] = (void *)raid6_empty_zero_page;
NeilBrownd0dabf72009-03-31 14:39:38 +11001864 count = 0;
1865 i = d0_idx;
1866 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001867 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1868
NeilBrownd0dabf72009-03-31 14:39:38 +11001869 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001870
NeilBrownd0dabf72009-03-31 14:39:38 +11001871 if (i == dd_idx1)
1872 faila = slot;
1873 if (i == dd_idx2)
1874 failb = slot;
1875 i = raid6_next_disk(i, disks);
1876 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001877 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001878
1879 BUG_ON(faila == failb);
1880 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1881
Dan Williams45b42332007-07-09 11:56:43 -07001882 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11001883 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
1884 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07001885
NeilBrown67cc2b82009-03-31 14:39:38 +11001886 if (failb == syndrome_disks+1) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001887 /* Q disk is one of the missing disks */
NeilBrown67cc2b82009-03-31 14:39:38 +11001888 if (faila == syndrome_disks) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001889 /* Missing P+Q, just recompute */
1890 compute_parity6(sh, UPDATE_PARITY);
1891 return;
1892 } else {
1893 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11001894 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
1895 dd_idx2 : dd_idx1),
1896 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07001897 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1898 return;
1899 }
1900 }
1901
NeilBrownd0dabf72009-03-31 14:39:38 +11001902 /* We're missing D+P or D+D; */
NeilBrown67cc2b82009-03-31 14:39:38 +11001903 if (failb == syndrome_disks) {
NeilBrownd0dabf72009-03-31 14:39:38 +11001904 /* We're missing D+P. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001905 raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
NeilBrownd0dabf72009-03-31 14:39:38 +11001906 } else {
1907 /* We're missing D+D. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001908 raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
1909 ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001910 }
NeilBrownd0dabf72009-03-31 14:39:38 +11001911
1912 /* Both the above update both missing blocks */
1913 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1914 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07001915}
1916
Dan Williams600aa102008-06-28 08:32:05 +10001917static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001918schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001919 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001920{
1921 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001922
Dan Williamse33129d2007-01-02 13:52:30 -07001923 if (rcw) {
1924 /* if we are not expanding this is a proper write request, and
1925 * there will be bios with new data to be drained into the
1926 * stripe cache
1927 */
1928 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001929 sh->reconstruct_state = reconstruct_state_drain_run;
1930 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1931 } else
1932 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001933
Dan Williams600aa102008-06-28 08:32:05 +10001934 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001935
1936 for (i = disks; i--; ) {
1937 struct r5dev *dev = &sh->dev[i];
1938
1939 if (dev->towrite) {
1940 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001941 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001942 if (!expand)
1943 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001944 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001945 }
1946 }
Dan Williams600aa102008-06-28 08:32:05 +10001947 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001948 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1949 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001950 } else {
1951 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1952 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1953
Dan Williamsd8ee0722008-06-28 08:32:06 +10001954 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001955 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1956 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1957 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001958
1959 for (i = disks; i--; ) {
1960 struct r5dev *dev = &sh->dev[i];
1961 if (i == pd_idx)
1962 continue;
1963
Dan Williamse33129d2007-01-02 13:52:30 -07001964 if (dev->towrite &&
1965 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001966 test_bit(R5_Wantcompute, &dev->flags))) {
1967 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001968 set_bit(R5_LOCKED, &dev->flags);
1969 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001970 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001971 }
1972 }
1973 }
1974
1975 /* keep the parity disk locked while asynchronous operations
1976 * are in flight
1977 */
1978 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1979 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001980 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001981
Dan Williams600aa102008-06-28 08:32:05 +10001982 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001983 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001984 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001985}
NeilBrown16a53ec2006-06-26 00:27:38 -07001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987/*
1988 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001989 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 * The bi_next chain must be in order.
1991 */
1992static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1993{
1994 struct bio **bip;
1995 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001996 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Dan Williams45b42332007-07-09 11:56:43 -07001998 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 (unsigned long long)bi->bi_sector,
2000 (unsigned long long)sh->sector);
2001
2002
2003 spin_lock(&sh->lock);
2004 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002005 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002007 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2008 firstwrite = 1;
2009 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 bip = &sh->dev[dd_idx].toread;
2011 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2012 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2013 goto overlap;
2014 bip = & (*bip)->bi_next;
2015 }
2016 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2017 goto overlap;
2018
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002019 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (*bip)
2021 bi->bi_next = *bip;
2022 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002023 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 spin_unlock_irq(&conf->device_lock);
2025 spin_unlock(&sh->lock);
2026
Dan Williams45b42332007-07-09 11:56:43 -07002027 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 (unsigned long long)bi->bi_sector,
2029 (unsigned long long)sh->sector, dd_idx);
2030
NeilBrown72626682005-09-09 16:23:54 -07002031 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002032 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2033 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002034 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002035 set_bit(STRIPE_BIT_DELAY, &sh->state);
2036 }
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (forwrite) {
2039 /* check if page is covered */
2040 sector_t sector = sh->dev[dd_idx].sector;
2041 for (bi=sh->dev[dd_idx].towrite;
2042 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2043 bi && bi->bi_sector <= sector;
2044 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2045 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2046 sector = bi->bi_sector + (bi->bi_size>>9);
2047 }
2048 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2049 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2050 }
2051 return 1;
2052
2053 overlap:
2054 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2055 spin_unlock_irq(&conf->device_lock);
2056 spin_unlock(&sh->lock);
2057 return 0;
2058}
2059
NeilBrown29269552006-03-27 01:18:10 -08002060static void end_reshape(raid5_conf_t *conf);
2061
NeilBrown16a53ec2006-06-26 00:27:38 -07002062static int page_is_zero(struct page *p)
2063{
2064 char *a = page_address(p);
2065 return ((*(u32*)a) == 0 &&
2066 memcmp(a, a+4, STRIPE_SIZE-4)==0);
2067}
2068
NeilBrown911d4ee2009-03-31 14:39:38 +11002069static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2070 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002071{
NeilBrown784052e2009-03-31 15:19:07 +11002072 int sectors_per_chunk =
2073 previous ? (conf->prev_chunk >> 9)
2074 : (conf->chunk_size >> 9);
NeilBrown911d4ee2009-03-31 14:39:38 +11002075 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002076 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002077 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002078
NeilBrown112bf892009-03-31 14:39:38 +11002079 raid5_compute_sector(conf,
2080 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002081 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002082 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002083 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002084}
2085
Dan Williamsa4456852007-07-09 11:56:43 -07002086static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002087handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002088 struct stripe_head_state *s, int disks,
2089 struct bio **return_bi)
2090{
2091 int i;
2092 for (i = disks; i--; ) {
2093 struct bio *bi;
2094 int bitmap_end = 0;
2095
2096 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2097 mdk_rdev_t *rdev;
2098 rcu_read_lock();
2099 rdev = rcu_dereference(conf->disks[i].rdev);
2100 if (rdev && test_bit(In_sync, &rdev->flags))
2101 /* multiple read failures in one stripe */
2102 md_error(conf->mddev, rdev);
2103 rcu_read_unlock();
2104 }
2105 spin_lock_irq(&conf->device_lock);
2106 /* fail all writes first */
2107 bi = sh->dev[i].towrite;
2108 sh->dev[i].towrite = NULL;
2109 if (bi) {
2110 s->to_write--;
2111 bitmap_end = 1;
2112 }
2113
2114 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2115 wake_up(&conf->wait_for_overlap);
2116
2117 while (bi && bi->bi_sector <
2118 sh->dev[i].sector + STRIPE_SECTORS) {
2119 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2120 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002121 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002122 md_write_end(conf->mddev);
2123 bi->bi_next = *return_bi;
2124 *return_bi = bi;
2125 }
2126 bi = nextbi;
2127 }
2128 /* and fail all 'written' */
2129 bi = sh->dev[i].written;
2130 sh->dev[i].written = NULL;
2131 if (bi) bitmap_end = 1;
2132 while (bi && bi->bi_sector <
2133 sh->dev[i].sector + STRIPE_SECTORS) {
2134 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2135 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002136 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002137 md_write_end(conf->mddev);
2138 bi->bi_next = *return_bi;
2139 *return_bi = bi;
2140 }
2141 bi = bi2;
2142 }
2143
Dan Williamsb5e98d62007-01-02 13:52:31 -07002144 /* fail any reads if this device is non-operational and
2145 * the data has not reached the cache yet.
2146 */
2147 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2148 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2149 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002150 bi = sh->dev[i].toread;
2151 sh->dev[i].toread = NULL;
2152 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2153 wake_up(&conf->wait_for_overlap);
2154 if (bi) s->to_read--;
2155 while (bi && bi->bi_sector <
2156 sh->dev[i].sector + STRIPE_SECTORS) {
2157 struct bio *nextbi =
2158 r5_next_bio(bi, sh->dev[i].sector);
2159 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002160 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002161 bi->bi_next = *return_bi;
2162 *return_bi = bi;
2163 }
2164 bi = nextbi;
2165 }
2166 }
2167 spin_unlock_irq(&conf->device_lock);
2168 if (bitmap_end)
2169 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2170 STRIPE_SECTORS, 0, 0);
2171 }
2172
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002173 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2174 if (atomic_dec_and_test(&conf->pending_full_writes))
2175 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002176}
2177
Dan Williams1fe797e2008-06-28 09:16:30 +10002178/* fetch_block5 - checks the given member device to see if its data needs
2179 * to be read or computed to satisfy a request.
2180 *
2181 * Returns 1 when no more member devices need to be checked, otherwise returns
2182 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002183 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002184static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2185 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002186{
2187 struct r5dev *dev = &sh->dev[disk_idx];
2188 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2189
Dan Williamsf38e1212007-01-02 13:52:30 -07002190 /* is the data in this block needed, and can we get it? */
2191 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002192 !test_bit(R5_UPTODATE, &dev->flags) &&
2193 (dev->toread ||
2194 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2195 s->syncing || s->expanding ||
2196 (s->failed &&
2197 (failed_dev->toread ||
2198 (failed_dev->towrite &&
2199 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002200 /* We would like to get this block, possibly by computing it,
2201 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002202 */
2203 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002204 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002205 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2206 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002207 set_bit(R5_Wantcompute, &dev->flags);
2208 sh->ops.target = disk_idx;
2209 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002210 /* Careful: from this point on 'uptodate' is in the eye
2211 * of raid5_run_ops which services 'compute' operations
2212 * before writes. R5_Wantcompute flags a block that will
2213 * be R5_UPTODATE by the time it is needed for a
2214 * subsequent operation.
2215 */
2216 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002217 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002218 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002219 set_bit(R5_LOCKED, &dev->flags);
2220 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002221 s->locked++;
2222 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2223 s->syncing);
2224 }
2225 }
2226
Dan Williams1fe797e2008-06-28 09:16:30 +10002227 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002228}
2229
Dan Williams1fe797e2008-06-28 09:16:30 +10002230/**
2231 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2232 */
2233static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002234 struct stripe_head_state *s, int disks)
2235{
2236 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002237
Dan Williamsf38e1212007-01-02 13:52:30 -07002238 /* look for blocks to read/compute, skip this if a compute
2239 * is already in flight, or if the stripe contents are in the
2240 * midst of changing due to a write
2241 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002242 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002243 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002244 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002245 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002246 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002247 set_bit(STRIPE_HANDLE, &sh->state);
2248}
2249
Dan Williams1fe797e2008-06-28 09:16:30 +10002250static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002251 struct stripe_head_state *s, struct r6_state *r6s,
2252 int disks)
2253{
2254 int i;
2255 for (i = disks; i--; ) {
2256 struct r5dev *dev = &sh->dev[i];
2257 if (!test_bit(R5_LOCKED, &dev->flags) &&
2258 !test_bit(R5_UPTODATE, &dev->flags) &&
2259 (dev->toread || (dev->towrite &&
2260 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2261 s->syncing || s->expanding ||
2262 (s->failed >= 1 &&
2263 (sh->dev[r6s->failed_num[0]].toread ||
2264 s->to_write)) ||
2265 (s->failed >= 2 &&
2266 (sh->dev[r6s->failed_num[1]].toread ||
2267 s->to_write)))) {
2268 /* we would like to get this block, possibly
2269 * by computing it, but we might not be able to
2270 */
Dan Williamsc3378692008-06-05 22:45:54 -07002271 if ((s->uptodate == disks - 1) &&
2272 (s->failed && (i == r6s->failed_num[0] ||
2273 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07002274 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002275 (unsigned long long)sh->sector, i);
2276 compute_block_1(sh, i, 0);
2277 s->uptodate++;
2278 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2279 /* Computing 2-failure is *very* expensive; only
2280 * do it if failed >= 2
2281 */
2282 int other;
2283 for (other = disks; other--; ) {
2284 if (other == i)
2285 continue;
2286 if (!test_bit(R5_UPTODATE,
2287 &sh->dev[other].flags))
2288 break;
2289 }
2290 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07002291 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002292 (unsigned long long)sh->sector,
2293 i, other);
2294 compute_block_2(sh, i, other);
2295 s->uptodate += 2;
2296 } else if (test_bit(R5_Insync, &dev->flags)) {
2297 set_bit(R5_LOCKED, &dev->flags);
2298 set_bit(R5_Wantread, &dev->flags);
2299 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002300 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002301 i, s->syncing);
2302 }
2303 }
2304 }
2305 set_bit(STRIPE_HANDLE, &sh->state);
2306}
2307
2308
Dan Williams1fe797e2008-06-28 09:16:30 +10002309/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002310 * any written block on an uptodate or failed drive can be returned.
2311 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2312 * never LOCKED, so we don't need to test 'failed' directly.
2313 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002314static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002315 struct stripe_head *sh, int disks, struct bio **return_bi)
2316{
2317 int i;
2318 struct r5dev *dev;
2319
2320 for (i = disks; i--; )
2321 if (sh->dev[i].written) {
2322 dev = &sh->dev[i];
2323 if (!test_bit(R5_LOCKED, &dev->flags) &&
2324 test_bit(R5_UPTODATE, &dev->flags)) {
2325 /* We can return any write requests */
2326 struct bio *wbi, *wbi2;
2327 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002328 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002329 spin_lock_irq(&conf->device_lock);
2330 wbi = dev->written;
2331 dev->written = NULL;
2332 while (wbi && wbi->bi_sector <
2333 dev->sector + STRIPE_SECTORS) {
2334 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002335 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002336 md_write_end(conf->mddev);
2337 wbi->bi_next = *return_bi;
2338 *return_bi = wbi;
2339 }
2340 wbi = wbi2;
2341 }
2342 if (dev->towrite == NULL)
2343 bitmap_end = 1;
2344 spin_unlock_irq(&conf->device_lock);
2345 if (bitmap_end)
2346 bitmap_endwrite(conf->mddev->bitmap,
2347 sh->sector,
2348 STRIPE_SECTORS,
2349 !test_bit(STRIPE_DEGRADED, &sh->state),
2350 0);
2351 }
2352 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002353
2354 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2355 if (atomic_dec_and_test(&conf->pending_full_writes))
2356 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002357}
2358
Dan Williams1fe797e2008-06-28 09:16:30 +10002359static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002360 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2361{
2362 int rmw = 0, rcw = 0, i;
2363 for (i = disks; i--; ) {
2364 /* would I have to read this buffer for read_modify_write */
2365 struct r5dev *dev = &sh->dev[i];
2366 if ((dev->towrite || i == sh->pd_idx) &&
2367 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002368 !(test_bit(R5_UPTODATE, &dev->flags) ||
2369 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002370 if (test_bit(R5_Insync, &dev->flags))
2371 rmw++;
2372 else
2373 rmw += 2*disks; /* cannot read it */
2374 }
2375 /* Would I have to read this buffer for reconstruct_write */
2376 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2377 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002378 !(test_bit(R5_UPTODATE, &dev->flags) ||
2379 test_bit(R5_Wantcompute, &dev->flags))) {
2380 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002381 else
2382 rcw += 2*disks;
2383 }
2384 }
Dan Williams45b42332007-07-09 11:56:43 -07002385 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002386 (unsigned long long)sh->sector, rmw, rcw);
2387 set_bit(STRIPE_HANDLE, &sh->state);
2388 if (rmw < rcw && rmw > 0)
2389 /* prefer read-modify-write, but need to get some data */
2390 for (i = disks; i--; ) {
2391 struct r5dev *dev = &sh->dev[i];
2392 if ((dev->towrite || i == sh->pd_idx) &&
2393 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002394 !(test_bit(R5_UPTODATE, &dev->flags) ||
2395 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002396 test_bit(R5_Insync, &dev->flags)) {
2397 if (
2398 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002399 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002400 "%d for r-m-w\n", i);
2401 set_bit(R5_LOCKED, &dev->flags);
2402 set_bit(R5_Wantread, &dev->flags);
2403 s->locked++;
2404 } else {
2405 set_bit(STRIPE_DELAYED, &sh->state);
2406 set_bit(STRIPE_HANDLE, &sh->state);
2407 }
2408 }
2409 }
2410 if (rcw <= rmw && rcw > 0)
2411 /* want reconstruct write, but need to get some data */
2412 for (i = disks; i--; ) {
2413 struct r5dev *dev = &sh->dev[i];
2414 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2415 i != sh->pd_idx &&
2416 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002417 !(test_bit(R5_UPTODATE, &dev->flags) ||
2418 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002419 test_bit(R5_Insync, &dev->flags)) {
2420 if (
2421 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002422 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002423 "%d for Reconstruct\n", i);
2424 set_bit(R5_LOCKED, &dev->flags);
2425 set_bit(R5_Wantread, &dev->flags);
2426 s->locked++;
2427 } else {
2428 set_bit(STRIPE_DELAYED, &sh->state);
2429 set_bit(STRIPE_HANDLE, &sh->state);
2430 }
2431 }
2432 }
2433 /* now if nothing is locked, and if we have enough data,
2434 * we can start a write request
2435 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002436 /* since handle_stripe can be called at any time we need to handle the
2437 * case where a compute block operation has been submitted and then a
2438 * subsequent call wants to start a write request. raid5_run_ops only
2439 * handles the case where compute block and postxor are requested
2440 * simultaneously. If this is not the case then new writes need to be
2441 * held off until the compute completes.
2442 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002443 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2444 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2445 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002446 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002447}
2448
Dan Williams1fe797e2008-06-28 09:16:30 +10002449static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002450 struct stripe_head *sh, struct stripe_head_state *s,
2451 struct r6_state *r6s, int disks)
2452{
2453 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002454 int qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07002455 for (i = disks; i--; ) {
2456 struct r5dev *dev = &sh->dev[i];
2457 /* Would I have to read this buffer for reconstruct_write */
2458 if (!test_bit(R5_OVERWRITE, &dev->flags)
2459 && i != pd_idx && i != qd_idx
2460 && (!test_bit(R5_LOCKED, &dev->flags)
2461 ) &&
2462 !test_bit(R5_UPTODATE, &dev->flags)) {
2463 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2464 else {
Dan Williams45b42332007-07-09 11:56:43 -07002465 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002466 "disk %d flags=%#lx\n", i, dev->flags);
2467 must_compute++;
2468 }
2469 }
2470 }
Dan Williams45b42332007-07-09 11:56:43 -07002471 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002472 (unsigned long long)sh->sector, rcw, must_compute);
2473 set_bit(STRIPE_HANDLE, &sh->state);
2474
2475 if (rcw > 0)
2476 /* want reconstruct write, but need to get some data */
2477 for (i = disks; i--; ) {
2478 struct r5dev *dev = &sh->dev[i];
2479 if (!test_bit(R5_OVERWRITE, &dev->flags)
2480 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2481 && !test_bit(R5_LOCKED, &dev->flags) &&
2482 !test_bit(R5_UPTODATE, &dev->flags) &&
2483 test_bit(R5_Insync, &dev->flags)) {
2484 if (
2485 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002486 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002487 "block %d for Reconstruct\n",
2488 (unsigned long long)sh->sector, i);
2489 set_bit(R5_LOCKED, &dev->flags);
2490 set_bit(R5_Wantread, &dev->flags);
2491 s->locked++;
2492 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002493 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002494 "block %d for Reconstruct\n",
2495 (unsigned long long)sh->sector, i);
2496 set_bit(STRIPE_DELAYED, &sh->state);
2497 set_bit(STRIPE_HANDLE, &sh->state);
2498 }
2499 }
2500 }
2501 /* now if nothing is locked, and if we have enough data, we can start a
2502 * write request
2503 */
2504 if (s->locked == 0 && rcw == 0 &&
2505 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2506 if (must_compute > 0) {
2507 /* We have failed blocks and need to compute them */
2508 switch (s->failed) {
2509 case 0:
2510 BUG();
2511 case 1:
2512 compute_block_1(sh, r6s->failed_num[0], 0);
2513 break;
2514 case 2:
2515 compute_block_2(sh, r6s->failed_num[0],
2516 r6s->failed_num[1]);
2517 break;
2518 default: /* This request should have been failed? */
2519 BUG();
2520 }
2521 }
2522
Dan Williams45b42332007-07-09 11:56:43 -07002523 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002524 (unsigned long long)sh->sector);
2525 compute_parity6(sh, RECONSTRUCT_WRITE);
2526 /* now every locked buffer is ready to be written */
2527 for (i = disks; i--; )
2528 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002529 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002530 (unsigned long long)sh->sector, i);
2531 s->locked++;
2532 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2533 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002534 if (s->locked == disks)
2535 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2536 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002537 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2538 set_bit(STRIPE_INSYNC, &sh->state);
2539
2540 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2541 atomic_dec(&conf->preread_active_stripes);
2542 if (atomic_read(&conf->preread_active_stripes) <
2543 IO_THRESHOLD)
2544 md_wakeup_thread(conf->mddev->thread);
2545 }
2546 }
2547}
2548
2549static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2550 struct stripe_head_state *s, int disks)
2551{
Dan Williamsecc65c92008-06-28 08:31:57 +10002552 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002553
Dan Williamsbd2ab672008-04-10 21:29:27 -07002554 set_bit(STRIPE_HANDLE, &sh->state);
2555
Dan Williamsecc65c92008-06-28 08:31:57 +10002556 switch (sh->check_state) {
2557 case check_state_idle:
2558 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002559 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002560 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002561 sh->check_state = check_state_run;
2562 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002563 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002564 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002565 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002566 }
Dan Williamsa4456852007-07-09 11:56:43 -07002567 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002568 /* fall through */
2569 case check_state_compute_result:
2570 sh->check_state = check_state_idle;
2571 if (!dev)
2572 dev = &sh->dev[sh->pd_idx];
2573
2574 /* check that a write has not made the stripe insync */
2575 if (test_bit(STRIPE_INSYNC, &sh->state))
2576 break;
2577
2578 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002579 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2580 BUG_ON(s->uptodate != disks);
2581
2582 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002583 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002584 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002585
Dan Williamsa4456852007-07-09 11:56:43 -07002586 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002587 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002588 break;
2589 case check_state_run:
2590 break; /* we will be called again upon completion */
2591 case check_state_check_result:
2592 sh->check_state = check_state_idle;
2593
2594 /* if a failure occurred during the check operation, leave
2595 * STRIPE_INSYNC not set and let the stripe be handled again
2596 */
2597 if (s->failed)
2598 break;
2599
2600 /* handle a successful check operation, if parity is correct
2601 * we are done. Otherwise update the mismatch count and repair
2602 * parity if !MD_RECOVERY_CHECK
2603 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002604 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002605 /* parity is correct (on disc,
2606 * not in buffer any more)
2607 */
2608 set_bit(STRIPE_INSYNC, &sh->state);
2609 else {
2610 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2611 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2612 /* don't try to repair!! */
2613 set_bit(STRIPE_INSYNC, &sh->state);
2614 else {
2615 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002616 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002617 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2618 set_bit(R5_Wantcompute,
2619 &sh->dev[sh->pd_idx].flags);
2620 sh->ops.target = sh->pd_idx;
2621 s->uptodate++;
2622 }
2623 }
2624 break;
2625 case check_state_compute_run:
2626 break;
2627 default:
2628 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2629 __func__, sh->check_state,
2630 (unsigned long long) sh->sector);
2631 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002632 }
2633}
2634
2635
2636static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002637 struct stripe_head_state *s,
2638 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002639{
2640 int update_p = 0, update_q = 0;
2641 struct r5dev *dev;
2642 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002643 int qd_idx = sh->qd_idx;
Dan Williams36d1c642009-07-14 11:48:22 -07002644 unsigned long cpu;
2645 struct page *tmp_page;
Dan Williamsa4456852007-07-09 11:56:43 -07002646
2647 set_bit(STRIPE_HANDLE, &sh->state);
2648
2649 BUG_ON(s->failed > 2);
2650 BUG_ON(s->uptodate < disks);
2651 /* Want to check and possibly repair P and Q.
2652 * However there could be one 'failed' device, in which
2653 * case we can only check one of them, possibly using the
2654 * other to generate missing data
2655 */
Dan Williams36d1c642009-07-14 11:48:22 -07002656 cpu = get_cpu();
2657 tmp_page = per_cpu_ptr(conf->percpu, cpu)->spare_page;
2658 if (s->failed == r6s->q_failed) {
2659 /* The only possible failed device holds 'Q', so it
2660 * makes sense to check P (If anything else were failed,
2661 * we would have used P to recreate it).
Dan Williamsa4456852007-07-09 11:56:43 -07002662 */
Dan Williams36d1c642009-07-14 11:48:22 -07002663 compute_block_1(sh, pd_idx, 1);
2664 if (!page_is_zero(sh->dev[pd_idx].page)) {
2665 compute_block_1(sh, pd_idx, 0);
2666 update_p = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002667 }
Dan Williamsa4456852007-07-09 11:56:43 -07002668 }
Dan Williams36d1c642009-07-14 11:48:22 -07002669 if (!r6s->q_failed && s->failed < 2) {
2670 /* q is not failed, and we didn't use it to generate
2671 * anything, so it makes sense to check it
2672 */
2673 memcpy(page_address(tmp_page),
2674 page_address(sh->dev[qd_idx].page),
2675 STRIPE_SIZE);
2676 compute_parity6(sh, UPDATE_PARITY);
2677 if (memcmp(page_address(tmp_page),
2678 page_address(sh->dev[qd_idx].page),
2679 STRIPE_SIZE) != 0) {
2680 clear_bit(STRIPE_INSYNC, &sh->state);
2681 update_q = 1;
2682 }
2683 }
2684 put_cpu();
2685
2686 if (update_p || update_q) {
2687 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2688 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2689 /* don't try to repair!! */
2690 update_p = update_q = 0;
2691 }
2692
2693 /* now write out any block on a failed drive,
2694 * or P or Q if they need it
2695 */
2696
2697 if (s->failed == 2) {
2698 dev = &sh->dev[r6s->failed_num[1]];
2699 s->locked++;
2700 set_bit(R5_LOCKED, &dev->flags);
2701 set_bit(R5_Wantwrite, &dev->flags);
2702 }
2703 if (s->failed >= 1) {
2704 dev = &sh->dev[r6s->failed_num[0]];
2705 s->locked++;
2706 set_bit(R5_LOCKED, &dev->flags);
2707 set_bit(R5_Wantwrite, &dev->flags);
2708 }
2709
2710 if (update_p) {
2711 dev = &sh->dev[pd_idx];
2712 s->locked++;
2713 set_bit(R5_LOCKED, &dev->flags);
2714 set_bit(R5_Wantwrite, &dev->flags);
2715 }
2716 if (update_q) {
2717 dev = &sh->dev[qd_idx];
2718 s->locked++;
2719 set_bit(R5_LOCKED, &dev->flags);
2720 set_bit(R5_Wantwrite, &dev->flags);
2721 }
2722 clear_bit(STRIPE_DEGRADED, &sh->state);
2723
2724 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002725}
2726
2727static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2728 struct r6_state *r6s)
2729{
2730 int i;
2731
2732 /* We have read all the blocks in this stripe and now we need to
2733 * copy some of them into a target stripe for expand.
2734 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002735 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002736 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2737 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002738 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002739 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002740 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002741 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002742
NeilBrown784052e2009-03-31 15:19:07 +11002743 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002744 sector_t s = raid5_compute_sector(conf, bn, 0,
2745 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11002746 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002747 if (sh2 == NULL)
2748 /* so far only the early blocks of this stripe
2749 * have been requested. When later blocks
2750 * get requested, we will try again
2751 */
2752 continue;
2753 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2754 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2755 /* must have already done this block */
2756 release_stripe(sh2);
2757 continue;
2758 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002759
2760 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002761 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002762 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002763 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002764 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002765
Dan Williamsa4456852007-07-09 11:56:43 -07002766 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2767 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2768 for (j = 0; j < conf->raid_disks; j++)
2769 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002770 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002771 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2772 break;
2773 if (j == conf->raid_disks) {
2774 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2775 set_bit(STRIPE_HANDLE, &sh2->state);
2776 }
2777 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002778
Dan Williamsa4456852007-07-09 11:56:43 -07002779 }
NeilBrowna2e08552007-09-11 15:23:36 -07002780 /* done submitting copies, wait for them to complete */
2781 if (tx) {
2782 async_tx_ack(tx);
2783 dma_wait_for_async_tx(tx);
2784 }
Dan Williamsa4456852007-07-09 11:56:43 -07002785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Dan Williams6bfe0b42008-04-30 00:52:32 -07002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/*
2789 * handle_stripe - do things to a stripe.
2790 *
2791 * We lock the stripe and then examine the state of various bits
2792 * to see what needs to be done.
2793 * Possible results:
2794 * return some read request which now have data
2795 * return some write requests which are safely on disc
2796 * schedule a read on some buffers
2797 * schedule a write of some buffers
2798 * return confirmation of parity correctness
2799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 * buffers are taken off read_list or write_list, and bh_cache buffers
2801 * get BH_Lock set before the stripe lock is released.
2802 *
2803 */
Dan Williamsa4456852007-07-09 11:56:43 -07002804
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002805static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002808 int disks = sh->disks, i;
2809 struct bio *return_bi = NULL;
2810 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002812 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002813 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Dan Williamsa4456852007-07-09 11:56:43 -07002815 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002816 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2817 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2818 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2819 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 spin_lock(&sh->lock);
2822 clear_bit(STRIPE_HANDLE, &sh->state);
2823 clear_bit(STRIPE_DELAYED, &sh->state);
2824
Dan Williamsa4456852007-07-09 11:56:43 -07002825 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2826 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2827 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002830 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 for (i=disks; i--; ) {
2832 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002833 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Dan Williamsb5e98d62007-01-02 13:52:31 -07002836 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2837 "written %p\n", i, dev->flags, dev->toread, dev->read,
2838 dev->towrite, dev->written);
2839
2840 /* maybe we can request a biofill operation
2841 *
2842 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002843 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002844 */
2845 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002846 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002847 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002850 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2851 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002852 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Dan Williamsb5e98d62007-01-02 13:52:31 -07002854 if (test_bit(R5_Wantfill, &dev->flags))
2855 s.to_fill++;
2856 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002857 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002859 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002861 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Dan Williamsa4456852007-07-09 11:56:43 -07002863 if (dev->written)
2864 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002865 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002866 if (blocked_rdev == NULL &&
2867 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002868 blocked_rdev = rdev;
2869 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002870 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002871 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002872 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002873 clear_bit(R5_ReadError, &dev->flags);
2874 clear_bit(R5_ReWrite, &dev->flags);
2875 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002876 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002877 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002878 s.failed++;
2879 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 } else
2881 set_bit(R5_Insync, &dev->flags);
2882 }
NeilBrown9910f162006-01-06 00:20:24 -08002883 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002884
Dan Williams6bfe0b42008-04-30 00:52:32 -07002885 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002886 if (s.syncing || s.expanding || s.expanded ||
2887 s.to_write || s.written) {
2888 set_bit(STRIPE_HANDLE, &sh->state);
2889 goto unlock;
2890 }
2891 /* There is nothing for the blocked_rdev to block */
2892 rdev_dec_pending(blocked_rdev, conf->mddev);
2893 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002894 }
2895
Dan Williams83de75c2008-06-28 08:31:58 +10002896 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2897 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2898 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2899 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002900
Dan Williams45b42332007-07-09 11:56:43 -07002901 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002903 s.locked, s.uptodate, s.to_read, s.to_write,
2904 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 /* check if the array has lost two devices and, if so, some requests might
2906 * need to be failed
2907 */
Dan Williamsa4456852007-07-09 11:56:43 -07002908 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002909 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002910 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2912 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002913 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
2916 /* might be able to return some write requests if the parity block
2917 * is safe, or on a failed drive
2918 */
2919 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002920 if ( s.written &&
2921 ((test_bit(R5_Insync, &dev->flags) &&
2922 !test_bit(R5_LOCKED, &dev->flags) &&
2923 test_bit(R5_UPTODATE, &dev->flags)) ||
2924 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002925 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 /* Now we might consider reading some blocks, either to check/generate
2928 * parity, or to satisfy requests
2929 * or to load a block that is being partially written.
2930 */
Dan Williamsa4456852007-07-09 11:56:43 -07002931 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002932 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002933 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Dan Williamse33129d2007-01-02 13:52:30 -07002935 /* Now we check to see if any write operations have recently
2936 * completed
2937 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002938 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002939 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07002940 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002941 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2942 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002943 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002944
2945 /* All the 'written' buffers and the parity block are ready to
2946 * be written back to disk
2947 */
2948 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2949 for (i = disks; i--; ) {
2950 dev = &sh->dev[i];
2951 if (test_bit(R5_LOCKED, &dev->flags) &&
2952 (i == sh->pd_idx || dev->written)) {
2953 pr_debug("Writing block %d\n", i);
2954 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002955 if (prexor)
2956 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002957 if (!test_bit(R5_Insync, &dev->flags) ||
2958 (i == sh->pd_idx && s.failed == 0))
2959 set_bit(STRIPE_INSYNC, &sh->state);
2960 }
2961 }
2962 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2963 atomic_dec(&conf->preread_active_stripes);
2964 if (atomic_read(&conf->preread_active_stripes) <
2965 IO_THRESHOLD)
2966 md_wakeup_thread(conf->mddev->thread);
2967 }
2968 }
2969
2970 /* Now to consider new write requests and what else, if anything
2971 * should be read. We do not handle new writes when:
2972 * 1/ A 'write' operation (copy+xor) is already in flight.
2973 * 2/ A 'check' operation is in flight, as it may clobber the parity
2974 * block.
2975 */
Dan Williams600aa102008-06-28 08:32:05 +10002976 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002977 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002980 * Any reads will already have been scheduled, so we just see if enough
2981 * data is available. The parity check is held off while parity
2982 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002984 if (sh->check_state ||
2985 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002986 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002987 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002988 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002989
Dan Williamsa4456852007-07-09 11:56:43 -07002990 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2992 clear_bit(STRIPE_SYNCING, &sh->state);
2993 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002994
2995 /* If the failed drive is just a ReadError, then we might need to progress
2996 * the repair/check process
2997 */
Dan Williamsa4456852007-07-09 11:56:43 -07002998 if (s.failed == 1 && !conf->mddev->ro &&
2999 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3000 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3001 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003002 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003003 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003004 if (!test_bit(R5_ReWrite, &dev->flags)) {
3005 set_bit(R5_Wantwrite, &dev->flags);
3006 set_bit(R5_ReWrite, &dev->flags);
3007 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003008 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003009 } else {
3010 /* let's read it back */
3011 set_bit(R5_Wantread, &dev->flags);
3012 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003013 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003014 }
3015 }
3016
Dan Williams600aa102008-06-28 08:32:05 +10003017 /* Finish reconstruct operations initiated by the expansion process */
3018 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003019 struct stripe_head *sh2
3020 = get_active_stripe(conf, sh->sector, 1, 1);
3021 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3022 /* sh cannot be written until sh2 has been read.
3023 * so arrange for sh to be delayed a little
3024 */
3025 set_bit(STRIPE_DELAYED, &sh->state);
3026 set_bit(STRIPE_HANDLE, &sh->state);
3027 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3028 &sh2->state))
3029 atomic_inc(&conf->preread_active_stripes);
3030 release_stripe(sh2);
3031 goto unlock;
3032 }
3033 if (sh2)
3034 release_stripe(sh2);
3035
Dan Williams600aa102008-06-28 08:32:05 +10003036 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003037 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003038 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003039 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003040 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003041 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003042 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003043 }
3044
3045 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003046 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003047 /* Need to write out all blocks after computing parity */
3048 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003049 stripe_set_idx(sh->sector, conf, 0, sh);
Dan Williams1fe797e2008-06-28 09:16:30 +10003050 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003051 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003052 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003053 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003054 wake_up(&conf->wait_for_overlap);
3055 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3056 }
3057
Dan Williams0f94e872008-01-08 15:32:53 -08003058 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003059 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003060 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003061
Dan Williams6bfe0b42008-04-30 00:52:32 -07003062 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 spin_unlock(&sh->lock);
3064
Dan Williams6bfe0b42008-04-30 00:52:32 -07003065 /* wait for this device to become unblocked */
3066 if (unlikely(blocked_rdev))
3067 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3068
Dan Williams600aa102008-06-28 08:32:05 +10003069 if (s.ops_request)
3070 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003071
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003072 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Dan Williamsa4456852007-07-09 11:56:43 -07003074 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003075
3076 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Dan Williams36d1c642009-07-14 11:48:22 -07003079static bool handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003080{
NeilBrownbff61972009-03-31 14:33:13 +11003081 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003082 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003083 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003084 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003085 struct stripe_head_state s;
3086 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003087 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003088 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003089
Dan Williams45b42332007-07-09 11:56:43 -07003090 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07003091 "pd_idx=%d, qd_idx=%d\n",
3092 (unsigned long long)sh->sector, sh->state,
NeilBrown34e04e82009-03-31 15:10:16 +11003093 atomic_read(&sh->count), pd_idx, qd_idx);
Dan Williamsa4456852007-07-09 11:56:43 -07003094 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003095
3096 spin_lock(&sh->lock);
3097 clear_bit(STRIPE_HANDLE, &sh->state);
3098 clear_bit(STRIPE_DELAYED, &sh->state);
3099
Dan Williamsa4456852007-07-09 11:56:43 -07003100 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3101 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3102 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003103 /* Now to look around and see what can be done */
3104
3105 rcu_read_lock();
3106 for (i=disks; i--; ) {
3107 mdk_rdev_t *rdev;
3108 dev = &sh->dev[i];
3109 clear_bit(R5_Insync, &dev->flags);
3110
Dan Williams45b42332007-07-09 11:56:43 -07003111 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003112 i, dev->flags, dev->toread, dev->towrite, dev->written);
3113 /* maybe we can reply to a read */
3114 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3115 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003116 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003117 spin_lock_irq(&conf->device_lock);
3118 rbi = dev->toread;
3119 dev->toread = NULL;
3120 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3121 wake_up(&conf->wait_for_overlap);
3122 spin_unlock_irq(&conf->device_lock);
3123 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3124 copy_data(0, rbi, dev->page, dev->sector);
3125 rbi2 = r5_next_bio(rbi, dev->sector);
3126 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003127 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003128 rbi->bi_next = return_bi;
3129 return_bi = rbi;
3130 }
3131 spin_unlock_irq(&conf->device_lock);
3132 rbi = rbi2;
3133 }
3134 }
3135
3136 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003137 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3138 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003139
3140
Dan Williamsa4456852007-07-09 11:56:43 -07003141 if (dev->toread)
3142 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003143 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003144 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003145 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003146 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003147 }
Dan Williamsa4456852007-07-09 11:56:43 -07003148 if (dev->written)
3149 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003150 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003151 if (blocked_rdev == NULL &&
3152 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003153 blocked_rdev = rdev;
3154 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003155 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003156 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3157 /* The ReadError flag will just be confusing now */
3158 clear_bit(R5_ReadError, &dev->flags);
3159 clear_bit(R5_ReWrite, &dev->flags);
3160 }
3161 if (!rdev || !test_bit(In_sync, &rdev->flags)
3162 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003163 if (s.failed < 2)
3164 r6s.failed_num[s.failed] = i;
3165 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003166 } else
3167 set_bit(R5_Insync, &dev->flags);
3168 }
3169 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003170
3171 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003172 if (s.syncing || s.expanding || s.expanded ||
3173 s.to_write || s.written) {
3174 set_bit(STRIPE_HANDLE, &sh->state);
3175 goto unlock;
3176 }
3177 /* There is nothing for the blocked_rdev to block */
3178 rdev_dec_pending(blocked_rdev, conf->mddev);
3179 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003180 }
NeilBrownac4090d2008-08-05 15:54:13 +10003181
Dan Williams45b42332007-07-09 11:56:43 -07003182 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003183 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003184 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3185 r6s.failed_num[0], r6s.failed_num[1]);
3186 /* check if the array has lost >2 devices and, if so, some requests
3187 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003188 */
Dan Williamsa4456852007-07-09 11:56:43 -07003189 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003190 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003191 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003192 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3193 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003194 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003195 }
3196
3197 /*
3198 * might be able to return some write requests if the parity blocks
3199 * are safe, or on a failed drive
3200 */
3201 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003202 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3203 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003204 qdev = &sh->dev[qd_idx];
3205 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3206 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003207
Dan Williamsa4456852007-07-09 11:56:43 -07003208 if ( s.written &&
3209 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003210 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003211 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3212 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003213 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003214 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003215 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003216
3217 /* Now we might consider reading some blocks, either to check/generate
3218 * parity, or to satisfy requests
3219 * or to load a block that is being partially written.
3220 */
Dan Williamsa4456852007-07-09 11:56:43 -07003221 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3222 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003223 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003224
3225 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07003226 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10003227 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003228
3229 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003230 * Any reads will already have been scheduled, so we just see if enough
3231 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003232 */
Dan Williamsa4456852007-07-09 11:56:43 -07003233 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
Dan Williams36d1c642009-07-14 11:48:22 -07003234 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003235
Dan Williamsa4456852007-07-09 11:56:43 -07003236 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003237 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3238 clear_bit(STRIPE_SYNCING, &sh->state);
3239 }
3240
3241 /* If the failed drives are just a ReadError, then we might need
3242 * to progress the repair/check process
3243 */
Dan Williamsa4456852007-07-09 11:56:43 -07003244 if (s.failed <= 2 && !conf->mddev->ro)
3245 for (i = 0; i < s.failed; i++) {
3246 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003247 if (test_bit(R5_ReadError, &dev->flags)
3248 && !test_bit(R5_LOCKED, &dev->flags)
3249 && test_bit(R5_UPTODATE, &dev->flags)
3250 ) {
3251 if (!test_bit(R5_ReWrite, &dev->flags)) {
3252 set_bit(R5_Wantwrite, &dev->flags);
3253 set_bit(R5_ReWrite, &dev->flags);
3254 set_bit(R5_LOCKED, &dev->flags);
3255 } else {
3256 /* let's read it back */
3257 set_bit(R5_Wantread, &dev->flags);
3258 set_bit(R5_LOCKED, &dev->flags);
3259 }
3260 }
3261 }
NeilBrownf4168852007-02-28 20:11:53 -08003262
Dan Williamsa4456852007-07-09 11:56:43 -07003263 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownab69ae12009-03-31 15:26:47 +11003264 struct stripe_head *sh2
3265 = get_active_stripe(conf, sh->sector, 1, 1);
3266 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3267 /* sh cannot be written until sh2 has been read.
3268 * so arrange for sh to be delayed a little
3269 */
3270 set_bit(STRIPE_DELAYED, &sh->state);
3271 set_bit(STRIPE_HANDLE, &sh->state);
3272 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3273 &sh2->state))
3274 atomic_inc(&conf->preread_active_stripes);
3275 release_stripe(sh2);
3276 goto unlock;
3277 }
3278 if (sh2)
3279 release_stripe(sh2);
3280
NeilBrownf4168852007-02-28 20:11:53 -08003281 /* Need to write out all blocks after computing P&Q */
3282 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003283 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003284 compute_parity6(sh, RECONSTRUCT_WRITE);
3285 for (i = conf->raid_disks ; i-- ; ) {
3286 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003287 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003288 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3289 }
3290 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003291 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003292 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3293 atomic_dec(&conf->reshape_stripes);
3294 wake_up(&conf->wait_for_overlap);
3295 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3296 }
3297
Dan Williams0f94e872008-01-08 15:32:53 -08003298 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003299 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003300 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003301
Dan Williams6bfe0b42008-04-30 00:52:32 -07003302 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003303 spin_unlock(&sh->lock);
3304
Dan Williams6bfe0b42008-04-30 00:52:32 -07003305 /* wait for this device to become unblocked */
3306 if (unlikely(blocked_rdev))
3307 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3308
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003309 ops_run_io(sh, &s);
3310
Dan Williamsa4456852007-07-09 11:56:43 -07003311 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003312
3313 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003314}
3315
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003316/* returns true if the stripe was handled */
Dan Williams36d1c642009-07-14 11:48:22 -07003317static bool handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003318{
3319 if (sh->raid_conf->level == 6)
Dan Williams36d1c642009-07-14 11:48:22 -07003320 return handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003321 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003322 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003323}
3324
Arjan van de Ven858119e2006-01-14 13:20:43 -08003325static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326{
3327 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3328 while (!list_empty(&conf->delayed_list)) {
3329 struct list_head *l = conf->delayed_list.next;
3330 struct stripe_head *sh;
3331 sh = list_entry(l, struct stripe_head, lru);
3332 list_del_init(l);
3333 clear_bit(STRIPE_DELAYED, &sh->state);
3334 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3335 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003336 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
NeilBrown6ed30032008-02-06 01:40:00 -08003338 } else
3339 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
3341
Arjan van de Ven858119e2006-01-14 13:20:43 -08003342static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003343{
3344 /* device_lock is held */
3345 struct list_head head;
3346 list_add(&head, &conf->bitmap_list);
3347 list_del_init(&conf->bitmap_list);
3348 while (!list_empty(&head)) {
3349 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3350 list_del_init(&sh->lru);
3351 atomic_inc(&sh->count);
3352 __release_stripe(conf, sh);
3353 }
3354}
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356static void unplug_slaves(mddev_t *mddev)
3357{
3358 raid5_conf_t *conf = mddev_to_conf(mddev);
3359 int i;
3360
3361 rcu_read_lock();
3362 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003363 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003364 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003365 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 atomic_inc(&rdev->nr_pending);
3368 rcu_read_unlock();
3369
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003370 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 rdev_dec_pending(rdev, mddev);
3373 rcu_read_lock();
3374 }
3375 }
3376 rcu_read_unlock();
3377}
3378
Jens Axboe165125e2007-07-24 09:28:11 +02003379static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
3381 mddev_t *mddev = q->queuedata;
3382 raid5_conf_t *conf = mddev_to_conf(mddev);
3383 unsigned long flags;
3384
3385 spin_lock_irqsave(&conf->device_lock, flags);
3386
NeilBrown72626682005-09-09 16:23:54 -07003387 if (blk_remove_plug(q)) {
3388 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 md_wakeup_thread(mddev->thread);
3392
3393 spin_unlock_irqrestore(&conf->device_lock, flags);
3394
3395 unplug_slaves(mddev);
3396}
3397
NeilBrownf022b2f2006-10-03 01:15:56 -07003398static int raid5_congested(void *data, int bits)
3399{
3400 mddev_t *mddev = data;
3401 raid5_conf_t *conf = mddev_to_conf(mddev);
3402
3403 /* No difference between reads and writes. Just check
3404 * how busy the stripe_cache is
3405 */
3406 if (conf->inactive_blocked)
3407 return 1;
3408 if (conf->quiesce)
3409 return 1;
3410 if (list_empty_careful(&conf->inactive_list))
3411 return 1;
3412
3413 return 0;
3414}
3415
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003416/* We want read requests to align with chunks where possible,
3417 * but write requests don't need to.
3418 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003419static int raid5_mergeable_bvec(struct request_queue *q,
3420 struct bvec_merge_data *bvm,
3421 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003422{
3423 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003424 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003425 int max;
3426 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003427 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003428
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003429 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003430 return biovec->bv_len; /* always allow writes to be mergeable */
3431
NeilBrown784052e2009-03-31 15:19:07 +11003432 if (mddev->new_chunk < mddev->chunk_size)
3433 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003434 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3435 if (max < 0) max = 0;
3436 if (max <= biovec->bv_len && bio_sectors == 0)
3437 return biovec->bv_len;
3438 else
3439 return max;
3440}
3441
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003442
3443static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3444{
3445 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3446 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3447 unsigned int bio_sectors = bio->bi_size >> 9;
3448
NeilBrown784052e2009-03-31 15:19:07 +11003449 if (mddev->new_chunk < mddev->chunk_size)
3450 chunk_sectors = mddev->new_chunk >> 9;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003451 return chunk_sectors >=
3452 ((sector & (chunk_sectors - 1)) + bio_sectors);
3453}
3454
3455/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003456 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3457 * later sampled by raid5d.
3458 */
3459static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3460{
3461 unsigned long flags;
3462
3463 spin_lock_irqsave(&conf->device_lock, flags);
3464
3465 bi->bi_next = conf->retry_read_aligned_list;
3466 conf->retry_read_aligned_list = bi;
3467
3468 spin_unlock_irqrestore(&conf->device_lock, flags);
3469 md_wakeup_thread(conf->mddev->thread);
3470}
3471
3472
3473static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3474{
3475 struct bio *bi;
3476
3477 bi = conf->retry_read_aligned;
3478 if (bi) {
3479 conf->retry_read_aligned = NULL;
3480 return bi;
3481 }
3482 bi = conf->retry_read_aligned_list;
3483 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003484 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003485 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003486 /*
3487 * this sets the active strip count to 1 and the processed
3488 * strip count to zero (upper 8 bits)
3489 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003490 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003491 }
3492
3493 return bi;
3494}
3495
3496
3497/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003498 * The "raid5_align_endio" should check if the read succeeded and if it
3499 * did, call bio_endio on the original bio (having bio_put the new bio
3500 * first).
3501 * If the read failed..
3502 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003503static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003504{
3505 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003506 mddev_t *mddev;
3507 raid5_conf_t *conf;
3508 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3509 mdk_rdev_t *rdev;
3510
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003511 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003512
3513 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3514 conf = mddev_to_conf(mddev);
3515 rdev = (void*)raid_bi->bi_next;
3516 raid_bi->bi_next = NULL;
3517
3518 rdev_dec_pending(rdev, conf->mddev);
3519
3520 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003521 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003522 if (atomic_dec_and_test(&conf->active_aligned_reads))
3523 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003524 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003525 }
3526
3527
Dan Williams45b42332007-07-09 11:56:43 -07003528 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003529
3530 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003531}
3532
Neil Brown387bb172007-02-08 14:20:29 -08003533static int bio_fits_rdev(struct bio *bi)
3534{
Jens Axboe165125e2007-07-24 09:28:11 +02003535 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003536
3537 if ((bi->bi_size>>9) > q->max_sectors)
3538 return 0;
3539 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003540 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003541 return 0;
3542
3543 if (q->merge_bvec_fn)
3544 /* it's too hard to apply the merge_bvec_fn at this stage,
3545 * just just give up
3546 */
3547 return 0;
3548
3549 return 1;
3550}
3551
3552
Jens Axboe165125e2007-07-24 09:28:11 +02003553static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003554{
3555 mddev_t *mddev = q->queuedata;
3556 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003557 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003558 struct bio* align_bi;
3559 mdk_rdev_t *rdev;
3560
3561 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003562 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003563 return 0;
3564 }
3565 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003566 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003567 */
3568 align_bi = bio_clone(raid_bio, GFP_NOIO);
3569 if (!align_bi)
3570 return 0;
3571 /*
3572 * set bi_end_io to a new function, and set bi_private to the
3573 * original bio.
3574 */
3575 align_bi->bi_end_io = raid5_align_endio;
3576 align_bi->bi_private = raid_bio;
3577 /*
3578 * compute position
3579 */
NeilBrown112bf892009-03-31 14:39:38 +11003580 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3581 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003582 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003583
3584 rcu_read_lock();
3585 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3586 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003587 atomic_inc(&rdev->nr_pending);
3588 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003589 raid_bio->bi_next = (void*)rdev;
3590 align_bi->bi_bdev = rdev->bdev;
3591 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3592 align_bi->bi_sector += rdev->data_offset;
3593
Neil Brown387bb172007-02-08 14:20:29 -08003594 if (!bio_fits_rdev(align_bi)) {
3595 /* too big in some way */
3596 bio_put(align_bi);
3597 rdev_dec_pending(rdev, mddev);
3598 return 0;
3599 }
3600
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003601 spin_lock_irq(&conf->device_lock);
3602 wait_event_lock_irq(conf->wait_for_stripe,
3603 conf->quiesce == 0,
3604 conf->device_lock, /* nothing */);
3605 atomic_inc(&conf->active_aligned_reads);
3606 spin_unlock_irq(&conf->device_lock);
3607
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003608 generic_make_request(align_bi);
3609 return 1;
3610 } else {
3611 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003612 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003613 return 0;
3614 }
3615}
3616
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003617/* __get_priority_stripe - get the next stripe to process
3618 *
3619 * Full stripe writes are allowed to pass preread active stripes up until
3620 * the bypass_threshold is exceeded. In general the bypass_count
3621 * increments when the handle_list is handled before the hold_list; however, it
3622 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3623 * stripe with in flight i/o. The bypass_count will be reset when the
3624 * head of the hold_list has changed, i.e. the head was promoted to the
3625 * handle_list.
3626 */
3627static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3628{
3629 struct stripe_head *sh;
3630
3631 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3632 __func__,
3633 list_empty(&conf->handle_list) ? "empty" : "busy",
3634 list_empty(&conf->hold_list) ? "empty" : "busy",
3635 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3636
3637 if (!list_empty(&conf->handle_list)) {
3638 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3639
3640 if (list_empty(&conf->hold_list))
3641 conf->bypass_count = 0;
3642 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3643 if (conf->hold_list.next == conf->last_hold)
3644 conf->bypass_count++;
3645 else {
3646 conf->last_hold = conf->hold_list.next;
3647 conf->bypass_count -= conf->bypass_threshold;
3648 if (conf->bypass_count < 0)
3649 conf->bypass_count = 0;
3650 }
3651 }
3652 } else if (!list_empty(&conf->hold_list) &&
3653 ((conf->bypass_threshold &&
3654 conf->bypass_count > conf->bypass_threshold) ||
3655 atomic_read(&conf->pending_full_writes) == 0)) {
3656 sh = list_entry(conf->hold_list.next,
3657 typeof(*sh), lru);
3658 conf->bypass_count -= conf->bypass_threshold;
3659 if (conf->bypass_count < 0)
3660 conf->bypass_count = 0;
3661 } else
3662 return NULL;
3663
3664 list_del_init(&sh->lru);
3665 atomic_inc(&sh->count);
3666 BUG_ON(atomic_read(&sh->count) != 1);
3667 return sh;
3668}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003669
Jens Axboe165125e2007-07-24 09:28:11 +02003670static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 mddev_t *mddev = q->queuedata;
3673 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003674 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 sector_t new_sector;
3676 sector_t logical_sector, last_sector;
3677 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003678 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003679 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
NeilBrowne5dcdd82005-09-09 16:23:41 -07003681 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003682 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003683 return 0;
3684 }
3685
NeilBrown3d310eb2005-06-21 17:17:26 -07003686 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003687
Tejun Heo074a7ac2008-08-25 19:56:14 +09003688 cpu = part_stat_lock();
3689 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3690 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3691 bio_sectors(bi));
3692 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
NeilBrown802ba062006-12-13 00:34:13 -08003694 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003695 mddev->reshape_position == MaxSector &&
3696 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003697 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3700 last_sector = bi->bi_sector + (bi->bi_size>>9);
3701 bi->bi_next = NULL;
3702 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3705 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003706 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003707 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003708
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003709 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003710 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003711 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003712 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003713 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003714 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003715 * 64bit on a 32bit platform, and so it might be
3716 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003717 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003718 * the lock is dropped, so once we get a reference
3719 * to the stripe that we think it is, we will have
3720 * to check again.
3721 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003722 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003723 if (mddev->delta_disks < 0
3724 ? logical_sector < conf->reshape_progress
3725 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003726 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003727 previous = 1;
3728 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003729 if (mddev->delta_disks < 0
3730 ? logical_sector < conf->reshape_safe
3731 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003732 spin_unlock_irq(&conf->device_lock);
3733 schedule();
3734 goto retry;
3735 }
3736 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003737 spin_unlock_irq(&conf->device_lock);
3738 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003739 data_disks = disks - conf->max_degraded;
3740
NeilBrown112bf892009-03-31 14:39:38 +11003741 new_sector = raid5_compute_sector(conf, logical_sector,
3742 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003743 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003744 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 (unsigned long long)new_sector,
3746 (unsigned long long)logical_sector);
3747
NeilBrownb5663ba2009-03-31 14:39:38 +11003748 sh = get_active_stripe(conf, new_sector, previous,
3749 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003751 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003752 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003753 * stripe, so we must do the range check again.
3754 * Expansion could still move past after this
3755 * test, but as we are holding a reference to
3756 * 'sh', we know that if that happens,
3757 * STRIPE_EXPANDING will get set and the expansion
3758 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003759 */
3760 int must_retry = 0;
3761 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003762 if (mddev->delta_disks < 0
3763 ? logical_sector >= conf->reshape_progress
3764 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003765 /* mismatch, need to try again */
3766 must_retry = 1;
3767 spin_unlock_irq(&conf->device_lock);
3768 if (must_retry) {
3769 release_stripe(sh);
3770 goto retry;
3771 }
3772 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003773 /* FIXME what if we get a false positive because these
3774 * are being updated.
3775 */
3776 if (logical_sector >= mddev->suspend_lo &&
3777 logical_sector < mddev->suspend_hi) {
3778 release_stripe(sh);
3779 schedule();
3780 goto retry;
3781 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003782
3783 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3784 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3785 /* Stripe is busy expanding or
3786 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 * and wait a while
3788 */
3789 raid5_unplug_device(mddev->queue);
3790 release_stripe(sh);
3791 schedule();
3792 goto retry;
3793 }
3794 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003795 set_bit(STRIPE_HANDLE, &sh->state);
3796 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 } else {
3799 /* cannot get stripe for read-ahead, just give-up */
3800 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3801 finish_wait(&conf->wait_for_overlap, &w);
3802 break;
3803 }
3804
3805 }
3806 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003807 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003808 spin_unlock_irq(&conf->device_lock);
3809 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
NeilBrown16a53ec2006-06-26 00:27:38 -07003811 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003813
Neil Brown0e13fe232008-06-28 08:31:20 +10003814 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return 0;
3817}
3818
Dan Williamsb522adc2009-03-31 15:00:31 +11003819static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3820
NeilBrown52c03292006-06-26 00:27:43 -07003821static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
NeilBrown52c03292006-06-26 00:27:43 -07003823 /* reshaping is quite different to recovery/resync so it is
3824 * handled quite separately ... here.
3825 *
3826 * On each call to sync_request, we gather one chunk worth of
3827 * destination stripes and flag them as expanding.
3828 * Then we find all the source stripes and request reads.
3829 * As the reads complete, handle_stripe will copy the data
3830 * into the destination stripe and release that stripe.
3831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3833 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003834 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003835 int raid_disks = conf->previous_raid_disks;
3836 int data_disks = raid_disks - conf->max_degraded;
3837 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003838 int i;
3839 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003840 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003841 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003842 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003843 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003844
NeilBrownfef9c612009-03-31 15:16:46 +11003845 if (sector_nr == 0) {
3846 /* If restarting in the middle, skip the initial sectors */
3847 if (mddev->delta_disks < 0 &&
3848 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3849 sector_nr = raid5_size(mddev, 0, 0)
3850 - conf->reshape_progress;
3851 } else if (mddev->delta_disks > 0 &&
3852 conf->reshape_progress > 0)
3853 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003854 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003855 if (sector_nr) {
3856 *skipped = 1;
3857 return sector_nr;
3858 }
NeilBrown52c03292006-06-26 00:27:43 -07003859 }
3860
NeilBrown7a661382009-03-31 15:21:40 +11003861 /* We need to process a full chunk at a time.
3862 * If old and new chunk sizes differ, we need to process the
3863 * largest of these
3864 */
3865 if (mddev->new_chunk > mddev->chunk_size)
3866 reshape_sectors = mddev->new_chunk / 512;
3867 else
3868 reshape_sectors = mddev->chunk_size / 512;
3869
NeilBrown52c03292006-06-26 00:27:43 -07003870 /* we update the metadata when there is more than 3Meg
3871 * in the block range (that is rather arbitrary, should
3872 * probably be time based) or when the data about to be
3873 * copied would over-write the source of the data at
3874 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003875 * i.e. one new_stripe along from reshape_progress new_maps
3876 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003877 */
NeilBrownfef9c612009-03-31 15:16:46 +11003878 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003879 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11003880 readpos = conf->reshape_progress;
3881 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003882 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003883 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003884 if (mddev->delta_disks < 0) {
NeilBrown7a661382009-03-31 15:21:40 +11003885 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11003886 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003887 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003888 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003889 writepos += reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11003890 readpos -= reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11003891 safepos -= reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11003892 }
NeilBrown52c03292006-06-26 00:27:43 -07003893
NeilBrownc8f517c2009-03-31 15:28:40 +11003894 /* 'writepos' is the most advanced device address we might write.
3895 * 'readpos' is the least advanced device address we might read.
3896 * 'safepos' is the least address recorded in the metadata as having
3897 * been reshaped.
3898 * If 'readpos' is behind 'writepos', then there is no way that we can
3899 * ensure safety in the face of a crash - that must be done by userspace
3900 * making a backup of the data. So in that case there is no particular
3901 * rush to update metadata.
3902 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3903 * update the metadata to advance 'safepos' to match 'readpos' so that
3904 * we can be safe in the event of a crash.
3905 * So we insist on updating metadata if safepos is behind writepos and
3906 * readpos is beyond writepos.
3907 * In any case, update the metadata every 10 seconds.
3908 * Maybe that number should be configurable, but I'm not sure it is
3909 * worth it.... maybe it could be a multiple of safemode_delay???
3910 */
NeilBrownfef9c612009-03-31 15:16:46 +11003911 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11003912 ? (safepos > writepos && readpos < writepos)
3913 : (safepos < writepos && readpos > writepos)) ||
3914 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07003915 /* Cannot proceed until we've updated the superblock... */
3916 wait_event(conf->wait_for_overlap,
3917 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003918 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11003919 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07003920 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003921 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003922 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003923 kthread_should_stop());
3924 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003925 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003926 spin_unlock_irq(&conf->device_lock);
3927 wake_up(&conf->wait_for_overlap);
3928 }
3929
NeilBrownec32a2b2009-03-31 15:17:38 +11003930 if (mddev->delta_disks < 0) {
3931 BUG_ON(conf->reshape_progress == 0);
3932 stripe_addr = writepos;
3933 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11003934 ~((sector_t)reshape_sectors - 1))
3935 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11003936 != sector_nr);
3937 } else {
NeilBrown7a661382009-03-31 15:21:40 +11003938 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11003939 stripe_addr = sector_nr;
3940 }
NeilBrownab69ae12009-03-31 15:26:47 +11003941 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11003942 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07003943 int j;
3944 int skipped = 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11003945 sh = get_active_stripe(conf, stripe_addr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003946 set_bit(STRIPE_EXPANDING, &sh->state);
3947 atomic_inc(&conf->reshape_stripes);
3948 /* If any of this stripe is beyond the end of the old
3949 * array, then we need to zero those blocks
3950 */
3951 for (j=sh->disks; j--;) {
3952 sector_t s;
3953 if (j == sh->pd_idx)
3954 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003955 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003956 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003957 continue;
NeilBrown784052e2009-03-31 15:19:07 +11003958 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11003959 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07003960 skipped = 1;
3961 continue;
3962 }
3963 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3964 set_bit(R5_Expanded, &sh->dev[j].flags);
3965 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3966 }
3967 if (!skipped) {
3968 set_bit(STRIPE_EXPAND_READY, &sh->state);
3969 set_bit(STRIPE_HANDLE, &sh->state);
3970 }
NeilBrownab69ae12009-03-31 15:26:47 +11003971 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07003972 }
3973 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003974 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11003975 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11003976 else
NeilBrown7a661382009-03-31 15:21:40 +11003977 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003978 spin_unlock_irq(&conf->device_lock);
3979 /* Ok, those stripe are ready. We can start scheduling
3980 * reads on the source stripes.
3981 * The source stripes are determined by mapping the first and last
3982 * block on the destination stripes.
3983 */
NeilBrown52c03292006-06-26 00:27:43 -07003984 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003985 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11003986 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07003987 last_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003988 raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512)
NeilBrown112bf892009-03-31 14:39:38 +11003989 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11003990 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11003991 if (last_sector >= mddev->dev_sectors)
3992 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07003993 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003994 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003995 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3996 set_bit(STRIPE_HANDLE, &sh->state);
3997 release_stripe(sh);
3998 first_sector += STRIPE_SECTORS;
3999 }
NeilBrownab69ae12009-03-31 15:26:47 +11004000 /* Now that the sources are clearly marked, we can release
4001 * the destination stripes
4002 */
4003 while (!list_empty(&stripes)) {
4004 sh = list_entry(stripes.next, struct stripe_head, lru);
4005 list_del_init(&sh->lru);
4006 release_stripe(sh);
4007 }
NeilBrownc6207272008-02-06 01:39:52 -08004008 /* If this takes us to the resync_max point where we have to pause,
4009 * then we need to write out the superblock.
4010 */
NeilBrown7a661382009-03-31 15:21:40 +11004011 sector_nr += reshape_sectors;
NeilBrownc6207272008-02-06 01:39:52 -08004012 if (sector_nr >= mddev->resync_max) {
4013 /* Cannot proceed until we've updated the superblock... */
4014 wait_event(conf->wait_for_overlap,
4015 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004016 mddev->reshape_position = conf->reshape_progress;
NeilBrownc8f517c2009-03-31 15:28:40 +11004017 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004018 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4019 md_wakeup_thread(mddev->thread);
4020 wait_event(mddev->sb_wait,
4021 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4022 || kthread_should_stop());
4023 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004024 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004025 spin_unlock_irq(&conf->device_lock);
4026 wake_up(&conf->wait_for_overlap);
4027 }
NeilBrown7a661382009-03-31 15:21:40 +11004028 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004029}
4030
4031/* FIXME go_faster isn't used */
4032static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4033{
4034 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4035 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004036 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004037 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004038 int still_degraded = 0;
4039 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
NeilBrown72626682005-09-09 16:23:54 -07004041 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 /* just being told to finish up .. nothing much to do */
4043 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004044
NeilBrown29269552006-03-27 01:18:10 -08004045 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4046 end_reshape(conf);
4047 return 0;
4048 }
NeilBrown72626682005-09-09 16:23:54 -07004049
4050 if (mddev->curr_resync < max_sector) /* aborted */
4051 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4052 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004053 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004054 conf->fullsync = 0;
4055 bitmap_close_sync(mddev->bitmap);
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return 0;
4058 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004059
NeilBrown52c03292006-06-26 00:27:43 -07004060 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4061 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004062
NeilBrownc6207272008-02-06 01:39:52 -08004063 /* No need to check resync_max as we never do more than one
4064 * stripe, and as resync_max will always be on a chunk boundary,
4065 * if the check in md_do_sync didn't fire, there is no chance
4066 * of overstepping resync_max here
4067 */
4068
NeilBrown16a53ec2006-06-26 00:27:38 -07004069 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 * to resync, then assert that we are finished, because there is
4071 * nothing we can do.
4072 */
NeilBrown3285edf2006-06-26 00:27:55 -07004073 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004074 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004075 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004076 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return rv;
4078 }
NeilBrown72626682005-09-09 16:23:54 -07004079 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004080 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004081 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4082 /* we can skip this block, and probably more */
4083 sync_blocks /= STRIPE_SECTORS;
4084 *skipped = 1;
4085 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
NeilBrownb47490c2008-02-06 01:39:50 -08004088
4089 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4090
NeilBrownb5663ba2009-03-31 14:39:38 +11004091 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004093 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004095 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004097 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004099 /* Need to check if array will still be degraded after recovery/resync
4100 * We don't need to check the 'failed' flag as when that gets set,
4101 * recovery aborts.
4102 */
4103 for (i=0; i<mddev->raid_disks; i++)
4104 if (conf->disks[i].rdev == NULL)
4105 still_degraded = 1;
4106
4107 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4108
4109 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 set_bit(STRIPE_SYNCING, &sh->state);
4111 clear_bit(STRIPE_INSYNC, &sh->state);
4112 spin_unlock(&sh->lock);
4113
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004114 /* wait for any blocked device to be handled */
Dan Williams36d1c642009-07-14 11:48:22 -07004115 while (unlikely(!handle_stripe(sh)))
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004116 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 release_stripe(sh);
4118
4119 return STRIPE_SECTORS;
4120}
4121
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004122static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4123{
4124 /* We may not be able to submit a whole bio at once as there
4125 * may not be enough stripe_heads available.
4126 * We cannot pre-allocate enough stripe_heads as we may need
4127 * more than exist in the cache (if we allow ever large chunks).
4128 * So we do one stripe head at a time and record in
4129 * ->bi_hw_segments how many have been done.
4130 *
4131 * We *know* that this entire raid_bio is in one chunk, so
4132 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4133 */
4134 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004135 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004136 sector_t sector, logical_sector, last_sector;
4137 int scnt = 0;
4138 int remaining;
4139 int handled = 0;
4140
4141 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004142 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004143 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004144 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4145
4146 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004147 logical_sector += STRIPE_SECTORS,
4148 sector += STRIPE_SECTORS,
4149 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004150
Jens Axboe960e7392008-08-15 10:41:18 +02004151 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004152 /* already done this stripe */
4153 continue;
4154
NeilBrownb5663ba2009-03-31 14:39:38 +11004155 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004156
4157 if (!sh) {
4158 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004159 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004160 conf->retry_read_aligned = raid_bio;
4161 return handled;
4162 }
4163
4164 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004165 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4166 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004167 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004168 conf->retry_read_aligned = raid_bio;
4169 return handled;
4170 }
4171
Dan Williams36d1c642009-07-14 11:48:22 -07004172 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004173 release_stripe(sh);
4174 handled++;
4175 }
4176 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004177 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004178 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004179 if (remaining == 0)
4180 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004181 if (atomic_dec_and_test(&conf->active_aligned_reads))
4182 wake_up(&conf->wait_for_stripe);
4183 return handled;
4184}
4185
4186
4187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188/*
4189 * This is our raid5 kernel thread.
4190 *
4191 * We scan the hash table for stripes which can be handled now.
4192 * During the scan, completed stripes are saved for us by the interrupt
4193 * handler, so that they will not have to wait for our next wakeup.
4194 */
NeilBrown6ed30032008-02-06 01:40:00 -08004195static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 struct stripe_head *sh;
4198 raid5_conf_t *conf = mddev_to_conf(mddev);
4199 int handled;
4200
Dan Williams45b42332007-07-09 11:56:43 -07004201 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 handled = 0;
4206 spin_lock_irq(&conf->device_lock);
4207 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004208 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
NeilBrownae3c20c2006-07-10 04:44:17 -07004210 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004211 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004212 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004213 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004214 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004215 conf->seq_write = seq;
4216 activate_bit_delay(conf);
4217 }
4218
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004219 while ((bio = remove_bio_from_retry(conf))) {
4220 int ok;
4221 spin_unlock_irq(&conf->device_lock);
4222 ok = retry_aligned_read(conf, bio);
4223 spin_lock_irq(&conf->device_lock);
4224 if (!ok)
4225 break;
4226 handled++;
4227 }
4228
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004229 sh = __get_priority_stripe(conf);
4230
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004231 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 spin_unlock_irq(&conf->device_lock);
4234
4235 handled++;
Dan Williams36d1c642009-07-14 11:48:22 -07004236 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 release_stripe(sh);
4238
4239 spin_lock_irq(&conf->device_lock);
4240 }
Dan Williams45b42332007-07-09 11:56:43 -07004241 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243 spin_unlock_irq(&conf->device_lock);
4244
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004245 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 unplug_slaves(mddev);
4247
Dan Williams45b42332007-07-09 11:56:43 -07004248 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
NeilBrown3f294f42005-11-08 21:39:25 -08004251static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004252raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004253{
NeilBrown007583c2005-11-08 21:39:30 -08004254 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004255 if (conf)
4256 return sprintf(page, "%d\n", conf->max_nr_stripes);
4257 else
4258 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004259}
4260
4261static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004262raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004263{
NeilBrown007583c2005-11-08 21:39:30 -08004264 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004265 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004266 int err;
4267
NeilBrown3f294f42005-11-08 21:39:25 -08004268 if (len >= PAGE_SIZE)
4269 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004270 if (!conf)
4271 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004272
Dan Williams4ef197d82008-04-28 02:15:54 -07004273 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004274 return -EINVAL;
4275 if (new <= 16 || new > 32768)
4276 return -EINVAL;
4277 while (new < conf->max_nr_stripes) {
4278 if (drop_one_stripe(conf))
4279 conf->max_nr_stripes--;
4280 else
4281 break;
4282 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004283 err = md_allow_write(mddev);
4284 if (err)
4285 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004286 while (new > conf->max_nr_stripes) {
4287 if (grow_one_stripe(conf))
4288 conf->max_nr_stripes++;
4289 else break;
4290 }
4291 return len;
4292}
NeilBrown007583c2005-11-08 21:39:30 -08004293
NeilBrown96de1e62005-11-08 21:39:39 -08004294static struct md_sysfs_entry
4295raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4296 raid5_show_stripe_cache_size,
4297 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004298
4299static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004300raid5_show_preread_threshold(mddev_t *mddev, char *page)
4301{
4302 raid5_conf_t *conf = mddev_to_conf(mddev);
4303 if (conf)
4304 return sprintf(page, "%d\n", conf->bypass_threshold);
4305 else
4306 return 0;
4307}
4308
4309static ssize_t
4310raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4311{
4312 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004313 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004314 if (len >= PAGE_SIZE)
4315 return -EINVAL;
4316 if (!conf)
4317 return -ENODEV;
4318
Dan Williams4ef197d82008-04-28 02:15:54 -07004319 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004320 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004321 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004322 return -EINVAL;
4323 conf->bypass_threshold = new;
4324 return len;
4325}
4326
4327static struct md_sysfs_entry
4328raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4329 S_IRUGO | S_IWUSR,
4330 raid5_show_preread_threshold,
4331 raid5_store_preread_threshold);
4332
4333static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004334stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004335{
NeilBrown007583c2005-11-08 21:39:30 -08004336 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004337 if (conf)
4338 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4339 else
4340 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004341}
4342
NeilBrown96de1e62005-11-08 21:39:39 -08004343static struct md_sysfs_entry
4344raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004345
NeilBrown007583c2005-11-08 21:39:30 -08004346static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004347 &raid5_stripecache_size.attr,
4348 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004349 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004350 NULL,
4351};
NeilBrown007583c2005-11-08 21:39:30 -08004352static struct attribute_group raid5_attrs_group = {
4353 .name = NULL,
4354 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004355};
4356
Dan Williams80c3a6c2009-03-17 18:10:40 -07004357static sector_t
4358raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4359{
4360 raid5_conf_t *conf = mddev_to_conf(mddev);
4361
4362 if (!sectors)
4363 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004364 if (!raid_disks) {
4365 /* size is defined by the smallest of previous and new size */
4366 if (conf->raid_disks < conf->previous_raid_disks)
4367 raid_disks = conf->raid_disks;
4368 else
4369 raid_disks = conf->previous_raid_disks;
4370 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004371
4372 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown784052e2009-03-31 15:19:07 +11004373 sectors &= ~((sector_t)mddev->new_chunk/512 - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004374 return sectors * (raid_disks - conf->max_degraded);
4375}
4376
Dan Williams36d1c642009-07-14 11:48:22 -07004377static void raid5_free_percpu(raid5_conf_t *conf)
4378{
4379 struct raid5_percpu *percpu;
4380 unsigned long cpu;
4381
4382 if (!conf->percpu)
4383 return;
4384
4385 get_online_cpus();
4386 for_each_possible_cpu(cpu) {
4387 percpu = per_cpu_ptr(conf->percpu, cpu);
4388 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004389 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004390 }
4391#ifdef CONFIG_HOTPLUG_CPU
4392 unregister_cpu_notifier(&conf->cpu_notify);
4393#endif
4394 put_online_cpus();
4395
4396 free_percpu(conf->percpu);
4397}
4398
Dan Williamsa11034b2009-07-14 11:48:16 -07004399static void free_conf(raid5_conf_t *conf)
4400{
4401 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004402 raid5_free_percpu(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004403 kfree(conf->disks);
4404 kfree(conf->stripe_hashtbl);
4405 kfree(conf);
4406}
4407
Dan Williams36d1c642009-07-14 11:48:22 -07004408#ifdef CONFIG_HOTPLUG_CPU
4409static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4410 void *hcpu)
4411{
4412 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4413 long cpu = (long)hcpu;
4414 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4415
4416 switch (action) {
4417 case CPU_UP_PREPARE:
4418 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004419 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004420 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004421 if (!percpu->scribble)
4422 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4423
4424 if (!percpu->scribble ||
4425 (conf->level == 6 && !percpu->spare_page)) {
4426 safe_put_page(percpu->spare_page);
4427 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004428 pr_err("%s: failed memory allocation for cpu%ld\n",
4429 __func__, cpu);
4430 return NOTIFY_BAD;
4431 }
4432 break;
4433 case CPU_DEAD:
4434 case CPU_DEAD_FROZEN:
4435 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004436 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004437 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004438 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004439 break;
4440 default:
4441 break;
4442 }
4443 return NOTIFY_OK;
4444}
4445#endif
4446
4447static int raid5_alloc_percpu(raid5_conf_t *conf)
4448{
4449 unsigned long cpu;
4450 struct page *spare_page;
4451 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004452 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004453 int err;
4454
Dan Williams36d1c642009-07-14 11:48:22 -07004455 allcpus = alloc_percpu(struct raid5_percpu);
4456 if (!allcpus)
4457 return -ENOMEM;
4458 conf->percpu = allcpus;
4459
4460 get_online_cpus();
4461 err = 0;
4462 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004463 if (conf->level == 6) {
4464 spare_page = alloc_page(GFP_KERNEL);
4465 if (!spare_page) {
4466 err = -ENOMEM;
4467 break;
4468 }
4469 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4470 }
4471 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4472 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004473 err = -ENOMEM;
4474 break;
4475 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004476 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004477 }
4478#ifdef CONFIG_HOTPLUG_CPU
4479 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4480 conf->cpu_notify.priority = 0;
4481 if (err == 0)
4482 err = register_cpu_notifier(&conf->cpu_notify);
4483#endif
4484 put_online_cpus();
4485
4486 return err;
4487}
4488
NeilBrown91adb562009-03-31 14:39:39 +11004489static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
4491 raid5_conf_t *conf;
4492 int raid_disk, memory;
4493 mdk_rdev_t *rdev;
4494 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
NeilBrown91adb562009-03-31 14:39:39 +11004496 if (mddev->new_level != 5
4497 && mddev->new_level != 4
4498 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004499 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004500 mdname(mddev), mddev->new_level);
4501 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
NeilBrown91adb562009-03-31 14:39:39 +11004503 if ((mddev->new_level == 5
4504 && !algorithm_valid_raid5(mddev->new_layout)) ||
4505 (mddev->new_level == 6
4506 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004507 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004508 mdname(mddev), mddev->new_layout);
4509 return ERR_PTR(-EIO);
4510 }
4511 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4512 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4513 mdname(mddev), mddev->raid_disks);
4514 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
NeilBrown91adb562009-03-31 14:39:39 +11004517 if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
4518 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4519 mddev->new_chunk, mdname(mddev));
4520 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004521 }
4522
NeilBrown91adb562009-03-31 14:39:39 +11004523 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4524 if (conf == NULL)
4525 goto abort;
4526
4527 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004528 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004529 if (mddev->reshape_position == MaxSector)
4530 conf->previous_raid_disks = mddev->raid_disks;
4531 else
4532 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4533
4534 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4535 GFP_KERNEL);
4536 if (!conf->disks)
4537 goto abort;
4538
4539 conf->mddev = mddev;
4540
4541 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4542 goto abort;
4543
Dan Williams36d1c642009-07-14 11:48:22 -07004544 conf->level = mddev->new_level;
4545 if (raid5_alloc_percpu(conf) != 0)
4546 goto abort;
4547
NeilBrown91adb562009-03-31 14:39:39 +11004548 spin_lock_init(&conf->device_lock);
4549 init_waitqueue_head(&conf->wait_for_stripe);
4550 init_waitqueue_head(&conf->wait_for_overlap);
4551 INIT_LIST_HEAD(&conf->handle_list);
4552 INIT_LIST_HEAD(&conf->hold_list);
4553 INIT_LIST_HEAD(&conf->delayed_list);
4554 INIT_LIST_HEAD(&conf->bitmap_list);
4555 INIT_LIST_HEAD(&conf->inactive_list);
4556 atomic_set(&conf->active_stripes, 0);
4557 atomic_set(&conf->preread_active_stripes, 0);
4558 atomic_set(&conf->active_aligned_reads, 0);
4559 conf->bypass_threshold = BYPASS_THRESHOLD;
4560
4561 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4562
4563 list_for_each_entry(rdev, &mddev->disks, same_set) {
4564 raid_disk = rdev->raid_disk;
4565 if (raid_disk >= conf->raid_disks
4566 || raid_disk < 0)
4567 continue;
4568 disk = conf->disks + raid_disk;
4569
4570 disk->rdev = rdev;
4571
4572 if (test_bit(In_sync, &rdev->flags)) {
4573 char b[BDEVNAME_SIZE];
4574 printk(KERN_INFO "raid5: device %s operational as raid"
4575 " disk %d\n", bdevname(rdev->bdev,b),
4576 raid_disk);
4577 } else
4578 /* Cannot rely on bitmap to complete recovery */
4579 conf->fullsync = 1;
4580 }
4581
4582 conf->chunk_size = mddev->new_chunk;
NeilBrown91adb562009-03-31 14:39:39 +11004583 if (conf->level == 6)
4584 conf->max_degraded = 2;
4585 else
4586 conf->max_degraded = 1;
4587 conf->algorithm = mddev->new_layout;
4588 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004589 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004590 if (conf->reshape_progress != MaxSector) {
NeilBrown784052e2009-03-31 15:19:07 +11004591 conf->prev_chunk = mddev->chunk_size;
NeilBrowne183eae2009-03-31 15:20:22 +11004592 conf->prev_algo = mddev->layout;
4593 }
NeilBrown91adb562009-03-31 14:39:39 +11004594
4595 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4596 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4597 if (grow_stripes(conf, conf->max_nr_stripes)) {
4598 printk(KERN_ERR
4599 "raid5: couldn't allocate %dkB for buffers\n", memory);
4600 goto abort;
4601 } else
4602 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4603 memory, mdname(mddev));
4604
4605 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4606 if (!conf->thread) {
4607 printk(KERN_ERR
4608 "raid5: couldn't allocate thread for %s\n",
4609 mdname(mddev));
4610 goto abort;
4611 }
4612
4613 return conf;
4614
4615 abort:
4616 if (conf) {
Dan Williamsa11034b2009-07-14 11:48:16 -07004617 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004618 return ERR_PTR(-EIO);
4619 } else
4620 return ERR_PTR(-ENOMEM);
4621}
4622
4623static int run(mddev_t *mddev)
4624{
4625 raid5_conf_t *conf;
4626 int working_disks = 0;
4627 mdk_rdev_t *rdev;
4628
NeilBrownf6705572006-03-27 01:18:11 -08004629 if (mddev->reshape_position != MaxSector) {
4630 /* Check that we can continue the reshape.
4631 * Currently only disks can change, it must
4632 * increase, and we must be past the point where
4633 * a stripe over-writes itself
4634 */
4635 sector_t here_new, here_old;
4636 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004637 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004638
NeilBrown88ce4932009-03-31 15:24:23 +11004639 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004640 printk(KERN_ERR "raid5: %s: unsupported reshape "
4641 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004642 mdname(mddev));
4643 return -EINVAL;
4644 }
NeilBrownf6705572006-03-27 01:18:11 -08004645 old_disks = mddev->raid_disks - mddev->delta_disks;
4646 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004647 * further up in new geometry must map after here in old
4648 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004649 */
4650 here_new = mddev->reshape_position;
NeilBrown784052e2009-03-31 15:19:07 +11004651 if (sector_div(here_new, (mddev->new_chunk>>9)*
NeilBrownf4168852007-02-28 20:11:53 -08004652 (mddev->raid_disks - max_degraded))) {
4653 printk(KERN_ERR "raid5: reshape_position not "
4654 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004655 return -EINVAL;
4656 }
4657 /* here_new is the stripe we will write to */
4658 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004659 sector_div(here_old, (mddev->chunk_size>>9)*
4660 (old_disks-max_degraded));
4661 /* here_old is the first stripe that we might need to read
4662 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004663 if (here_new >= here_old) {
4664 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004665 printk(KERN_ERR "raid5: reshape_position too early for "
4666 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004667 return -EINVAL;
4668 }
4669 printk(KERN_INFO "raid5: reshape will continue\n");
4670 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004671 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004672 BUG_ON(mddev->level != mddev->new_level);
4673 BUG_ON(mddev->layout != mddev->new_layout);
4674 BUG_ON(mddev->chunk_size != mddev->new_chunk);
4675 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004676 }
4677
NeilBrown245f46c2009-03-31 14:39:39 +11004678 if (mddev->private == NULL)
4679 conf = setup_conf(mddev);
4680 else
4681 conf = mddev->private;
4682
NeilBrown91adb562009-03-31 14:39:39 +11004683 if (IS_ERR(conf))
4684 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004685
NeilBrown91adb562009-03-31 14:39:39 +11004686 mddev->thread = conf->thread;
4687 conf->thread = NULL;
4688 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004691 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 */
NeilBrown91adb562009-03-31 14:39:39 +11004693 list_for_each_entry(rdev, &mddev->disks, same_set)
4694 if (rdev->raid_disk >= 0 &&
4695 test_bit(In_sync, &rdev->flags))
4696 working_disks++;
4697
NeilBrown02c2de82006-10-03 01:15:47 -07004698 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
NeilBrown16a53ec2006-06-26 00:27:38 -07004700 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 printk(KERN_ERR "raid5: not enough operational devices for %s"
4702 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004703 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 goto abort;
4705 }
4706
NeilBrown91adb562009-03-31 14:39:39 +11004707 /* device size must be a multiple of chunk size */
4708 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4709 mddev->resync_max_sectors = mddev->dev_sectors;
4710
NeilBrown16a53ec2006-06-26 00:27:38 -07004711 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004713 if (mddev->ok_start_degraded)
4714 printk(KERN_WARNING
4715 "raid5: starting dirty degraded array: %s"
4716 "- data corruption possible.\n",
4717 mdname(mddev));
4718 else {
4719 printk(KERN_ERR
4720 "raid5: cannot start dirty degraded array for %s\n",
4721 mdname(mddev));
4722 goto abort;
4723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 }
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 if (mddev->degraded == 0)
4727 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004728 " devices, algorithm %d\n", conf->level, mdname(mddev),
4729 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4730 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 else
4732 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4733 " out of %d devices, algorithm %d\n", conf->level,
4734 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004735 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 print_raid5_conf(conf);
4738
NeilBrownfef9c612009-03-31 15:16:46 +11004739 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004740 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004741 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004742 atomic_set(&conf->reshape_stripes, 0);
4743 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4744 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4745 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4746 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4747 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4748 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004749 }
4750
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004752 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 */
4754 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004755 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4756 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004757 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4759 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4760 }
4761
4762 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004763 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4764 printk(KERN_WARNING
4765 "raid5: failed to create sysfs attributes for %s\n",
4766 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004767
NeilBrown91adb562009-03-31 14:39:39 +11004768 mddev->queue->queue_lock = &conf->device_lock;
4769
NeilBrown7a5febe2005-05-16 21:53:16 -07004770 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004771 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004772 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004773
Dan Williams1f403622009-03-31 14:59:03 +11004774 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004775
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004776 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 return 0;
4779abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11004780 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11004781 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (conf) {
4783 print_raid5_conf(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004784 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
4786 mddev->private = NULL;
4787 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4788 return -EIO;
4789}
4790
4791
4792
NeilBrown3f294f42005-11-08 21:39:25 -08004793static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
4795 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4796
4797 md_unregister_thread(mddev->thread);
4798 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004799 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004801 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williamsa11034b2009-07-14 11:48:16 -07004802 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 mddev->private = NULL;
4804 return 0;
4805}
4806
Dan Williams45b42332007-07-09 11:56:43 -07004807#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004808static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
4810 int i;
4811
NeilBrown16a53ec2006-06-26 00:27:38 -07004812 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4813 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4814 seq_printf(seq, "sh %llu, count %d.\n",
4815 (unsigned long long)sh->sector, atomic_read(&sh->count));
4816 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004817 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004818 seq_printf(seq, "(cache%d: %p %ld) ",
4819 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004821 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
4823
NeilBrownd710e132008-10-13 11:55:12 +11004824static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825{
4826 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004827 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 int i;
4829
4830 spin_lock_irq(&conf->device_lock);
4831 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004832 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (sh->raid_conf != conf)
4834 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004835 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
4837 }
4838 spin_unlock_irq(&conf->device_lock);
4839}
4840#endif
4841
NeilBrownd710e132008-10-13 11:55:12 +11004842static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
4844 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4845 int i;
4846
4847 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004848 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 for (i = 0; i < conf->raid_disks; i++)
4850 seq_printf (seq, "%s",
4851 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004852 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004854#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004855 seq_printf (seq, "\n");
4856 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857#endif
4858}
4859
4860static void print_raid5_conf (raid5_conf_t *conf)
4861{
4862 int i;
4863 struct disk_info *tmp;
4864
4865 printk("RAID5 conf printout:\n");
4866 if (!conf) {
4867 printk("(conf==NULL)\n");
4868 return;
4869 }
NeilBrown02c2de82006-10-03 01:15:47 -07004870 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4871 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 for (i = 0; i < conf->raid_disks; i++) {
4874 char b[BDEVNAME_SIZE];
4875 tmp = conf->disks + i;
4876 if (tmp->rdev)
4877 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004878 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 bdevname(tmp->rdev->bdev,b));
4880 }
4881}
4882
4883static int raid5_spare_active(mddev_t *mddev)
4884{
4885 int i;
4886 raid5_conf_t *conf = mddev->private;
4887 struct disk_info *tmp;
4888
4889 for (i = 0; i < conf->raid_disks; i++) {
4890 tmp = conf->disks + i;
4891 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004892 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004893 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4894 unsigned long flags;
4895 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004897 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899 }
4900 print_raid5_conf(conf);
4901 return 0;
4902}
4903
4904static int raid5_remove_disk(mddev_t *mddev, int number)
4905{
4906 raid5_conf_t *conf = mddev->private;
4907 int err = 0;
4908 mdk_rdev_t *rdev;
4909 struct disk_info *p = conf->disks + number;
4910
4911 print_raid5_conf(conf);
4912 rdev = p->rdev;
4913 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11004914 if (number >= conf->raid_disks &&
4915 conf->reshape_progress == MaxSector)
4916 clear_bit(In_sync, &rdev->flags);
4917
NeilBrownb2d444d2005-11-08 21:39:31 -08004918 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 atomic_read(&rdev->nr_pending)) {
4920 err = -EBUSY;
4921 goto abort;
4922 }
NeilBrowndfc70642008-05-23 13:04:39 -07004923 /* Only remove non-faulty devices if recovery
4924 * isn't possible.
4925 */
4926 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11004927 mddev->degraded <= conf->max_degraded &&
4928 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07004929 err = -EBUSY;
4930 goto abort;
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004933 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (atomic_read(&rdev->nr_pending)) {
4935 /* lost the race, try later */
4936 err = -EBUSY;
4937 p->rdev = rdev;
4938 }
4939 }
4940abort:
4941
4942 print_raid5_conf(conf);
4943 return err;
4944}
4945
4946static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4947{
4948 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004949 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int disk;
4951 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004952 int first = 0;
4953 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
NeilBrown16a53ec2006-06-26 00:27:38 -07004955 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Neil Brown6c2fce22008-06-28 08:31:31 +10004959 if (rdev->raid_disk >= 0)
4960 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004963 * find the disk ... but prefer rdev->saved_raid_disk
4964 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004966 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004967 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004968 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4969 disk = rdev->saved_raid_disk;
4970 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004971 disk = first;
4972 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004974 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004976 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004977 if (rdev->saved_raid_disk != disk)
4978 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004979 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 break;
4981 }
4982 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
4986static int raid5_resize(mddev_t *mddev, sector_t sectors)
4987{
4988 /* no resync is happening, and there is enough space
4989 * on all devices, so we can resize.
4990 * We need to make sure resync covers any new space.
4991 * If the array is shrinking we should possibly wait until
4992 * any io in the removed space completes, but it hardly seems
4993 * worth it.
4994 */
4995 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Dan Williams1f403622009-03-31 14:59:03 +11004996 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
4997 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11004998 if (mddev->array_sectors >
4999 raid5_size(mddev, sectors, mddev->raid_disks))
5000 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005001 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07005002 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11005003 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5004 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5006 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005007 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005008 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 return 0;
5010}
5011
NeilBrown63c70c42006-03-27 01:18:13 -08005012static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005013{
5014 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown29269552006-03-27 01:18:10 -08005015
NeilBrown88ce4932009-03-31 15:24:23 +11005016 if (mddev->delta_disks == 0 &&
5017 mddev->new_layout == mddev->layout &&
5018 mddev->new_chunk == mddev->chunk_size)
5019 return -EINVAL; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005020 if (mddev->bitmap)
5021 /* Cannot grow a bitmap yet */
5022 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005023 if (mddev->degraded > conf->max_degraded)
5024 return -EINVAL;
5025 if (mddev->delta_disks < 0) {
5026 /* We might be able to shrink, but the devices must
5027 * be made bigger first.
5028 * For raid6, 4 is the minimum size.
5029 * Otherwise 2 is the minimum
5030 */
5031 int min = 2;
5032 if (mddev->level == 6)
5033 min = 4;
5034 if (mddev->raid_disks + mddev->delta_disks < min)
5035 return -EINVAL;
5036 }
NeilBrown29269552006-03-27 01:18:10 -08005037
5038 /* Can only proceed if there are plenty of stripe_heads.
5039 * We need a minimum of one full stripe,, and for sensible progress
5040 * it is best to have about 4 times that.
5041 * If we require 4 times, then the default 256 4K stripe_heads will
5042 * allow for chunk sizes up to 256K, which is probably OK.
5043 * If the chunk size is greater, user-space should request more
5044 * stripe_heads first.
5045 */
NeilBrown63c70c42006-03-27 01:18:13 -08005046 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
5047 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08005048 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
NeilBrown784052e2009-03-31 15:19:07 +11005049 (max(mddev->chunk_size, mddev->new_chunk)
5050 / STRIPE_SIZE)*4);
NeilBrown29269552006-03-27 01:18:10 -08005051 return -ENOSPC;
5052 }
5053
NeilBrownec32a2b2009-03-31 15:17:38 +11005054 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005055}
5056
5057static int raid5_start_reshape(mddev_t *mddev)
5058{
5059 raid5_conf_t *conf = mddev_to_conf(mddev);
5060 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005061 int spares = 0;
5062 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005063 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005064
NeilBrownf4168852007-02-28 20:11:53 -08005065 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005066 return -EBUSY;
5067
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005068 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005069 if (rdev->raid_disk < 0 &&
5070 !test_bit(Faulty, &rdev->flags))
5071 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005072
NeilBrownf4168852007-02-28 20:11:53 -08005073 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005074 /* Not enough devices even to make a degraded array
5075 * of that size
5076 */
5077 return -EINVAL;
5078
NeilBrownec32a2b2009-03-31 15:17:38 +11005079 /* Refuse to reduce size of the array. Any reductions in
5080 * array size must be through explicit setting of array_size
5081 * attribute.
5082 */
5083 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5084 < mddev->array_sectors) {
5085 printk(KERN_ERR "md: %s: array size must be reduced "
5086 "before number of disks\n", mdname(mddev));
5087 return -EINVAL;
5088 }
5089
NeilBrownf6705572006-03-27 01:18:11 -08005090 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005091 spin_lock_irq(&conf->device_lock);
5092 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005093 conf->raid_disks += mddev->delta_disks;
NeilBrown88ce4932009-03-31 15:24:23 +11005094 conf->prev_chunk = conf->chunk_size;
5095 conf->chunk_size = mddev->new_chunk;
5096 conf->prev_algo = conf->algorithm;
5097 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005098 if (mddev->delta_disks < 0)
5099 conf->reshape_progress = raid5_size(mddev, 0, 0);
5100 else
5101 conf->reshape_progress = 0;
5102 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005103 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005104 spin_unlock_irq(&conf->device_lock);
5105
5106 /* Add some new drives, as many as will fit.
5107 * We know there are enough to make the newly sized array work.
5108 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005109 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005110 if (rdev->raid_disk < 0 &&
5111 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005112 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005113 char nm[20];
5114 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005115 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005116 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005117 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005118 if (sysfs_create_link(&mddev->kobj,
5119 &rdev->kobj, nm))
5120 printk(KERN_WARNING
5121 "raid5: failed to create "
5122 " link %s for %s\n",
5123 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005124 } else
5125 break;
5126 }
5127
NeilBrownec32a2b2009-03-31 15:17:38 +11005128 if (mddev->delta_disks > 0) {
5129 spin_lock_irqsave(&conf->device_lock, flags);
5130 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5131 - added_devices;
5132 spin_unlock_irqrestore(&conf->device_lock, flags);
5133 }
NeilBrown63c70c42006-03-27 01:18:13 -08005134 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08005135 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005136 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005137
NeilBrown29269552006-03-27 01:18:10 -08005138 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5139 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5140 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5141 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5142 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5143 "%s_reshape");
5144 if (!mddev->sync_thread) {
5145 mddev->recovery = 0;
5146 spin_lock_irq(&conf->device_lock);
5147 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005148 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005149 spin_unlock_irq(&conf->device_lock);
5150 return -EAGAIN;
5151 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005152 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005153 md_wakeup_thread(mddev->sync_thread);
5154 md_new_event(mddev);
5155 return 0;
5156}
NeilBrown29269552006-03-27 01:18:10 -08005157
NeilBrownec32a2b2009-03-31 15:17:38 +11005158/* This is called from the reshape thread and should make any
5159 * changes needed in 'conf'
5160 */
NeilBrown29269552006-03-27 01:18:10 -08005161static void end_reshape(raid5_conf_t *conf)
5162{
NeilBrown29269552006-03-27 01:18:10 -08005163
NeilBrownf6705572006-03-27 01:18:11 -08005164 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005165
NeilBrownf6705572006-03-27 01:18:11 -08005166 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005167 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005168 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005169 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005170 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005171
5172 /* read-ahead size must cover two whole stripes, which is
5173 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5174 */
5175 {
NeilBrowncea9c222009-03-31 15:15:05 +11005176 int data_disks = conf->raid_disks - conf->max_degraded;
5177 int stripe = data_disks * (conf->chunk_size
5178 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005179 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5180 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5181 }
NeilBrown29269552006-03-27 01:18:10 -08005182 }
NeilBrown29269552006-03-27 01:18:10 -08005183}
5184
NeilBrownec32a2b2009-03-31 15:17:38 +11005185/* This is called from the raid5d thread with mddev_lock held.
5186 * It makes config changes to the device.
5187 */
NeilBrowncea9c222009-03-31 15:15:05 +11005188static void raid5_finish_reshape(mddev_t *mddev)
5189{
5190 struct block_device *bdev;
NeilBrown88ce4932009-03-31 15:24:23 +11005191 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11005192
5193 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5194
NeilBrownec32a2b2009-03-31 15:17:38 +11005195 if (mddev->delta_disks > 0) {
5196 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5197 set_capacity(mddev->gendisk, mddev->array_sectors);
5198 mddev->changed = 1;
NeilBrowncea9c222009-03-31 15:15:05 +11005199
NeilBrownec32a2b2009-03-31 15:17:38 +11005200 bdev = bdget_disk(mddev->gendisk, 0);
5201 if (bdev) {
5202 mutex_lock(&bdev->bd_inode->i_mutex);
5203 i_size_write(bdev->bd_inode,
5204 (loff_t)mddev->array_sectors << 9);
5205 mutex_unlock(&bdev->bd_inode->i_mutex);
5206 bdput(bdev);
5207 }
5208 } else {
5209 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005210 mddev->degraded = conf->raid_disks;
5211 for (d = 0; d < conf->raid_disks ; d++)
5212 if (conf->disks[d].rdev &&
5213 test_bit(In_sync,
5214 &conf->disks[d].rdev->flags))
5215 mddev->degraded--;
5216 for (d = conf->raid_disks ;
5217 d < conf->raid_disks - mddev->delta_disks;
5218 d++)
5219 raid5_remove_disk(mddev, d);
NeilBrowncea9c222009-03-31 15:15:05 +11005220 }
NeilBrown88ce4932009-03-31 15:24:23 +11005221 mddev->layout = conf->algorithm;
5222 mddev->chunk_size = conf->chunk_size;
NeilBrownec32a2b2009-03-31 15:17:38 +11005223 mddev->reshape_position = MaxSector;
5224 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005225 }
5226}
5227
NeilBrown72626682005-09-09 16:23:54 -07005228static void raid5_quiesce(mddev_t *mddev, int state)
5229{
5230 raid5_conf_t *conf = mddev_to_conf(mddev);
5231
5232 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005233 case 2: /* resume for a suspend */
5234 wake_up(&conf->wait_for_overlap);
5235 break;
5236
NeilBrown72626682005-09-09 16:23:54 -07005237 case 1: /* stop all writes */
5238 spin_lock_irq(&conf->device_lock);
5239 conf->quiesce = 1;
5240 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005241 atomic_read(&conf->active_stripes) == 0 &&
5242 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005243 conf->device_lock, /* nothing */);
5244 spin_unlock_irq(&conf->device_lock);
5245 break;
5246
5247 case 0: /* re-enable writes */
5248 spin_lock_irq(&conf->device_lock);
5249 conf->quiesce = 0;
5250 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005251 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005252 spin_unlock_irq(&conf->device_lock);
5253 break;
5254 }
NeilBrown72626682005-09-09 16:23:54 -07005255}
NeilBrownb15c2e52006-01-06 00:20:16 -08005256
NeilBrownd562b0c2009-03-31 14:39:39 +11005257
5258static void *raid5_takeover_raid1(mddev_t *mddev)
5259{
5260 int chunksect;
5261
5262 if (mddev->raid_disks != 2 ||
5263 mddev->degraded > 1)
5264 return ERR_PTR(-EINVAL);
5265
5266 /* Should check if there are write-behind devices? */
5267
5268 chunksect = 64*2; /* 64K by default */
5269
5270 /* The array must be an exact multiple of chunksize */
5271 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5272 chunksect >>= 1;
5273
5274 if ((chunksect<<9) < STRIPE_SIZE)
5275 /* array size does not allow a suitable chunk size */
5276 return ERR_PTR(-EINVAL);
5277
5278 mddev->new_level = 5;
5279 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5280 mddev->new_chunk = chunksect << 9;
5281
5282 return setup_conf(mddev);
5283}
5284
NeilBrownfc9739c2009-03-31 14:57:20 +11005285static void *raid5_takeover_raid6(mddev_t *mddev)
5286{
5287 int new_layout;
5288
5289 switch (mddev->layout) {
5290 case ALGORITHM_LEFT_ASYMMETRIC_6:
5291 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5292 break;
5293 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5294 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5295 break;
5296 case ALGORITHM_LEFT_SYMMETRIC_6:
5297 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5298 break;
5299 case ALGORITHM_RIGHT_SYMMETRIC_6:
5300 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5301 break;
5302 case ALGORITHM_PARITY_0_6:
5303 new_layout = ALGORITHM_PARITY_0;
5304 break;
5305 case ALGORITHM_PARITY_N:
5306 new_layout = ALGORITHM_PARITY_N;
5307 break;
5308 default:
5309 return ERR_PTR(-EINVAL);
5310 }
5311 mddev->new_level = 5;
5312 mddev->new_layout = new_layout;
5313 mddev->delta_disks = -1;
5314 mddev->raid_disks -= 1;
5315 return setup_conf(mddev);
5316}
5317
NeilBrownd562b0c2009-03-31 14:39:39 +11005318
NeilBrownb3546032009-03-31 14:56:41 +11005319static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5320{
NeilBrown88ce4932009-03-31 15:24:23 +11005321 /* For a 2-drive array, the layout and chunk size can be changed
5322 * immediately as not restriping is needed.
5323 * For larger arrays we record the new value - after validation
5324 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005325 */
5326 raid5_conf_t *conf = mddev_to_conf(mddev);
5327
5328 if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
5329 return -EINVAL;
5330 if (new_chunk > 0) {
5331 if (new_chunk & (new_chunk-1))
5332 /* not a power of 2 */
5333 return -EINVAL;
5334 if (new_chunk < PAGE_SIZE)
5335 return -EINVAL;
5336 if (mddev->array_sectors & ((new_chunk>>9)-1))
5337 /* not factor of array size */
5338 return -EINVAL;
5339 }
5340
5341 /* They look valid */
5342
NeilBrown88ce4932009-03-31 15:24:23 +11005343 if (mddev->raid_disks == 2) {
NeilBrownb3546032009-03-31 14:56:41 +11005344
NeilBrown88ce4932009-03-31 15:24:23 +11005345 if (new_layout >= 0) {
5346 conf->algorithm = new_layout;
5347 mddev->layout = mddev->new_layout = new_layout;
5348 }
5349 if (new_chunk > 0) {
5350 conf->chunk_size = new_chunk;
5351 mddev->chunk_size = mddev->new_chunk = new_chunk;
5352 }
5353 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5354 md_wakeup_thread(mddev->thread);
5355 } else {
5356 if (new_layout >= 0)
5357 mddev->new_layout = new_layout;
5358 if (new_chunk > 0)
5359 mddev->new_chunk = new_chunk;
NeilBrownb3546032009-03-31 14:56:41 +11005360 }
NeilBrown88ce4932009-03-31 15:24:23 +11005361 return 0;
5362}
5363
5364static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5365{
5366 if (new_layout >= 0 && !algorithm_valid_raid6(new_layout))
5367 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005368 if (new_chunk > 0) {
NeilBrown88ce4932009-03-31 15:24:23 +11005369 if (new_chunk & (new_chunk-1))
5370 /* not a power of 2 */
5371 return -EINVAL;
5372 if (new_chunk < PAGE_SIZE)
5373 return -EINVAL;
5374 if (mddev->array_sectors & ((new_chunk>>9)-1))
5375 /* not factor of array size */
5376 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005377 }
NeilBrown88ce4932009-03-31 15:24:23 +11005378
5379 /* They look valid */
5380
5381 if (new_layout >= 0)
5382 mddev->new_layout = new_layout;
5383 if (new_chunk > 0)
5384 mddev->new_chunk = new_chunk;
5385
NeilBrownb3546032009-03-31 14:56:41 +11005386 return 0;
5387}
5388
NeilBrownd562b0c2009-03-31 14:39:39 +11005389static void *raid5_takeover(mddev_t *mddev)
5390{
5391 /* raid5 can take over:
5392 * raid0 - if all devices are the same - make it a raid4 layout
5393 * raid1 - if there are two drives. We need to know the chunk size
5394 * raid4 - trivial - just use a raid4 layout.
5395 * raid6 - Providing it is a *_6 layout
5396 *
5397 * For now, just do raid1
5398 */
5399
5400 if (mddev->level == 1)
5401 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005402 if (mddev->level == 4) {
5403 mddev->new_layout = ALGORITHM_PARITY_N;
5404 mddev->new_level = 5;
5405 return setup_conf(mddev);
5406 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005407 if (mddev->level == 6)
5408 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005409
5410 return ERR_PTR(-EINVAL);
5411}
5412
5413
NeilBrown245f46c2009-03-31 14:39:39 +11005414static struct mdk_personality raid5_personality;
5415
5416static void *raid6_takeover(mddev_t *mddev)
5417{
5418 /* Currently can only take over a raid5. We map the
5419 * personality to an equivalent raid6 personality
5420 * with the Q block at the end.
5421 */
5422 int new_layout;
5423
5424 if (mddev->pers != &raid5_personality)
5425 return ERR_PTR(-EINVAL);
5426 if (mddev->degraded > 1)
5427 return ERR_PTR(-EINVAL);
5428 if (mddev->raid_disks > 253)
5429 return ERR_PTR(-EINVAL);
5430 if (mddev->raid_disks < 3)
5431 return ERR_PTR(-EINVAL);
5432
5433 switch (mddev->layout) {
5434 case ALGORITHM_LEFT_ASYMMETRIC:
5435 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5436 break;
5437 case ALGORITHM_RIGHT_ASYMMETRIC:
5438 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5439 break;
5440 case ALGORITHM_LEFT_SYMMETRIC:
5441 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5442 break;
5443 case ALGORITHM_RIGHT_SYMMETRIC:
5444 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5445 break;
5446 case ALGORITHM_PARITY_0:
5447 new_layout = ALGORITHM_PARITY_0_6;
5448 break;
5449 case ALGORITHM_PARITY_N:
5450 new_layout = ALGORITHM_PARITY_N;
5451 break;
5452 default:
5453 return ERR_PTR(-EINVAL);
5454 }
5455 mddev->new_level = 6;
5456 mddev->new_layout = new_layout;
5457 mddev->delta_disks = 1;
5458 mddev->raid_disks += 1;
5459 return setup_conf(mddev);
5460}
5461
5462
NeilBrown16a53ec2006-06-26 00:27:38 -07005463static struct mdk_personality raid6_personality =
5464{
5465 .name = "raid6",
5466 .level = 6,
5467 .owner = THIS_MODULE,
5468 .make_request = make_request,
5469 .run = run,
5470 .stop = stop,
5471 .status = status,
5472 .error_handler = error,
5473 .hot_add_disk = raid5_add_disk,
5474 .hot_remove_disk= raid5_remove_disk,
5475 .spare_active = raid5_spare_active,
5476 .sync_request = sync_request,
5477 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005478 .size = raid5_size,
NeilBrownf4168852007-02-28 20:11:53 -08005479 .check_reshape = raid5_check_reshape,
5480 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005481 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005482 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005483 .takeover = raid6_takeover,
NeilBrown88ce4932009-03-31 15:24:23 +11005484 .reconfig = raid6_reconfig,
NeilBrown16a53ec2006-06-26 00:27:38 -07005485};
NeilBrown2604b702006-01-06 00:20:36 -08005486static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487{
5488 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005489 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 .owner = THIS_MODULE,
5491 .make_request = make_request,
5492 .run = run,
5493 .stop = stop,
5494 .status = status,
5495 .error_handler = error,
5496 .hot_add_disk = raid5_add_disk,
5497 .hot_remove_disk= raid5_remove_disk,
5498 .spare_active = raid5_spare_active,
5499 .sync_request = sync_request,
5500 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005501 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005502 .check_reshape = raid5_check_reshape,
5503 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005504 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005505 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005506 .takeover = raid5_takeover,
NeilBrownb3546032009-03-31 14:56:41 +11005507 .reconfig = raid5_reconfig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508};
5509
NeilBrown2604b702006-01-06 00:20:36 -08005510static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
NeilBrown2604b702006-01-06 00:20:36 -08005512 .name = "raid4",
5513 .level = 4,
5514 .owner = THIS_MODULE,
5515 .make_request = make_request,
5516 .run = run,
5517 .stop = stop,
5518 .status = status,
5519 .error_handler = error,
5520 .hot_add_disk = raid5_add_disk,
5521 .hot_remove_disk= raid5_remove_disk,
5522 .spare_active = raid5_spare_active,
5523 .sync_request = sync_request,
5524 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005525 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005526 .check_reshape = raid5_check_reshape,
5527 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005528 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005529 .quiesce = raid5_quiesce,
5530};
5531
5532static int __init raid5_init(void)
5533{
NeilBrown16a53ec2006-06-26 00:27:38 -07005534 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005535 register_md_personality(&raid5_personality);
5536 register_md_personality(&raid4_personality);
5537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538}
5539
NeilBrown2604b702006-01-06 00:20:36 -08005540static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
NeilBrown16a53ec2006-06-26 00:27:38 -07005542 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005543 unregister_md_personality(&raid5_personality);
5544 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545}
5546
5547module_init(raid5_init);
5548module_exit(raid5_exit);
5549MODULE_LICENSE("GPL");
5550MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005551MODULE_ALIAS("md-raid5");
5552MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005553MODULE_ALIAS("md-level-5");
5554MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005555MODULE_ALIAS("md-personality-8"); /* RAID6 */
5556MODULE_ALIAS("md-raid6");
5557MODULE_ALIAS("md-level-6");
5558
5559/* This used to be two separate modules, they were: */
5560MODULE_ALIAS("raid5");
5561MODULE_ALIAS("raid6");