blob: 73cdf43a647958c93328cab488609532aa5c8c13 [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>
NeilBrown43b2e5d2009-03-31 14:33:13 +110051#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110052#include "raid5.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110053#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * Stripe cache
57 */
58
59#define NR_STRIPES 256
60#define STRIPE_SIZE PAGE_SIZE
61#define STRIPE_SHIFT (PAGE_SHIFT - 9)
62#define STRIPE_SECTORS (STRIPE_SIZE>>9)
63#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070064#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080065#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define HASH_MASK (NR_HASH - 1)
67
NeilBrownfccddba2006-01-06 00:20:33 -080068#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
75 * be valid.
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
78 */
79#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
80/*
81 * The following can be used to debug the driver
82 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define RAID5_PARANOIA 1
84#if RAID5_PARANOIA && defined(CONFIG_SMP)
85# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
86#else
87# define CHECK_DEVLOCK()
88#endif
89
Dan Williams45b42332007-07-09 11:56:43 -070090#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define inline
92#define __inline__
93#endif
94
Bernd Schubert6be9d492008-05-23 13:04:34 -070095#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
96
Jens Axboe960e7392008-08-15 10:41:18 +020097/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020098 * We maintain a biased count of active stripes in the bottom 16 bits of
99 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200100 */
101static inline int raid5_bi_phys_segments(struct bio *bio)
102{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200103 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200104}
105
106static inline int raid5_bi_hw_segments(struct bio *bio)
107{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200108 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200109}
110
111static inline int raid5_dec_bi_phys_segments(struct bio *bio)
112{
113 --bio->bi_phys_segments;
114 return raid5_bi_phys_segments(bio);
115}
116
117static inline int raid5_dec_bi_hw_segments(struct bio *bio)
118{
119 unsigned short val = raid5_bi_hw_segments(bio);
120
121 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200122 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200123 return val;
124}
125
126static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
127{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200128 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200129}
130
NeilBrownd0dabf72009-03-31 14:39:38 +1100131/* Find first data disk in a raid6 stripe */
132static inline int raid6_d0(struct stripe_head *sh)
133{
NeilBrown67cc2b82009-03-31 14:39:38 +1100134 if (sh->ddf_layout)
135 /* ddf always start from first device */
136 return 0;
137 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100138 if (sh->qd_idx == sh->disks - 1)
139 return 0;
140 else
141 return sh->qd_idx + 1;
142}
NeilBrown16a53ec2006-06-26 00:27:38 -0700143static inline int raid6_next_disk(int disk, int raid_disks)
144{
145 disk++;
146 return (disk < raid_disks) ? disk : 0;
147}
Dan Williamsa4456852007-07-09 11:56:43 -0700148
NeilBrownd0dabf72009-03-31 14:39:38 +1100149/* When walking through the disks in a raid5, starting at raid6_d0,
150 * We need to map each disk to a 'slot', where the data disks are slot
151 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
152 * is raid_disks-1. This help does that mapping.
153 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100154static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
155 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100156{
157 int slot;
NeilBrown67cc2b82009-03-31 14:39:38 +1100158
NeilBrownd0dabf72009-03-31 14:39:38 +1100159 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100160 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100161 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100162 return syndrome_disks + 1;
NeilBrownd0dabf72009-03-31 14:39:38 +1100163 slot = (*count)++;
164 return slot;
165}
166
Dan Williamsa4456852007-07-09 11:56:43 -0700167static void return_io(struct bio *return_bi)
168{
169 struct bio *bi = return_bi;
170 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700171
172 return_bi = bi->bi_next;
173 bi->bi_next = NULL;
174 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000175 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700176 bi = return_bi;
177 }
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void print_raid5_conf (raid5_conf_t *conf);
181
Dan Williams600aa102008-06-28 08:32:05 +1000182static int stripe_operations_active(struct stripe_head *sh)
183{
184 return sh->check_state || sh->reconstruct_state ||
185 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
186 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
187}
188
Arjan van de Ven858119e2006-01-14 13:20:43 -0800189static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200192 BUG_ON(!list_empty(&sh->lru));
193 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700195 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700197 blk_plug_device(conf->mddev->queue);
198 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700199 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700200 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700201 blk_plug_device(conf->mddev->queue);
202 } else {
NeilBrown72626682005-09-09 16:23:54 -0700203 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 md_wakeup_thread(conf->mddev->thread);
207 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000208 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
210 atomic_dec(&conf->preread_active_stripes);
211 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
212 md_wakeup_thread(conf->mddev->thread);
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800215 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
216 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800218 if (conf->retry_read_aligned)
219 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 }
223}
NeilBrownd0dabf72009-03-31 14:39:38 +1100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static void release_stripe(struct stripe_head *sh)
226{
227 raid5_conf_t *conf = sh->raid_conf;
228 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 spin_lock_irqsave(&conf->device_lock, flags);
231 __release_stripe(conf, sh);
232 spin_unlock_irqrestore(&conf->device_lock, flags);
233}
234
NeilBrownfccddba2006-01-06 00:20:33 -0800235static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Dan Williams45b42332007-07-09 11:56:43 -0700237 pr_debug("remove_hash(), stripe %llu\n",
238 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
NeilBrownfccddba2006-01-06 00:20:33 -0800240 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
NeilBrown16a53ec2006-06-26 00:27:38 -0700243static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
NeilBrownfccddba2006-01-06 00:20:33 -0800245 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Dan Williams45b42332007-07-09 11:56:43 -0700247 pr_debug("insert_hash(), stripe %llu\n",
248 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800251 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254
255/* find an idle stripe, make sure it is unhashed, and return it. */
256static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
257{
258 struct stripe_head *sh = NULL;
259 struct list_head *first;
260
261 CHECK_DEVLOCK();
262 if (list_empty(&conf->inactive_list))
263 goto out;
264 first = conf->inactive_list.next;
265 sh = list_entry(first, struct stripe_head, lru);
266 list_del_init(first);
267 remove_hash(sh);
268 atomic_inc(&conf->active_stripes);
269out:
270 return sh;
271}
272
273static void shrink_buffers(struct stripe_head *sh, int num)
274{
275 struct page *p;
276 int i;
277
278 for (i=0; i<num ; i++) {
279 p = sh->dev[i].page;
280 if (!p)
281 continue;
282 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800283 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285}
286
287static int grow_buffers(struct stripe_head *sh, int num)
288{
289 int i;
290
291 for (i=0; i<num; i++) {
292 struct page *page;
293
294 if (!(page = alloc_page(GFP_KERNEL))) {
295 return 1;
296 }
297 sh->dev[i].page = page;
298 }
299 return 0;
300}
301
NeilBrownd710e132008-10-13 11:55:12 +1100302static void raid5_build_block(struct stripe_head *sh, int i);
NeilBrown911d4ee2009-03-31 14:39:38 +1100303static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
304 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
NeilBrownb5663ba2009-03-31 14:39:38 +1100306static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200311 BUG_ON(atomic_read(&sh->count) != 0);
312 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000313 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700316 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 (unsigned long long)sh->sector);
318
319 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700320
NeilBrown86b42c72009-03-31 15:19:03 +1100321 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100322 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->state = 0;
326
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800327
328 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct r5dev *dev = &sh->dev[i];
330
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 test_bit(R5_LOCKED, &dev->flags));
337 BUG();
338 }
339 dev->flags = 0;
340 raid5_build_block(sh, i);
341 }
342 insert_hash(conf, sh);
343}
344
NeilBrown86b42c72009-03-31 15:19:03 +1100345static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
346 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800349 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700352 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800353 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100354 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700356 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return NULL;
358}
359
360static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200361static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
NeilBrownb5663ba2009-03-31 14:39:38 +1100363static struct stripe_head *
364get_active_stripe(raid5_conf_t *conf, sector_t sector,
365 int previous, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct stripe_head *sh;
368
Dan Williams45b42332007-07-09 11:56:43 -0700369 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 spin_lock_irq(&conf->device_lock);
372
373 do {
NeilBrown72626682005-09-09 16:23:54 -0700374 wait_event_lock_irq(conf->wait_for_stripe,
375 conf->quiesce == 0,
376 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100377 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!sh) {
379 if (!conf->inactive_blocked)
380 sh = get_free_stripe(conf);
381 if (noblock && sh == NULL)
382 break;
383 if (!sh) {
384 conf->inactive_blocked = 1;
385 wait_event_lock_irq(conf->wait_for_stripe,
386 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800387 (atomic_read(&conf->active_stripes)
388 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 || !conf->inactive_blocked),
390 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700391 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 );
393 conf->inactive_blocked = 0;
394 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100395 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
397 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200398 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 } else {
400 if (!test_bit(STRIPE_HANDLE, &sh->state))
401 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700402 if (list_empty(&sh->lru) &&
403 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700404 BUG();
405 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 }
408 } while (sh == NULL);
409
410 if (sh)
411 atomic_inc(&sh->count);
412
413 spin_unlock_irq(&conf->device_lock);
414 return sh;
415}
416
NeilBrown6712ecf2007-09-27 12:47:43 +0200417static void
418raid5_end_read_request(struct bio *bi, int error);
419static void
420raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700421
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000422static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700423{
424 raid5_conf_t *conf = sh->raid_conf;
425 int i, disks = sh->disks;
426
427 might_sleep();
428
429 for (i = disks; i--; ) {
430 int rw;
431 struct bio *bi;
432 mdk_rdev_t *rdev;
433 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
434 rw = WRITE;
435 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
436 rw = READ;
437 else
438 continue;
439
440 bi = &sh->dev[i].req;
441
442 bi->bi_rw = rw;
443 if (rw == WRITE)
444 bi->bi_end_io = raid5_end_write_request;
445 else
446 bi->bi_end_io = raid5_end_read_request;
447
448 rcu_read_lock();
449 rdev = rcu_dereference(conf->disks[i].rdev);
450 if (rdev && test_bit(Faulty, &rdev->flags))
451 rdev = NULL;
452 if (rdev)
453 atomic_inc(&rdev->nr_pending);
454 rcu_read_unlock();
455
456 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000457 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700458 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
459
Dan Williams2b7497f2008-06-28 08:31:52 +1000460 set_bit(STRIPE_IO_STARTED, &sh->state);
461
Dan Williams91c00922007-01-02 13:52:30 -0700462 bi->bi_bdev = rdev->bdev;
463 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700464 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700465 bi->bi_rw, i);
466 atomic_inc(&sh->count);
467 bi->bi_sector = sh->sector + rdev->data_offset;
468 bi->bi_flags = 1 << BIO_UPTODATE;
469 bi->bi_vcnt = 1;
470 bi->bi_max_vecs = 1;
471 bi->bi_idx = 0;
472 bi->bi_io_vec = &sh->dev[i].vec;
473 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
474 bi->bi_io_vec[0].bv_offset = 0;
475 bi->bi_size = STRIPE_SIZE;
476 bi->bi_next = NULL;
477 if (rw == WRITE &&
478 test_bit(R5_ReWrite, &sh->dev[i].flags))
479 atomic_add(STRIPE_SECTORS,
480 &rdev->corrected_errors);
481 generic_make_request(bi);
482 } else {
483 if (rw == WRITE)
484 set_bit(STRIPE_DEGRADED, &sh->state);
485 pr_debug("skip op %ld on disc %d for sector %llu\n",
486 bi->bi_rw, i, (unsigned long long)sh->sector);
487 clear_bit(R5_LOCKED, &sh->dev[i].flags);
488 set_bit(STRIPE_HANDLE, &sh->state);
489 }
490 }
491}
492
493static struct dma_async_tx_descriptor *
494async_copy_data(int frombio, struct bio *bio, struct page *page,
495 sector_t sector, struct dma_async_tx_descriptor *tx)
496{
497 struct bio_vec *bvl;
498 struct page *bio_page;
499 int i;
500 int page_offset;
501
502 if (bio->bi_sector >= sector)
503 page_offset = (signed)(bio->bi_sector - sector) * 512;
504 else
505 page_offset = (signed)(sector - bio->bi_sector) * -512;
506 bio_for_each_segment(bvl, bio, i) {
507 int len = bio_iovec_idx(bio, i)->bv_len;
508 int clen;
509 int b_offset = 0;
510
511 if (page_offset < 0) {
512 b_offset = -page_offset;
513 page_offset += b_offset;
514 len -= b_offset;
515 }
516
517 if (len > 0 && page_offset + len > STRIPE_SIZE)
518 clen = STRIPE_SIZE - page_offset;
519 else
520 clen = len;
521
522 if (clen > 0) {
523 b_offset += bio_iovec_idx(bio, i)->bv_offset;
524 bio_page = bio_iovec_idx(bio, i)->bv_page;
525 if (frombio)
526 tx = async_memcpy(page, bio_page, page_offset,
527 b_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700528 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700529 tx, NULL, NULL);
530 else
531 tx = async_memcpy(bio_page, page, b_offset,
532 page_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700533 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700534 tx, NULL, NULL);
535 }
536 if (clen < len) /* hit end of page */
537 break;
538 page_offset += len;
539 }
540
541 return tx;
542}
543
544static void ops_complete_biofill(void *stripe_head_ref)
545{
546 struct stripe_head *sh = stripe_head_ref;
547 struct bio *return_bi = NULL;
548 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700549 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700550
Harvey Harrisone46b2722008-04-28 02:15:50 -0700551 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700552 (unsigned long long)sh->sector);
553
554 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000555 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700556 for (i = sh->disks; i--; ) {
557 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700558
559 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700560 /* and check if we need to reply to a read request,
561 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000562 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700563 */
564 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700565 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700566
Dan Williams91c00922007-01-02 13:52:30 -0700567 BUG_ON(!dev->read);
568 rbi = dev->read;
569 dev->read = NULL;
570 while (rbi && rbi->bi_sector <
571 dev->sector + STRIPE_SECTORS) {
572 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200573 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700574 rbi->bi_next = return_bi;
575 return_bi = rbi;
576 }
Dan Williams91c00922007-01-02 13:52:30 -0700577 rbi = rbi2;
578 }
579 }
580 }
Dan Williams83de75c2008-06-28 08:31:58 +1000581 spin_unlock_irq(&conf->device_lock);
582 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700583
584 return_io(return_bi);
585
Dan Williamse4d84902007-09-24 10:06:13 -0700586 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700587 release_stripe(sh);
588}
589
590static void ops_run_biofill(struct stripe_head *sh)
591{
592 struct dma_async_tx_descriptor *tx = NULL;
593 raid5_conf_t *conf = sh->raid_conf;
594 int i;
595
Harvey Harrisone46b2722008-04-28 02:15:50 -0700596 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700597 (unsigned long long)sh->sector);
598
599 for (i = sh->disks; i--; ) {
600 struct r5dev *dev = &sh->dev[i];
601 if (test_bit(R5_Wantfill, &dev->flags)) {
602 struct bio *rbi;
603 spin_lock_irq(&conf->device_lock);
604 dev->read = rbi = dev->toread;
605 dev->toread = NULL;
606 spin_unlock_irq(&conf->device_lock);
607 while (rbi && rbi->bi_sector <
608 dev->sector + STRIPE_SECTORS) {
609 tx = async_copy_data(0, rbi, dev->page,
610 dev->sector, tx);
611 rbi = r5_next_bio(rbi, dev->sector);
612 }
613 }
614 }
615
616 atomic_inc(&sh->count);
617 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
618 ops_complete_biofill, sh);
619}
620
621static void ops_complete_compute5(void *stripe_head_ref)
622{
623 struct stripe_head *sh = stripe_head_ref;
624 int target = sh->ops.target;
625 struct r5dev *tgt = &sh->dev[target];
626
Harvey Harrisone46b2722008-04-28 02:15:50 -0700627 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700628 (unsigned long long)sh->sector);
629
630 set_bit(R5_UPTODATE, &tgt->flags);
631 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
632 clear_bit(R5_Wantcompute, &tgt->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +1000633 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
634 if (sh->check_state == check_state_compute_run)
635 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700636 set_bit(STRIPE_HANDLE, &sh->state);
637 release_stripe(sh);
638}
639
Dan Williams7b3a8712008-06-28 08:32:09 +1000640static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700641{
642 /* kernel stack size limits the total number of disks */
643 int disks = sh->disks;
644 struct page *xor_srcs[disks];
645 int target = sh->ops.target;
646 struct r5dev *tgt = &sh->dev[target];
647 struct page *xor_dest = tgt->page;
648 int count = 0;
649 struct dma_async_tx_descriptor *tx;
650 int i;
651
652 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700653 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700654 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
655
656 for (i = disks; i--; )
657 if (i != target)
658 xor_srcs[count++] = sh->dev[i].page;
659
660 atomic_inc(&sh->count);
661
662 if (unlikely(count == 1))
663 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
664 0, NULL, ops_complete_compute5, sh);
665 else
666 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
667 ASYNC_TX_XOR_ZERO_DST, NULL,
668 ops_complete_compute5, sh);
669
Dan Williams91c00922007-01-02 13:52:30 -0700670 return tx;
671}
672
673static void ops_complete_prexor(void *stripe_head_ref)
674{
675 struct stripe_head *sh = stripe_head_ref;
676
Harvey Harrisone46b2722008-04-28 02:15:50 -0700677 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700678 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700679}
680
681static struct dma_async_tx_descriptor *
682ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
683{
684 /* kernel stack size limits the total number of disks */
685 int disks = sh->disks;
686 struct page *xor_srcs[disks];
687 int count = 0, pd_idx = sh->pd_idx, i;
688
689 /* existing parity data subtracted */
690 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
691
Harvey Harrisone46b2722008-04-28 02:15:50 -0700692 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700693 (unsigned long long)sh->sector);
694
695 for (i = disks; i--; ) {
696 struct r5dev *dev = &sh->dev[i];
697 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000698 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700699 xor_srcs[count++] = dev->page;
700 }
701
702 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
703 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
704 ops_complete_prexor, sh);
705
706 return tx;
707}
708
709static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000710ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700711{
712 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000713 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700714
Harvey Harrisone46b2722008-04-28 02:15:50 -0700715 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700716 (unsigned long long)sh->sector);
717
718 for (i = disks; i--; ) {
719 struct r5dev *dev = &sh->dev[i];
720 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700721
Dan Williamsd8ee0722008-06-28 08:32:06 +1000722 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700723 struct bio *wbi;
724
725 spin_lock(&sh->lock);
726 chosen = dev->towrite;
727 dev->towrite = NULL;
728 BUG_ON(dev->written);
729 wbi = dev->written = chosen;
730 spin_unlock(&sh->lock);
731
732 while (wbi && wbi->bi_sector <
733 dev->sector + STRIPE_SECTORS) {
734 tx = async_copy_data(1, wbi, dev->page,
735 dev->sector, tx);
736 wbi = r5_next_bio(wbi, dev->sector);
737 }
738 }
739 }
740
741 return tx;
742}
743
744static void ops_complete_postxor(void *stripe_head_ref)
745{
746 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700747 int disks = sh->disks, i, pd_idx = sh->pd_idx;
748
Harvey Harrisone46b2722008-04-28 02:15:50 -0700749 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700750 (unsigned long long)sh->sector);
751
752 for (i = disks; i--; ) {
753 struct r5dev *dev = &sh->dev[i];
754 if (dev->written || i == pd_idx)
755 set_bit(R5_UPTODATE, &dev->flags);
756 }
757
Dan Williamsd8ee0722008-06-28 08:32:06 +1000758 if (sh->reconstruct_state == reconstruct_state_drain_run)
759 sh->reconstruct_state = reconstruct_state_drain_result;
760 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
761 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
762 else {
763 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
764 sh->reconstruct_state = reconstruct_state_result;
765 }
Dan Williams91c00922007-01-02 13:52:30 -0700766
767 set_bit(STRIPE_HANDLE, &sh->state);
768 release_stripe(sh);
769}
770
771static void
Dan Williamsd8ee0722008-06-28 08:32:06 +1000772ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700773{
774 /* kernel stack size limits the total number of disks */
775 int disks = sh->disks;
776 struct page *xor_srcs[disks];
777
778 int count = 0, pd_idx = sh->pd_idx, i;
779 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000780 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700781 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700782
Harvey Harrisone46b2722008-04-28 02:15:50 -0700783 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700784 (unsigned long long)sh->sector);
785
786 /* check if prexor is active which means only process blocks
787 * that are part of a read-modify-write (written)
788 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000789 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
790 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700791 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
792 for (i = disks; i--; ) {
793 struct r5dev *dev = &sh->dev[i];
794 if (dev->written)
795 xor_srcs[count++] = dev->page;
796 }
797 } else {
798 xor_dest = sh->dev[pd_idx].page;
799 for (i = disks; i--; ) {
800 struct r5dev *dev = &sh->dev[i];
801 if (i != pd_idx)
802 xor_srcs[count++] = dev->page;
803 }
804 }
805
Dan Williams91c00922007-01-02 13:52:30 -0700806 /* 1/ if we prexor'd then the dest is reused as a source
807 * 2/ if we did not prexor then we are redoing the parity
808 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
809 * for the synchronous xor case
810 */
811 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
812 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
813
814 atomic_inc(&sh->count);
815
816 if (unlikely(count == 1)) {
817 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
818 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000819 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700820 } else
821 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000822 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700823}
824
825static void ops_complete_check(void *stripe_head_ref)
826{
827 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700828
Harvey Harrisone46b2722008-04-28 02:15:50 -0700829 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700830 (unsigned long long)sh->sector);
831
Dan Williamsecc65c92008-06-28 08:31:57 +1000832 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700833 set_bit(STRIPE_HANDLE, &sh->state);
834 release_stripe(sh);
835}
836
837static void ops_run_check(struct stripe_head *sh)
838{
839 /* kernel stack size limits the total number of disks */
840 int disks = sh->disks;
841 struct page *xor_srcs[disks];
842 struct dma_async_tx_descriptor *tx;
843
844 int count = 0, pd_idx = sh->pd_idx, i;
845 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
846
Harvey Harrisone46b2722008-04-28 02:15:50 -0700847 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700848 (unsigned long long)sh->sector);
849
850 for (i = disks; i--; ) {
851 struct r5dev *dev = &sh->dev[i];
852 if (i != pd_idx)
853 xor_srcs[count++] = dev->page;
854 }
855
856 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
857 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
858
Dan Williams91c00922007-01-02 13:52:30 -0700859 atomic_inc(&sh->count);
860 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
861 ops_complete_check, sh);
862}
863
Dan Williams600aa102008-06-28 08:32:05 +1000864static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700865{
866 int overlap_clear = 0, i, disks = sh->disks;
867 struct dma_async_tx_descriptor *tx = NULL;
868
Dan Williams83de75c2008-06-28 08:31:58 +1000869 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700870 ops_run_biofill(sh);
871 overlap_clear++;
872 }
873
Dan Williams7b3a8712008-06-28 08:32:09 +1000874 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
875 tx = ops_run_compute5(sh);
876 /* terminate the chain if postxor is not set to be run */
877 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
878 async_tx_ack(tx);
879 }
Dan Williams91c00922007-01-02 13:52:30 -0700880
Dan Williams600aa102008-06-28 08:32:05 +1000881 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700882 tx = ops_run_prexor(sh, tx);
883
Dan Williams600aa102008-06-28 08:32:05 +1000884 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000885 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700886 overlap_clear++;
887 }
888
Dan Williams600aa102008-06-28 08:32:05 +1000889 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd8ee0722008-06-28 08:32:06 +1000890 ops_run_postxor(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700891
Dan Williamsecc65c92008-06-28 08:31:57 +1000892 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700893 ops_run_check(sh);
894
Dan Williams91c00922007-01-02 13:52:30 -0700895 if (overlap_clear)
896 for (i = disks; i--; ) {
897 struct r5dev *dev = &sh->dev[i];
898 if (test_and_clear_bit(R5_Overlap, &dev->flags))
899 wake_up(&sh->raid_conf->wait_for_overlap);
900 }
901}
902
NeilBrown3f294f42005-11-08 21:39:25 -0800903static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800906 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
907 if (!sh)
908 return 0;
909 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
910 sh->raid_conf = conf;
911 spin_lock_init(&sh->lock);
912
913 if (grow_buffers(sh, conf->raid_disks)) {
914 shrink_buffers(sh, conf->raid_disks);
915 kmem_cache_free(conf->slab_cache, sh);
916 return 0;
917 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800918 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800919 /* we just created an active stripe so... */
920 atomic_set(&sh->count, 1);
921 atomic_inc(&conf->active_stripes);
922 INIT_LIST_HEAD(&sh->lru);
923 release_stripe(sh);
924 return 1;
925}
926
927static int grow_stripes(raid5_conf_t *conf, int num)
928{
Christoph Lametere18b8902006-12-06 20:33:20 -0800929 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int devs = conf->raid_disks;
931
NeilBrown245f46c2009-03-31 14:39:39 +1100932 sprintf(conf->cache_name[0],
933 "raid%d-%s", conf->level, mdname(conf->mddev));
934 sprintf(conf->cache_name[1],
935 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800936 conf->active_name = 0;
937 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900939 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!sc)
941 return 1;
942 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800943 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700944 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800945 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948}
NeilBrown29269552006-03-27 01:18:10 -0800949
950#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800951static int resize_stripes(raid5_conf_t *conf, int newsize)
952{
953 /* Make all the stripes able to hold 'newsize' devices.
954 * New slots in each stripe get 'page' set to a new page.
955 *
956 * This happens in stages:
957 * 1/ create a new kmem_cache and allocate the required number of
958 * stripe_heads.
959 * 2/ gather all the old stripe_heads and tranfer the pages across
960 * to the new stripe_heads. This will have the side effect of
961 * freezing the array as once all stripe_heads have been collected,
962 * no IO will be possible. Old stripe heads are freed once their
963 * pages have been transferred over, and the old kmem_cache is
964 * freed when all stripes are done.
965 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
966 * we simple return a failre status - no need to clean anything up.
967 * 4/ allocate new pages for the new slots in the new stripe_heads.
968 * If this fails, we don't bother trying the shrink the
969 * stripe_heads down again, we just leave them as they are.
970 * As each stripe_head is processed the new one is released into
971 * active service.
972 *
973 * Once step2 is started, we cannot afford to wait for a write,
974 * so we use GFP_NOIO allocations.
975 */
976 struct stripe_head *osh, *nsh;
977 LIST_HEAD(newstripes);
978 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -0700979 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -0800980 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800981 int i;
982
983 if (newsize <= conf->pool_size)
984 return 0; /* never bother to shrink */
985
Dan Williamsb5470dc2008-06-27 21:44:04 -0700986 err = md_allow_write(conf->mddev);
987 if (err)
988 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -0800989
NeilBrownad01c9e2006-03-27 01:18:07 -0800990 /* Step 1 */
991 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
992 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900993 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -0800994 if (!sc)
995 return -ENOMEM;
996
997 for (i = conf->max_nr_stripes; i; i--) {
998 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
999 if (!nsh)
1000 break;
1001
1002 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1003
1004 nsh->raid_conf = conf;
1005 spin_lock_init(&nsh->lock);
1006
1007 list_add(&nsh->lru, &newstripes);
1008 }
1009 if (i) {
1010 /* didn't get enough, give up */
1011 while (!list_empty(&newstripes)) {
1012 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1013 list_del(&nsh->lru);
1014 kmem_cache_free(sc, nsh);
1015 }
1016 kmem_cache_destroy(sc);
1017 return -ENOMEM;
1018 }
1019 /* Step 2 - Must use GFP_NOIO now.
1020 * OK, we have enough stripes, start collecting inactive
1021 * stripes and copying them over
1022 */
1023 list_for_each_entry(nsh, &newstripes, lru) {
1024 spin_lock_irq(&conf->device_lock);
1025 wait_event_lock_irq(conf->wait_for_stripe,
1026 !list_empty(&conf->inactive_list),
1027 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001028 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001029 );
1030 osh = get_free_stripe(conf);
1031 spin_unlock_irq(&conf->device_lock);
1032 atomic_set(&nsh->count, 1);
1033 for(i=0; i<conf->pool_size; i++)
1034 nsh->dev[i].page = osh->dev[i].page;
1035 for( ; i<newsize; i++)
1036 nsh->dev[i].page = NULL;
1037 kmem_cache_free(conf->slab_cache, osh);
1038 }
1039 kmem_cache_destroy(conf->slab_cache);
1040
1041 /* Step 3.
1042 * At this point, we are holding all the stripes so the array
1043 * is completely stalled, so now is a good time to resize
1044 * conf->disks.
1045 */
1046 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1047 if (ndisks) {
1048 for (i=0; i<conf->raid_disks; i++)
1049 ndisks[i] = conf->disks[i];
1050 kfree(conf->disks);
1051 conf->disks = ndisks;
1052 } else
1053 err = -ENOMEM;
1054
1055 /* Step 4, return new stripes to service */
1056 while(!list_empty(&newstripes)) {
1057 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1058 list_del_init(&nsh->lru);
1059 for (i=conf->raid_disks; i < newsize; i++)
1060 if (nsh->dev[i].page == NULL) {
1061 struct page *p = alloc_page(GFP_NOIO);
1062 nsh->dev[i].page = p;
1063 if (!p)
1064 err = -ENOMEM;
1065 }
1066 release_stripe(nsh);
1067 }
1068 /* critical section pass, GFP_NOIO no longer needed */
1069
1070 conf->slab_cache = sc;
1071 conf->active_name = 1-conf->active_name;
1072 conf->pool_size = newsize;
1073 return err;
1074}
NeilBrown29269552006-03-27 01:18:10 -08001075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
NeilBrown3f294f42005-11-08 21:39:25 -08001077static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct stripe_head *sh;
1080
NeilBrown3f294f42005-11-08 21:39:25 -08001081 spin_lock_irq(&conf->device_lock);
1082 sh = get_free_stripe(conf);
1083 spin_unlock_irq(&conf->device_lock);
1084 if (!sh)
1085 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001086 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001087 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001088 kmem_cache_free(conf->slab_cache, sh);
1089 atomic_dec(&conf->active_stripes);
1090 return 1;
1091}
1092
1093static void shrink_stripes(raid5_conf_t *conf)
1094{
1095 while (drop_one_stripe(conf))
1096 ;
1097
NeilBrown29fc7e32006-02-03 03:03:41 -08001098 if (conf->slab_cache)
1099 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 conf->slab_cache = NULL;
1101}
1102
NeilBrown6712ecf2007-09-27 12:47:43 +02001103static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
NeilBrown99c0fb52009-03-31 14:39:38 +11001105 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001107 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001109 char b[BDEVNAME_SIZE];
1110 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 for (i=0 ; i<disks; i++)
1114 if (bi == &sh->dev[i].req)
1115 break;
1116
Dan Williams45b42332007-07-09 11:56:43 -07001117 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1118 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 uptodate);
1120 if (i == disks) {
1121 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001127 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001128 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001129 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1130 " (%lu sectors at %llu on %s)\n",
1131 mdname(conf->mddev), STRIPE_SECTORS,
1132 (unsigned long long)(sh->sector
1133 + rdev->data_offset),
1134 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001135 clear_bit(R5_ReadError, &sh->dev[i].flags);
1136 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1137 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001138 if (atomic_read(&conf->disks[i].rdev->read_errors))
1139 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001141 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001142 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001143 rdev = conf->disks[i].rdev;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001146 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001147 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001148 printk_rl(KERN_WARNING
1149 "raid5:%s: read error not correctable "
1150 "(sector %llu on %s).\n",
1151 mdname(conf->mddev),
1152 (unsigned long long)(sh->sector
1153 + rdev->data_offset),
1154 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001155 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001156 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001157 printk_rl(KERN_WARNING
1158 "raid5:%s: read error NOT corrected!! "
1159 "(sector %llu on %s).\n",
1160 mdname(conf->mddev),
1161 (unsigned long long)(sh->sector
1162 + rdev->data_offset),
1163 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001164 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001165 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001166 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001167 "raid5:%s: Too many read errors, failing device %s.\n",
1168 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001169 else
1170 retry = 1;
1171 if (retry)
1172 set_bit(R5_ReadError, &sh->dev[i].flags);
1173 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001174 clear_bit(R5_ReadError, &sh->dev[i].flags);
1175 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001176 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1181 set_bit(STRIPE_HANDLE, &sh->state);
1182 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
NeilBrownd710e132008-10-13 11:55:12 +11001185static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
NeilBrown99c0fb52009-03-31 14:39:38 +11001187 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001189 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 for (i=0 ; i<disks; i++)
1193 if (bi == &sh->dev[i].req)
1194 break;
1195
Dan Williams45b42332007-07-09 11:56:43 -07001196 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1198 uptodate);
1199 if (i == disks) {
1200 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!uptodate)
1205 md_error(conf->mddev, conf->disks[i].rdev);
1206
1207 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1208
1209 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1210 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001211 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214
1215static sector_t compute_blocknr(struct stripe_head *sh, int i);
1216
NeilBrownd710e132008-10-13 11:55:12 +11001217static void raid5_build_block(struct stripe_head *sh, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct r5dev *dev = &sh->dev[i];
1220
1221 bio_init(&dev->req);
1222 dev->req.bi_io_vec = &dev->vec;
1223 dev->req.bi_vcnt++;
1224 dev->req.bi_max_vecs++;
1225 dev->vec.bv_page = dev->page;
1226 dev->vec.bv_len = STRIPE_SIZE;
1227 dev->vec.bv_offset = 0;
1228
1229 dev->req.bi_sector = sh->sector;
1230 dev->req.bi_private = sh;
1231
1232 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001233 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1237{
1238 char b[BDEVNAME_SIZE];
1239 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001240 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
NeilBrownb2d444d2005-11-08 21:39:31 -08001242 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001243 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001244 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1245 unsigned long flags;
1246 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001248 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * if recovery was running, make sure it aborts.
1251 */
NeilBrowndfc70642008-05-23 13:04:39 -07001252 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001254 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001255 printk(KERN_ALERT
1256 "raid5: Disk failure on %s, disabling device.\n"
1257 "raid5: Operation continuing on %d devices.\n",
1258 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262/*
1263 * Input: a 'big' sector number,
1264 * Output: index of the data and parity disk, and the sector # in them.
1265 */
NeilBrown112bf892009-03-31 14:39:38 +11001266static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001267 int previous, int *dd_idx,
1268 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 long stripe;
1271 unsigned long chunk_number;
1272 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001273 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001274 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sector_t new_sector;
1276 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown112bf892009-03-31 14:39:38 +11001277 int raid_disks = previous ? conf->previous_raid_disks
1278 : conf->raid_disks;
1279 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* First compute the information on this sector */
1282
1283 /*
1284 * Compute the chunk number and the sector offset inside the chunk
1285 */
1286 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1287 chunk_number = r_sector;
1288 BUG_ON(r_sector != chunk_number);
1289
1290 /*
1291 * Compute the stripe number
1292 */
1293 stripe = chunk_number / data_disks;
1294
1295 /*
1296 * Compute the data disk and parity disk indexes inside the stripe
1297 */
1298 *dd_idx = chunk_number % data_disks;
1299
1300 /*
1301 * Select the parity disk based on the user selected algorithm.
1302 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001303 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001304 switch(conf->level) {
1305 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001306 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001307 break;
1308 case 5:
1309 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001311 pd_idx = data_disks - stripe % raid_disks;
1312 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 (*dd_idx)++;
1314 break;
1315 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001316 pd_idx = stripe % raid_disks;
1317 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 (*dd_idx)++;
1319 break;
1320 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001321 pd_idx = data_disks - stripe % raid_disks;
1322 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001325 pd_idx = stripe % raid_disks;
1326 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001328 case ALGORITHM_PARITY_0:
1329 pd_idx = 0;
1330 (*dd_idx)++;
1331 break;
1332 case ALGORITHM_PARITY_N:
1333 pd_idx = data_disks;
1334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001336 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001338 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001339 }
1340 break;
1341 case 6:
1342
NeilBrown16a53ec2006-06-26 00:27:38 -07001343 switch (conf->algorithm) {
1344 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001345 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1346 qd_idx = pd_idx + 1;
1347 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001348 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001349 qd_idx = 0;
1350 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001351 (*dd_idx) += 2; /* D D P Q D */
1352 break;
1353 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001354 pd_idx = stripe % raid_disks;
1355 qd_idx = pd_idx + 1;
1356 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001357 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001358 qd_idx = 0;
1359 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001360 (*dd_idx) += 2; /* D D P Q D */
1361 break;
1362 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001363 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1364 qd_idx = (pd_idx + 1) % raid_disks;
1365 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001366 break;
1367 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001368 pd_idx = stripe % raid_disks;
1369 qd_idx = (pd_idx + 1) % raid_disks;
1370 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001371 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001372
1373 case ALGORITHM_PARITY_0:
1374 pd_idx = 0;
1375 qd_idx = 1;
1376 (*dd_idx) += 2;
1377 break;
1378 case ALGORITHM_PARITY_N:
1379 pd_idx = data_disks;
1380 qd_idx = data_disks + 1;
1381 break;
1382
1383 case ALGORITHM_ROTATING_ZERO_RESTART:
1384 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1385 * of blocks for computing Q is different.
1386 */
1387 pd_idx = stripe % raid_disks;
1388 qd_idx = pd_idx + 1;
1389 if (pd_idx == raid_disks-1) {
1390 (*dd_idx)++; /* Q D D D P */
1391 qd_idx = 0;
1392 } else if (*dd_idx >= pd_idx)
1393 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001394 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001395 break;
1396
1397 case ALGORITHM_ROTATING_N_RESTART:
1398 /* Same a left_asymmetric, by first stripe is
1399 * D D D P Q rather than
1400 * Q D D D P
1401 */
1402 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1403 qd_idx = pd_idx + 1;
1404 if (pd_idx == raid_disks-1) {
1405 (*dd_idx)++; /* Q D D D P */
1406 qd_idx = 0;
1407 } else if (*dd_idx >= pd_idx)
1408 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001409 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001410 break;
1411
1412 case ALGORITHM_ROTATING_N_CONTINUE:
1413 /* Same as left_symmetric but Q is before P */
1414 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1415 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1416 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001417 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001418 break;
1419
1420 case ALGORITHM_LEFT_ASYMMETRIC_6:
1421 /* RAID5 left_asymmetric, with Q on last device */
1422 pd_idx = data_disks - stripe % (raid_disks-1);
1423 if (*dd_idx >= pd_idx)
1424 (*dd_idx)++;
1425 qd_idx = raid_disks - 1;
1426 break;
1427
1428 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1429 pd_idx = stripe % (raid_disks-1);
1430 if (*dd_idx >= pd_idx)
1431 (*dd_idx)++;
1432 qd_idx = raid_disks - 1;
1433 break;
1434
1435 case ALGORITHM_LEFT_SYMMETRIC_6:
1436 pd_idx = data_disks - stripe % (raid_disks-1);
1437 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1438 qd_idx = raid_disks - 1;
1439 break;
1440
1441 case ALGORITHM_RIGHT_SYMMETRIC_6:
1442 pd_idx = stripe % (raid_disks-1);
1443 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1444 qd_idx = raid_disks - 1;
1445 break;
1446
1447 case ALGORITHM_PARITY_0_6:
1448 pd_idx = 0;
1449 (*dd_idx)++;
1450 qd_idx = raid_disks - 1;
1451 break;
1452
1453
NeilBrown16a53ec2006-06-26 00:27:38 -07001454 default:
NeilBrownd710e132008-10-13 11:55:12 +11001455 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1456 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001457 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001458 }
1459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
NeilBrown911d4ee2009-03-31 14:39:38 +11001462 if (sh) {
1463 sh->pd_idx = pd_idx;
1464 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001465 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Finally, compute the new sector number
1469 */
1470 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1471 return new_sector;
1472}
1473
1474
1475static sector_t compute_blocknr(struct stripe_head *sh, int i)
1476{
1477 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001478 int raid_disks = sh->disks;
1479 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 sector_t new_sector = sh->sector, check;
1481 int sectors_per_chunk = conf->chunk_size >> 9;
1482 sector_t stripe;
1483 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001484 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001486 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
NeilBrown16a53ec2006-06-26 00:27:38 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1490 stripe = new_sector;
1491 BUG_ON(new_sector != stripe);
1492
NeilBrown16a53ec2006-06-26 00:27:38 -07001493 if (i == sh->pd_idx)
1494 return 0;
1495 switch(conf->level) {
1496 case 4: break;
1497 case 5:
1498 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 case ALGORITHM_LEFT_ASYMMETRIC:
1500 case ALGORITHM_RIGHT_ASYMMETRIC:
1501 if (i > sh->pd_idx)
1502 i--;
1503 break;
1504 case ALGORITHM_LEFT_SYMMETRIC:
1505 case ALGORITHM_RIGHT_SYMMETRIC:
1506 if (i < sh->pd_idx)
1507 i += raid_disks;
1508 i -= (sh->pd_idx + 1);
1509 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001510 case ALGORITHM_PARITY_0:
1511 i -= 1;
1512 break;
1513 case ALGORITHM_PARITY_N:
1514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001516 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001517 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001518 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001519 }
1520 break;
1521 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001522 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001523 return 0; /* It is the Q disk */
1524 switch (conf->algorithm) {
1525 case ALGORITHM_LEFT_ASYMMETRIC:
1526 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001527 case ALGORITHM_ROTATING_ZERO_RESTART:
1528 case ALGORITHM_ROTATING_N_RESTART:
1529 if (sh->pd_idx == raid_disks-1)
1530 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001531 else if (i > sh->pd_idx)
1532 i -= 2; /* D D P Q D */
1533 break;
1534 case ALGORITHM_LEFT_SYMMETRIC:
1535 case ALGORITHM_RIGHT_SYMMETRIC:
1536 if (sh->pd_idx == raid_disks-1)
1537 i--; /* Q D D D P */
1538 else {
1539 /* D D P Q D */
1540 if (i < sh->pd_idx)
1541 i += raid_disks;
1542 i -= (sh->pd_idx + 2);
1543 }
1544 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001545 case ALGORITHM_PARITY_0:
1546 i -= 2;
1547 break;
1548 case ALGORITHM_PARITY_N:
1549 break;
1550 case ALGORITHM_ROTATING_N_CONTINUE:
1551 if (sh->pd_idx == 0)
1552 i--; /* P D D D Q */
1553 else if (i > sh->pd_idx)
1554 i -= 2; /* D D Q P D */
1555 break;
1556 case ALGORITHM_LEFT_ASYMMETRIC_6:
1557 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1558 if (i > sh->pd_idx)
1559 i--;
1560 break;
1561 case ALGORITHM_LEFT_SYMMETRIC_6:
1562 case ALGORITHM_RIGHT_SYMMETRIC_6:
1563 if (i < sh->pd_idx)
1564 i += data_disks + 1;
1565 i -= (sh->pd_idx + 1);
1566 break;
1567 case ALGORITHM_PARITY_0_6:
1568 i -= 1;
1569 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001570 default:
NeilBrownd710e132008-10-13 11:55:12 +11001571 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1572 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001573 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001574 }
1575 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 chunk_number = stripe * data_disks + i;
1579 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1580
NeilBrown112bf892009-03-31 14:39:38 +11001581 check = raid5_compute_sector(conf, r_sector,
1582 (raid_disks != conf->raid_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11001583 &dummy1, &sh2);
1584 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1585 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001586 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588 }
1589 return r_sector;
1590}
1591
1592
1593
1594/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001595 * Copy data between a page in the stripe cache, and one or more bion
1596 * The page could align with the middle of the bio, or there could be
1597 * several bion, each with several bio_vecs, which cover part of the page
1598 * Multiple bion are linked together on bi_next. There may be extras
1599 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
1601static void copy_data(int frombio, struct bio *bio,
1602 struct page *page,
1603 sector_t sector)
1604{
1605 char *pa = page_address(page);
1606 struct bio_vec *bvl;
1607 int i;
1608 int page_offset;
1609
1610 if (bio->bi_sector >= sector)
1611 page_offset = (signed)(bio->bi_sector - sector) * 512;
1612 else
1613 page_offset = (signed)(sector - bio->bi_sector) * -512;
1614 bio_for_each_segment(bvl, bio, i) {
1615 int len = bio_iovec_idx(bio,i)->bv_len;
1616 int clen;
1617 int b_offset = 0;
1618
1619 if (page_offset < 0) {
1620 b_offset = -page_offset;
1621 page_offset += b_offset;
1622 len -= b_offset;
1623 }
1624
1625 if (len > 0 && page_offset + len > STRIPE_SIZE)
1626 clen = STRIPE_SIZE - page_offset;
1627 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (clen > 0) {
1630 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1631 if (frombio)
1632 memcpy(pa+page_offset, ba+b_offset, clen);
1633 else
1634 memcpy(ba+b_offset, pa+page_offset, clen);
1635 __bio_kunmap_atomic(ba, KM_USER0);
1636 }
1637 if (clen < len) /* hit end of page */
1638 break;
1639 page_offset += len;
1640 }
1641}
1642
Dan Williams9bc89cd2007-01-02 11:10:44 -07001643#define check_xor() do { \
1644 if (count == MAX_XOR_BLOCKS) { \
1645 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1646 count = 0; \
1647 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 } while(0)
1649
NeilBrown16a53ec2006-06-26 00:27:38 -07001650static void compute_parity6(struct stripe_head *sh, int method)
1651{
NeilBrownbff61972009-03-31 14:33:13 +11001652 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001653 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown67cc2b82009-03-31 14:39:38 +11001654 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
NeilBrown16a53ec2006-06-26 00:27:38 -07001655 struct bio *chosen;
1656 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001657 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001658
NeilBrownd0dabf72009-03-31 14:39:38 +11001659 pd_idx = sh->pd_idx;
1660 qd_idx = sh->qd_idx;
1661 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001662
Dan Williams45b42332007-07-09 11:56:43 -07001663 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001664 (unsigned long long)sh->sector, method);
1665
1666 switch(method) {
1667 case READ_MODIFY_WRITE:
1668 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1669 case RECONSTRUCT_WRITE:
1670 for (i= disks; i-- ;)
1671 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1672 chosen = sh->dev[i].towrite;
1673 sh->dev[i].towrite = NULL;
1674
1675 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1676 wake_up(&conf->wait_for_overlap);
1677
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001678 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001679 sh->dev[i].written = chosen;
1680 }
1681 break;
1682 case CHECK_PARITY:
1683 BUG(); /* Not implemented yet */
1684 }
1685
1686 for (i = disks; i--;)
1687 if (sh->dev[i].written) {
1688 sector_t sector = sh->dev[i].sector;
1689 struct bio *wbi = sh->dev[i].written;
1690 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1691 copy_data(1, wbi, sh->dev[i].page, sector);
1692 wbi = r5_next_bio(wbi, sector);
1693 }
1694
1695 set_bit(R5_LOCKED, &sh->dev[i].flags);
1696 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1697 }
1698
NeilBrownd0dabf72009-03-31 14:39:38 +11001699 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
NeilBrown67cc2b82009-03-31 14:39:38 +11001700
1701 for (i = 0; i < disks; i++)
1702 ptrs[i] = (void *)raid6_empty_zero_page;
1703
NeilBrownd0dabf72009-03-31 14:39:38 +11001704 count = 0;
1705 i = d0_idx;
1706 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001707 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1708
NeilBrownd0dabf72009-03-31 14:39:38 +11001709 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001710 if (slot < syndrome_disks &&
NeilBrownd0dabf72009-03-31 14:39:38 +11001711 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1712 printk(KERN_ERR "block %d/%d not uptodate "
1713 "on parity calc\n", i, count);
1714 BUG();
1715 }
NeilBrown67cc2b82009-03-31 14:39:38 +11001716
NeilBrownd0dabf72009-03-31 14:39:38 +11001717 i = raid6_next_disk(i, disks);
1718 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001719 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001720
NeilBrown67cc2b82009-03-31 14:39:38 +11001721 raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001722
1723 switch(method) {
1724 case RECONSTRUCT_WRITE:
1725 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1726 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1727 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1728 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1729 break;
1730 case UPDATE_PARITY:
1731 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1732 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1733 break;
1734 }
1735}
1736
1737
1738/* Compute one missing block */
1739static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1740{
NeilBrownf4168852007-02-28 20:11:53 -08001741 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001742 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11001743 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07001744
Dan Williams45b42332007-07-09 11:56:43 -07001745 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001746 (unsigned long long)sh->sector, dd_idx);
1747
1748 if ( dd_idx == qd_idx ) {
1749 /* We're actually computing the Q drive */
1750 compute_parity6(sh, UPDATE_PARITY);
1751 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001752 dest = page_address(sh->dev[dd_idx].page);
1753 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1754 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001755 for (i = disks ; i--; ) {
1756 if (i == dd_idx || i == qd_idx)
1757 continue;
1758 p = page_address(sh->dev[i].page);
1759 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1760 ptr[count++] = p;
1761 else
1762 printk("compute_block() %d, stripe %llu, %d"
1763 " not present\n", dd_idx,
1764 (unsigned long long)sh->sector, i);
1765
1766 check_xor();
1767 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001768 if (count)
1769 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001770 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1771 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1772 }
1773}
1774
1775/* Compute two missing blocks */
1776static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1777{
NeilBrownf4168852007-02-28 20:11:53 -08001778 int i, count, disks = sh->disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001779 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
NeilBrownd0dabf72009-03-31 14:39:38 +11001780 int d0_idx = raid6_d0(sh);
1781 int faila = -1, failb = -1;
1782 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001783 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001784
NeilBrown67cc2b82009-03-31 14:39:38 +11001785 for (i = 0; i < disks ; i++)
1786 ptrs[i] = (void *)raid6_empty_zero_page;
NeilBrownd0dabf72009-03-31 14:39:38 +11001787 count = 0;
1788 i = d0_idx;
1789 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001790 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1791
NeilBrownd0dabf72009-03-31 14:39:38 +11001792 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001793
NeilBrownd0dabf72009-03-31 14:39:38 +11001794 if (i == dd_idx1)
1795 faila = slot;
1796 if (i == dd_idx2)
1797 failb = slot;
1798 i = raid6_next_disk(i, disks);
1799 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001800 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001801
1802 BUG_ON(faila == failb);
1803 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1804
Dan Williams45b42332007-07-09 11:56:43 -07001805 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11001806 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
1807 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07001808
NeilBrown67cc2b82009-03-31 14:39:38 +11001809 if (failb == syndrome_disks+1) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001810 /* Q disk is one of the missing disks */
NeilBrown67cc2b82009-03-31 14:39:38 +11001811 if (faila == syndrome_disks) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001812 /* Missing P+Q, just recompute */
1813 compute_parity6(sh, UPDATE_PARITY);
1814 return;
1815 } else {
1816 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11001817 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
1818 dd_idx2 : dd_idx1),
1819 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07001820 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1821 return;
1822 }
1823 }
1824
NeilBrownd0dabf72009-03-31 14:39:38 +11001825 /* We're missing D+P or D+D; */
NeilBrown67cc2b82009-03-31 14:39:38 +11001826 if (failb == syndrome_disks) {
NeilBrownd0dabf72009-03-31 14:39:38 +11001827 /* We're missing D+P. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001828 raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
NeilBrownd0dabf72009-03-31 14:39:38 +11001829 } else {
1830 /* We're missing D+D. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001831 raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
1832 ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001833 }
NeilBrownd0dabf72009-03-31 14:39:38 +11001834
1835 /* Both the above update both missing blocks */
1836 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1837 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07001838}
1839
Dan Williams600aa102008-06-28 08:32:05 +10001840static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001841schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001842 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001843{
1844 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001845
Dan Williamse33129d2007-01-02 13:52:30 -07001846 if (rcw) {
1847 /* if we are not expanding this is a proper write request, and
1848 * there will be bios with new data to be drained into the
1849 * stripe cache
1850 */
1851 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001852 sh->reconstruct_state = reconstruct_state_drain_run;
1853 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1854 } else
1855 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001856
Dan Williams600aa102008-06-28 08:32:05 +10001857 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001858
1859 for (i = disks; i--; ) {
1860 struct r5dev *dev = &sh->dev[i];
1861
1862 if (dev->towrite) {
1863 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001864 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001865 if (!expand)
1866 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001867 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001868 }
1869 }
Dan Williams600aa102008-06-28 08:32:05 +10001870 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001871 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1872 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001873 } else {
1874 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1875 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1876
Dan Williamsd8ee0722008-06-28 08:32:06 +10001877 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001878 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1879 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1880 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001881
1882 for (i = disks; i--; ) {
1883 struct r5dev *dev = &sh->dev[i];
1884 if (i == pd_idx)
1885 continue;
1886
Dan Williamse33129d2007-01-02 13:52:30 -07001887 if (dev->towrite &&
1888 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001889 test_bit(R5_Wantcompute, &dev->flags))) {
1890 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001891 set_bit(R5_LOCKED, &dev->flags);
1892 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001893 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001894 }
1895 }
1896 }
1897
1898 /* keep the parity disk locked while asynchronous operations
1899 * are in flight
1900 */
1901 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1902 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001903 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001904
Dan Williams600aa102008-06-28 08:32:05 +10001905 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001906 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001907 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001908}
NeilBrown16a53ec2006-06-26 00:27:38 -07001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910/*
1911 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * The bi_next chain must be in order.
1914 */
1915static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1916{
1917 struct bio **bip;
1918 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001919 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Dan Williams45b42332007-07-09 11:56:43 -07001921 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 (unsigned long long)bi->bi_sector,
1923 (unsigned long long)sh->sector);
1924
1925
1926 spin_lock(&sh->lock);
1927 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001928 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001930 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1931 firstwrite = 1;
1932 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 bip = &sh->dev[dd_idx].toread;
1934 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1935 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1936 goto overlap;
1937 bip = & (*bip)->bi_next;
1938 }
1939 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1940 goto overlap;
1941
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001942 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (*bip)
1944 bi->bi_next = *bip;
1945 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02001946 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 spin_unlock_irq(&conf->device_lock);
1948 spin_unlock(&sh->lock);
1949
Dan Williams45b42332007-07-09 11:56:43 -07001950 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 (unsigned long long)bi->bi_sector,
1952 (unsigned long long)sh->sector, dd_idx);
1953
NeilBrown72626682005-09-09 16:23:54 -07001954 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001955 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1956 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001957 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001958 set_bit(STRIPE_BIT_DELAY, &sh->state);
1959 }
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (forwrite) {
1962 /* check if page is covered */
1963 sector_t sector = sh->dev[dd_idx].sector;
1964 for (bi=sh->dev[dd_idx].towrite;
1965 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1966 bi && bi->bi_sector <= sector;
1967 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1968 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1969 sector = bi->bi_sector + (bi->bi_size>>9);
1970 }
1971 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1972 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1973 }
1974 return 1;
1975
1976 overlap:
1977 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1978 spin_unlock_irq(&conf->device_lock);
1979 spin_unlock(&sh->lock);
1980 return 0;
1981}
1982
NeilBrown29269552006-03-27 01:18:10 -08001983static void end_reshape(raid5_conf_t *conf);
1984
NeilBrown16a53ec2006-06-26 00:27:38 -07001985static int page_is_zero(struct page *p)
1986{
1987 char *a = page_address(p);
1988 return ((*(u32*)a) == 0 &&
1989 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1990}
1991
NeilBrown911d4ee2009-03-31 14:39:38 +11001992static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
1993 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08001994{
1995 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown911d4ee2009-03-31 14:39:38 +11001996 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001997 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11001998 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001999
NeilBrown112bf892009-03-31 14:39:38 +11002000 raid5_compute_sector(conf,
2001 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002002 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002003 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002004 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002005}
2006
Dan Williamsa4456852007-07-09 11:56:43 -07002007static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002008handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002009 struct stripe_head_state *s, int disks,
2010 struct bio **return_bi)
2011{
2012 int i;
2013 for (i = disks; i--; ) {
2014 struct bio *bi;
2015 int bitmap_end = 0;
2016
2017 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2018 mdk_rdev_t *rdev;
2019 rcu_read_lock();
2020 rdev = rcu_dereference(conf->disks[i].rdev);
2021 if (rdev && test_bit(In_sync, &rdev->flags))
2022 /* multiple read failures in one stripe */
2023 md_error(conf->mddev, rdev);
2024 rcu_read_unlock();
2025 }
2026 spin_lock_irq(&conf->device_lock);
2027 /* fail all writes first */
2028 bi = sh->dev[i].towrite;
2029 sh->dev[i].towrite = NULL;
2030 if (bi) {
2031 s->to_write--;
2032 bitmap_end = 1;
2033 }
2034
2035 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2036 wake_up(&conf->wait_for_overlap);
2037
2038 while (bi && bi->bi_sector <
2039 sh->dev[i].sector + STRIPE_SECTORS) {
2040 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2041 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002042 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002043 md_write_end(conf->mddev);
2044 bi->bi_next = *return_bi;
2045 *return_bi = bi;
2046 }
2047 bi = nextbi;
2048 }
2049 /* and fail all 'written' */
2050 bi = sh->dev[i].written;
2051 sh->dev[i].written = NULL;
2052 if (bi) bitmap_end = 1;
2053 while (bi && bi->bi_sector <
2054 sh->dev[i].sector + STRIPE_SECTORS) {
2055 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2056 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002057 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002058 md_write_end(conf->mddev);
2059 bi->bi_next = *return_bi;
2060 *return_bi = bi;
2061 }
2062 bi = bi2;
2063 }
2064
Dan Williamsb5e98d62007-01-02 13:52:31 -07002065 /* fail any reads if this device is non-operational and
2066 * the data has not reached the cache yet.
2067 */
2068 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2069 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2070 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002071 bi = sh->dev[i].toread;
2072 sh->dev[i].toread = NULL;
2073 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2074 wake_up(&conf->wait_for_overlap);
2075 if (bi) s->to_read--;
2076 while (bi && bi->bi_sector <
2077 sh->dev[i].sector + STRIPE_SECTORS) {
2078 struct bio *nextbi =
2079 r5_next_bio(bi, sh->dev[i].sector);
2080 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002081 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002082 bi->bi_next = *return_bi;
2083 *return_bi = bi;
2084 }
2085 bi = nextbi;
2086 }
2087 }
2088 spin_unlock_irq(&conf->device_lock);
2089 if (bitmap_end)
2090 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2091 STRIPE_SECTORS, 0, 0);
2092 }
2093
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002094 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2095 if (atomic_dec_and_test(&conf->pending_full_writes))
2096 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002097}
2098
Dan Williams1fe797e2008-06-28 09:16:30 +10002099/* fetch_block5 - checks the given member device to see if its data needs
2100 * to be read or computed to satisfy a request.
2101 *
2102 * Returns 1 when no more member devices need to be checked, otherwise returns
2103 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002104 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002105static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2106 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002107{
2108 struct r5dev *dev = &sh->dev[disk_idx];
2109 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2110
Dan Williamsf38e1212007-01-02 13:52:30 -07002111 /* is the data in this block needed, and can we get it? */
2112 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002113 !test_bit(R5_UPTODATE, &dev->flags) &&
2114 (dev->toread ||
2115 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2116 s->syncing || s->expanding ||
2117 (s->failed &&
2118 (failed_dev->toread ||
2119 (failed_dev->towrite &&
2120 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002121 /* We would like to get this block, possibly by computing it,
2122 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002123 */
2124 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002125 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002126 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2127 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002128 set_bit(R5_Wantcompute, &dev->flags);
2129 sh->ops.target = disk_idx;
2130 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002131 /* Careful: from this point on 'uptodate' is in the eye
2132 * of raid5_run_ops which services 'compute' operations
2133 * before writes. R5_Wantcompute flags a block that will
2134 * be R5_UPTODATE by the time it is needed for a
2135 * subsequent operation.
2136 */
2137 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002138 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002139 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002140 set_bit(R5_LOCKED, &dev->flags);
2141 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002142 s->locked++;
2143 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2144 s->syncing);
2145 }
2146 }
2147
Dan Williams1fe797e2008-06-28 09:16:30 +10002148 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002149}
2150
Dan Williams1fe797e2008-06-28 09:16:30 +10002151/**
2152 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2153 */
2154static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002155 struct stripe_head_state *s, int disks)
2156{
2157 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002158
Dan Williamsf38e1212007-01-02 13:52:30 -07002159 /* look for blocks to read/compute, skip this if a compute
2160 * is already in flight, or if the stripe contents are in the
2161 * midst of changing due to a write
2162 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002163 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002164 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002165 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002166 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002167 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002168 set_bit(STRIPE_HANDLE, &sh->state);
2169}
2170
Dan Williams1fe797e2008-06-28 09:16:30 +10002171static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002172 struct stripe_head_state *s, struct r6_state *r6s,
2173 int disks)
2174{
2175 int i;
2176 for (i = disks; i--; ) {
2177 struct r5dev *dev = &sh->dev[i];
2178 if (!test_bit(R5_LOCKED, &dev->flags) &&
2179 !test_bit(R5_UPTODATE, &dev->flags) &&
2180 (dev->toread || (dev->towrite &&
2181 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2182 s->syncing || s->expanding ||
2183 (s->failed >= 1 &&
2184 (sh->dev[r6s->failed_num[0]].toread ||
2185 s->to_write)) ||
2186 (s->failed >= 2 &&
2187 (sh->dev[r6s->failed_num[1]].toread ||
2188 s->to_write)))) {
2189 /* we would like to get this block, possibly
2190 * by computing it, but we might not be able to
2191 */
Dan Williamsc3378692008-06-05 22:45:54 -07002192 if ((s->uptodate == disks - 1) &&
2193 (s->failed && (i == r6s->failed_num[0] ||
2194 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07002195 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002196 (unsigned long long)sh->sector, i);
2197 compute_block_1(sh, i, 0);
2198 s->uptodate++;
2199 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2200 /* Computing 2-failure is *very* expensive; only
2201 * do it if failed >= 2
2202 */
2203 int other;
2204 for (other = disks; other--; ) {
2205 if (other == i)
2206 continue;
2207 if (!test_bit(R5_UPTODATE,
2208 &sh->dev[other].flags))
2209 break;
2210 }
2211 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07002212 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002213 (unsigned long long)sh->sector,
2214 i, other);
2215 compute_block_2(sh, i, other);
2216 s->uptodate += 2;
2217 } else if (test_bit(R5_Insync, &dev->flags)) {
2218 set_bit(R5_LOCKED, &dev->flags);
2219 set_bit(R5_Wantread, &dev->flags);
2220 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002221 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002222 i, s->syncing);
2223 }
2224 }
2225 }
2226 set_bit(STRIPE_HANDLE, &sh->state);
2227}
2228
2229
Dan Williams1fe797e2008-06-28 09:16:30 +10002230/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002231 * any written block on an uptodate or failed drive can be returned.
2232 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2233 * never LOCKED, so we don't need to test 'failed' directly.
2234 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002235static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002236 struct stripe_head *sh, int disks, struct bio **return_bi)
2237{
2238 int i;
2239 struct r5dev *dev;
2240
2241 for (i = disks; i--; )
2242 if (sh->dev[i].written) {
2243 dev = &sh->dev[i];
2244 if (!test_bit(R5_LOCKED, &dev->flags) &&
2245 test_bit(R5_UPTODATE, &dev->flags)) {
2246 /* We can return any write requests */
2247 struct bio *wbi, *wbi2;
2248 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002249 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002250 spin_lock_irq(&conf->device_lock);
2251 wbi = dev->written;
2252 dev->written = NULL;
2253 while (wbi && wbi->bi_sector <
2254 dev->sector + STRIPE_SECTORS) {
2255 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002256 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002257 md_write_end(conf->mddev);
2258 wbi->bi_next = *return_bi;
2259 *return_bi = wbi;
2260 }
2261 wbi = wbi2;
2262 }
2263 if (dev->towrite == NULL)
2264 bitmap_end = 1;
2265 spin_unlock_irq(&conf->device_lock);
2266 if (bitmap_end)
2267 bitmap_endwrite(conf->mddev->bitmap,
2268 sh->sector,
2269 STRIPE_SECTORS,
2270 !test_bit(STRIPE_DEGRADED, &sh->state),
2271 0);
2272 }
2273 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002274
2275 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2276 if (atomic_dec_and_test(&conf->pending_full_writes))
2277 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002278}
2279
Dan Williams1fe797e2008-06-28 09:16:30 +10002280static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002281 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2282{
2283 int rmw = 0, rcw = 0, i;
2284 for (i = disks; i--; ) {
2285 /* would I have to read this buffer for read_modify_write */
2286 struct r5dev *dev = &sh->dev[i];
2287 if ((dev->towrite || i == sh->pd_idx) &&
2288 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002289 !(test_bit(R5_UPTODATE, &dev->flags) ||
2290 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002291 if (test_bit(R5_Insync, &dev->flags))
2292 rmw++;
2293 else
2294 rmw += 2*disks; /* cannot read it */
2295 }
2296 /* Would I have to read this buffer for reconstruct_write */
2297 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2298 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002299 !(test_bit(R5_UPTODATE, &dev->flags) ||
2300 test_bit(R5_Wantcompute, &dev->flags))) {
2301 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002302 else
2303 rcw += 2*disks;
2304 }
2305 }
Dan Williams45b42332007-07-09 11:56:43 -07002306 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002307 (unsigned long long)sh->sector, rmw, rcw);
2308 set_bit(STRIPE_HANDLE, &sh->state);
2309 if (rmw < rcw && rmw > 0)
2310 /* prefer read-modify-write, but need to get some data */
2311 for (i = disks; i--; ) {
2312 struct r5dev *dev = &sh->dev[i];
2313 if ((dev->towrite || i == sh->pd_idx) &&
2314 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002315 !(test_bit(R5_UPTODATE, &dev->flags) ||
2316 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002317 test_bit(R5_Insync, &dev->flags)) {
2318 if (
2319 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002320 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002321 "%d for r-m-w\n", i);
2322 set_bit(R5_LOCKED, &dev->flags);
2323 set_bit(R5_Wantread, &dev->flags);
2324 s->locked++;
2325 } else {
2326 set_bit(STRIPE_DELAYED, &sh->state);
2327 set_bit(STRIPE_HANDLE, &sh->state);
2328 }
2329 }
2330 }
2331 if (rcw <= rmw && rcw > 0)
2332 /* want reconstruct write, but need to get some data */
2333 for (i = disks; i--; ) {
2334 struct r5dev *dev = &sh->dev[i];
2335 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2336 i != sh->pd_idx &&
2337 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002338 !(test_bit(R5_UPTODATE, &dev->flags) ||
2339 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002340 test_bit(R5_Insync, &dev->flags)) {
2341 if (
2342 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002343 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002344 "%d for Reconstruct\n", i);
2345 set_bit(R5_LOCKED, &dev->flags);
2346 set_bit(R5_Wantread, &dev->flags);
2347 s->locked++;
2348 } else {
2349 set_bit(STRIPE_DELAYED, &sh->state);
2350 set_bit(STRIPE_HANDLE, &sh->state);
2351 }
2352 }
2353 }
2354 /* now if nothing is locked, and if we have enough data,
2355 * we can start a write request
2356 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002357 /* since handle_stripe can be called at any time we need to handle the
2358 * case where a compute block operation has been submitted and then a
2359 * subsequent call wants to start a write request. raid5_run_ops only
2360 * handles the case where compute block and postxor are requested
2361 * simultaneously. If this is not the case then new writes need to be
2362 * held off until the compute completes.
2363 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002364 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2365 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2366 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002367 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002368}
2369
Dan Williams1fe797e2008-06-28 09:16:30 +10002370static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002371 struct stripe_head *sh, struct stripe_head_state *s,
2372 struct r6_state *r6s, int disks)
2373{
2374 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002375 int qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07002376 for (i = disks; i--; ) {
2377 struct r5dev *dev = &sh->dev[i];
2378 /* Would I have to read this buffer for reconstruct_write */
2379 if (!test_bit(R5_OVERWRITE, &dev->flags)
2380 && i != pd_idx && i != qd_idx
2381 && (!test_bit(R5_LOCKED, &dev->flags)
2382 ) &&
2383 !test_bit(R5_UPTODATE, &dev->flags)) {
2384 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2385 else {
Dan Williams45b42332007-07-09 11:56:43 -07002386 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002387 "disk %d flags=%#lx\n", i, dev->flags);
2388 must_compute++;
2389 }
2390 }
2391 }
Dan Williams45b42332007-07-09 11:56:43 -07002392 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002393 (unsigned long long)sh->sector, rcw, must_compute);
2394 set_bit(STRIPE_HANDLE, &sh->state);
2395
2396 if (rcw > 0)
2397 /* want reconstruct write, but need to get some data */
2398 for (i = disks; i--; ) {
2399 struct r5dev *dev = &sh->dev[i];
2400 if (!test_bit(R5_OVERWRITE, &dev->flags)
2401 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2402 && !test_bit(R5_LOCKED, &dev->flags) &&
2403 !test_bit(R5_UPTODATE, &dev->flags) &&
2404 test_bit(R5_Insync, &dev->flags)) {
2405 if (
2406 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002407 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002408 "block %d for Reconstruct\n",
2409 (unsigned long long)sh->sector, i);
2410 set_bit(R5_LOCKED, &dev->flags);
2411 set_bit(R5_Wantread, &dev->flags);
2412 s->locked++;
2413 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002414 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002415 "block %d for Reconstruct\n",
2416 (unsigned long long)sh->sector, i);
2417 set_bit(STRIPE_DELAYED, &sh->state);
2418 set_bit(STRIPE_HANDLE, &sh->state);
2419 }
2420 }
2421 }
2422 /* now if nothing is locked, and if we have enough data, we can start a
2423 * write request
2424 */
2425 if (s->locked == 0 && rcw == 0 &&
2426 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2427 if (must_compute > 0) {
2428 /* We have failed blocks and need to compute them */
2429 switch (s->failed) {
2430 case 0:
2431 BUG();
2432 case 1:
2433 compute_block_1(sh, r6s->failed_num[0], 0);
2434 break;
2435 case 2:
2436 compute_block_2(sh, r6s->failed_num[0],
2437 r6s->failed_num[1]);
2438 break;
2439 default: /* This request should have been failed? */
2440 BUG();
2441 }
2442 }
2443
Dan Williams45b42332007-07-09 11:56:43 -07002444 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002445 (unsigned long long)sh->sector);
2446 compute_parity6(sh, RECONSTRUCT_WRITE);
2447 /* now every locked buffer is ready to be written */
2448 for (i = disks; i--; )
2449 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002450 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002451 (unsigned long long)sh->sector, i);
2452 s->locked++;
2453 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2454 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002455 if (s->locked == disks)
2456 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2457 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002458 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2459 set_bit(STRIPE_INSYNC, &sh->state);
2460
2461 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2462 atomic_dec(&conf->preread_active_stripes);
2463 if (atomic_read(&conf->preread_active_stripes) <
2464 IO_THRESHOLD)
2465 md_wakeup_thread(conf->mddev->thread);
2466 }
2467 }
2468}
2469
2470static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2471 struct stripe_head_state *s, int disks)
2472{
Dan Williamsecc65c92008-06-28 08:31:57 +10002473 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002474
Dan Williamsbd2ab672008-04-10 21:29:27 -07002475 set_bit(STRIPE_HANDLE, &sh->state);
2476
Dan Williamsecc65c92008-06-28 08:31:57 +10002477 switch (sh->check_state) {
2478 case check_state_idle:
2479 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002480 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002481 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002482 sh->check_state = check_state_run;
2483 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002484 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002485 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002486 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002487 }
Dan Williamsa4456852007-07-09 11:56:43 -07002488 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002489 /* fall through */
2490 case check_state_compute_result:
2491 sh->check_state = check_state_idle;
2492 if (!dev)
2493 dev = &sh->dev[sh->pd_idx];
2494
2495 /* check that a write has not made the stripe insync */
2496 if (test_bit(STRIPE_INSYNC, &sh->state))
2497 break;
2498
2499 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002500 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2501 BUG_ON(s->uptodate != disks);
2502
2503 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002504 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002505 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002506
Dan Williamsa4456852007-07-09 11:56:43 -07002507 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002508 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002509 break;
2510 case check_state_run:
2511 break; /* we will be called again upon completion */
2512 case check_state_check_result:
2513 sh->check_state = check_state_idle;
2514
2515 /* if a failure occurred during the check operation, leave
2516 * STRIPE_INSYNC not set and let the stripe be handled again
2517 */
2518 if (s->failed)
2519 break;
2520
2521 /* handle a successful check operation, if parity is correct
2522 * we are done. Otherwise update the mismatch count and repair
2523 * parity if !MD_RECOVERY_CHECK
2524 */
2525 if (sh->ops.zero_sum_result == 0)
2526 /* parity is correct (on disc,
2527 * not in buffer any more)
2528 */
2529 set_bit(STRIPE_INSYNC, &sh->state);
2530 else {
2531 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2532 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2533 /* don't try to repair!! */
2534 set_bit(STRIPE_INSYNC, &sh->state);
2535 else {
2536 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002537 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002538 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2539 set_bit(R5_Wantcompute,
2540 &sh->dev[sh->pd_idx].flags);
2541 sh->ops.target = sh->pd_idx;
2542 s->uptodate++;
2543 }
2544 }
2545 break;
2546 case check_state_compute_run:
2547 break;
2548 default:
2549 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2550 __func__, sh->check_state,
2551 (unsigned long long) sh->sector);
2552 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002553 }
2554}
2555
2556
2557static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2558 struct stripe_head_state *s,
2559 struct r6_state *r6s, struct page *tmp_page,
2560 int disks)
2561{
2562 int update_p = 0, update_q = 0;
2563 struct r5dev *dev;
2564 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002565 int qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07002566
2567 set_bit(STRIPE_HANDLE, &sh->state);
2568
2569 BUG_ON(s->failed > 2);
2570 BUG_ON(s->uptodate < disks);
2571 /* Want to check and possibly repair P and Q.
2572 * However there could be one 'failed' device, in which
2573 * case we can only check one of them, possibly using the
2574 * other to generate missing data
2575 */
2576
2577 /* If !tmp_page, we cannot do the calculations,
2578 * but as we have set STRIPE_HANDLE, we will soon be called
2579 * by stripe_handle with a tmp_page - just wait until then.
2580 */
2581 if (tmp_page) {
2582 if (s->failed == r6s->q_failed) {
2583 /* The only possible failed device holds 'Q', so it
2584 * makes sense to check P (If anything else were failed,
2585 * we would have used P to recreate it).
2586 */
2587 compute_block_1(sh, pd_idx, 1);
2588 if (!page_is_zero(sh->dev[pd_idx].page)) {
2589 compute_block_1(sh, pd_idx, 0);
2590 update_p = 1;
2591 }
2592 }
2593 if (!r6s->q_failed && s->failed < 2) {
2594 /* q is not failed, and we didn't use it to generate
2595 * anything, so it makes sense to check it
2596 */
2597 memcpy(page_address(tmp_page),
2598 page_address(sh->dev[qd_idx].page),
2599 STRIPE_SIZE);
2600 compute_parity6(sh, UPDATE_PARITY);
2601 if (memcmp(page_address(tmp_page),
2602 page_address(sh->dev[qd_idx].page),
2603 STRIPE_SIZE) != 0) {
2604 clear_bit(STRIPE_INSYNC, &sh->state);
2605 update_q = 1;
2606 }
2607 }
2608 if (update_p || update_q) {
2609 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2610 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2611 /* don't try to repair!! */
2612 update_p = update_q = 0;
2613 }
2614
2615 /* now write out any block on a failed drive,
2616 * or P or Q if they need it
2617 */
2618
2619 if (s->failed == 2) {
2620 dev = &sh->dev[r6s->failed_num[1]];
2621 s->locked++;
2622 set_bit(R5_LOCKED, &dev->flags);
2623 set_bit(R5_Wantwrite, &dev->flags);
2624 }
2625 if (s->failed >= 1) {
2626 dev = &sh->dev[r6s->failed_num[0]];
2627 s->locked++;
2628 set_bit(R5_LOCKED, &dev->flags);
2629 set_bit(R5_Wantwrite, &dev->flags);
2630 }
2631
2632 if (update_p) {
2633 dev = &sh->dev[pd_idx];
2634 s->locked++;
2635 set_bit(R5_LOCKED, &dev->flags);
2636 set_bit(R5_Wantwrite, &dev->flags);
2637 }
2638 if (update_q) {
2639 dev = &sh->dev[qd_idx];
2640 s->locked++;
2641 set_bit(R5_LOCKED, &dev->flags);
2642 set_bit(R5_Wantwrite, &dev->flags);
2643 }
2644 clear_bit(STRIPE_DEGRADED, &sh->state);
2645
2646 set_bit(STRIPE_INSYNC, &sh->state);
2647 }
2648}
2649
2650static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2651 struct r6_state *r6s)
2652{
2653 int i;
2654
2655 /* We have read all the blocks in this stripe and now we need to
2656 * copy some of them into a target stripe for expand.
2657 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002658 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002659 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2660 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002661 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002662 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002663 struct stripe_head *sh2;
2664
2665 sector_t bn = compute_blocknr(sh, i);
NeilBrown911d4ee2009-03-31 14:39:38 +11002666 sector_t s = raid5_compute_sector(conf, bn, 0,
2667 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11002668 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002669 if (sh2 == NULL)
2670 /* so far only the early blocks of this stripe
2671 * have been requested. When later blocks
2672 * get requested, we will try again
2673 */
2674 continue;
2675 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2676 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2677 /* must have already done this block */
2678 release_stripe(sh2);
2679 continue;
2680 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002681
2682 /* place all the copies on one channel */
2683 tx = async_memcpy(sh2->dev[dd_idx].page,
2684 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2685 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2686
Dan Williamsa4456852007-07-09 11:56:43 -07002687 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2688 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2689 for (j = 0; j < conf->raid_disks; j++)
2690 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002691 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002692 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2693 break;
2694 if (j == conf->raid_disks) {
2695 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2696 set_bit(STRIPE_HANDLE, &sh2->state);
2697 }
2698 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002699
Dan Williamsa4456852007-07-09 11:56:43 -07002700 }
NeilBrowna2e08552007-09-11 15:23:36 -07002701 /* done submitting copies, wait for them to complete */
2702 if (tx) {
2703 async_tx_ack(tx);
2704 dma_wait_for_async_tx(tx);
2705 }
Dan Williamsa4456852007-07-09 11:56:43 -07002706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Dan Williams6bfe0b42008-04-30 00:52:32 -07002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709/*
2710 * handle_stripe - do things to a stripe.
2711 *
2712 * We lock the stripe and then examine the state of various bits
2713 * to see what needs to be done.
2714 * Possible results:
2715 * return some read request which now have data
2716 * return some write requests which are safely on disc
2717 * schedule a read on some buffers
2718 * schedule a write of some buffers
2719 * return confirmation of parity correctness
2720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * buffers are taken off read_list or write_list, and bh_cache buffers
2722 * get BH_Lock set before the stripe lock is released.
2723 *
2724 */
Dan Williamsa4456852007-07-09 11:56:43 -07002725
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002726static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002729 int disks = sh->disks, i;
2730 struct bio *return_bi = NULL;
2731 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002733 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002734 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Dan Williamsa4456852007-07-09 11:56:43 -07002736 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002737 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2738 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2739 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2740 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 spin_lock(&sh->lock);
2743 clear_bit(STRIPE_HANDLE, &sh->state);
2744 clear_bit(STRIPE_DELAYED, &sh->state);
2745
Dan Williamsa4456852007-07-09 11:56:43 -07002746 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2747 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2748 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002751 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 for (i=disks; i--; ) {
2753 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002754 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Dan Williamsb5e98d62007-01-02 13:52:31 -07002757 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2758 "written %p\n", i, dev->flags, dev->toread, dev->read,
2759 dev->towrite, dev->written);
2760
2761 /* maybe we can request a biofill operation
2762 *
2763 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002764 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002765 */
2766 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002767 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002768 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002771 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2772 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002773 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Dan Williamsb5e98d62007-01-02 13:52:31 -07002775 if (test_bit(R5_Wantfill, &dev->flags))
2776 s.to_fill++;
2777 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002778 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002780 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002782 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Dan Williamsa4456852007-07-09 11:56:43 -07002784 if (dev->written)
2785 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002786 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002787 if (blocked_rdev == NULL &&
2788 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002789 blocked_rdev = rdev;
2790 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002791 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002792 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002793 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002794 clear_bit(R5_ReadError, &dev->flags);
2795 clear_bit(R5_ReWrite, &dev->flags);
2796 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002797 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002798 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002799 s.failed++;
2800 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 } else
2802 set_bit(R5_Insync, &dev->flags);
2803 }
NeilBrown9910f162006-01-06 00:20:24 -08002804 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002805
Dan Williams6bfe0b42008-04-30 00:52:32 -07002806 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002807 if (s.syncing || s.expanding || s.expanded ||
2808 s.to_write || s.written) {
2809 set_bit(STRIPE_HANDLE, &sh->state);
2810 goto unlock;
2811 }
2812 /* There is nothing for the blocked_rdev to block */
2813 rdev_dec_pending(blocked_rdev, conf->mddev);
2814 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002815 }
2816
Dan Williams83de75c2008-06-28 08:31:58 +10002817 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2818 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2819 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2820 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002821
Dan Williams45b42332007-07-09 11:56:43 -07002822 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002824 s.locked, s.uptodate, s.to_read, s.to_write,
2825 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /* check if the array has lost two devices and, if so, some requests might
2827 * need to be failed
2828 */
Dan Williamsa4456852007-07-09 11:56:43 -07002829 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002830 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002831 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2833 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002834 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
2837 /* might be able to return some write requests if the parity block
2838 * is safe, or on a failed drive
2839 */
2840 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002841 if ( s.written &&
2842 ((test_bit(R5_Insync, &dev->flags) &&
2843 !test_bit(R5_LOCKED, &dev->flags) &&
2844 test_bit(R5_UPTODATE, &dev->flags)) ||
2845 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002846 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 /* Now we might consider reading some blocks, either to check/generate
2849 * parity, or to satisfy requests
2850 * or to load a block that is being partially written.
2851 */
Dan Williamsa4456852007-07-09 11:56:43 -07002852 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002853 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002854 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Dan Williamse33129d2007-01-02 13:52:30 -07002856 /* Now we check to see if any write operations have recently
2857 * completed
2858 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002859 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002860 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07002861 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002862 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2863 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002864 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002865
2866 /* All the 'written' buffers and the parity block are ready to
2867 * be written back to disk
2868 */
2869 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2870 for (i = disks; i--; ) {
2871 dev = &sh->dev[i];
2872 if (test_bit(R5_LOCKED, &dev->flags) &&
2873 (i == sh->pd_idx || dev->written)) {
2874 pr_debug("Writing block %d\n", i);
2875 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002876 if (prexor)
2877 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002878 if (!test_bit(R5_Insync, &dev->flags) ||
2879 (i == sh->pd_idx && s.failed == 0))
2880 set_bit(STRIPE_INSYNC, &sh->state);
2881 }
2882 }
2883 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2884 atomic_dec(&conf->preread_active_stripes);
2885 if (atomic_read(&conf->preread_active_stripes) <
2886 IO_THRESHOLD)
2887 md_wakeup_thread(conf->mddev->thread);
2888 }
2889 }
2890
2891 /* Now to consider new write requests and what else, if anything
2892 * should be read. We do not handle new writes when:
2893 * 1/ A 'write' operation (copy+xor) is already in flight.
2894 * 2/ A 'check' operation is in flight, as it may clobber the parity
2895 * block.
2896 */
Dan Williams600aa102008-06-28 08:32:05 +10002897 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002898 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002901 * Any reads will already have been scheduled, so we just see if enough
2902 * data is available. The parity check is held off while parity
2903 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002905 if (sh->check_state ||
2906 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002907 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002908 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002909 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002910
Dan Williamsa4456852007-07-09 11:56:43 -07002911 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2913 clear_bit(STRIPE_SYNCING, &sh->state);
2914 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002915
2916 /* If the failed drive is just a ReadError, then we might need to progress
2917 * the repair/check process
2918 */
Dan Williamsa4456852007-07-09 11:56:43 -07002919 if (s.failed == 1 && !conf->mddev->ro &&
2920 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2921 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2922 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002923 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002924 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08002925 if (!test_bit(R5_ReWrite, &dev->flags)) {
2926 set_bit(R5_Wantwrite, &dev->flags);
2927 set_bit(R5_ReWrite, &dev->flags);
2928 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002929 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002930 } else {
2931 /* let's read it back */
2932 set_bit(R5_Wantread, &dev->flags);
2933 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002934 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002935 }
2936 }
2937
Dan Williams600aa102008-06-28 08:32:05 +10002938 /* Finish reconstruct operations initiated by the expansion process */
2939 if (sh->reconstruct_state == reconstruct_state_result) {
2940 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002941 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07002942 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07002943 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07002944 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10002945 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07002946 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002947 }
2948
2949 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10002950 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002951 /* Need to write out all blocks after computing parity */
2952 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11002953 stripe_set_idx(sh->sector, conf, 0, sh);
Dan Williams1fe797e2008-06-28 09:16:30 +10002954 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10002955 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002956 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08002957 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002958 wake_up(&conf->wait_for_overlap);
2959 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2960 }
2961
Dan Williams0f94e872008-01-08 15:32:53 -08002962 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002963 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002964 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002965
Dan Williams6bfe0b42008-04-30 00:52:32 -07002966 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 spin_unlock(&sh->lock);
2968
Dan Williams6bfe0b42008-04-30 00:52:32 -07002969 /* wait for this device to become unblocked */
2970 if (unlikely(blocked_rdev))
2971 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2972
Dan Williams600aa102008-06-28 08:32:05 +10002973 if (s.ops_request)
2974 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07002975
Dan Williamsc4e5ac02008-06-28 08:31:53 +10002976 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Dan Williamsa4456852007-07-09 11:56:43 -07002978 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002979
2980 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002983static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07002984{
NeilBrownbff61972009-03-31 14:33:13 +11002985 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002986 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07002987 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11002988 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07002989 struct stripe_head_state s;
2990 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07002991 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002992 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07002993
Dan Williams45b42332007-07-09 11:56:43 -07002994 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07002995 "pd_idx=%d, qd_idx=%d\n",
2996 (unsigned long long)sh->sector, sh->state,
NeilBrown34e04e82009-03-31 15:10:16 +11002997 atomic_read(&sh->count), pd_idx, qd_idx);
Dan Williamsa4456852007-07-09 11:56:43 -07002998 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002999
3000 spin_lock(&sh->lock);
3001 clear_bit(STRIPE_HANDLE, &sh->state);
3002 clear_bit(STRIPE_DELAYED, &sh->state);
3003
Dan Williamsa4456852007-07-09 11:56:43 -07003004 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3005 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3006 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003007 /* Now to look around and see what can be done */
3008
3009 rcu_read_lock();
3010 for (i=disks; i--; ) {
3011 mdk_rdev_t *rdev;
3012 dev = &sh->dev[i];
3013 clear_bit(R5_Insync, &dev->flags);
3014
Dan Williams45b42332007-07-09 11:56:43 -07003015 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003016 i, dev->flags, dev->toread, dev->towrite, dev->written);
3017 /* maybe we can reply to a read */
3018 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3019 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003020 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003021 spin_lock_irq(&conf->device_lock);
3022 rbi = dev->toread;
3023 dev->toread = NULL;
3024 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3025 wake_up(&conf->wait_for_overlap);
3026 spin_unlock_irq(&conf->device_lock);
3027 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3028 copy_data(0, rbi, dev->page, dev->sector);
3029 rbi2 = r5_next_bio(rbi, dev->sector);
3030 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003031 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003032 rbi->bi_next = return_bi;
3033 return_bi = rbi;
3034 }
3035 spin_unlock_irq(&conf->device_lock);
3036 rbi = rbi2;
3037 }
3038 }
3039
3040 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003041 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3042 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003043
3044
Dan Williamsa4456852007-07-09 11:56:43 -07003045 if (dev->toread)
3046 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003047 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003048 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003049 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003050 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003051 }
Dan Williamsa4456852007-07-09 11:56:43 -07003052 if (dev->written)
3053 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003054 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003055 if (blocked_rdev == NULL &&
3056 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003057 blocked_rdev = rdev;
3058 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003059 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003060 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3061 /* The ReadError flag will just be confusing now */
3062 clear_bit(R5_ReadError, &dev->flags);
3063 clear_bit(R5_ReWrite, &dev->flags);
3064 }
3065 if (!rdev || !test_bit(In_sync, &rdev->flags)
3066 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003067 if (s.failed < 2)
3068 r6s.failed_num[s.failed] = i;
3069 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003070 } else
3071 set_bit(R5_Insync, &dev->flags);
3072 }
3073 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003074
3075 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003076 if (s.syncing || s.expanding || s.expanded ||
3077 s.to_write || s.written) {
3078 set_bit(STRIPE_HANDLE, &sh->state);
3079 goto unlock;
3080 }
3081 /* There is nothing for the blocked_rdev to block */
3082 rdev_dec_pending(blocked_rdev, conf->mddev);
3083 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003084 }
NeilBrownac4090d2008-08-05 15:54:13 +10003085
Dan Williams45b42332007-07-09 11:56:43 -07003086 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003087 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003088 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3089 r6s.failed_num[0], r6s.failed_num[1]);
3090 /* check if the array has lost >2 devices and, if so, some requests
3091 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003092 */
Dan Williamsa4456852007-07-09 11:56:43 -07003093 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003094 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003095 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003096 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3097 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003098 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003099 }
3100
3101 /*
3102 * might be able to return some write requests if the parity blocks
3103 * are safe, or on a failed drive
3104 */
3105 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003106 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3107 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003108 qdev = &sh->dev[qd_idx];
3109 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3110 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003111
Dan Williamsa4456852007-07-09 11:56:43 -07003112 if ( s.written &&
3113 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003114 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003115 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3116 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003117 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003118 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003119 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003120
3121 /* Now we might consider reading some blocks, either to check/generate
3122 * parity, or to satisfy requests
3123 * or to load a block that is being partially written.
3124 */
Dan Williamsa4456852007-07-09 11:56:43 -07003125 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3126 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003127 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003128
3129 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07003130 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10003131 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003132
3133 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003134 * Any reads will already have been scheduled, so we just see if enough
3135 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003136 */
Dan Williamsa4456852007-07-09 11:56:43 -07003137 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
3138 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003139
Dan Williamsa4456852007-07-09 11:56:43 -07003140 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003141 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3142 clear_bit(STRIPE_SYNCING, &sh->state);
3143 }
3144
3145 /* If the failed drives are just a ReadError, then we might need
3146 * to progress the repair/check process
3147 */
Dan Williamsa4456852007-07-09 11:56:43 -07003148 if (s.failed <= 2 && !conf->mddev->ro)
3149 for (i = 0; i < s.failed; i++) {
3150 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003151 if (test_bit(R5_ReadError, &dev->flags)
3152 && !test_bit(R5_LOCKED, &dev->flags)
3153 && test_bit(R5_UPTODATE, &dev->flags)
3154 ) {
3155 if (!test_bit(R5_ReWrite, &dev->flags)) {
3156 set_bit(R5_Wantwrite, &dev->flags);
3157 set_bit(R5_ReWrite, &dev->flags);
3158 set_bit(R5_LOCKED, &dev->flags);
3159 } else {
3160 /* let's read it back */
3161 set_bit(R5_Wantread, &dev->flags);
3162 set_bit(R5_LOCKED, &dev->flags);
3163 }
3164 }
3165 }
NeilBrownf4168852007-02-28 20:11:53 -08003166
Dan Williamsa4456852007-07-09 11:56:43 -07003167 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownf4168852007-02-28 20:11:53 -08003168 /* Need to write out all blocks after computing P&Q */
3169 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003170 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003171 compute_parity6(sh, RECONSTRUCT_WRITE);
3172 for (i = conf->raid_disks ; i-- ; ) {
3173 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003174 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003175 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3176 }
3177 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003178 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003179 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3180 atomic_dec(&conf->reshape_stripes);
3181 wake_up(&conf->wait_for_overlap);
3182 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3183 }
3184
Dan Williams0f94e872008-01-08 15:32:53 -08003185 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003186 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003187 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003188
Dan Williams6bfe0b42008-04-30 00:52:32 -07003189 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003190 spin_unlock(&sh->lock);
3191
Dan Williams6bfe0b42008-04-30 00:52:32 -07003192 /* wait for this device to become unblocked */
3193 if (unlikely(blocked_rdev))
3194 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3195
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003196 ops_run_io(sh, &s);
3197
Dan Williamsa4456852007-07-09 11:56:43 -07003198 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003199
3200 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003201}
3202
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003203/* returns true if the stripe was handled */
3204static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07003205{
3206 if (sh->raid_conf->level == 6)
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003207 return handle_stripe6(sh, tmp_page);
NeilBrown16a53ec2006-06-26 00:27:38 -07003208 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003209 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003210}
3211
3212
3213
Arjan van de Ven858119e2006-01-14 13:20:43 -08003214static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3217 while (!list_empty(&conf->delayed_list)) {
3218 struct list_head *l = conf->delayed_list.next;
3219 struct stripe_head *sh;
3220 sh = list_entry(l, struct stripe_head, lru);
3221 list_del_init(l);
3222 clear_bit(STRIPE_DELAYED, &sh->state);
3223 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3224 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003225 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
NeilBrown6ed30032008-02-06 01:40:00 -08003227 } else
3228 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Arjan van de Ven858119e2006-01-14 13:20:43 -08003231static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003232{
3233 /* device_lock is held */
3234 struct list_head head;
3235 list_add(&head, &conf->bitmap_list);
3236 list_del_init(&conf->bitmap_list);
3237 while (!list_empty(&head)) {
3238 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3239 list_del_init(&sh->lru);
3240 atomic_inc(&sh->count);
3241 __release_stripe(conf, sh);
3242 }
3243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245static void unplug_slaves(mddev_t *mddev)
3246{
3247 raid5_conf_t *conf = mddev_to_conf(mddev);
3248 int i;
3249
3250 rcu_read_lock();
3251 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003252 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003253 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003254 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
3256 atomic_inc(&rdev->nr_pending);
3257 rcu_read_unlock();
3258
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003259 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 rdev_dec_pending(rdev, mddev);
3262 rcu_read_lock();
3263 }
3264 }
3265 rcu_read_unlock();
3266}
3267
Jens Axboe165125e2007-07-24 09:28:11 +02003268static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 mddev_t *mddev = q->queuedata;
3271 raid5_conf_t *conf = mddev_to_conf(mddev);
3272 unsigned long flags;
3273
3274 spin_lock_irqsave(&conf->device_lock, flags);
3275
NeilBrown72626682005-09-09 16:23:54 -07003276 if (blk_remove_plug(q)) {
3277 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 md_wakeup_thread(mddev->thread);
3281
3282 spin_unlock_irqrestore(&conf->device_lock, flags);
3283
3284 unplug_slaves(mddev);
3285}
3286
NeilBrownf022b2f2006-10-03 01:15:56 -07003287static int raid5_congested(void *data, int bits)
3288{
3289 mddev_t *mddev = data;
3290 raid5_conf_t *conf = mddev_to_conf(mddev);
3291
3292 /* No difference between reads and writes. Just check
3293 * how busy the stripe_cache is
3294 */
3295 if (conf->inactive_blocked)
3296 return 1;
3297 if (conf->quiesce)
3298 return 1;
3299 if (list_empty_careful(&conf->inactive_list))
3300 return 1;
3301
3302 return 0;
3303}
3304
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003305/* We want read requests to align with chunks where possible,
3306 * but write requests don't need to.
3307 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003308static int raid5_mergeable_bvec(struct request_queue *q,
3309 struct bvec_merge_data *bvm,
3310 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003311{
3312 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003313 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003314 int max;
3315 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003316 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003317
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003318 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003319 return biovec->bv_len; /* always allow writes to be mergeable */
3320
3321 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3322 if (max < 0) max = 0;
3323 if (max <= biovec->bv_len && bio_sectors == 0)
3324 return biovec->bv_len;
3325 else
3326 return max;
3327}
3328
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003329
3330static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3331{
3332 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3333 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3334 unsigned int bio_sectors = bio->bi_size >> 9;
3335
3336 return chunk_sectors >=
3337 ((sector & (chunk_sectors - 1)) + bio_sectors);
3338}
3339
3340/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003341 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3342 * later sampled by raid5d.
3343 */
3344static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3345{
3346 unsigned long flags;
3347
3348 spin_lock_irqsave(&conf->device_lock, flags);
3349
3350 bi->bi_next = conf->retry_read_aligned_list;
3351 conf->retry_read_aligned_list = bi;
3352
3353 spin_unlock_irqrestore(&conf->device_lock, flags);
3354 md_wakeup_thread(conf->mddev->thread);
3355}
3356
3357
3358static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3359{
3360 struct bio *bi;
3361
3362 bi = conf->retry_read_aligned;
3363 if (bi) {
3364 conf->retry_read_aligned = NULL;
3365 return bi;
3366 }
3367 bi = conf->retry_read_aligned_list;
3368 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003369 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003370 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003371 /*
3372 * this sets the active strip count to 1 and the processed
3373 * strip count to zero (upper 8 bits)
3374 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003375 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003376 }
3377
3378 return bi;
3379}
3380
3381
3382/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003383 * The "raid5_align_endio" should check if the read succeeded and if it
3384 * did, call bio_endio on the original bio (having bio_put the new bio
3385 * first).
3386 * If the read failed..
3387 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003388static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003389{
3390 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003391 mddev_t *mddev;
3392 raid5_conf_t *conf;
3393 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3394 mdk_rdev_t *rdev;
3395
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003396 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003397
3398 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3399 conf = mddev_to_conf(mddev);
3400 rdev = (void*)raid_bi->bi_next;
3401 raid_bi->bi_next = NULL;
3402
3403 rdev_dec_pending(rdev, conf->mddev);
3404
3405 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003406 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003407 if (atomic_dec_and_test(&conf->active_aligned_reads))
3408 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003409 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003410 }
3411
3412
Dan Williams45b42332007-07-09 11:56:43 -07003413 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003414
3415 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003416}
3417
Neil Brown387bb172007-02-08 14:20:29 -08003418static int bio_fits_rdev(struct bio *bi)
3419{
Jens Axboe165125e2007-07-24 09:28:11 +02003420 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003421
3422 if ((bi->bi_size>>9) > q->max_sectors)
3423 return 0;
3424 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003425 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003426 return 0;
3427
3428 if (q->merge_bvec_fn)
3429 /* it's too hard to apply the merge_bvec_fn at this stage,
3430 * just just give up
3431 */
3432 return 0;
3433
3434 return 1;
3435}
3436
3437
Jens Axboe165125e2007-07-24 09:28:11 +02003438static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003439{
3440 mddev_t *mddev = q->queuedata;
3441 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003442 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003443 struct bio* align_bi;
3444 mdk_rdev_t *rdev;
3445
3446 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003447 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003448 return 0;
3449 }
3450 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003451 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003452 */
3453 align_bi = bio_clone(raid_bio, GFP_NOIO);
3454 if (!align_bi)
3455 return 0;
3456 /*
3457 * set bi_end_io to a new function, and set bi_private to the
3458 * original bio.
3459 */
3460 align_bi->bi_end_io = raid5_align_endio;
3461 align_bi->bi_private = raid_bio;
3462 /*
3463 * compute position
3464 */
NeilBrown112bf892009-03-31 14:39:38 +11003465 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3466 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003467 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003468
3469 rcu_read_lock();
3470 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3471 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003472 atomic_inc(&rdev->nr_pending);
3473 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003474 raid_bio->bi_next = (void*)rdev;
3475 align_bi->bi_bdev = rdev->bdev;
3476 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3477 align_bi->bi_sector += rdev->data_offset;
3478
Neil Brown387bb172007-02-08 14:20:29 -08003479 if (!bio_fits_rdev(align_bi)) {
3480 /* too big in some way */
3481 bio_put(align_bi);
3482 rdev_dec_pending(rdev, mddev);
3483 return 0;
3484 }
3485
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003486 spin_lock_irq(&conf->device_lock);
3487 wait_event_lock_irq(conf->wait_for_stripe,
3488 conf->quiesce == 0,
3489 conf->device_lock, /* nothing */);
3490 atomic_inc(&conf->active_aligned_reads);
3491 spin_unlock_irq(&conf->device_lock);
3492
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003493 generic_make_request(align_bi);
3494 return 1;
3495 } else {
3496 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003497 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003498 return 0;
3499 }
3500}
3501
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003502/* __get_priority_stripe - get the next stripe to process
3503 *
3504 * Full stripe writes are allowed to pass preread active stripes up until
3505 * the bypass_threshold is exceeded. In general the bypass_count
3506 * increments when the handle_list is handled before the hold_list; however, it
3507 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3508 * stripe with in flight i/o. The bypass_count will be reset when the
3509 * head of the hold_list has changed, i.e. the head was promoted to the
3510 * handle_list.
3511 */
3512static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3513{
3514 struct stripe_head *sh;
3515
3516 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3517 __func__,
3518 list_empty(&conf->handle_list) ? "empty" : "busy",
3519 list_empty(&conf->hold_list) ? "empty" : "busy",
3520 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3521
3522 if (!list_empty(&conf->handle_list)) {
3523 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3524
3525 if (list_empty(&conf->hold_list))
3526 conf->bypass_count = 0;
3527 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3528 if (conf->hold_list.next == conf->last_hold)
3529 conf->bypass_count++;
3530 else {
3531 conf->last_hold = conf->hold_list.next;
3532 conf->bypass_count -= conf->bypass_threshold;
3533 if (conf->bypass_count < 0)
3534 conf->bypass_count = 0;
3535 }
3536 }
3537 } else if (!list_empty(&conf->hold_list) &&
3538 ((conf->bypass_threshold &&
3539 conf->bypass_count > conf->bypass_threshold) ||
3540 atomic_read(&conf->pending_full_writes) == 0)) {
3541 sh = list_entry(conf->hold_list.next,
3542 typeof(*sh), lru);
3543 conf->bypass_count -= conf->bypass_threshold;
3544 if (conf->bypass_count < 0)
3545 conf->bypass_count = 0;
3546 } else
3547 return NULL;
3548
3549 list_del_init(&sh->lru);
3550 atomic_inc(&sh->count);
3551 BUG_ON(atomic_read(&sh->count) != 1);
3552 return sh;
3553}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003554
Jens Axboe165125e2007-07-24 09:28:11 +02003555static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 mddev_t *mddev = q->queuedata;
3558 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003559 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 sector_t new_sector;
3561 sector_t logical_sector, last_sector;
3562 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003563 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003564 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
NeilBrowne5dcdd82005-09-09 16:23:41 -07003566 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003567 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003568 return 0;
3569 }
3570
NeilBrown3d310eb2005-06-21 17:17:26 -07003571 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003572
Tejun Heo074a7ac2008-08-25 19:56:14 +09003573 cpu = part_stat_lock();
3574 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3575 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3576 bio_sectors(bi));
3577 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
NeilBrown802ba062006-12-13 00:34:13 -08003579 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003580 mddev->reshape_position == MaxSector &&
3581 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003582 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3585 last_sector = bi->bi_sector + (bi->bi_size>>9);
3586 bi->bi_next = NULL;
3587 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3590 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003591 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003592 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003593
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003594 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003595 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003596 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownfef9c612009-03-31 15:16:46 +11003597 if (likely(conf->reshape_progress == MaxSector))
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003598 disks = conf->raid_disks;
3599 else {
NeilBrownfef9c612009-03-31 15:16:46 +11003600 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003601 * 64bit on a 32bit platform, and so it might be
3602 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003603 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003604 * the lock is dropped, so once we get a reference
3605 * to the stripe that we think it is, we will have
3606 * to check again.
3607 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003608 spin_lock_irq(&conf->device_lock);
3609 disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11003610 if (mddev->delta_disks < 0
3611 ? logical_sector < conf->reshape_progress
3612 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003613 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003614 previous = 1;
3615 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003616 if (mddev->delta_disks < 0
3617 ? logical_sector < conf->reshape_safe
3618 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003619 spin_unlock_irq(&conf->device_lock);
3620 schedule();
3621 goto retry;
3622 }
3623 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003624 spin_unlock_irq(&conf->device_lock);
3625 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003626 data_disks = disks - conf->max_degraded;
3627
NeilBrown112bf892009-03-31 14:39:38 +11003628 new_sector = raid5_compute_sector(conf, logical_sector,
3629 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003630 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003631 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 (unsigned long long)new_sector,
3633 (unsigned long long)logical_sector);
3634
NeilBrownb5663ba2009-03-31 14:39:38 +11003635 sh = get_active_stripe(conf, new_sector, previous,
3636 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (sh) {
NeilBrownfef9c612009-03-31 15:16:46 +11003638 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003639 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003640 * stripe, so we must do the range check again.
3641 * Expansion could still move past after this
3642 * test, but as we are holding a reference to
3643 * 'sh', we know that if that happens,
3644 * STRIPE_EXPANDING will get set and the expansion
3645 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003646 */
3647 int must_retry = 0;
3648 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003649 if ((mddev->delta_disks < 0
3650 ? logical_sector >= conf->reshape_progress
3651 : logical_sector < conf->reshape_progress)
NeilBrown86b42c72009-03-31 15:19:03 +11003652 && previous)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003653 /* mismatch, need to try again */
3654 must_retry = 1;
3655 spin_unlock_irq(&conf->device_lock);
3656 if (must_retry) {
3657 release_stripe(sh);
3658 goto retry;
3659 }
3660 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003661 /* FIXME what if we get a false positive because these
3662 * are being updated.
3663 */
3664 if (logical_sector >= mddev->suspend_lo &&
3665 logical_sector < mddev->suspend_hi) {
3666 release_stripe(sh);
3667 schedule();
3668 goto retry;
3669 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003670
3671 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3672 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3673 /* Stripe is busy expanding or
3674 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * and wait a while
3676 */
3677 raid5_unplug_device(mddev->queue);
3678 release_stripe(sh);
3679 schedule();
3680 goto retry;
3681 }
3682 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003683 set_bit(STRIPE_HANDLE, &sh->state);
3684 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 } else {
3687 /* cannot get stripe for read-ahead, just give-up */
3688 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3689 finish_wait(&conf->wait_for_overlap, &w);
3690 break;
3691 }
3692
3693 }
3694 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003695 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003696 spin_unlock_irq(&conf->device_lock);
3697 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
NeilBrown16a53ec2006-06-26 00:27:38 -07003699 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003701
Neil Brown0e13fe232008-06-28 08:31:20 +10003702 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705}
3706
Dan Williamsb522adc2009-03-31 15:00:31 +11003707static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3708
NeilBrown52c03292006-06-26 00:27:43 -07003709static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
NeilBrown52c03292006-06-26 00:27:43 -07003711 /* reshaping is quite different to recovery/resync so it is
3712 * handled quite separately ... here.
3713 *
3714 * On each call to sync_request, we gather one chunk worth of
3715 * destination stripes and flag them as expanding.
3716 * Then we find all the source stripes and request reads.
3717 * As the reads complete, handle_stripe will copy the data
3718 * into the destination stripe and release that stripe.
3719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3721 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003722 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003723 int raid_disks = conf->previous_raid_disks;
3724 int data_disks = raid_disks - conf->max_degraded;
3725 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003726 int i;
3727 int dd_idx;
3728 sector_t writepos, safepos, gap;
NeilBrownec32a2b2009-03-31 15:17:38 +11003729 sector_t stripe_addr;
NeilBrown52c03292006-06-26 00:27:43 -07003730
NeilBrownfef9c612009-03-31 15:16:46 +11003731 if (sector_nr == 0) {
3732 /* If restarting in the middle, skip the initial sectors */
3733 if (mddev->delta_disks < 0 &&
3734 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3735 sector_nr = raid5_size(mddev, 0, 0)
3736 - conf->reshape_progress;
3737 } else if (mddev->delta_disks > 0 &&
3738 conf->reshape_progress > 0)
3739 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003740 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003741 if (sector_nr) {
3742 *skipped = 1;
3743 return sector_nr;
3744 }
NeilBrown52c03292006-06-26 00:27:43 -07003745 }
3746
3747 /* we update the metadata when there is more than 3Meg
3748 * in the block range (that is rather arbitrary, should
3749 * probably be time based) or when the data about to be
3750 * copied would over-write the source of the data at
3751 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11003752 * i.e. one new_stripe along from reshape_progress new_maps
3753 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07003754 */
NeilBrownfef9c612009-03-31 15:16:46 +11003755 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003756 sector_div(writepos, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003757 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08003758 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11003759 if (mddev->delta_disks < 0) {
3760 writepos -= conf->chunk_size/512;
3761 safepos += conf->chunk_size/512;
3762 gap = conf->reshape_safe - conf->reshape_progress;
3763 } else {
3764 writepos += conf->chunk_size/512;
3765 safepos -= conf->chunk_size/512;
3766 gap = conf->reshape_progress - conf->reshape_safe;
3767 }
NeilBrown52c03292006-06-26 00:27:43 -07003768
NeilBrownfef9c612009-03-31 15:16:46 +11003769 if ((mddev->delta_disks < 0
3770 ? writepos < safepos
3771 : writepos > safepos) ||
NeilBrownf4168852007-02-28 20:11:53 -08003772 gap > (new_data_disks)*3000*2 /*3Meg*/) {
NeilBrown52c03292006-06-26 00:27:43 -07003773 /* Cannot proceed until we've updated the superblock... */
3774 wait_event(conf->wait_for_overlap,
3775 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11003776 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07003777 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003778 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003779 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003780 kthread_should_stop());
3781 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003782 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07003783 spin_unlock_irq(&conf->device_lock);
3784 wake_up(&conf->wait_for_overlap);
3785 }
3786
NeilBrownec32a2b2009-03-31 15:17:38 +11003787 if (mddev->delta_disks < 0) {
3788 BUG_ON(conf->reshape_progress == 0);
3789 stripe_addr = writepos;
3790 BUG_ON((mddev->dev_sectors &
3791 ~((sector_t)mddev->chunk_size / 512 - 1))
3792 - (conf->chunk_size / 512) - stripe_addr
3793 != sector_nr);
3794 } else {
3795 BUG_ON(writepos != sector_nr + conf->chunk_size / 512);
3796 stripe_addr = sector_nr;
3797 }
NeilBrown52c03292006-06-26 00:27:43 -07003798 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3799 int j;
3800 int skipped = 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11003801 sh = get_active_stripe(conf, stripe_addr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003802 set_bit(STRIPE_EXPANDING, &sh->state);
3803 atomic_inc(&conf->reshape_stripes);
3804 /* If any of this stripe is beyond the end of the old
3805 * array, then we need to zero those blocks
3806 */
3807 for (j=sh->disks; j--;) {
3808 sector_t s;
3809 if (j == sh->pd_idx)
3810 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003811 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003812 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003813 continue;
NeilBrown52c03292006-06-26 00:27:43 -07003814 s = compute_blocknr(sh, j);
Dan Williamsb522adc2009-03-31 15:00:31 +11003815 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07003816 skipped = 1;
3817 continue;
3818 }
3819 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3820 set_bit(R5_Expanded, &sh->dev[j].flags);
3821 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3822 }
3823 if (!skipped) {
3824 set_bit(STRIPE_EXPAND_READY, &sh->state);
3825 set_bit(STRIPE_HANDLE, &sh->state);
3826 }
3827 release_stripe(sh);
3828 }
3829 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003830 if (mddev->delta_disks < 0)
3831 conf->reshape_progress -= i * new_data_disks;
3832 else
3833 conf->reshape_progress += i * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003834 spin_unlock_irq(&conf->device_lock);
3835 /* Ok, those stripe are ready. We can start scheduling
3836 * reads on the source stripes.
3837 * The source stripes are determined by mapping the first and last
3838 * block on the destination stripes.
3839 */
NeilBrown52c03292006-06-26 00:27:43 -07003840 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003841 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11003842 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07003843 last_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11003844 raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512)
NeilBrown112bf892009-03-31 14:39:38 +11003845 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11003846 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11003847 if (last_sector >= mddev->dev_sectors)
3848 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07003849 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003850 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003851 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3852 set_bit(STRIPE_HANDLE, &sh->state);
3853 release_stripe(sh);
3854 first_sector += STRIPE_SECTORS;
3855 }
NeilBrownc6207272008-02-06 01:39:52 -08003856 /* If this takes us to the resync_max point where we have to pause,
3857 * then we need to write out the superblock.
3858 */
3859 sector_nr += conf->chunk_size>>9;
3860 if (sector_nr >= mddev->resync_max) {
3861 /* Cannot proceed until we've updated the superblock... */
3862 wait_event(conf->wait_for_overlap,
3863 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11003864 mddev->reshape_position = conf->reshape_progress;
NeilBrownc6207272008-02-06 01:39:52 -08003865 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3866 md_wakeup_thread(mddev->thread);
3867 wait_event(mddev->sb_wait,
3868 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3869 || kthread_should_stop());
3870 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003871 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08003872 spin_unlock_irq(&conf->device_lock);
3873 wake_up(&conf->wait_for_overlap);
3874 }
NeilBrown52c03292006-06-26 00:27:43 -07003875 return conf->chunk_size>>9;
3876}
3877
3878/* FIXME go_faster isn't used */
3879static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3880{
3881 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3882 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11003883 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07003884 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003885 int still_degraded = 0;
3886 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
NeilBrown72626682005-09-09 16:23:54 -07003888 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /* just being told to finish up .. nothing much to do */
3890 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11003891
NeilBrown29269552006-03-27 01:18:10 -08003892 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3893 end_reshape(conf);
3894 return 0;
3895 }
NeilBrown72626682005-09-09 16:23:54 -07003896
3897 if (mddev->curr_resync < max_sector) /* aborted */
3898 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3899 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003900 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003901 conf->fullsync = 0;
3902 bitmap_close_sync(mddev->bitmap);
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return 0;
3905 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003906
NeilBrown52c03292006-06-26 00:27:43 -07003907 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3908 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003909
NeilBrownc6207272008-02-06 01:39:52 -08003910 /* No need to check resync_max as we never do more than one
3911 * stripe, and as resync_max will always be on a chunk boundary,
3912 * if the check in md_do_sync didn't fire, there is no chance
3913 * of overstepping resync_max here
3914 */
3915
NeilBrown16a53ec2006-06-26 00:27:38 -07003916 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 * to resync, then assert that we are finished, because there is
3918 * nothing we can do.
3919 */
NeilBrown3285edf2006-06-26 00:27:55 -07003920 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003921 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003922 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07003923 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return rv;
3925 }
NeilBrown72626682005-09-09 16:23:54 -07003926 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003927 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003928 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3929 /* we can skip this block, and probably more */
3930 sync_blocks /= STRIPE_SECTORS;
3931 *skipped = 1;
3932 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
NeilBrownb47490c2008-02-06 01:39:50 -08003935
3936 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3937
NeilBrownb5663ba2009-03-31 14:39:38 +11003938 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003940 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003942 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003944 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003946 /* Need to check if array will still be degraded after recovery/resync
3947 * We don't need to check the 'failed' flag as when that gets set,
3948 * recovery aborts.
3949 */
3950 for (i=0; i<mddev->raid_disks; i++)
3951 if (conf->disks[i].rdev == NULL)
3952 still_degraded = 1;
3953
3954 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3955
3956 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 set_bit(STRIPE_SYNCING, &sh->state);
3958 clear_bit(STRIPE_INSYNC, &sh->state);
3959 spin_unlock(&sh->lock);
3960
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003961 /* wait for any blocked device to be handled */
3962 while(unlikely(!handle_stripe(sh, NULL)))
3963 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 release_stripe(sh);
3965
3966 return STRIPE_SECTORS;
3967}
3968
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003969static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3970{
3971 /* We may not be able to submit a whole bio at once as there
3972 * may not be enough stripe_heads available.
3973 * We cannot pre-allocate enough stripe_heads as we may need
3974 * more than exist in the cache (if we allow ever large chunks).
3975 * So we do one stripe head at a time and record in
3976 * ->bi_hw_segments how many have been done.
3977 *
3978 * We *know* that this entire raid_bio is in one chunk, so
3979 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3980 */
3981 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11003982 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003983 sector_t sector, logical_sector, last_sector;
3984 int scnt = 0;
3985 int remaining;
3986 int handled = 0;
3987
3988 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11003989 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11003990 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003991 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3992
3993 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08003994 logical_sector += STRIPE_SECTORS,
3995 sector += STRIPE_SECTORS,
3996 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003997
Jens Axboe960e7392008-08-15 10:41:18 +02003998 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003999 /* already done this stripe */
4000 continue;
4001
NeilBrownb5663ba2009-03-31 14:39:38 +11004002 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004003
4004 if (!sh) {
4005 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004006 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004007 conf->retry_read_aligned = raid_bio;
4008 return handled;
4009 }
4010
4011 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004012 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4013 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004014 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004015 conf->retry_read_aligned = raid_bio;
4016 return handled;
4017 }
4018
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004019 handle_stripe(sh, NULL);
4020 release_stripe(sh);
4021 handled++;
4022 }
4023 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004024 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004025 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004026 if (remaining == 0)
4027 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004028 if (atomic_dec_and_test(&conf->active_aligned_reads))
4029 wake_up(&conf->wait_for_stripe);
4030 return handled;
4031}
4032
4033
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035/*
4036 * This is our raid5 kernel thread.
4037 *
4038 * We scan the hash table for stripes which can be handled now.
4039 * During the scan, completed stripes are saved for us by the interrupt
4040 * handler, so that they will not have to wait for our next wakeup.
4041 */
NeilBrown6ed30032008-02-06 01:40:00 -08004042static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
4044 struct stripe_head *sh;
4045 raid5_conf_t *conf = mddev_to_conf(mddev);
4046 int handled;
4047
Dan Williams45b42332007-07-09 11:56:43 -07004048 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
4050 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 handled = 0;
4053 spin_lock_irq(&conf->device_lock);
4054 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004055 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
NeilBrownae3c20c2006-07-10 04:44:17 -07004057 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004058 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004059 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004060 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004061 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004062 conf->seq_write = seq;
4063 activate_bit_delay(conf);
4064 }
4065
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004066 while ((bio = remove_bio_from_retry(conf))) {
4067 int ok;
4068 spin_unlock_irq(&conf->device_lock);
4069 ok = retry_aligned_read(conf, bio);
4070 spin_lock_irq(&conf->device_lock);
4071 if (!ok)
4072 break;
4073 handled++;
4074 }
4075
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004076 sh = __get_priority_stripe(conf);
4077
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004078 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 spin_unlock_irq(&conf->device_lock);
4081
4082 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004083 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 release_stripe(sh);
4085
4086 spin_lock_irq(&conf->device_lock);
4087 }
Dan Williams45b42332007-07-09 11:56:43 -07004088 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 spin_unlock_irq(&conf->device_lock);
4091
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004092 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 unplug_slaves(mddev);
4094
Dan Williams45b42332007-07-09 11:56:43 -07004095 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
NeilBrown3f294f42005-11-08 21:39:25 -08004098static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004099raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004100{
NeilBrown007583c2005-11-08 21:39:30 -08004101 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004102 if (conf)
4103 return sprintf(page, "%d\n", conf->max_nr_stripes);
4104 else
4105 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004106}
4107
4108static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004109raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004110{
NeilBrown007583c2005-11-08 21:39:30 -08004111 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004112 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004113 int err;
4114
NeilBrown3f294f42005-11-08 21:39:25 -08004115 if (len >= PAGE_SIZE)
4116 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004117 if (!conf)
4118 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004119
Dan Williams4ef197d82008-04-28 02:15:54 -07004120 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004121 return -EINVAL;
4122 if (new <= 16 || new > 32768)
4123 return -EINVAL;
4124 while (new < conf->max_nr_stripes) {
4125 if (drop_one_stripe(conf))
4126 conf->max_nr_stripes--;
4127 else
4128 break;
4129 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004130 err = md_allow_write(mddev);
4131 if (err)
4132 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004133 while (new > conf->max_nr_stripes) {
4134 if (grow_one_stripe(conf))
4135 conf->max_nr_stripes++;
4136 else break;
4137 }
4138 return len;
4139}
NeilBrown007583c2005-11-08 21:39:30 -08004140
NeilBrown96de1e62005-11-08 21:39:39 -08004141static struct md_sysfs_entry
4142raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4143 raid5_show_stripe_cache_size,
4144 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004145
4146static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004147raid5_show_preread_threshold(mddev_t *mddev, char *page)
4148{
4149 raid5_conf_t *conf = mddev_to_conf(mddev);
4150 if (conf)
4151 return sprintf(page, "%d\n", conf->bypass_threshold);
4152 else
4153 return 0;
4154}
4155
4156static ssize_t
4157raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4158{
4159 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004160 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004161 if (len >= PAGE_SIZE)
4162 return -EINVAL;
4163 if (!conf)
4164 return -ENODEV;
4165
Dan Williams4ef197d82008-04-28 02:15:54 -07004166 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004167 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004168 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004169 return -EINVAL;
4170 conf->bypass_threshold = new;
4171 return len;
4172}
4173
4174static struct md_sysfs_entry
4175raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4176 S_IRUGO | S_IWUSR,
4177 raid5_show_preread_threshold,
4178 raid5_store_preread_threshold);
4179
4180static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004181stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004182{
NeilBrown007583c2005-11-08 21:39:30 -08004183 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004184 if (conf)
4185 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4186 else
4187 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004188}
4189
NeilBrown96de1e62005-11-08 21:39:39 -08004190static struct md_sysfs_entry
4191raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004192
NeilBrown007583c2005-11-08 21:39:30 -08004193static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004194 &raid5_stripecache_size.attr,
4195 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004196 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004197 NULL,
4198};
NeilBrown007583c2005-11-08 21:39:30 -08004199static struct attribute_group raid5_attrs_group = {
4200 .name = NULL,
4201 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004202};
4203
Dan Williams80c3a6c2009-03-17 18:10:40 -07004204static sector_t
4205raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4206{
4207 raid5_conf_t *conf = mddev_to_conf(mddev);
4208
4209 if (!sectors)
4210 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004211 if (!raid_disks) {
4212 /* size is defined by the smallest of previous and new size */
4213 if (conf->raid_disks < conf->previous_raid_disks)
4214 raid_disks = conf->raid_disks;
4215 else
4216 raid_disks = conf->previous_raid_disks;
4217 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004218
4219 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
4220 return sectors * (raid_disks - conf->max_degraded);
4221}
4222
NeilBrown91adb562009-03-31 14:39:39 +11004223static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 raid5_conf_t *conf;
4226 int raid_disk, memory;
4227 mdk_rdev_t *rdev;
4228 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
NeilBrown91adb562009-03-31 14:39:39 +11004230 if (mddev->new_level != 5
4231 && mddev->new_level != 4
4232 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004233 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004234 mdname(mddev), mddev->new_level);
4235 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 }
NeilBrown91adb562009-03-31 14:39:39 +11004237 if ((mddev->new_level == 5
4238 && !algorithm_valid_raid5(mddev->new_layout)) ||
4239 (mddev->new_level == 6
4240 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004241 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004242 mdname(mddev), mddev->new_layout);
4243 return ERR_PTR(-EIO);
4244 }
4245 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4246 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4247 mdname(mddev), mddev->raid_disks);
4248 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
NeilBrown91adb562009-03-31 14:39:39 +11004251 if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
4252 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4253 mddev->new_chunk, mdname(mddev));
4254 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004255 }
4256
NeilBrown91adb562009-03-31 14:39:39 +11004257 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4258 if (conf == NULL)
4259 goto abort;
4260
4261 conf->raid_disks = mddev->raid_disks;
4262 if (mddev->reshape_position == MaxSector)
4263 conf->previous_raid_disks = mddev->raid_disks;
4264 else
4265 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4266
4267 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4268 GFP_KERNEL);
4269 if (!conf->disks)
4270 goto abort;
4271
4272 conf->mddev = mddev;
4273
4274 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4275 goto abort;
4276
4277 if (mddev->new_level == 6) {
4278 conf->spare_page = alloc_page(GFP_KERNEL);
4279 if (!conf->spare_page)
4280 goto abort;
4281 }
4282 spin_lock_init(&conf->device_lock);
4283 init_waitqueue_head(&conf->wait_for_stripe);
4284 init_waitqueue_head(&conf->wait_for_overlap);
4285 INIT_LIST_HEAD(&conf->handle_list);
4286 INIT_LIST_HEAD(&conf->hold_list);
4287 INIT_LIST_HEAD(&conf->delayed_list);
4288 INIT_LIST_HEAD(&conf->bitmap_list);
4289 INIT_LIST_HEAD(&conf->inactive_list);
4290 atomic_set(&conf->active_stripes, 0);
4291 atomic_set(&conf->preread_active_stripes, 0);
4292 atomic_set(&conf->active_aligned_reads, 0);
4293 conf->bypass_threshold = BYPASS_THRESHOLD;
4294
4295 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4296
4297 list_for_each_entry(rdev, &mddev->disks, same_set) {
4298 raid_disk = rdev->raid_disk;
4299 if (raid_disk >= conf->raid_disks
4300 || raid_disk < 0)
4301 continue;
4302 disk = conf->disks + raid_disk;
4303
4304 disk->rdev = rdev;
4305
4306 if (test_bit(In_sync, &rdev->flags)) {
4307 char b[BDEVNAME_SIZE];
4308 printk(KERN_INFO "raid5: device %s operational as raid"
4309 " disk %d\n", bdevname(rdev->bdev,b),
4310 raid_disk);
4311 } else
4312 /* Cannot rely on bitmap to complete recovery */
4313 conf->fullsync = 1;
4314 }
4315
4316 conf->chunk_size = mddev->new_chunk;
4317 conf->level = mddev->new_level;
4318 if (conf->level == 6)
4319 conf->max_degraded = 2;
4320 else
4321 conf->max_degraded = 1;
4322 conf->algorithm = mddev->new_layout;
4323 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004324 conf->reshape_progress = mddev->reshape_position;
NeilBrown91adb562009-03-31 14:39:39 +11004325
4326 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4327 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4328 if (grow_stripes(conf, conf->max_nr_stripes)) {
4329 printk(KERN_ERR
4330 "raid5: couldn't allocate %dkB for buffers\n", memory);
4331 goto abort;
4332 } else
4333 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4334 memory, mdname(mddev));
4335
4336 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4337 if (!conf->thread) {
4338 printk(KERN_ERR
4339 "raid5: couldn't allocate thread for %s\n",
4340 mdname(mddev));
4341 goto abort;
4342 }
4343
4344 return conf;
4345
4346 abort:
4347 if (conf) {
4348 shrink_stripes(conf);
4349 safe_put_page(conf->spare_page);
4350 kfree(conf->disks);
4351 kfree(conf->stripe_hashtbl);
4352 kfree(conf);
4353 return ERR_PTR(-EIO);
4354 } else
4355 return ERR_PTR(-ENOMEM);
4356}
4357
4358static int run(mddev_t *mddev)
4359{
4360 raid5_conf_t *conf;
4361 int working_disks = 0;
4362 mdk_rdev_t *rdev;
4363
NeilBrownf6705572006-03-27 01:18:11 -08004364 if (mddev->reshape_position != MaxSector) {
4365 /* Check that we can continue the reshape.
4366 * Currently only disks can change, it must
4367 * increase, and we must be past the point where
4368 * a stripe over-writes itself
4369 */
4370 sector_t here_new, here_old;
4371 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004372 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004373
4374 if (mddev->new_level != mddev->level ||
4375 mddev->new_layout != mddev->layout ||
4376 mddev->new_chunk != mddev->chunk_size) {
NeilBrownf4168852007-02-28 20:11:53 -08004377 printk(KERN_ERR "raid5: %s: unsupported reshape "
4378 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004379 mdname(mddev));
4380 return -EINVAL;
4381 }
NeilBrownf6705572006-03-27 01:18:11 -08004382 old_disks = mddev->raid_disks - mddev->delta_disks;
4383 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004384 * further up in new geometry must map after here in old
4385 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004386 */
4387 here_new = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004388 if (sector_div(here_new, (mddev->chunk_size>>9)*
4389 (mddev->raid_disks - max_degraded))) {
4390 printk(KERN_ERR "raid5: reshape_position not "
4391 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004392 return -EINVAL;
4393 }
4394 /* here_new is the stripe we will write to */
4395 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004396 sector_div(here_old, (mddev->chunk_size>>9)*
4397 (old_disks-max_degraded));
4398 /* here_old is the first stripe that we might need to read
4399 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004400 if (here_new >= here_old) {
4401 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004402 printk(KERN_ERR "raid5: reshape_position too early for "
4403 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004404 return -EINVAL;
4405 }
4406 printk(KERN_INFO "raid5: reshape will continue\n");
4407 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004408 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004409 BUG_ON(mddev->level != mddev->new_level);
4410 BUG_ON(mddev->layout != mddev->new_layout);
4411 BUG_ON(mddev->chunk_size != mddev->new_chunk);
4412 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004413 }
4414
NeilBrown245f46c2009-03-31 14:39:39 +11004415 if (mddev->private == NULL)
4416 conf = setup_conf(mddev);
4417 else
4418 conf = mddev->private;
4419
NeilBrown91adb562009-03-31 14:39:39 +11004420 if (IS_ERR(conf))
4421 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004422
NeilBrown91adb562009-03-31 14:39:39 +11004423 mddev->thread = conf->thread;
4424 conf->thread = NULL;
4425 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004428 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 */
NeilBrown91adb562009-03-31 14:39:39 +11004430 list_for_each_entry(rdev, &mddev->disks, same_set)
4431 if (rdev->raid_disk >= 0 &&
4432 test_bit(In_sync, &rdev->flags))
4433 working_disks++;
4434
NeilBrown02c2de82006-10-03 01:15:47 -07004435 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
NeilBrown16a53ec2006-06-26 00:27:38 -07004437 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 printk(KERN_ERR "raid5: not enough operational devices for %s"
4439 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004440 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 goto abort;
4442 }
4443
NeilBrown91adb562009-03-31 14:39:39 +11004444 /* device size must be a multiple of chunk size */
4445 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4446 mddev->resync_max_sectors = mddev->dev_sectors;
4447
NeilBrown16a53ec2006-06-26 00:27:38 -07004448 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004450 if (mddev->ok_start_degraded)
4451 printk(KERN_WARNING
4452 "raid5: starting dirty degraded array: %s"
4453 "- data corruption possible.\n",
4454 mdname(mddev));
4455 else {
4456 printk(KERN_ERR
4457 "raid5: cannot start dirty degraded array for %s\n",
4458 mdname(mddev));
4459 goto abort;
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 }
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (mddev->degraded == 0)
4464 printk("raid5: raid level %d set %s active with %d out of %d"
4465 " devices, algorithm %d\n", conf->level, mdname(mddev),
4466 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4467 conf->algorithm);
4468 else
4469 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4470 " out of %d devices, algorithm %d\n", conf->level,
4471 mdname(mddev), mddev->raid_disks - mddev->degraded,
4472 mddev->raid_disks, conf->algorithm);
4473
4474 print_raid5_conf(conf);
4475
NeilBrownfef9c612009-03-31 15:16:46 +11004476 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004477 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004478 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004479 atomic_set(&conf->reshape_stripes, 0);
4480 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4481 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4482 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4483 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4484 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4485 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004486 }
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004489 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 */
4491 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004492 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4493 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004494 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4496 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4497 }
4498
4499 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004500 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4501 printk(KERN_WARNING
4502 "raid5: failed to create sysfs attributes for %s\n",
4503 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004504
NeilBrown91adb562009-03-31 14:39:39 +11004505 mddev->queue->queue_lock = &conf->device_lock;
4506
NeilBrown7a5febe2005-05-16 21:53:16 -07004507 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004508 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004509 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004510
Dan Williams1f403622009-03-31 14:59:03 +11004511 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004512
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004513 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return 0;
4516abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11004517 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11004518 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (conf) {
NeilBrown91adb562009-03-31 14:39:39 +11004520 shrink_stripes(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07004522 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004523 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08004524 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 kfree(conf);
4526 }
4527 mddev->private = NULL;
4528 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4529 return -EIO;
4530}
4531
4532
4533
NeilBrown3f294f42005-11-08 21:39:25 -08004534static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4537
4538 md_unregister_thread(mddev->thread);
4539 mddev->thread = NULL;
4540 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08004541 kfree(conf->stripe_hashtbl);
NeilBrown041ae522007-03-26 21:32:14 -08004542 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004544 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004545 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08004546 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 mddev->private = NULL;
4548 return 0;
4549}
4550
Dan Williams45b42332007-07-09 11:56:43 -07004551#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004552static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
4554 int i;
4555
NeilBrown16a53ec2006-06-26 00:27:38 -07004556 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4557 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4558 seq_printf(seq, "sh %llu, count %d.\n",
4559 (unsigned long long)sh->sector, atomic_read(&sh->count));
4560 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004561 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004562 seq_printf(seq, "(cache%d: %p %ld) ",
4563 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004565 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566}
4567
NeilBrownd710e132008-10-13 11:55:12 +11004568static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
4570 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004571 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 int i;
4573
4574 spin_lock_irq(&conf->device_lock);
4575 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004576 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (sh->raid_conf != conf)
4578 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004579 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 }
4581 }
4582 spin_unlock_irq(&conf->device_lock);
4583}
4584#endif
4585
NeilBrownd710e132008-10-13 11:55:12 +11004586static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587{
4588 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4589 int i;
4590
4591 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004592 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 for (i = 0; i < conf->raid_disks; i++)
4594 seq_printf (seq, "%s",
4595 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004596 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004598#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004599 seq_printf (seq, "\n");
4600 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601#endif
4602}
4603
4604static void print_raid5_conf (raid5_conf_t *conf)
4605{
4606 int i;
4607 struct disk_info *tmp;
4608
4609 printk("RAID5 conf printout:\n");
4610 if (!conf) {
4611 printk("(conf==NULL)\n");
4612 return;
4613 }
NeilBrown02c2de82006-10-03 01:15:47 -07004614 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4615 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 for (i = 0; i < conf->raid_disks; i++) {
4618 char b[BDEVNAME_SIZE];
4619 tmp = conf->disks + i;
4620 if (tmp->rdev)
4621 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004622 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 bdevname(tmp->rdev->bdev,b));
4624 }
4625}
4626
4627static int raid5_spare_active(mddev_t *mddev)
4628{
4629 int i;
4630 raid5_conf_t *conf = mddev->private;
4631 struct disk_info *tmp;
4632
4633 for (i = 0; i < conf->raid_disks; i++) {
4634 tmp = conf->disks + i;
4635 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004636 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004637 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4638 unsigned long flags;
4639 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004641 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
4643 }
4644 print_raid5_conf(conf);
4645 return 0;
4646}
4647
4648static int raid5_remove_disk(mddev_t *mddev, int number)
4649{
4650 raid5_conf_t *conf = mddev->private;
4651 int err = 0;
4652 mdk_rdev_t *rdev;
4653 struct disk_info *p = conf->disks + number;
4654
4655 print_raid5_conf(conf);
4656 rdev = p->rdev;
4657 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11004658 if (number >= conf->raid_disks &&
4659 conf->reshape_progress == MaxSector)
4660 clear_bit(In_sync, &rdev->flags);
4661
NeilBrownb2d444d2005-11-08 21:39:31 -08004662 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 atomic_read(&rdev->nr_pending)) {
4664 err = -EBUSY;
4665 goto abort;
4666 }
NeilBrowndfc70642008-05-23 13:04:39 -07004667 /* Only remove non-faulty devices if recovery
4668 * isn't possible.
4669 */
4670 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11004671 mddev->degraded <= conf->max_degraded &&
4672 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07004673 err = -EBUSY;
4674 goto abort;
4675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004677 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (atomic_read(&rdev->nr_pending)) {
4679 /* lost the race, try later */
4680 err = -EBUSY;
4681 p->rdev = rdev;
4682 }
4683 }
4684abort:
4685
4686 print_raid5_conf(conf);
4687 return err;
4688}
4689
4690static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4691{
4692 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004693 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 int disk;
4695 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004696 int first = 0;
4697 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
NeilBrown16a53ec2006-06-26 00:27:38 -07004699 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004701 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Neil Brown6c2fce22008-06-28 08:31:31 +10004703 if (rdev->raid_disk >= 0)
4704 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004707 * find the disk ... but prefer rdev->saved_raid_disk
4708 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004710 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004711 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004712 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4713 disk = rdev->saved_raid_disk;
4714 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004715 disk = first;
4716 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004718 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004720 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004721 if (rdev->saved_raid_disk != disk)
4722 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004723 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 break;
4725 }
4726 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
4730static int raid5_resize(mddev_t *mddev, sector_t sectors)
4731{
4732 /* no resync is happening, and there is enough space
4733 * on all devices, so we can resize.
4734 * We need to make sure resync covers any new space.
4735 * If the array is shrinking we should possibly wait until
4736 * any io in the removed space completes, but it hardly seems
4737 * worth it.
4738 */
4739 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Dan Williams1f403622009-03-31 14:59:03 +11004740 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
4741 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11004742 if (mddev->array_sectors >
4743 raid5_size(mddev, sectors, mddev->raid_disks))
4744 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10004745 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004746 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11004747 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
4748 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4750 }
Andre Noll58c0fed2009-03-31 14:33:13 +11004751 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07004752 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 return 0;
4754}
4755
NeilBrown29269552006-03-27 01:18:10 -08004756#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004757static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08004758{
4759 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown29269552006-03-27 01:18:10 -08004760
NeilBrown63c70c42006-03-27 01:18:13 -08004761 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08004762 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10004763 if (mddev->bitmap)
4764 /* Cannot grow a bitmap yet */
4765 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11004766 if (mddev->degraded > conf->max_degraded)
4767 return -EINVAL;
4768 if (mddev->delta_disks < 0) {
4769 /* We might be able to shrink, but the devices must
4770 * be made bigger first.
4771 * For raid6, 4 is the minimum size.
4772 * Otherwise 2 is the minimum
4773 */
4774 int min = 2;
4775 if (mddev->level == 6)
4776 min = 4;
4777 if (mddev->raid_disks + mddev->delta_disks < min)
4778 return -EINVAL;
4779 }
NeilBrown29269552006-03-27 01:18:10 -08004780
4781 /* Can only proceed if there are plenty of stripe_heads.
4782 * We need a minimum of one full stripe,, and for sensible progress
4783 * it is best to have about 4 times that.
4784 * If we require 4 times, then the default 256 4K stripe_heads will
4785 * allow for chunk sizes up to 256K, which is probably OK.
4786 * If the chunk size is greater, user-space should request more
4787 * stripe_heads first.
4788 */
NeilBrown63c70c42006-03-27 01:18:13 -08004789 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4790 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08004791 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
4792 (mddev->chunk_size / STRIPE_SIZE)*4);
4793 return -ENOSPC;
4794 }
4795
NeilBrownec32a2b2009-03-31 15:17:38 +11004796 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08004797}
4798
4799static int raid5_start_reshape(mddev_t *mddev)
4800{
4801 raid5_conf_t *conf = mddev_to_conf(mddev);
4802 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08004803 int spares = 0;
4804 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07004805 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08004806
NeilBrownf4168852007-02-28 20:11:53 -08004807 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08004808 return -EBUSY;
4809
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004810 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004811 if (rdev->raid_disk < 0 &&
4812 !test_bit(Faulty, &rdev->flags))
4813 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08004814
NeilBrownf4168852007-02-28 20:11:53 -08004815 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08004816 /* Not enough devices even to make a degraded array
4817 * of that size
4818 */
4819 return -EINVAL;
4820
NeilBrownec32a2b2009-03-31 15:17:38 +11004821 /* Refuse to reduce size of the array. Any reductions in
4822 * array size must be through explicit setting of array_size
4823 * attribute.
4824 */
4825 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
4826 < mddev->array_sectors) {
4827 printk(KERN_ERR "md: %s: array size must be reduced "
4828 "before number of disks\n", mdname(mddev));
4829 return -EINVAL;
4830 }
4831
NeilBrownf6705572006-03-27 01:18:11 -08004832 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08004833 spin_lock_irq(&conf->device_lock);
4834 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08004835 conf->raid_disks += mddev->delta_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004836 if (mddev->delta_disks < 0)
4837 conf->reshape_progress = raid5_size(mddev, 0, 0);
4838 else
4839 conf->reshape_progress = 0;
4840 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11004841 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08004842 spin_unlock_irq(&conf->device_lock);
4843
4844 /* Add some new drives, as many as will fit.
4845 * We know there are enough to make the newly sized array work.
4846 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004847 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004848 if (rdev->raid_disk < 0 &&
4849 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10004850 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08004851 char nm[20];
4852 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08004853 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004854 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08004855 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004856 if (sysfs_create_link(&mddev->kobj,
4857 &rdev->kobj, nm))
4858 printk(KERN_WARNING
4859 "raid5: failed to create "
4860 " link %s for %s\n",
4861 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08004862 } else
4863 break;
4864 }
4865
NeilBrownec32a2b2009-03-31 15:17:38 +11004866 if (mddev->delta_disks > 0) {
4867 spin_lock_irqsave(&conf->device_lock, flags);
4868 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
4869 - added_devices;
4870 spin_unlock_irqrestore(&conf->device_lock, flags);
4871 }
NeilBrown63c70c42006-03-27 01:18:13 -08004872 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08004873 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004874 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08004875
NeilBrown29269552006-03-27 01:18:10 -08004876 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4877 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4878 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4879 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4880 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4881 "%s_reshape");
4882 if (!mddev->sync_thread) {
4883 mddev->recovery = 0;
4884 spin_lock_irq(&conf->device_lock);
4885 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004886 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08004887 spin_unlock_irq(&conf->device_lock);
4888 return -EAGAIN;
4889 }
4890 md_wakeup_thread(mddev->sync_thread);
4891 md_new_event(mddev);
4892 return 0;
4893}
4894#endif
4895
NeilBrownec32a2b2009-03-31 15:17:38 +11004896/* This is called from the reshape thread and should make any
4897 * changes needed in 'conf'
4898 */
NeilBrown29269552006-03-27 01:18:10 -08004899static void end_reshape(raid5_conf_t *conf)
4900{
NeilBrown29269552006-03-27 01:18:10 -08004901
NeilBrownf6705572006-03-27 01:18:11 -08004902 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07004903
NeilBrownf6705572006-03-27 01:18:11 -08004904 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11004905 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004906 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08004907 spin_unlock_irq(&conf->device_lock);
NeilBrown16a53ec2006-06-26 00:27:38 -07004908
4909 /* read-ahead size must cover two whole stripes, which is
4910 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4911 */
4912 {
NeilBrowncea9c222009-03-31 15:15:05 +11004913 int data_disks = conf->raid_disks - conf->max_degraded;
4914 int stripe = data_disks * (conf->chunk_size
4915 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07004916 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4917 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4918 }
NeilBrown29269552006-03-27 01:18:10 -08004919 }
NeilBrown29269552006-03-27 01:18:10 -08004920}
4921
NeilBrownec32a2b2009-03-31 15:17:38 +11004922/* This is called from the raid5d thread with mddev_lock held.
4923 * It makes config changes to the device.
4924 */
NeilBrowncea9c222009-03-31 15:15:05 +11004925static void raid5_finish_reshape(mddev_t *mddev)
4926{
4927 struct block_device *bdev;
4928
4929 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4930
NeilBrownec32a2b2009-03-31 15:17:38 +11004931 if (mddev->delta_disks > 0) {
4932 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4933 set_capacity(mddev->gendisk, mddev->array_sectors);
4934 mddev->changed = 1;
NeilBrowncea9c222009-03-31 15:15:05 +11004935
NeilBrownec32a2b2009-03-31 15:17:38 +11004936 bdev = bdget_disk(mddev->gendisk, 0);
4937 if (bdev) {
4938 mutex_lock(&bdev->bd_inode->i_mutex);
4939 i_size_write(bdev->bd_inode,
4940 (loff_t)mddev->array_sectors << 9);
4941 mutex_unlock(&bdev->bd_inode->i_mutex);
4942 bdput(bdev);
4943 }
4944 } else {
4945 int d;
4946 raid5_conf_t *conf = mddev_to_conf(mddev);
4947 mddev->degraded = conf->raid_disks;
4948 for (d = 0; d < conf->raid_disks ; d++)
4949 if (conf->disks[d].rdev &&
4950 test_bit(In_sync,
4951 &conf->disks[d].rdev->flags))
4952 mddev->degraded--;
4953 for (d = conf->raid_disks ;
4954 d < conf->raid_disks - mddev->delta_disks;
4955 d++)
4956 raid5_remove_disk(mddev, d);
NeilBrowncea9c222009-03-31 15:15:05 +11004957 }
NeilBrownec32a2b2009-03-31 15:17:38 +11004958 mddev->reshape_position = MaxSector;
4959 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11004960 }
4961}
4962
NeilBrown72626682005-09-09 16:23:54 -07004963static void raid5_quiesce(mddev_t *mddev, int state)
4964{
4965 raid5_conf_t *conf = mddev_to_conf(mddev);
4966
4967 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08004968 case 2: /* resume for a suspend */
4969 wake_up(&conf->wait_for_overlap);
4970 break;
4971
NeilBrown72626682005-09-09 16:23:54 -07004972 case 1: /* stop all writes */
4973 spin_lock_irq(&conf->device_lock);
4974 conf->quiesce = 1;
4975 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004976 atomic_read(&conf->active_stripes) == 0 &&
4977 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07004978 conf->device_lock, /* nothing */);
4979 spin_unlock_irq(&conf->device_lock);
4980 break;
4981
4982 case 0: /* re-enable writes */
4983 spin_lock_irq(&conf->device_lock);
4984 conf->quiesce = 0;
4985 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08004986 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07004987 spin_unlock_irq(&conf->device_lock);
4988 break;
4989 }
NeilBrown72626682005-09-09 16:23:54 -07004990}
NeilBrownb15c2e52006-01-06 00:20:16 -08004991
NeilBrownd562b0c2009-03-31 14:39:39 +11004992
4993static void *raid5_takeover_raid1(mddev_t *mddev)
4994{
4995 int chunksect;
4996
4997 if (mddev->raid_disks != 2 ||
4998 mddev->degraded > 1)
4999 return ERR_PTR(-EINVAL);
5000
5001 /* Should check if there are write-behind devices? */
5002
5003 chunksect = 64*2; /* 64K by default */
5004
5005 /* The array must be an exact multiple of chunksize */
5006 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5007 chunksect >>= 1;
5008
5009 if ((chunksect<<9) < STRIPE_SIZE)
5010 /* array size does not allow a suitable chunk size */
5011 return ERR_PTR(-EINVAL);
5012
5013 mddev->new_level = 5;
5014 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5015 mddev->new_chunk = chunksect << 9;
5016
5017 return setup_conf(mddev);
5018}
5019
NeilBrownfc9739c2009-03-31 14:57:20 +11005020static void *raid5_takeover_raid6(mddev_t *mddev)
5021{
5022 int new_layout;
5023
5024 switch (mddev->layout) {
5025 case ALGORITHM_LEFT_ASYMMETRIC_6:
5026 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5027 break;
5028 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5029 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5030 break;
5031 case ALGORITHM_LEFT_SYMMETRIC_6:
5032 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5033 break;
5034 case ALGORITHM_RIGHT_SYMMETRIC_6:
5035 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5036 break;
5037 case ALGORITHM_PARITY_0_6:
5038 new_layout = ALGORITHM_PARITY_0;
5039 break;
5040 case ALGORITHM_PARITY_N:
5041 new_layout = ALGORITHM_PARITY_N;
5042 break;
5043 default:
5044 return ERR_PTR(-EINVAL);
5045 }
5046 mddev->new_level = 5;
5047 mddev->new_layout = new_layout;
5048 mddev->delta_disks = -1;
5049 mddev->raid_disks -= 1;
5050 return setup_conf(mddev);
5051}
5052
NeilBrownd562b0c2009-03-31 14:39:39 +11005053
NeilBrownb3546032009-03-31 14:56:41 +11005054static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
5055{
5056 /* Currently the layout and chunk size can only be changed
5057 * for a 2-drive raid array, as in that case no data shuffling
5058 * is required.
5059 * Later we might validate these and set new_* so a reshape
5060 * can complete the change.
5061 */
5062 raid5_conf_t *conf = mddev_to_conf(mddev);
5063
5064 if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
5065 return -EINVAL;
5066 if (new_chunk > 0) {
5067 if (new_chunk & (new_chunk-1))
5068 /* not a power of 2 */
5069 return -EINVAL;
5070 if (new_chunk < PAGE_SIZE)
5071 return -EINVAL;
5072 if (mddev->array_sectors & ((new_chunk>>9)-1))
5073 /* not factor of array size */
5074 return -EINVAL;
5075 }
5076
5077 /* They look valid */
5078
5079 if (mddev->raid_disks != 2)
5080 return -EINVAL;
5081
5082 if (new_layout >= 0) {
5083 conf->algorithm = new_layout;
5084 mddev->layout = mddev->new_layout = new_layout;
5085 }
5086 if (new_chunk > 0) {
5087 conf->chunk_size = new_chunk;
5088 mddev->chunk_size = mddev->new_chunk = new_chunk;
5089 }
5090 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5091 md_wakeup_thread(mddev->thread);
5092 return 0;
5093}
5094
NeilBrownd562b0c2009-03-31 14:39:39 +11005095static void *raid5_takeover(mddev_t *mddev)
5096{
5097 /* raid5 can take over:
5098 * raid0 - if all devices are the same - make it a raid4 layout
5099 * raid1 - if there are two drives. We need to know the chunk size
5100 * raid4 - trivial - just use a raid4 layout.
5101 * raid6 - Providing it is a *_6 layout
5102 *
5103 * For now, just do raid1
5104 */
5105
5106 if (mddev->level == 1)
5107 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005108 if (mddev->level == 4) {
5109 mddev->new_layout = ALGORITHM_PARITY_N;
5110 mddev->new_level = 5;
5111 return setup_conf(mddev);
5112 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005113 if (mddev->level == 6)
5114 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005115
5116 return ERR_PTR(-EINVAL);
5117}
5118
5119
NeilBrown245f46c2009-03-31 14:39:39 +11005120static struct mdk_personality raid5_personality;
5121
5122static void *raid6_takeover(mddev_t *mddev)
5123{
5124 /* Currently can only take over a raid5. We map the
5125 * personality to an equivalent raid6 personality
5126 * with the Q block at the end.
5127 */
5128 int new_layout;
5129
5130 if (mddev->pers != &raid5_personality)
5131 return ERR_PTR(-EINVAL);
5132 if (mddev->degraded > 1)
5133 return ERR_PTR(-EINVAL);
5134 if (mddev->raid_disks > 253)
5135 return ERR_PTR(-EINVAL);
5136 if (mddev->raid_disks < 3)
5137 return ERR_PTR(-EINVAL);
5138
5139 switch (mddev->layout) {
5140 case ALGORITHM_LEFT_ASYMMETRIC:
5141 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5142 break;
5143 case ALGORITHM_RIGHT_ASYMMETRIC:
5144 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5145 break;
5146 case ALGORITHM_LEFT_SYMMETRIC:
5147 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5148 break;
5149 case ALGORITHM_RIGHT_SYMMETRIC:
5150 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5151 break;
5152 case ALGORITHM_PARITY_0:
5153 new_layout = ALGORITHM_PARITY_0_6;
5154 break;
5155 case ALGORITHM_PARITY_N:
5156 new_layout = ALGORITHM_PARITY_N;
5157 break;
5158 default:
5159 return ERR_PTR(-EINVAL);
5160 }
5161 mddev->new_level = 6;
5162 mddev->new_layout = new_layout;
5163 mddev->delta_disks = 1;
5164 mddev->raid_disks += 1;
5165 return setup_conf(mddev);
5166}
5167
5168
NeilBrown16a53ec2006-06-26 00:27:38 -07005169static struct mdk_personality raid6_personality =
5170{
5171 .name = "raid6",
5172 .level = 6,
5173 .owner = THIS_MODULE,
5174 .make_request = make_request,
5175 .run = run,
5176 .stop = stop,
5177 .status = status,
5178 .error_handler = error,
5179 .hot_add_disk = raid5_add_disk,
5180 .hot_remove_disk= raid5_remove_disk,
5181 .spare_active = raid5_spare_active,
5182 .sync_request = sync_request,
5183 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005184 .size = raid5_size,
NeilBrownf4168852007-02-28 20:11:53 -08005185#ifdef CONFIG_MD_RAID5_RESHAPE
5186 .check_reshape = raid5_check_reshape,
5187 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005188 .finish_reshape = raid5_finish_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005189#endif
NeilBrown16a53ec2006-06-26 00:27:38 -07005190 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005191 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005192};
NeilBrown2604b702006-01-06 00:20:36 -08005193static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
5195 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005196 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 .owner = THIS_MODULE,
5198 .make_request = make_request,
5199 .run = run,
5200 .stop = stop,
5201 .status = status,
5202 .error_handler = error,
5203 .hot_add_disk = raid5_add_disk,
5204 .hot_remove_disk= raid5_remove_disk,
5205 .spare_active = raid5_spare_active,
5206 .sync_request = sync_request,
5207 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005208 .size = raid5_size,
NeilBrown29269552006-03-27 01:18:10 -08005209#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08005210 .check_reshape = raid5_check_reshape,
5211 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005212 .finish_reshape = raid5_finish_reshape,
NeilBrown29269552006-03-27 01:18:10 -08005213#endif
NeilBrown72626682005-09-09 16:23:54 -07005214 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005215 .takeover = raid5_takeover,
NeilBrownb3546032009-03-31 14:56:41 +11005216 .reconfig = raid5_reconfig,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217};
5218
NeilBrown2604b702006-01-06 00:20:36 -08005219static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
NeilBrown2604b702006-01-06 00:20:36 -08005221 .name = "raid4",
5222 .level = 4,
5223 .owner = THIS_MODULE,
5224 .make_request = make_request,
5225 .run = run,
5226 .stop = stop,
5227 .status = status,
5228 .error_handler = error,
5229 .hot_add_disk = raid5_add_disk,
5230 .hot_remove_disk= raid5_remove_disk,
5231 .spare_active = raid5_spare_active,
5232 .sync_request = sync_request,
5233 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005234 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005235#ifdef CONFIG_MD_RAID5_RESHAPE
5236 .check_reshape = raid5_check_reshape,
5237 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005238 .finish_reshape = raid5_finish_reshape,
NeilBrown3d378902007-03-26 21:32:13 -08005239#endif
NeilBrown2604b702006-01-06 00:20:36 -08005240 .quiesce = raid5_quiesce,
5241};
5242
5243static int __init raid5_init(void)
5244{
NeilBrown16a53ec2006-06-26 00:27:38 -07005245 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005246 register_md_personality(&raid5_personality);
5247 register_md_personality(&raid4_personality);
5248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
NeilBrown2604b702006-01-06 00:20:36 -08005251static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
NeilBrown16a53ec2006-06-26 00:27:38 -07005253 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005254 unregister_md_personality(&raid5_personality);
5255 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256}
5257
5258module_init(raid5_init);
5259module_exit(raid5_exit);
5260MODULE_LICENSE("GPL");
5261MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005262MODULE_ALIAS("md-raid5");
5263MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005264MODULE_ALIAS("md-level-5");
5265MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005266MODULE_ALIAS("md-personality-8"); /* RAID6 */
5267MODULE_ALIAS("md-raid6");
5268MODULE_ALIAS("md-level-6");
5269
5270/* This used to be two separate modules, they were: */
5271MODULE_ALIAS("raid5");
5272MODULE_ALIAS("raid6");