blob: 42a480ba767b6d2618aaba1be2cd3d78970b66f0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/highmem.h>
49#include <linux/bitops.h>
NeilBrownf6705572006-03-27 01:18:11 -080050#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/atomic.h>
NeilBrown16a53ec2006-06-26 00:27:38 -070052#include "raid6.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
NeilBrown72626682005-09-09 16:23:54 -070054#include <linux/raid/bitmap.h>
Dan Williams91c00922007-01-02 13:52:30 -070055#include <linux/async_tx.h>
NeilBrown72626682005-09-09 16:23:54 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Stripe cache
59 */
60
61#define NR_STRIPES 256
62#define STRIPE_SIZE PAGE_SIZE
63#define STRIPE_SHIFT (PAGE_SHIFT - 9)
64#define STRIPE_SECTORS (STRIPE_SIZE>>9)
65#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070066#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080067#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define HASH_MASK (NR_HASH - 1)
69
NeilBrownfccddba2006-01-06 00:20:33 -080070#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* bio's attached to a stripe+device for I/O are linked together in bi_sector
73 * order without overlap. There may be several bio's per stripe+device, and
74 * a bio could span several devices.
75 * When walking this list for a particular stripe+device, we must never proceed
76 * beyond a bio that extends past this device, as the next bio might no longer
77 * be valid.
78 * This macro is used to determine the 'next' bio in the list, given the sector
79 * of the current stripe+device
80 */
81#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
82/*
83 * The following can be used to debug the driver
84 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define RAID5_PARANOIA 1
86#if RAID5_PARANOIA && defined(CONFIG_SMP)
87# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
88#else
89# define CHECK_DEVLOCK()
90#endif
91
Dan Williams45b42332007-07-09 11:56:43 -070092#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define inline
94#define __inline__
95#endif
96
Bernd Schubert6be9d492008-05-23 13:04:34 -070097#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
98
NeilBrown16a53ec2006-06-26 00:27:38 -070099#if !RAID6_USE_EMPTY_ZERO_PAGE
100/* In .bss so it's zeroed */
101const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
102#endif
103
104static inline int raid6_next_disk(int disk, int raid_disks)
105{
106 disk++;
107 return (disk < raid_disks) ? disk : 0;
108}
Dan Williamsa4456852007-07-09 11:56:43 -0700109
110static void return_io(struct bio *return_bi)
111{
112 struct bio *bi = return_bi;
113 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700114
115 return_bi = bi->bi_next;
116 bi->bi_next = NULL;
117 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000118 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700119 bi = return_bi;
120 }
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void print_raid5_conf (raid5_conf_t *conf);
124
Dan Williams600aa102008-06-28 08:32:05 +1000125static int stripe_operations_active(struct stripe_head *sh)
126{
127 return sh->check_state || sh->reconstruct_state ||
128 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
129 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
130}
131
Arjan van de Ven858119e2006-01-14 13:20:43 -0800132static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200135 BUG_ON(!list_empty(&sh->lru));
136 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700138 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700140 blk_plug_device(conf->mddev->queue);
141 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700142 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700143 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700144 blk_plug_device(conf->mddev->queue);
145 } else {
NeilBrown72626682005-09-09 16:23:54 -0700146 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 md_wakeup_thread(conf->mddev->thread);
150 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000151 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
153 atomic_dec(&conf->preread_active_stripes);
154 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
155 md_wakeup_thread(conf->mddev->thread);
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800158 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
159 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800161 if (conf->retry_read_aligned)
162 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 }
166}
167static void release_stripe(struct stripe_head *sh)
168{
169 raid5_conf_t *conf = sh->raid_conf;
170 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_lock_irqsave(&conf->device_lock, flags);
173 __release_stripe(conf, sh);
174 spin_unlock_irqrestore(&conf->device_lock, flags);
175}
176
NeilBrownfccddba2006-01-06 00:20:33 -0800177static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Dan Williams45b42332007-07-09 11:56:43 -0700179 pr_debug("remove_hash(), stripe %llu\n",
180 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
NeilBrownfccddba2006-01-06 00:20:33 -0800182 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
NeilBrown16a53ec2006-06-26 00:27:38 -0700185static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
NeilBrownfccddba2006-01-06 00:20:33 -0800187 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Dan Williams45b42332007-07-09 11:56:43 -0700189 pr_debug("insert_hash(), stripe %llu\n",
190 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800193 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196
197/* find an idle stripe, make sure it is unhashed, and return it. */
198static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
199{
200 struct stripe_head *sh = NULL;
201 struct list_head *first;
202
203 CHECK_DEVLOCK();
204 if (list_empty(&conf->inactive_list))
205 goto out;
206 first = conf->inactive_list.next;
207 sh = list_entry(first, struct stripe_head, lru);
208 list_del_init(first);
209 remove_hash(sh);
210 atomic_inc(&conf->active_stripes);
211out:
212 return sh;
213}
214
215static void shrink_buffers(struct stripe_head *sh, int num)
216{
217 struct page *p;
218 int i;
219
220 for (i=0; i<num ; i++) {
221 p = sh->dev[i].page;
222 if (!p)
223 continue;
224 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800225 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229static int grow_buffers(struct stripe_head *sh, int num)
230{
231 int i;
232
233 for (i=0; i<num; i++) {
234 struct page *page;
235
236 if (!(page = alloc_page(GFP_KERNEL))) {
237 return 1;
238 }
239 sh->dev[i].page = page;
240 }
241 return 0;
242}
243
244static void raid5_build_block (struct stripe_head *sh, int i);
245
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800246static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800249 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200251 BUG_ON(atomic_read(&sh->count) != 0);
252 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000253 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700256 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 (unsigned long long)sh->sector);
258
259 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sh->sector = sector;
262 sh->pd_idx = pd_idx;
263 sh->state = 0;
264
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800265 sh->disks = disks;
266
267 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct r5dev *dev = &sh->dev[i];
269
Dan Williamsd84e0f12007-01-02 13:52:30 -0700270 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700272 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700274 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 test_bit(R5_LOCKED, &dev->flags));
276 BUG();
277 }
278 dev->flags = 0;
279 raid5_build_block(sh, i);
280 }
281 insert_hash(conf, sh);
282}
283
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800284static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800287 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700290 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800291 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800292 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700294 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return NULL;
296}
297
298static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200299static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800301static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
302 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct stripe_head *sh;
305
Dan Williams45b42332007-07-09 11:56:43 -0700306 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 spin_lock_irq(&conf->device_lock);
309
310 do {
NeilBrown72626682005-09-09 16:23:54 -0700311 wait_event_lock_irq(conf->wait_for_stripe,
312 conf->quiesce == 0,
313 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800314 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!sh) {
316 if (!conf->inactive_blocked)
317 sh = get_free_stripe(conf);
318 if (noblock && sh == NULL)
319 break;
320 if (!sh) {
321 conf->inactive_blocked = 1;
322 wait_event_lock_irq(conf->wait_for_stripe,
323 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800324 (atomic_read(&conf->active_stripes)
325 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 || !conf->inactive_blocked),
327 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700328 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 );
330 conf->inactive_blocked = 0;
331 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800332 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 } else {
334 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200335 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 } else {
337 if (!test_bit(STRIPE_HANDLE, &sh->state))
338 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700339 if (list_empty(&sh->lru) &&
340 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700341 BUG();
342 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 }
345 } while (sh == NULL);
346
347 if (sh)
348 atomic_inc(&sh->count);
349
350 spin_unlock_irq(&conf->device_lock);
351 return sh;
352}
353
NeilBrown6712ecf2007-09-27 12:47:43 +0200354static void
355raid5_end_read_request(struct bio *bi, int error);
356static void
357raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700358
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000359static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700360{
361 raid5_conf_t *conf = sh->raid_conf;
362 int i, disks = sh->disks;
363
364 might_sleep();
365
366 for (i = disks; i--; ) {
367 int rw;
368 struct bio *bi;
369 mdk_rdev_t *rdev;
370 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
371 rw = WRITE;
372 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
373 rw = READ;
374 else
375 continue;
376
377 bi = &sh->dev[i].req;
378
379 bi->bi_rw = rw;
380 if (rw == WRITE)
381 bi->bi_end_io = raid5_end_write_request;
382 else
383 bi->bi_end_io = raid5_end_read_request;
384
385 rcu_read_lock();
386 rdev = rcu_dereference(conf->disks[i].rdev);
387 if (rdev && test_bit(Faulty, &rdev->flags))
388 rdev = NULL;
389 if (rdev)
390 atomic_inc(&rdev->nr_pending);
391 rcu_read_unlock();
392
393 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000394 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700395 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
396
Dan Williams2b7497f2008-06-28 08:31:52 +1000397 set_bit(STRIPE_IO_STARTED, &sh->state);
398
Dan Williams91c00922007-01-02 13:52:30 -0700399 bi->bi_bdev = rdev->bdev;
400 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700401 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700402 bi->bi_rw, i);
403 atomic_inc(&sh->count);
404 bi->bi_sector = sh->sector + rdev->data_offset;
405 bi->bi_flags = 1 << BIO_UPTODATE;
406 bi->bi_vcnt = 1;
407 bi->bi_max_vecs = 1;
408 bi->bi_idx = 0;
409 bi->bi_io_vec = &sh->dev[i].vec;
410 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
411 bi->bi_io_vec[0].bv_offset = 0;
412 bi->bi_size = STRIPE_SIZE;
413 bi->bi_next = NULL;
414 if (rw == WRITE &&
415 test_bit(R5_ReWrite, &sh->dev[i].flags))
416 atomic_add(STRIPE_SECTORS,
417 &rdev->corrected_errors);
418 generic_make_request(bi);
419 } else {
420 if (rw == WRITE)
421 set_bit(STRIPE_DEGRADED, &sh->state);
422 pr_debug("skip op %ld on disc %d for sector %llu\n",
423 bi->bi_rw, i, (unsigned long long)sh->sector);
424 clear_bit(R5_LOCKED, &sh->dev[i].flags);
425 set_bit(STRIPE_HANDLE, &sh->state);
426 }
427 }
428}
429
430static struct dma_async_tx_descriptor *
431async_copy_data(int frombio, struct bio *bio, struct page *page,
432 sector_t sector, struct dma_async_tx_descriptor *tx)
433{
434 struct bio_vec *bvl;
435 struct page *bio_page;
436 int i;
437 int page_offset;
438
439 if (bio->bi_sector >= sector)
440 page_offset = (signed)(bio->bi_sector - sector) * 512;
441 else
442 page_offset = (signed)(sector - bio->bi_sector) * -512;
443 bio_for_each_segment(bvl, bio, i) {
444 int len = bio_iovec_idx(bio, i)->bv_len;
445 int clen;
446 int b_offset = 0;
447
448 if (page_offset < 0) {
449 b_offset = -page_offset;
450 page_offset += b_offset;
451 len -= b_offset;
452 }
453
454 if (len > 0 && page_offset + len > STRIPE_SIZE)
455 clen = STRIPE_SIZE - page_offset;
456 else
457 clen = len;
458
459 if (clen > 0) {
460 b_offset += bio_iovec_idx(bio, i)->bv_offset;
461 bio_page = bio_iovec_idx(bio, i)->bv_page;
462 if (frombio)
463 tx = async_memcpy(page, bio_page, page_offset,
464 b_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700465 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700466 tx, NULL, NULL);
467 else
468 tx = async_memcpy(bio_page, page, b_offset,
469 page_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700470 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700471 tx, NULL, NULL);
472 }
473 if (clen < len) /* hit end of page */
474 break;
475 page_offset += len;
476 }
477
478 return tx;
479}
480
481static void ops_complete_biofill(void *stripe_head_ref)
482{
483 struct stripe_head *sh = stripe_head_ref;
484 struct bio *return_bi = NULL;
485 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700486 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700487
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700488 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700489 (unsigned long long)sh->sector);
490
491 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000492 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700493 for (i = sh->disks; i--; ) {
494 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700495
496 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700497 /* and check if we need to reply to a read request,
498 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000499 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700500 */
501 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700502 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700503
Dan Williams91c00922007-01-02 13:52:30 -0700504 BUG_ON(!dev->read);
505 rbi = dev->read;
506 dev->read = NULL;
507 while (rbi && rbi->bi_sector <
508 dev->sector + STRIPE_SECTORS) {
509 rbi2 = r5_next_bio(rbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700510 if (--rbi->bi_phys_segments == 0) {
511 rbi->bi_next = return_bi;
512 return_bi = rbi;
513 }
Dan Williams91c00922007-01-02 13:52:30 -0700514 rbi = rbi2;
515 }
516 }
517 }
Dan Williams83de75c2008-06-28 08:31:58 +1000518 spin_unlock_irq(&conf->device_lock);
519 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700520
521 return_io(return_bi);
522
Dan Williamse4d84902007-09-24 10:06:13 -0700523 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700524 release_stripe(sh);
525}
526
527static void ops_run_biofill(struct stripe_head *sh)
528{
529 struct dma_async_tx_descriptor *tx = NULL;
530 raid5_conf_t *conf = sh->raid_conf;
531 int i;
532
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700533 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700534 (unsigned long long)sh->sector);
535
536 for (i = sh->disks; i--; ) {
537 struct r5dev *dev = &sh->dev[i];
538 if (test_bit(R5_Wantfill, &dev->flags)) {
539 struct bio *rbi;
540 spin_lock_irq(&conf->device_lock);
541 dev->read = rbi = dev->toread;
542 dev->toread = NULL;
543 spin_unlock_irq(&conf->device_lock);
544 while (rbi && rbi->bi_sector <
545 dev->sector + STRIPE_SECTORS) {
546 tx = async_copy_data(0, rbi, dev->page,
547 dev->sector, tx);
548 rbi = r5_next_bio(rbi, dev->sector);
549 }
550 }
551 }
552
553 atomic_inc(&sh->count);
554 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
555 ops_complete_biofill, sh);
556}
557
558static void ops_complete_compute5(void *stripe_head_ref)
559{
560 struct stripe_head *sh = stripe_head_ref;
561 int target = sh->ops.target;
562 struct r5dev *tgt = &sh->dev[target];
563
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700564 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700565 (unsigned long long)sh->sector);
566
567 set_bit(R5_UPTODATE, &tgt->flags);
568 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
569 clear_bit(R5_Wantcompute, &tgt->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +1000570 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
571 if (sh->check_state == check_state_compute_run)
572 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700573 set_bit(STRIPE_HANDLE, &sh->state);
574 release_stripe(sh);
575}
576
Dan Williams7b3a8712008-06-28 08:32:09 +1000577static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700578{
579 /* kernel stack size limits the total number of disks */
580 int disks = sh->disks;
581 struct page *xor_srcs[disks];
582 int target = sh->ops.target;
583 struct r5dev *tgt = &sh->dev[target];
584 struct page *xor_dest = tgt->page;
585 int count = 0;
586 struct dma_async_tx_descriptor *tx;
587 int i;
588
589 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700590 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700591 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
592
593 for (i = disks; i--; )
594 if (i != target)
595 xor_srcs[count++] = sh->dev[i].page;
596
597 atomic_inc(&sh->count);
598
599 if (unlikely(count == 1))
600 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
601 0, NULL, ops_complete_compute5, sh);
602 else
603 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
604 ASYNC_TX_XOR_ZERO_DST, NULL,
605 ops_complete_compute5, sh);
606
Dan Williams91c00922007-01-02 13:52:30 -0700607 return tx;
608}
609
610static void ops_complete_prexor(void *stripe_head_ref)
611{
612 struct stripe_head *sh = stripe_head_ref;
613
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700614 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700615 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700616}
617
618static struct dma_async_tx_descriptor *
619ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
620{
621 /* kernel stack size limits the total number of disks */
622 int disks = sh->disks;
623 struct page *xor_srcs[disks];
624 int count = 0, pd_idx = sh->pd_idx, i;
625
626 /* existing parity data subtracted */
627 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
628
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700629 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700630 (unsigned long long)sh->sector);
631
632 for (i = disks; i--; ) {
633 struct r5dev *dev = &sh->dev[i];
634 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000635 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700636 xor_srcs[count++] = dev->page;
637 }
638
639 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
640 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
641 ops_complete_prexor, sh);
642
643 return tx;
644}
645
646static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000647ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700648{
649 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000650 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700651
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700652 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700653 (unsigned long long)sh->sector);
654
655 for (i = disks; i--; ) {
656 struct r5dev *dev = &sh->dev[i];
657 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700658
Dan Williamsd8ee0722008-06-28 08:32:06 +1000659 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700660 struct bio *wbi;
661
662 spin_lock(&sh->lock);
663 chosen = dev->towrite;
664 dev->towrite = NULL;
665 BUG_ON(dev->written);
666 wbi = dev->written = chosen;
667 spin_unlock(&sh->lock);
668
669 while (wbi && wbi->bi_sector <
670 dev->sector + STRIPE_SECTORS) {
671 tx = async_copy_data(1, wbi, dev->page,
672 dev->sector, tx);
673 wbi = r5_next_bio(wbi, dev->sector);
674 }
675 }
676 }
677
678 return tx;
679}
680
681static void ops_complete_postxor(void *stripe_head_ref)
682{
683 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700684 int disks = sh->disks, i, pd_idx = sh->pd_idx;
685
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700686 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700687 (unsigned long long)sh->sector);
688
689 for (i = disks; i--; ) {
690 struct r5dev *dev = &sh->dev[i];
691 if (dev->written || i == pd_idx)
692 set_bit(R5_UPTODATE, &dev->flags);
693 }
694
Dan Williamsd8ee0722008-06-28 08:32:06 +1000695 if (sh->reconstruct_state == reconstruct_state_drain_run)
696 sh->reconstruct_state = reconstruct_state_drain_result;
697 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
698 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
699 else {
700 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
701 sh->reconstruct_state = reconstruct_state_result;
702 }
703
Dan Williams91c00922007-01-02 13:52:30 -0700704 set_bit(STRIPE_HANDLE, &sh->state);
705 release_stripe(sh);
706}
707
708static void
Dan Williamsd8ee0722008-06-28 08:32:06 +1000709ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700710{
711 /* kernel stack size limits the total number of disks */
712 int disks = sh->disks;
713 struct page *xor_srcs[disks];
714
715 int count = 0, pd_idx = sh->pd_idx, i;
716 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000717 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700718 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700719
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700720 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700721 (unsigned long long)sh->sector);
722
723 /* check if prexor is active which means only process blocks
724 * that are part of a read-modify-write (written)
725 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000726 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
727 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700728 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
729 for (i = disks; i--; ) {
730 struct r5dev *dev = &sh->dev[i];
731 if (dev->written)
732 xor_srcs[count++] = dev->page;
733 }
734 } else {
735 xor_dest = sh->dev[pd_idx].page;
736 for (i = disks; i--; ) {
737 struct r5dev *dev = &sh->dev[i];
738 if (i != pd_idx)
739 xor_srcs[count++] = dev->page;
740 }
741 }
742
Dan Williams91c00922007-01-02 13:52:30 -0700743 /* 1/ if we prexor'd then the dest is reused as a source
744 * 2/ if we did not prexor then we are redoing the parity
745 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
746 * for the synchronous xor case
747 */
748 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
749 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
750
751 atomic_inc(&sh->count);
752
753 if (unlikely(count == 1)) {
754 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
755 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000756 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700757 } else
758 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000759 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700760}
761
762static void ops_complete_check(void *stripe_head_ref)
763{
764 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700765
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700766 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700767 (unsigned long long)sh->sector);
768
Dan Williamsecc65c92008-06-28 08:31:57 +1000769 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700770 set_bit(STRIPE_HANDLE, &sh->state);
771 release_stripe(sh);
772}
773
774static void ops_run_check(struct stripe_head *sh)
775{
776 /* kernel stack size limits the total number of disks */
777 int disks = sh->disks;
778 struct page *xor_srcs[disks];
779 struct dma_async_tx_descriptor *tx;
780
781 int count = 0, pd_idx = sh->pd_idx, i;
782 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
783
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700784 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700785 (unsigned long long)sh->sector);
786
787 for (i = disks; i--; ) {
788 struct r5dev *dev = &sh->dev[i];
789 if (i != pd_idx)
790 xor_srcs[count++] = dev->page;
791 }
792
793 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
794 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
795
Dan Williams91c00922007-01-02 13:52:30 -0700796 atomic_inc(&sh->count);
797 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
798 ops_complete_check, sh);
799}
800
Dan Williams600aa102008-06-28 08:32:05 +1000801static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700802{
803 int overlap_clear = 0, i, disks = sh->disks;
804 struct dma_async_tx_descriptor *tx = NULL;
805
Dan Williams83de75c2008-06-28 08:31:58 +1000806 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700807 ops_run_biofill(sh);
808 overlap_clear++;
809 }
810
Dan Williams7b3a8712008-06-28 08:32:09 +1000811 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
812 tx = ops_run_compute5(sh);
813 /* terminate the chain if postxor is not set to be run */
814 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
815 async_tx_ack(tx);
816 }
Dan Williams91c00922007-01-02 13:52:30 -0700817
Dan Williams600aa102008-06-28 08:32:05 +1000818 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700819 tx = ops_run_prexor(sh, tx);
820
Dan Williams600aa102008-06-28 08:32:05 +1000821 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000822 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700823 overlap_clear++;
824 }
825
Dan Williams600aa102008-06-28 08:32:05 +1000826 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd8ee0722008-06-28 08:32:06 +1000827 ops_run_postxor(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700828
Dan Williamsecc65c92008-06-28 08:31:57 +1000829 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700830 ops_run_check(sh);
831
Dan Williams91c00922007-01-02 13:52:30 -0700832 if (overlap_clear)
833 for (i = disks; i--; ) {
834 struct r5dev *dev = &sh->dev[i];
835 if (test_and_clear_bit(R5_Overlap, &dev->flags))
836 wake_up(&sh->raid_conf->wait_for_overlap);
837 }
838}
839
NeilBrown3f294f42005-11-08 21:39:25 -0800840static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800843 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
844 if (!sh)
845 return 0;
846 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
847 sh->raid_conf = conf;
848 spin_lock_init(&sh->lock);
849
850 if (grow_buffers(sh, conf->raid_disks)) {
851 shrink_buffers(sh, conf->raid_disks);
852 kmem_cache_free(conf->slab_cache, sh);
853 return 0;
854 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800855 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800856 /* we just created an active stripe so... */
857 atomic_set(&sh->count, 1);
858 atomic_inc(&conf->active_stripes);
859 INIT_LIST_HEAD(&sh->lru);
860 release_stripe(sh);
861 return 1;
862}
863
864static int grow_stripes(raid5_conf_t *conf, int num)
865{
Christoph Lametere18b8902006-12-06 20:33:20 -0800866 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int devs = conf->raid_disks;
868
NeilBrown42b9beb2007-05-09 02:35:37 -0700869 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
870 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800871 conf->active_name = 0;
872 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900874 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!sc)
876 return 1;
877 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800878 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700879 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800880 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883}
NeilBrown29269552006-03-27 01:18:10 -0800884
885#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800886static int resize_stripes(raid5_conf_t *conf, int newsize)
887{
888 /* Make all the stripes able to hold 'newsize' devices.
889 * New slots in each stripe get 'page' set to a new page.
890 *
891 * This happens in stages:
892 * 1/ create a new kmem_cache and allocate the required number of
893 * stripe_heads.
894 * 2/ gather all the old stripe_heads and tranfer the pages across
895 * to the new stripe_heads. This will have the side effect of
896 * freezing the array as once all stripe_heads have been collected,
897 * no IO will be possible. Old stripe heads are freed once their
898 * pages have been transferred over, and the old kmem_cache is
899 * freed when all stripes are done.
900 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
901 * we simple return a failre status - no need to clean anything up.
902 * 4/ allocate new pages for the new slots in the new stripe_heads.
903 * If this fails, we don't bother trying the shrink the
904 * stripe_heads down again, we just leave them as they are.
905 * As each stripe_head is processed the new one is released into
906 * active service.
907 *
908 * Once step2 is started, we cannot afford to wait for a write,
909 * so we use GFP_NOIO allocations.
910 */
911 struct stripe_head *osh, *nsh;
912 LIST_HEAD(newstripes);
913 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -0700914 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -0800915 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800916 int i;
917
918 if (newsize <= conf->pool_size)
919 return 0; /* never bother to shrink */
920
Dan Williamsb5470dc2008-06-27 21:44:04 -0700921 err = md_allow_write(conf->mddev);
922 if (err)
923 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -0800924
NeilBrownad01c9e2006-03-27 01:18:07 -0800925 /* Step 1 */
926 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
927 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900928 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -0800929 if (!sc)
930 return -ENOMEM;
931
932 for (i = conf->max_nr_stripes; i; i--) {
933 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
934 if (!nsh)
935 break;
936
937 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
938
939 nsh->raid_conf = conf;
940 spin_lock_init(&nsh->lock);
941
942 list_add(&nsh->lru, &newstripes);
943 }
944 if (i) {
945 /* didn't get enough, give up */
946 while (!list_empty(&newstripes)) {
947 nsh = list_entry(newstripes.next, struct stripe_head, lru);
948 list_del(&nsh->lru);
949 kmem_cache_free(sc, nsh);
950 }
951 kmem_cache_destroy(sc);
952 return -ENOMEM;
953 }
954 /* Step 2 - Must use GFP_NOIO now.
955 * OK, we have enough stripes, start collecting inactive
956 * stripes and copying them over
957 */
958 list_for_each_entry(nsh, &newstripes, lru) {
959 spin_lock_irq(&conf->device_lock);
960 wait_event_lock_irq(conf->wait_for_stripe,
961 !list_empty(&conf->inactive_list),
962 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800963 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800964 );
965 osh = get_free_stripe(conf);
966 spin_unlock_irq(&conf->device_lock);
967 atomic_set(&nsh->count, 1);
968 for(i=0; i<conf->pool_size; i++)
969 nsh->dev[i].page = osh->dev[i].page;
970 for( ; i<newsize; i++)
971 nsh->dev[i].page = NULL;
972 kmem_cache_free(conf->slab_cache, osh);
973 }
974 kmem_cache_destroy(conf->slab_cache);
975
976 /* Step 3.
977 * At this point, we are holding all the stripes so the array
978 * is completely stalled, so now is a good time to resize
979 * conf->disks.
980 */
981 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
982 if (ndisks) {
983 for (i=0; i<conf->raid_disks; i++)
984 ndisks[i] = conf->disks[i];
985 kfree(conf->disks);
986 conf->disks = ndisks;
987 } else
988 err = -ENOMEM;
989
990 /* Step 4, return new stripes to service */
991 while(!list_empty(&newstripes)) {
992 nsh = list_entry(newstripes.next, struct stripe_head, lru);
993 list_del_init(&nsh->lru);
994 for (i=conf->raid_disks; i < newsize; i++)
995 if (nsh->dev[i].page == NULL) {
996 struct page *p = alloc_page(GFP_NOIO);
997 nsh->dev[i].page = p;
998 if (!p)
999 err = -ENOMEM;
1000 }
1001 release_stripe(nsh);
1002 }
1003 /* critical section pass, GFP_NOIO no longer needed */
1004
1005 conf->slab_cache = sc;
1006 conf->active_name = 1-conf->active_name;
1007 conf->pool_size = newsize;
1008 return err;
1009}
NeilBrown29269552006-03-27 01:18:10 -08001010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
NeilBrown3f294f42005-11-08 21:39:25 -08001012static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct stripe_head *sh;
1015
NeilBrown3f294f42005-11-08 21:39:25 -08001016 spin_lock_irq(&conf->device_lock);
1017 sh = get_free_stripe(conf);
1018 spin_unlock_irq(&conf->device_lock);
1019 if (!sh)
1020 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001021 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001022 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001023 kmem_cache_free(conf->slab_cache, sh);
1024 atomic_dec(&conf->active_stripes);
1025 return 1;
1026}
1027
1028static void shrink_stripes(raid5_conf_t *conf)
1029{
1030 while (drop_one_stripe(conf))
1031 ;
1032
NeilBrown29fc7e32006-02-03 03:03:41 -08001033 if (conf->slab_cache)
1034 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 conf->slab_cache = NULL;
1036}
1037
NeilBrown6712ecf2007-09-27 12:47:43 +02001038static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct stripe_head *sh = bi->bi_private;
1041 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001042 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001044 char b[BDEVNAME_SIZE];
1045 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 for (i=0 ; i<disks; i++)
1049 if (bi == &sh->dev[i].req)
1050 break;
1051
Dan Williams45b42332007-07-09 11:56:43 -07001052 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1053 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 uptodate);
1055 if (i == disks) {
1056 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001057 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001062 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001063 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001064 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1065 " (%lu sectors at %llu on %s)\n",
1066 mdname(conf->mddev), STRIPE_SECTORS,
1067 (unsigned long long)(sh->sector
1068 + rdev->data_offset),
1069 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001070 clear_bit(R5_ReadError, &sh->dev[i].flags);
1071 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1072 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001073 if (atomic_read(&conf->disks[i].rdev->read_errors))
1074 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001076 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001077 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001078 rdev = conf->disks[i].rdev;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001081 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001082 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001083 printk_rl(KERN_WARNING
1084 "raid5:%s: read error not correctable "
1085 "(sector %llu on %s).\n",
1086 mdname(conf->mddev),
1087 (unsigned long long)(sh->sector
1088 + rdev->data_offset),
1089 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001090 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001091 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001092 printk_rl(KERN_WARNING
1093 "raid5:%s: read error NOT corrected!! "
1094 "(sector %llu on %s).\n",
1095 mdname(conf->mddev),
1096 (unsigned long long)(sh->sector
1097 + rdev->data_offset),
1098 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001099 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001100 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001101 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001102 "raid5:%s: Too many read errors, failing device %s.\n",
1103 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001104 else
1105 retry = 1;
1106 if (retry)
1107 set_bit(R5_ReadError, &sh->dev[i].flags);
1108 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001109 clear_bit(R5_ReadError, &sh->dev[i].flags);
1110 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001111 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1116 set_bit(STRIPE_HANDLE, &sh->state);
1117 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
NeilBrown6712ecf2007-09-27 12:47:43 +02001120static void raid5_end_write_request (struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct stripe_head *sh = bi->bi_private;
1123 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001124 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 for (i=0 ; i<disks; i++)
1128 if (bi == &sh->dev[i].req)
1129 break;
1130
Dan Williams45b42332007-07-09 11:56:43 -07001131 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1133 uptodate);
1134 if (i == disks) {
1135 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!uptodate)
1140 md_error(conf->mddev, conf->disks[i].rdev);
1141
1142 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1143
1144 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1145 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001146 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149
1150static sector_t compute_blocknr(struct stripe_head *sh, int i);
1151
1152static void raid5_build_block (struct stripe_head *sh, int i)
1153{
1154 struct r5dev *dev = &sh->dev[i];
1155
1156 bio_init(&dev->req);
1157 dev->req.bi_io_vec = &dev->vec;
1158 dev->req.bi_vcnt++;
1159 dev->req.bi_max_vecs++;
1160 dev->vec.bv_page = dev->page;
1161 dev->vec.bv_len = STRIPE_SIZE;
1162 dev->vec.bv_offset = 0;
1163
1164 dev->req.bi_sector = sh->sector;
1165 dev->req.bi_private = sh;
1166
1167 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001168 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1172{
1173 char b[BDEVNAME_SIZE];
1174 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001175 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
NeilBrownb2d444d2005-11-08 21:39:31 -08001177 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b422006-10-03 01:15:46 -07001178 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001179 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1180 unsigned long flags;
1181 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001183 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /*
1185 * if recovery was running, make sure it aborts.
1186 */
NeilBrowndfc70642008-05-23 13:04:39 -07001187 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 printk (KERN_ALERT
Nick Andrewd7a420c2008-04-28 02:15:55 -07001191 "raid5: Disk failure on %s, disabling device.\n"
1192 "raid5: Operation continuing on %d devices.\n",
NeilBrown02c2de82006-10-03 01:15:47 -07001193 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197/*
1198 * Input: a 'big' sector number,
1199 * Output: index of the data and parity disk, and the sector # in them.
1200 */
1201static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
1202 unsigned int data_disks, unsigned int * dd_idx,
1203 unsigned int * pd_idx, raid5_conf_t *conf)
1204{
1205 long stripe;
1206 unsigned long chunk_number;
1207 unsigned int chunk_offset;
1208 sector_t new_sector;
1209 int sectors_per_chunk = conf->chunk_size >> 9;
1210
1211 /* First compute the information on this sector */
1212
1213 /*
1214 * Compute the chunk number and the sector offset inside the chunk
1215 */
1216 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1217 chunk_number = r_sector;
1218 BUG_ON(r_sector != chunk_number);
1219
1220 /*
1221 * Compute the stripe number
1222 */
1223 stripe = chunk_number / data_disks;
1224
1225 /*
1226 * Compute the data disk and parity disk indexes inside the stripe
1227 */
1228 *dd_idx = chunk_number % data_disks;
1229
1230 /*
1231 * Select the parity disk based on the user selected algorithm.
1232 */
NeilBrown16a53ec2006-06-26 00:27:38 -07001233 switch(conf->level) {
1234 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001236 break;
1237 case 5:
1238 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 case ALGORITHM_LEFT_ASYMMETRIC:
1240 *pd_idx = data_disks - stripe % raid_disks;
1241 if (*dd_idx >= *pd_idx)
1242 (*dd_idx)++;
1243 break;
1244 case ALGORITHM_RIGHT_ASYMMETRIC:
1245 *pd_idx = stripe % raid_disks;
1246 if (*dd_idx >= *pd_idx)
1247 (*dd_idx)++;
1248 break;
1249 case ALGORITHM_LEFT_SYMMETRIC:
1250 *pd_idx = data_disks - stripe % raid_disks;
1251 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1252 break;
1253 case ALGORITHM_RIGHT_SYMMETRIC:
1254 *pd_idx = stripe % raid_disks;
1255 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
1256 break;
1257 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001258 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -07001260 }
1261 break;
1262 case 6:
1263
1264 /**** FIX THIS ****/
1265 switch (conf->algorithm) {
1266 case ALGORITHM_LEFT_ASYMMETRIC:
1267 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1268 if (*pd_idx == raid_disks-1)
1269 (*dd_idx)++; /* Q D D D P */
1270 else if (*dd_idx >= *pd_idx)
1271 (*dd_idx) += 2; /* D D P Q D */
1272 break;
1273 case ALGORITHM_RIGHT_ASYMMETRIC:
1274 *pd_idx = stripe % raid_disks;
1275 if (*pd_idx == raid_disks-1)
1276 (*dd_idx)++; /* Q D D D P */
1277 else if (*dd_idx >= *pd_idx)
1278 (*dd_idx) += 2; /* D D P Q D */
1279 break;
1280 case ALGORITHM_LEFT_SYMMETRIC:
1281 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
1282 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1283 break;
1284 case ALGORITHM_RIGHT_SYMMETRIC:
1285 *pd_idx = stripe % raid_disks;
1286 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
1287 break;
1288 default:
1289 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
1290 conf->algorithm);
1291 }
1292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 /*
1296 * Finally, compute the new sector number
1297 */
1298 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1299 return new_sector;
1300}
1301
1302
1303static sector_t compute_blocknr(struct stripe_head *sh, int i)
1304{
1305 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001306 int raid_disks = sh->disks;
1307 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 sector_t new_sector = sh->sector, check;
1309 int sectors_per_chunk = conf->chunk_size >> 9;
1310 sector_t stripe;
1311 int chunk_offset;
1312 int chunk_number, dummy1, dummy2, dd_idx = i;
1313 sector_t r_sector;
1314
NeilBrown16a53ec2006-06-26 00:27:38 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1317 stripe = new_sector;
1318 BUG_ON(new_sector != stripe);
1319
NeilBrown16a53ec2006-06-26 00:27:38 -07001320 if (i == sh->pd_idx)
1321 return 0;
1322 switch(conf->level) {
1323 case 4: break;
1324 case 5:
1325 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case ALGORITHM_LEFT_ASYMMETRIC:
1327 case ALGORITHM_RIGHT_ASYMMETRIC:
1328 if (i > sh->pd_idx)
1329 i--;
1330 break;
1331 case ALGORITHM_LEFT_SYMMETRIC:
1332 case ALGORITHM_RIGHT_SYMMETRIC:
1333 if (i < sh->pd_idx)
1334 i += raid_disks;
1335 i -= (sh->pd_idx + 1);
1336 break;
1337 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001338 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001339 conf->algorithm);
1340 }
1341 break;
1342 case 6:
NeilBrown16a53ec2006-06-26 00:27:38 -07001343 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
1344 return 0; /* It is the Q disk */
1345 switch (conf->algorithm) {
1346 case ALGORITHM_LEFT_ASYMMETRIC:
1347 case ALGORITHM_RIGHT_ASYMMETRIC:
1348 if (sh->pd_idx == raid_disks-1)
1349 i--; /* Q D D D P */
1350 else if (i > sh->pd_idx)
1351 i -= 2; /* D D P Q D */
1352 break;
1353 case ALGORITHM_LEFT_SYMMETRIC:
1354 case ALGORITHM_RIGHT_SYMMETRIC:
1355 if (sh->pd_idx == raid_disks-1)
1356 i--; /* Q D D D P */
1357 else {
1358 /* D D P Q D */
1359 if (i < sh->pd_idx)
1360 i += raid_disks;
1361 i -= (sh->pd_idx + 2);
1362 }
1363 break;
1364 default:
1365 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -07001367 }
1368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
1371 chunk_number = stripe * data_disks + i;
1372 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1373
1374 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
1375 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001376 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 0;
1378 }
1379 return r_sector;
1380}
1381
1382
1383
1384/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001385 * Copy data between a page in the stripe cache, and one or more bion
1386 * The page could align with the middle of the bio, or there could be
1387 * several bion, each with several bio_vecs, which cover part of the page
1388 * Multiple bion are linked together on bi_next. There may be extras
1389 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391static void copy_data(int frombio, struct bio *bio,
1392 struct page *page,
1393 sector_t sector)
1394{
1395 char *pa = page_address(page);
1396 struct bio_vec *bvl;
1397 int i;
1398 int page_offset;
1399
1400 if (bio->bi_sector >= sector)
1401 page_offset = (signed)(bio->bi_sector - sector) * 512;
1402 else
1403 page_offset = (signed)(sector - bio->bi_sector) * -512;
1404 bio_for_each_segment(bvl, bio, i) {
1405 int len = bio_iovec_idx(bio,i)->bv_len;
1406 int clen;
1407 int b_offset = 0;
1408
1409 if (page_offset < 0) {
1410 b_offset = -page_offset;
1411 page_offset += b_offset;
1412 len -= b_offset;
1413 }
1414
1415 if (len > 0 && page_offset + len > STRIPE_SIZE)
1416 clen = STRIPE_SIZE - page_offset;
1417 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (clen > 0) {
1420 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1421 if (frombio)
1422 memcpy(pa+page_offset, ba+b_offset, clen);
1423 else
1424 memcpy(ba+b_offset, pa+page_offset, clen);
1425 __bio_kunmap_atomic(ba, KM_USER0);
1426 }
1427 if (clen < len) /* hit end of page */
1428 break;
1429 page_offset += len;
1430 }
1431}
1432
Dan Williams9bc89cd2007-01-02 11:10:44 -07001433#define check_xor() do { \
1434 if (count == MAX_XOR_BLOCKS) { \
1435 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1436 count = 0; \
1437 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 } while(0)
1439
NeilBrown16a53ec2006-06-26 00:27:38 -07001440static void compute_parity6(struct stripe_head *sh, int method)
1441{
1442 raid6_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08001443 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown16a53ec2006-06-26 00:27:38 -07001444 struct bio *chosen;
1445 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1446 void *ptrs[disks];
1447
1448 qd_idx = raid6_next_disk(pd_idx, disks);
1449 d0_idx = raid6_next_disk(qd_idx, disks);
1450
Dan Williams45b42332007-07-09 11:56:43 -07001451 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001452 (unsigned long long)sh->sector, method);
1453
1454 switch(method) {
1455 case READ_MODIFY_WRITE:
1456 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1457 case RECONSTRUCT_WRITE:
1458 for (i= disks; i-- ;)
1459 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1460 chosen = sh->dev[i].towrite;
1461 sh->dev[i].towrite = NULL;
1462
1463 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1464 wake_up(&conf->wait_for_overlap);
1465
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001466 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001467 sh->dev[i].written = chosen;
1468 }
1469 break;
1470 case CHECK_PARITY:
1471 BUG(); /* Not implemented yet */
1472 }
1473
1474 for (i = disks; i--;)
1475 if (sh->dev[i].written) {
1476 sector_t sector = sh->dev[i].sector;
1477 struct bio *wbi = sh->dev[i].written;
1478 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1479 copy_data(1, wbi, sh->dev[i].page, sector);
1480 wbi = r5_next_bio(wbi, sector);
1481 }
1482
1483 set_bit(R5_LOCKED, &sh->dev[i].flags);
1484 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1485 }
1486
1487// switch(method) {
1488// case RECONSTRUCT_WRITE:
1489// case CHECK_PARITY:
1490// case UPDATE_PARITY:
1491 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1492 /* FIX: Is this ordering of drives even remotely optimal? */
1493 count = 0;
1494 i = d0_idx;
1495 do {
1496 ptrs[count++] = page_address(sh->dev[i].page);
1497 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1498 printk("block %d/%d not uptodate on parity calc\n", i,count);
1499 i = raid6_next_disk(i, disks);
1500 } while ( i != d0_idx );
1501// break;
1502// }
1503
1504 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1505
1506 switch(method) {
1507 case RECONSTRUCT_WRITE:
1508 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1509 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1510 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1511 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1512 break;
1513 case UPDATE_PARITY:
1514 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1515 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1516 break;
1517 }
1518}
1519
1520
1521/* Compute one missing block */
1522static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1523{
NeilBrownf4168852007-02-28 20:11:53 -08001524 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001525 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrown16a53ec2006-06-26 00:27:38 -07001526 int pd_idx = sh->pd_idx;
1527 int qd_idx = raid6_next_disk(pd_idx, disks);
1528
Dan Williams45b42332007-07-09 11:56:43 -07001529 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001530 (unsigned long long)sh->sector, dd_idx);
1531
1532 if ( dd_idx == qd_idx ) {
1533 /* We're actually computing the Q drive */
1534 compute_parity6(sh, UPDATE_PARITY);
1535 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001536 dest = page_address(sh->dev[dd_idx].page);
1537 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1538 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001539 for (i = disks ; i--; ) {
1540 if (i == dd_idx || i == qd_idx)
1541 continue;
1542 p = page_address(sh->dev[i].page);
1543 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1544 ptr[count++] = p;
1545 else
1546 printk("compute_block() %d, stripe %llu, %d"
1547 " not present\n", dd_idx,
1548 (unsigned long long)sh->sector, i);
1549
1550 check_xor();
1551 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001552 if (count)
1553 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001554 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1555 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1556 }
1557}
1558
1559/* Compute two missing blocks */
1560static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1561{
NeilBrownf4168852007-02-28 20:11:53 -08001562 int i, count, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001563 int pd_idx = sh->pd_idx;
1564 int qd_idx = raid6_next_disk(pd_idx, disks);
1565 int d0_idx = raid6_next_disk(qd_idx, disks);
1566 int faila, failb;
1567
1568 /* faila and failb are disk numbers relative to d0_idx */
1569 /* pd_idx become disks-2 and qd_idx become disks-1 */
1570 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1571 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1572
1573 BUG_ON(faila == failb);
1574 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1575
Dan Williams45b42332007-07-09 11:56:43 -07001576 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001577 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1578
1579 if ( failb == disks-1 ) {
1580 /* Q disk is one of the missing disks */
1581 if ( faila == disks-2 ) {
1582 /* Missing P+Q, just recompute */
1583 compute_parity6(sh, UPDATE_PARITY);
1584 return;
1585 } else {
1586 /* We're missing D+Q; recompute D from P */
1587 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1588 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1589 return;
1590 }
1591 }
1592
1593 /* We're missing D+P or D+D; build pointer table */
1594 {
1595 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1596 void *ptrs[disks];
1597
1598 count = 0;
1599 i = d0_idx;
1600 do {
1601 ptrs[count++] = page_address(sh->dev[i].page);
1602 i = raid6_next_disk(i, disks);
1603 if (i != dd_idx1 && i != dd_idx2 &&
1604 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1605 printk("compute_2 with missing block %d/%d\n", count, i);
1606 } while ( i != d0_idx );
1607
1608 if ( failb == disks-2 ) {
1609 /* We're missing D+P. */
1610 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1611 } else {
1612 /* We're missing D+D. */
1613 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1614 }
1615
1616 /* Both the above update both missing blocks */
1617 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1618 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1619 }
1620}
1621
Dan Williams600aa102008-06-28 08:32:05 +10001622static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001623schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001624 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001625{
1626 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001627
Dan Williamse33129d2007-01-02 13:52:30 -07001628 if (rcw) {
1629 /* if we are not expanding this is a proper write request, and
1630 * there will be bios with new data to be drained into the
1631 * stripe cache
1632 */
1633 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001634 sh->reconstruct_state = reconstruct_state_drain_run;
1635 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1636 } else
1637 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001638
Dan Williams600aa102008-06-28 08:32:05 +10001639 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001640
1641 for (i = disks; i--; ) {
1642 struct r5dev *dev = &sh->dev[i];
1643
1644 if (dev->towrite) {
1645 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001646 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001647 if (!expand)
1648 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001649 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001650 }
1651 }
Dan Williams600aa102008-06-28 08:32:05 +10001652 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001653 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1654 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001655 } else {
1656 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1657 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1658
Dan Williamsd8ee0722008-06-28 08:32:06 +10001659 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001660 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1661 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1662 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001663
1664 for (i = disks; i--; ) {
1665 struct r5dev *dev = &sh->dev[i];
1666 if (i == pd_idx)
1667 continue;
1668
Dan Williamse33129d2007-01-02 13:52:30 -07001669 if (dev->towrite &&
1670 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001671 test_bit(R5_Wantcompute, &dev->flags))) {
1672 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001673 set_bit(R5_LOCKED, &dev->flags);
1674 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001675 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001676 }
1677 }
1678 }
1679
1680 /* keep the parity disk locked while asynchronous operations
1681 * are in flight
1682 */
1683 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1684 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001685 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001686
Dan Williams600aa102008-06-28 08:32:05 +10001687 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001688 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001689 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001690}
NeilBrown16a53ec2006-06-26 00:27:38 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692/*
1693 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001694 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 * The bi_next chain must be in order.
1696 */
1697static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1698{
1699 struct bio **bip;
1700 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001701 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Dan Williams45b42332007-07-09 11:56:43 -07001703 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 (unsigned long long)bi->bi_sector,
1705 (unsigned long long)sh->sector);
1706
1707
1708 spin_lock(&sh->lock);
1709 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001710 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001712 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1713 firstwrite = 1;
1714 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 bip = &sh->dev[dd_idx].toread;
1716 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1717 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1718 goto overlap;
1719 bip = & (*bip)->bi_next;
1720 }
1721 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1722 goto overlap;
1723
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001724 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (*bip)
1726 bi->bi_next = *bip;
1727 *bip = bi;
1728 bi->bi_phys_segments ++;
1729 spin_unlock_irq(&conf->device_lock);
1730 spin_unlock(&sh->lock);
1731
Dan Williams45b42332007-07-09 11:56:43 -07001732 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 (unsigned long long)bi->bi_sector,
1734 (unsigned long long)sh->sector, dd_idx);
1735
NeilBrown72626682005-09-09 16:23:54 -07001736 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001737 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1738 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001739 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001740 set_bit(STRIPE_BIT_DELAY, &sh->state);
1741 }
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (forwrite) {
1744 /* check if page is covered */
1745 sector_t sector = sh->dev[dd_idx].sector;
1746 for (bi=sh->dev[dd_idx].towrite;
1747 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1748 bi && bi->bi_sector <= sector;
1749 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1750 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1751 sector = bi->bi_sector + (bi->bi_size>>9);
1752 }
1753 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1754 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1755 }
1756 return 1;
1757
1758 overlap:
1759 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1760 spin_unlock_irq(&conf->device_lock);
1761 spin_unlock(&sh->lock);
1762 return 0;
1763}
1764
NeilBrown29269552006-03-27 01:18:10 -08001765static void end_reshape(raid5_conf_t *conf);
1766
NeilBrown16a53ec2006-06-26 00:27:38 -07001767static int page_is_zero(struct page *p)
1768{
1769 char *a = page_address(p);
1770 return ((*(u32*)a) == 0 &&
1771 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1772}
1773
NeilBrownccfcc3c2006-03-27 01:18:09 -08001774static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1775{
1776 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001777 int pd_idx, dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001778 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1779
NeilBrownb875e532006-12-10 02:20:49 -08001780 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1781 *sectors_per_chunk + chunk_offset,
1782 disks, disks - conf->max_degraded,
1783 &dd_idx, &pd_idx, conf);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001784 return pd_idx;
1785}
1786
Dan Williamsa4456852007-07-09 11:56:43 -07001787static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001788handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07001789 struct stripe_head_state *s, int disks,
1790 struct bio **return_bi)
1791{
1792 int i;
1793 for (i = disks; i--; ) {
1794 struct bio *bi;
1795 int bitmap_end = 0;
1796
1797 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1798 mdk_rdev_t *rdev;
1799 rcu_read_lock();
1800 rdev = rcu_dereference(conf->disks[i].rdev);
1801 if (rdev && test_bit(In_sync, &rdev->flags))
1802 /* multiple read failures in one stripe */
1803 md_error(conf->mddev, rdev);
1804 rcu_read_unlock();
1805 }
1806 spin_lock_irq(&conf->device_lock);
1807 /* fail all writes first */
1808 bi = sh->dev[i].towrite;
1809 sh->dev[i].towrite = NULL;
1810 if (bi) {
1811 s->to_write--;
1812 bitmap_end = 1;
1813 }
1814
1815 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1816 wake_up(&conf->wait_for_overlap);
1817
1818 while (bi && bi->bi_sector <
1819 sh->dev[i].sector + STRIPE_SECTORS) {
1820 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1821 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1822 if (--bi->bi_phys_segments == 0) {
1823 md_write_end(conf->mddev);
1824 bi->bi_next = *return_bi;
1825 *return_bi = bi;
1826 }
1827 bi = nextbi;
1828 }
1829 /* and fail all 'written' */
1830 bi = sh->dev[i].written;
1831 sh->dev[i].written = NULL;
1832 if (bi) bitmap_end = 1;
1833 while (bi && bi->bi_sector <
1834 sh->dev[i].sector + STRIPE_SECTORS) {
1835 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1836 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1837 if (--bi->bi_phys_segments == 0) {
1838 md_write_end(conf->mddev);
1839 bi->bi_next = *return_bi;
1840 *return_bi = bi;
1841 }
1842 bi = bi2;
1843 }
1844
Dan Williamsb5e98d62007-01-02 13:52:31 -07001845 /* fail any reads if this device is non-operational and
1846 * the data has not reached the cache yet.
1847 */
1848 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
1849 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1850 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07001851 bi = sh->dev[i].toread;
1852 sh->dev[i].toread = NULL;
1853 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1854 wake_up(&conf->wait_for_overlap);
1855 if (bi) s->to_read--;
1856 while (bi && bi->bi_sector <
1857 sh->dev[i].sector + STRIPE_SECTORS) {
1858 struct bio *nextbi =
1859 r5_next_bio(bi, sh->dev[i].sector);
1860 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1861 if (--bi->bi_phys_segments == 0) {
1862 bi->bi_next = *return_bi;
1863 *return_bi = bi;
1864 }
1865 bi = nextbi;
1866 }
1867 }
1868 spin_unlock_irq(&conf->device_lock);
1869 if (bitmap_end)
1870 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1871 STRIPE_SECTORS, 0, 0);
1872 }
1873
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001874 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
1875 if (atomic_dec_and_test(&conf->pending_full_writes))
1876 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07001877}
1878
Dan Williams1fe797e2008-06-28 09:16:30 +10001879/* fetch_block5 - checks the given member device to see if its data needs
1880 * to be read or computed to satisfy a request.
1881 *
1882 * Returns 1 when no more member devices need to be checked, otherwise returns
1883 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07001884 */
Dan Williams1fe797e2008-06-28 09:16:30 +10001885static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
1886 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07001887{
1888 struct r5dev *dev = &sh->dev[disk_idx];
1889 struct r5dev *failed_dev = &sh->dev[s->failed_num];
1890
Dan Williamsf38e1212007-01-02 13:52:30 -07001891 /* is the data in this block needed, and can we get it? */
1892 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10001893 !test_bit(R5_UPTODATE, &dev->flags) &&
1894 (dev->toread ||
1895 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1896 s->syncing || s->expanding ||
1897 (s->failed &&
1898 (failed_dev->toread ||
1899 (failed_dev->towrite &&
1900 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10001901 /* We would like to get this block, possibly by computing it,
1902 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07001903 */
Dan Williams976ea8d2008-06-28 08:32:03 +10001904 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10001905 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10001906 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
1907 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07001908 set_bit(R5_Wantcompute, &dev->flags);
1909 sh->ops.target = disk_idx;
1910 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07001911 /* Careful: from this point on 'uptodate' is in the eye
1912 * of raid5_run_ops which services 'compute' operations
1913 * before writes. R5_Wantcompute flags a block that will
1914 * be R5_UPTODATE by the time it is needed for a
1915 * subsequent operation.
1916 */
1917 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10001918 return 1; /* uptodate + compute == disks */
Dan Williams976ea8d2008-06-28 08:32:03 +10001919 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07001920 set_bit(R5_LOCKED, &dev->flags);
1921 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07001922 s->locked++;
1923 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
1924 s->syncing);
1925 }
1926 }
1927
Dan Williams1fe797e2008-06-28 09:16:30 +10001928 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07001929}
1930
Dan Williams1fe797e2008-06-28 09:16:30 +10001931/**
1932 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
1933 */
1934static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07001935 struct stripe_head_state *s, int disks)
1936{
1937 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07001938
Dan Williamsf38e1212007-01-02 13:52:30 -07001939 /* look for blocks to read/compute, skip this if a compute
1940 * is already in flight, or if the stripe contents are in the
1941 * midst of changing due to a write
1942 */
Dan Williams976ea8d2008-06-28 08:32:03 +10001943 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10001944 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07001945 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10001946 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07001947 break;
Dan Williamsa4456852007-07-09 11:56:43 -07001948 set_bit(STRIPE_HANDLE, &sh->state);
1949}
1950
Dan Williams1fe797e2008-06-28 09:16:30 +10001951static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07001952 struct stripe_head_state *s, struct r6_state *r6s,
1953 int disks)
1954{
1955 int i;
1956 for (i = disks; i--; ) {
1957 struct r5dev *dev = &sh->dev[i];
1958 if (!test_bit(R5_LOCKED, &dev->flags) &&
1959 !test_bit(R5_UPTODATE, &dev->flags) &&
1960 (dev->toread || (dev->towrite &&
1961 !test_bit(R5_OVERWRITE, &dev->flags)) ||
1962 s->syncing || s->expanding ||
1963 (s->failed >= 1 &&
1964 (sh->dev[r6s->failed_num[0]].toread ||
1965 s->to_write)) ||
1966 (s->failed >= 2 &&
1967 (sh->dev[r6s->failed_num[1]].toread ||
1968 s->to_write)))) {
1969 /* we would like to get this block, possibly
1970 * by computing it, but we might not be able to
1971 */
Dan Williamsc3378692008-06-05 22:45:54 -07001972 if ((s->uptodate == disks - 1) &&
1973 (s->failed && (i == r6s->failed_num[0] ||
1974 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07001975 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07001976 (unsigned long long)sh->sector, i);
1977 compute_block_1(sh, i, 0);
1978 s->uptodate++;
1979 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
1980 /* Computing 2-failure is *very* expensive; only
1981 * do it if failed >= 2
1982 */
1983 int other;
1984 for (other = disks; other--; ) {
1985 if (other == i)
1986 continue;
1987 if (!test_bit(R5_UPTODATE,
1988 &sh->dev[other].flags))
1989 break;
1990 }
1991 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07001992 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07001993 (unsigned long long)sh->sector,
1994 i, other);
1995 compute_block_2(sh, i, other);
1996 s->uptodate += 2;
1997 } else if (test_bit(R5_Insync, &dev->flags)) {
1998 set_bit(R5_LOCKED, &dev->flags);
1999 set_bit(R5_Wantread, &dev->flags);
2000 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002001 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002002 i, s->syncing);
2003 }
2004 }
2005 }
2006 set_bit(STRIPE_HANDLE, &sh->state);
2007}
2008
2009
Dan Williams1fe797e2008-06-28 09:16:30 +10002010/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002011 * any written block on an uptodate or failed drive can be returned.
2012 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2013 * never LOCKED, so we don't need to test 'failed' directly.
2014 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002015static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002016 struct stripe_head *sh, int disks, struct bio **return_bi)
2017{
2018 int i;
2019 struct r5dev *dev;
2020
2021 for (i = disks; i--; )
2022 if (sh->dev[i].written) {
2023 dev = &sh->dev[i];
2024 if (!test_bit(R5_LOCKED, &dev->flags) &&
2025 test_bit(R5_UPTODATE, &dev->flags)) {
2026 /* We can return any write requests */
2027 struct bio *wbi, *wbi2;
2028 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002029 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002030 spin_lock_irq(&conf->device_lock);
2031 wbi = dev->written;
2032 dev->written = NULL;
2033 while (wbi && wbi->bi_sector <
2034 dev->sector + STRIPE_SECTORS) {
2035 wbi2 = r5_next_bio(wbi, dev->sector);
2036 if (--wbi->bi_phys_segments == 0) {
2037 md_write_end(conf->mddev);
2038 wbi->bi_next = *return_bi;
2039 *return_bi = wbi;
2040 }
2041 wbi = wbi2;
2042 }
2043 if (dev->towrite == NULL)
2044 bitmap_end = 1;
2045 spin_unlock_irq(&conf->device_lock);
2046 if (bitmap_end)
2047 bitmap_endwrite(conf->mddev->bitmap,
2048 sh->sector,
2049 STRIPE_SECTORS,
2050 !test_bit(STRIPE_DEGRADED, &sh->state),
2051 0);
2052 }
2053 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002054
2055 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2056 if (atomic_dec_and_test(&conf->pending_full_writes))
2057 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002058}
2059
Dan Williams1fe797e2008-06-28 09:16:30 +10002060static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002061 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2062{
2063 int rmw = 0, rcw = 0, i;
2064 for (i = disks; i--; ) {
2065 /* would I have to read this buffer for read_modify_write */
2066 struct r5dev *dev = &sh->dev[i];
2067 if ((dev->towrite || i == sh->pd_idx) &&
2068 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002069 !(test_bit(R5_UPTODATE, &dev->flags) ||
2070 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002071 if (test_bit(R5_Insync, &dev->flags))
2072 rmw++;
2073 else
2074 rmw += 2*disks; /* cannot read it */
2075 }
2076 /* Would I have to read this buffer for reconstruct_write */
2077 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2078 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002079 !(test_bit(R5_UPTODATE, &dev->flags) ||
2080 test_bit(R5_Wantcompute, &dev->flags))) {
2081 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002082 else
2083 rcw += 2*disks;
2084 }
2085 }
Dan Williams45b42332007-07-09 11:56:43 -07002086 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002087 (unsigned long long)sh->sector, rmw, rcw);
2088 set_bit(STRIPE_HANDLE, &sh->state);
2089 if (rmw < rcw && rmw > 0)
2090 /* prefer read-modify-write, but need to get some data */
2091 for (i = disks; i--; ) {
2092 struct r5dev *dev = &sh->dev[i];
2093 if ((dev->towrite || i == sh->pd_idx) &&
2094 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002095 !(test_bit(R5_UPTODATE, &dev->flags) ||
2096 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002097 test_bit(R5_Insync, &dev->flags)) {
2098 if (
2099 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002100 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002101 "%d for r-m-w\n", i);
2102 set_bit(R5_LOCKED, &dev->flags);
2103 set_bit(R5_Wantread, &dev->flags);
2104 s->locked++;
2105 } else {
2106 set_bit(STRIPE_DELAYED, &sh->state);
2107 set_bit(STRIPE_HANDLE, &sh->state);
2108 }
2109 }
2110 }
2111 if (rcw <= rmw && rcw > 0)
2112 /* want reconstruct write, but need to get some data */
2113 for (i = disks; i--; ) {
2114 struct r5dev *dev = &sh->dev[i];
2115 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2116 i != sh->pd_idx &&
2117 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002118 !(test_bit(R5_UPTODATE, &dev->flags) ||
2119 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002120 test_bit(R5_Insync, &dev->flags)) {
2121 if (
2122 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002123 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002124 "%d for Reconstruct\n", i);
2125 set_bit(R5_LOCKED, &dev->flags);
2126 set_bit(R5_Wantread, &dev->flags);
2127 s->locked++;
2128 } else {
2129 set_bit(STRIPE_DELAYED, &sh->state);
2130 set_bit(STRIPE_HANDLE, &sh->state);
2131 }
2132 }
2133 }
2134 /* now if nothing is locked, and if we have enough data,
2135 * we can start a write request
2136 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002137 /* since handle_stripe can be called at any time we need to handle the
2138 * case where a compute block operation has been submitted and then a
2139 * subsequent call wants to start a write request. raid5_run_ops only
2140 * handles the case where compute block and postxor are requested
2141 * simultaneously. If this is not the case then new writes need to be
2142 * held off until the compute completes.
2143 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002144 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2145 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2146 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002147 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002148}
2149
Dan Williams1fe797e2008-06-28 09:16:30 +10002150static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002151 struct stripe_head *sh, struct stripe_head_state *s,
2152 struct r6_state *r6s, int disks)
2153{
2154 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
2155 int qd_idx = r6s->qd_idx;
2156 for (i = disks; i--; ) {
2157 struct r5dev *dev = &sh->dev[i];
2158 /* Would I have to read this buffer for reconstruct_write */
2159 if (!test_bit(R5_OVERWRITE, &dev->flags)
2160 && i != pd_idx && i != qd_idx
2161 && (!test_bit(R5_LOCKED, &dev->flags)
2162 ) &&
2163 !test_bit(R5_UPTODATE, &dev->flags)) {
2164 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2165 else {
Dan Williams45b42332007-07-09 11:56:43 -07002166 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002167 "disk %d flags=%#lx\n", i, dev->flags);
2168 must_compute++;
2169 }
2170 }
2171 }
Dan Williams45b42332007-07-09 11:56:43 -07002172 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002173 (unsigned long long)sh->sector, rcw, must_compute);
2174 set_bit(STRIPE_HANDLE, &sh->state);
2175
2176 if (rcw > 0)
2177 /* want reconstruct write, but need to get some data */
2178 for (i = disks; i--; ) {
2179 struct r5dev *dev = &sh->dev[i];
2180 if (!test_bit(R5_OVERWRITE, &dev->flags)
2181 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2182 && !test_bit(R5_LOCKED, &dev->flags) &&
2183 !test_bit(R5_UPTODATE, &dev->flags) &&
2184 test_bit(R5_Insync, &dev->flags)) {
2185 if (
2186 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002187 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002188 "block %d for Reconstruct\n",
2189 (unsigned long long)sh->sector, i);
2190 set_bit(R5_LOCKED, &dev->flags);
2191 set_bit(R5_Wantread, &dev->flags);
2192 s->locked++;
2193 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002194 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002195 "block %d for Reconstruct\n",
2196 (unsigned long long)sh->sector, i);
2197 set_bit(STRIPE_DELAYED, &sh->state);
2198 set_bit(STRIPE_HANDLE, &sh->state);
2199 }
2200 }
2201 }
2202 /* now if nothing is locked, and if we have enough data, we can start a
2203 * write request
2204 */
2205 if (s->locked == 0 && rcw == 0 &&
2206 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2207 if (must_compute > 0) {
2208 /* We have failed blocks and need to compute them */
2209 switch (s->failed) {
2210 case 0:
2211 BUG();
2212 case 1:
2213 compute_block_1(sh, r6s->failed_num[0], 0);
2214 break;
2215 case 2:
2216 compute_block_2(sh, r6s->failed_num[0],
2217 r6s->failed_num[1]);
2218 break;
2219 default: /* This request should have been failed? */
2220 BUG();
2221 }
2222 }
2223
Dan Williams45b42332007-07-09 11:56:43 -07002224 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002225 (unsigned long long)sh->sector);
2226 compute_parity6(sh, RECONSTRUCT_WRITE);
2227 /* now every locked buffer is ready to be written */
2228 for (i = disks; i--; )
2229 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002230 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002231 (unsigned long long)sh->sector, i);
2232 s->locked++;
2233 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2234 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002235 if (s->locked == disks)
2236 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2237 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002238 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2239 set_bit(STRIPE_INSYNC, &sh->state);
2240
2241 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2242 atomic_dec(&conf->preread_active_stripes);
2243 if (atomic_read(&conf->preread_active_stripes) <
2244 IO_THRESHOLD)
2245 md_wakeup_thread(conf->mddev->thread);
2246 }
2247 }
2248}
2249
2250static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2251 struct stripe_head_state *s, int disks)
2252{
Dan Williamsecc65c92008-06-28 08:31:57 +10002253 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002254
Dan Williamsbd2ab672008-04-10 21:29:27 -07002255 set_bit(STRIPE_HANDLE, &sh->state);
2256
Dan Williamsecc65c92008-06-28 08:31:57 +10002257 switch (sh->check_state) {
2258 case check_state_idle:
2259 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002260 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002261 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002262 sh->check_state = check_state_run;
2263 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002264 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002265 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002266 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002267 }
Dan Williamsa4456852007-07-09 11:56:43 -07002268 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002269 /* fall through */
2270 case check_state_compute_result:
2271 sh->check_state = check_state_idle;
2272 if (!dev)
2273 dev = &sh->dev[sh->pd_idx];
2274
2275 /* check that a write has not made the stripe insync */
2276 if (test_bit(STRIPE_INSYNC, &sh->state))
2277 break;
2278
2279 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002280 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2281 BUG_ON(s->uptodate != disks);
2282
2283 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002284 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002285 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002286
Dan Williamsa4456852007-07-09 11:56:43 -07002287 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002288 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002289 break;
2290 case check_state_run:
2291 break; /* we will be called again upon completion */
2292 case check_state_check_result:
2293 sh->check_state = check_state_idle;
2294
2295 /* if a failure occurred during the check operation, leave
2296 * STRIPE_INSYNC not set and let the stripe be handled again
2297 */
2298 if (s->failed)
2299 break;
2300
2301 /* handle a successful check operation, if parity is correct
2302 * we are done. Otherwise update the mismatch count and repair
2303 * parity if !MD_RECOVERY_CHECK
2304 */
2305 if (sh->ops.zero_sum_result == 0)
2306 /* parity is correct (on disc,
2307 * not in buffer any more)
2308 */
2309 set_bit(STRIPE_INSYNC, &sh->state);
2310 else {
2311 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2312 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2313 /* don't try to repair!! */
2314 set_bit(STRIPE_INSYNC, &sh->state);
2315 else {
2316 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002317 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002318 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2319 set_bit(R5_Wantcompute,
2320 &sh->dev[sh->pd_idx].flags);
2321 sh->ops.target = sh->pd_idx;
2322 s->uptodate++;
2323 }
2324 }
2325 break;
2326 case check_state_compute_run:
2327 break;
2328 default:
2329 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2330 __func__, sh->check_state,
2331 (unsigned long long) sh->sector);
2332 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002333 }
2334}
2335
2336
2337static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2338 struct stripe_head_state *s,
2339 struct r6_state *r6s, struct page *tmp_page,
2340 int disks)
2341{
2342 int update_p = 0, update_q = 0;
2343 struct r5dev *dev;
2344 int pd_idx = sh->pd_idx;
2345 int qd_idx = r6s->qd_idx;
2346
2347 set_bit(STRIPE_HANDLE, &sh->state);
2348
2349 BUG_ON(s->failed > 2);
2350 BUG_ON(s->uptodate < disks);
2351 /* Want to check and possibly repair P and Q.
2352 * However there could be one 'failed' device, in which
2353 * case we can only check one of them, possibly using the
2354 * other to generate missing data
2355 */
2356
2357 /* If !tmp_page, we cannot do the calculations,
2358 * but as we have set STRIPE_HANDLE, we will soon be called
2359 * by stripe_handle with a tmp_page - just wait until then.
2360 */
2361 if (tmp_page) {
2362 if (s->failed == r6s->q_failed) {
2363 /* The only possible failed device holds 'Q', so it
2364 * makes sense to check P (If anything else were failed,
2365 * we would have used P to recreate it).
2366 */
2367 compute_block_1(sh, pd_idx, 1);
2368 if (!page_is_zero(sh->dev[pd_idx].page)) {
2369 compute_block_1(sh, pd_idx, 0);
2370 update_p = 1;
2371 }
2372 }
2373 if (!r6s->q_failed && s->failed < 2) {
2374 /* q is not failed, and we didn't use it to generate
2375 * anything, so it makes sense to check it
2376 */
2377 memcpy(page_address(tmp_page),
2378 page_address(sh->dev[qd_idx].page),
2379 STRIPE_SIZE);
2380 compute_parity6(sh, UPDATE_PARITY);
2381 if (memcmp(page_address(tmp_page),
2382 page_address(sh->dev[qd_idx].page),
2383 STRIPE_SIZE) != 0) {
2384 clear_bit(STRIPE_INSYNC, &sh->state);
2385 update_q = 1;
2386 }
2387 }
2388 if (update_p || update_q) {
2389 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2390 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2391 /* don't try to repair!! */
2392 update_p = update_q = 0;
2393 }
2394
2395 /* now write out any block on a failed drive,
2396 * or P or Q if they need it
2397 */
2398
2399 if (s->failed == 2) {
2400 dev = &sh->dev[r6s->failed_num[1]];
2401 s->locked++;
2402 set_bit(R5_LOCKED, &dev->flags);
2403 set_bit(R5_Wantwrite, &dev->flags);
2404 }
2405 if (s->failed >= 1) {
2406 dev = &sh->dev[r6s->failed_num[0]];
2407 s->locked++;
2408 set_bit(R5_LOCKED, &dev->flags);
2409 set_bit(R5_Wantwrite, &dev->flags);
2410 }
2411
2412 if (update_p) {
2413 dev = &sh->dev[pd_idx];
2414 s->locked++;
2415 set_bit(R5_LOCKED, &dev->flags);
2416 set_bit(R5_Wantwrite, &dev->flags);
2417 }
2418 if (update_q) {
2419 dev = &sh->dev[qd_idx];
2420 s->locked++;
2421 set_bit(R5_LOCKED, &dev->flags);
2422 set_bit(R5_Wantwrite, &dev->flags);
2423 }
2424 clear_bit(STRIPE_DEGRADED, &sh->state);
2425
2426 set_bit(STRIPE_INSYNC, &sh->state);
2427 }
2428}
2429
2430static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2431 struct r6_state *r6s)
2432{
2433 int i;
2434
2435 /* We have read all the blocks in this stripe and now we need to
2436 * copy some of them into a target stripe for expand.
2437 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002438 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002439 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2440 for (i = 0; i < sh->disks; i++)
NeilBrowna2e08552007-09-11 15:23:36 -07002441 if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002442 int dd_idx, pd_idx, j;
2443 struct stripe_head *sh2;
2444
2445 sector_t bn = compute_blocknr(sh, i);
2446 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
2447 conf->raid_disks -
2448 conf->max_degraded, &dd_idx,
2449 &pd_idx, conf);
2450 sh2 = get_active_stripe(conf, s, conf->raid_disks,
2451 pd_idx, 1);
2452 if (sh2 == NULL)
2453 /* so far only the early blocks of this stripe
2454 * have been requested. When later blocks
2455 * get requested, we will try again
2456 */
2457 continue;
2458 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2459 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2460 /* must have already done this block */
2461 release_stripe(sh2);
2462 continue;
2463 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002464
2465 /* place all the copies on one channel */
2466 tx = async_memcpy(sh2->dev[dd_idx].page,
2467 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2468 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2469
Dan Williamsa4456852007-07-09 11:56:43 -07002470 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2471 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2472 for (j = 0; j < conf->raid_disks; j++)
2473 if (j != sh2->pd_idx &&
NeilBrowna2e08552007-09-11 15:23:36 -07002474 (!r6s || j != raid6_next_disk(sh2->pd_idx,
2475 sh2->disks)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002476 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2477 break;
2478 if (j == conf->raid_disks) {
2479 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2480 set_bit(STRIPE_HANDLE, &sh2->state);
2481 }
2482 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002483
Dan Williamsa4456852007-07-09 11:56:43 -07002484 }
NeilBrowna2e08552007-09-11 15:23:36 -07002485 /* done submitting copies, wait for them to complete */
2486 if (tx) {
2487 async_tx_ack(tx);
2488 dma_wait_for_async_tx(tx);
2489 }
Dan Williamsa4456852007-07-09 11:56:43 -07002490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Dan Williams6bfe0b42008-04-30 00:52:32 -07002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
2494 * handle_stripe - do things to a stripe.
2495 *
2496 * We lock the stripe and then examine the state of various bits
2497 * to see what needs to be done.
2498 * Possible results:
2499 * return some read request which now have data
2500 * return some write requests which are safely on disc
2501 * schedule a read on some buffers
2502 * schedule a write of some buffers
2503 * return confirmation of parity correctness
2504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 * buffers are taken off read_list or write_list, and bh_cache buffers
2506 * get BH_Lock set before the stripe lock is released.
2507 *
2508 */
Dan Williamsa4456852007-07-09 11:56:43 -07002509
NeilBrown16a53ec2006-06-26 00:27:38 -07002510static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002513 int disks = sh->disks, i;
2514 struct bio *return_bi = NULL;
2515 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002517 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002518 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Dan Williamsa4456852007-07-09 11:56:43 -07002520 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002521 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2522 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2523 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2524 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 spin_lock(&sh->lock);
2527 clear_bit(STRIPE_HANDLE, &sh->state);
2528 clear_bit(STRIPE_DELAYED, &sh->state);
2529
Dan Williamsa4456852007-07-09 11:56:43 -07002530 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2531 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2532 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002535 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 for (i=disks; i--; ) {
2537 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002538 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Dan Williamsb5e98d62007-01-02 13:52:31 -07002541 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2542 "written %p\n", i, dev->flags, dev->toread, dev->read,
2543 dev->towrite, dev->written);
2544
2545 /* maybe we can request a biofill operation
2546 *
2547 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002548 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002549 */
2550 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002551 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002552 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002555 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2556 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002557 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Dan Williamsb5e98d62007-01-02 13:52:31 -07002559 if (test_bit(R5_Wantfill, &dev->flags))
2560 s.to_fill++;
2561 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002562 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002564 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002566 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Dan Williamsa4456852007-07-09 11:56:43 -07002568 if (dev->written)
2569 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002570 rdev = rcu_dereference(conf->disks[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002571 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2572 blocked_rdev = rdev;
2573 atomic_inc(&rdev->nr_pending);
2574 break;
2575 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002576 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002577 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002578 clear_bit(R5_ReadError, &dev->flags);
2579 clear_bit(R5_ReWrite, &dev->flags);
2580 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002581 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002582 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002583 s.failed++;
2584 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 } else
2586 set_bit(R5_Insync, &dev->flags);
2587 }
NeilBrown9910f162006-01-06 00:20:24 -08002588 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002589
Dan Williams6bfe0b42008-04-30 00:52:32 -07002590 if (unlikely(blocked_rdev)) {
2591 set_bit(STRIPE_HANDLE, &sh->state);
2592 goto unlock;
2593 }
2594
Dan Williams83de75c2008-06-28 08:31:58 +10002595 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2596 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2597 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2598 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002599
Dan Williams45b42332007-07-09 11:56:43 -07002600 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002602 s.locked, s.uptodate, s.to_read, s.to_write,
2603 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* check if the array has lost two devices and, if so, some requests might
2605 * need to be failed
2606 */
Dan Williamsa4456852007-07-09 11:56:43 -07002607 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002608 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002609 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2611 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002612 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614
2615 /* might be able to return some write requests if the parity block
2616 * is safe, or on a failed drive
2617 */
2618 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002619 if ( s.written &&
2620 ((test_bit(R5_Insync, &dev->flags) &&
2621 !test_bit(R5_LOCKED, &dev->flags) &&
2622 test_bit(R5_UPTODATE, &dev->flags)) ||
2623 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002624 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 /* Now we might consider reading some blocks, either to check/generate
2627 * parity, or to satisfy requests
2628 * or to load a block that is being partially written.
2629 */
Dan Williamsa4456852007-07-09 11:56:43 -07002630 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002631 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002632 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Dan Williamse33129d2007-01-02 13:52:30 -07002634 /* Now we check to see if any write operations have recently
2635 * completed
2636 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002637 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002638 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
2639 prexor = 1;
2640 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2641 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002642 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002643
2644 /* All the 'written' buffers and the parity block are ready to
2645 * be written back to disk
2646 */
2647 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2648 for (i = disks; i--; ) {
2649 dev = &sh->dev[i];
2650 if (test_bit(R5_LOCKED, &dev->flags) &&
2651 (i == sh->pd_idx || dev->written)) {
2652 pr_debug("Writing block %d\n", i);
2653 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002654 if (prexor)
2655 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002656 if (!test_bit(R5_Insync, &dev->flags) ||
2657 (i == sh->pd_idx && s.failed == 0))
2658 set_bit(STRIPE_INSYNC, &sh->state);
2659 }
2660 }
2661 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2662 atomic_dec(&conf->preread_active_stripes);
2663 if (atomic_read(&conf->preread_active_stripes) <
2664 IO_THRESHOLD)
2665 md_wakeup_thread(conf->mddev->thread);
2666 }
2667 }
2668
2669 /* Now to consider new write requests and what else, if anything
2670 * should be read. We do not handle new writes when:
2671 * 1/ A 'write' operation (copy+xor) is already in flight.
2672 * 2/ A 'check' operation is in flight, as it may clobber the parity
2673 * block.
2674 */
Dan Williams600aa102008-06-28 08:32:05 +10002675 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002676 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002679 * Any reads will already have been scheduled, so we just see if enough
2680 * data is available. The parity check is held off while parity
2681 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002683 if (sh->check_state ||
2684 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002685 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002686 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002687 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002688
Dan Williamsa4456852007-07-09 11:56:43 -07002689 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2691 clear_bit(STRIPE_SYNCING, &sh->state);
2692 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002693
2694 /* If the failed drive is just a ReadError, then we might need to progress
2695 * the repair/check process
2696 */
Dan Williamsa4456852007-07-09 11:56:43 -07002697 if (s.failed == 1 && !conf->mddev->ro &&
2698 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2699 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2700 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002701 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002702 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08002703 if (!test_bit(R5_ReWrite, &dev->flags)) {
2704 set_bit(R5_Wantwrite, &dev->flags);
2705 set_bit(R5_ReWrite, &dev->flags);
2706 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002707 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002708 } else {
2709 /* let's read it back */
2710 set_bit(R5_Wantread, &dev->flags);
2711 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002712 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002713 }
2714 }
2715
Dan Williams600aa102008-06-28 08:32:05 +10002716 /* Finish reconstruct operations initiated by the expansion process */
2717 if (sh->reconstruct_state == reconstruct_state_result) {
2718 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002719 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams2b7497f2008-06-28 08:31:52 +10002720 for (i = conf->raid_disks; i--; )
Dan Williamsf0a50d32007-01-02 13:52:31 -07002721 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10002722 set_bit(R5_LOCKED, &dev->flags);
2723 s.locked++;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002724 }
2725
2726 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10002727 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002728 /* Need to write out all blocks after computing parity */
2729 sh->disks = conf->raid_disks;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002730 sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
2731 conf->raid_disks);
Dan Williams1fe797e2008-06-28 09:16:30 +10002732 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10002733 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002734 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08002735 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002736 wake_up(&conf->wait_for_overlap);
2737 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2738 }
2739
Dan Williams0f94e87c2008-01-08 15:32:53 -08002740 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002741 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002742 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002743
Dan Williams6bfe0b42008-04-30 00:52:32 -07002744 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 spin_unlock(&sh->lock);
2746
Dan Williams6bfe0b42008-04-30 00:52:32 -07002747 /* wait for this device to become unblocked */
2748 if (unlikely(blocked_rdev))
2749 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2750
Dan Williams600aa102008-06-28 08:32:05 +10002751 if (s.ops_request)
2752 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07002753
Dan Williamsc4e5ac02008-06-28 08:31:53 +10002754 ops_run_io(sh, &s);
Dan Williams2b7497f2008-06-28 08:31:52 +10002755
Dan Williamsa4456852007-07-09 11:56:43 -07002756 return_io(return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
NeilBrown16a53ec2006-06-26 00:27:38 -07002759static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
2760{
2761 raid6_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002762 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07002763 struct bio *return_bi = NULL;
2764 int i, pd_idx = sh->pd_idx;
2765 struct stripe_head_state s;
2766 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07002767 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002768 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07002769
Dan Williamsa4456852007-07-09 11:56:43 -07002770 r6s.qd_idx = raid6_next_disk(pd_idx, disks);
Dan Williams45b42332007-07-09 11:56:43 -07002771 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07002772 "pd_idx=%d, qd_idx=%d\n",
2773 (unsigned long long)sh->sector, sh->state,
2774 atomic_read(&sh->count), pd_idx, r6s.qd_idx);
2775 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002776
2777 spin_lock(&sh->lock);
2778 clear_bit(STRIPE_HANDLE, &sh->state);
2779 clear_bit(STRIPE_DELAYED, &sh->state);
2780
Dan Williamsa4456852007-07-09 11:56:43 -07002781 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2782 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2783 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07002784 /* Now to look around and see what can be done */
2785
2786 rcu_read_lock();
2787 for (i=disks; i--; ) {
2788 mdk_rdev_t *rdev;
2789 dev = &sh->dev[i];
2790 clear_bit(R5_Insync, &dev->flags);
2791
Dan Williams45b42332007-07-09 11:56:43 -07002792 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07002793 i, dev->flags, dev->toread, dev->towrite, dev->written);
2794 /* maybe we can reply to a read */
2795 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
2796 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002797 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07002798 spin_lock_irq(&conf->device_lock);
2799 rbi = dev->toread;
2800 dev->toread = NULL;
2801 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2802 wake_up(&conf->wait_for_overlap);
2803 spin_unlock_irq(&conf->device_lock);
2804 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2805 copy_data(0, rbi, dev->page, dev->sector);
2806 rbi2 = r5_next_bio(rbi, dev->sector);
2807 spin_lock_irq(&conf->device_lock);
2808 if (--rbi->bi_phys_segments == 0) {
2809 rbi->bi_next = return_bi;
2810 return_bi = rbi;
2811 }
2812 spin_unlock_irq(&conf->device_lock);
2813 rbi = rbi2;
2814 }
2815 }
2816
2817 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002818 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2819 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002820
2821
Dan Williamsa4456852007-07-09 11:56:43 -07002822 if (dev->toread)
2823 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002824 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002825 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002826 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002827 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002828 }
Dan Williamsa4456852007-07-09 11:56:43 -07002829 if (dev->written)
2830 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002831 rdev = rcu_dereference(conf->disks[i].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002832 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
2833 blocked_rdev = rdev;
2834 atomic_inc(&rdev->nr_pending);
2835 break;
2836 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002837 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2838 /* The ReadError flag will just be confusing now */
2839 clear_bit(R5_ReadError, &dev->flags);
2840 clear_bit(R5_ReWrite, &dev->flags);
2841 }
2842 if (!rdev || !test_bit(In_sync, &rdev->flags)
2843 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002844 if (s.failed < 2)
2845 r6s.failed_num[s.failed] = i;
2846 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002847 } else
2848 set_bit(R5_Insync, &dev->flags);
2849 }
2850 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07002851
2852 if (unlikely(blocked_rdev)) {
2853 set_bit(STRIPE_HANDLE, &sh->state);
2854 goto unlock;
2855 }
Dan Williams45b42332007-07-09 11:56:43 -07002856 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07002857 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002858 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
2859 r6s.failed_num[0], r6s.failed_num[1]);
2860 /* check if the array has lost >2 devices and, if so, some requests
2861 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07002862 */
Dan Williamsa4456852007-07-09 11:56:43 -07002863 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002864 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002865 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002866 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2867 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002868 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07002869 }
2870
2871 /*
2872 * might be able to return some write requests if the parity blocks
2873 * are safe, or on a failed drive
2874 */
2875 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002876 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
2877 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
2878 qdev = &sh->dev[r6s.qd_idx];
2879 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
2880 || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07002881
Dan Williamsa4456852007-07-09 11:56:43 -07002882 if ( s.written &&
2883 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07002884 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07002885 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
2886 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07002887 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07002888 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10002889 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07002890
2891 /* Now we might consider reading some blocks, either to check/generate
2892 * parity, or to satisfy requests
2893 * or to load a block that is being partially written.
2894 */
Dan Williamsa4456852007-07-09 11:56:43 -07002895 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
2896 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002897 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07002898
2899 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07002900 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10002901 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07002902
2903 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07002904 * Any reads will already have been scheduled, so we just see if enough
2905 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07002906 */
Dan Williamsa4456852007-07-09 11:56:43 -07002907 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
2908 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07002909
Dan Williamsa4456852007-07-09 11:56:43 -07002910 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002911 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2912 clear_bit(STRIPE_SYNCING, &sh->state);
2913 }
2914
2915 /* If the failed drives are just a ReadError, then we might need
2916 * to progress the repair/check process
2917 */
Dan Williamsa4456852007-07-09 11:56:43 -07002918 if (s.failed <= 2 && !conf->mddev->ro)
2919 for (i = 0; i < s.failed; i++) {
2920 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07002921 if (test_bit(R5_ReadError, &dev->flags)
2922 && !test_bit(R5_LOCKED, &dev->flags)
2923 && test_bit(R5_UPTODATE, &dev->flags)
2924 ) {
2925 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);
2929 } else {
2930 /* let's read it back */
2931 set_bit(R5_Wantread, &dev->flags);
2932 set_bit(R5_LOCKED, &dev->flags);
2933 }
2934 }
2935 }
NeilBrownf4168852007-02-28 20:11:53 -08002936
Dan Williamsa4456852007-07-09 11:56:43 -07002937 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownf4168852007-02-28 20:11:53 -08002938 /* Need to write out all blocks after computing P&Q */
2939 sh->disks = conf->raid_disks;
2940 sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
2941 conf->raid_disks);
2942 compute_parity6(sh, RECONSTRUCT_WRITE);
2943 for (i = conf->raid_disks ; i-- ; ) {
2944 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002945 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08002946 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2947 }
2948 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002949 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08002950 clear_bit(STRIPE_EXPAND_READY, &sh->state);
2951 atomic_dec(&conf->reshape_stripes);
2952 wake_up(&conf->wait_for_overlap);
2953 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2954 }
2955
Dan Williams0f94e87c2008-01-08 15:32:53 -08002956 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002957 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002958 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08002959
Dan Williams6bfe0b42008-04-30 00:52:32 -07002960 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07002961 spin_unlock(&sh->lock);
2962
Dan Williams6bfe0b42008-04-30 00:52:32 -07002963 /* wait for this device to become unblocked */
2964 if (unlikely(blocked_rdev))
2965 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2966
Dan Williamsf0e43bc2008-06-28 08:31:55 +10002967 ops_run_io(sh, &s);
2968
Dan Williamsa4456852007-07-09 11:56:43 -07002969 return_io(return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07002970}
2971
2972static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2973{
2974 if (sh->raid_conf->level == 6)
2975 handle_stripe6(sh, tmp_page);
2976 else
2977 handle_stripe5(sh);
2978}
2979
2980
2981
Arjan van de Ven858119e2006-01-14 13:20:43 -08002982static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2985 while (!list_empty(&conf->delayed_list)) {
2986 struct list_head *l = conf->delayed_list.next;
2987 struct stripe_head *sh;
2988 sh = list_entry(l, struct stripe_head, lru);
2989 list_del_init(l);
2990 clear_bit(STRIPE_DELAYED, &sh->state);
2991 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2992 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002993 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
NeilBrown6ed30032008-02-06 01:40:00 -08002995 } else
2996 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Arjan van de Ven858119e2006-01-14 13:20:43 -08002999static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003000{
3001 /* device_lock is held */
3002 struct list_head head;
3003 list_add(&head, &conf->bitmap_list);
3004 list_del_init(&conf->bitmap_list);
3005 while (!list_empty(&head)) {
3006 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3007 list_del_init(&sh->lru);
3008 atomic_inc(&sh->count);
3009 __release_stripe(conf, sh);
3010 }
3011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013static void unplug_slaves(mddev_t *mddev)
3014{
3015 raid5_conf_t *conf = mddev_to_conf(mddev);
3016 int i;
3017
3018 rcu_read_lock();
3019 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003020 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003021 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003022 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 atomic_inc(&rdev->nr_pending);
3025 rcu_read_unlock();
3026
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003027 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 rdev_dec_pending(rdev, mddev);
3030 rcu_read_lock();
3031 }
3032 }
3033 rcu_read_unlock();
3034}
3035
Jens Axboe165125e2007-07-24 09:28:11 +02003036static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 mddev_t *mddev = q->queuedata;
3039 raid5_conf_t *conf = mddev_to_conf(mddev);
3040 unsigned long flags;
3041
3042 spin_lock_irqsave(&conf->device_lock, flags);
3043
NeilBrown72626682005-09-09 16:23:54 -07003044 if (blk_remove_plug(q)) {
3045 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 md_wakeup_thread(mddev->thread);
3049
3050 spin_unlock_irqrestore(&conf->device_lock, flags);
3051
3052 unplug_slaves(mddev);
3053}
3054
NeilBrownf022b2f2006-10-03 01:15:56 -07003055static int raid5_congested(void *data, int bits)
3056{
3057 mddev_t *mddev = data;
3058 raid5_conf_t *conf = mddev_to_conf(mddev);
3059
3060 /* No difference between reads and writes. Just check
3061 * how busy the stripe_cache is
3062 */
3063 if (conf->inactive_blocked)
3064 return 1;
3065 if (conf->quiesce)
3066 return 1;
3067 if (list_empty_careful(&conf->inactive_list))
3068 return 1;
3069
3070 return 0;
3071}
3072
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003073/* We want read requests to align with chunks where possible,
3074 * but write requests don't need to.
3075 */
Jens Axboe165125e2007-07-24 09:28:11 +02003076static int raid5_mergeable_bvec(struct request_queue *q, struct bio *bio, struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003077{
3078 mddev_t *mddev = q->queuedata;
3079 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3080 int max;
3081 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3082 unsigned int bio_sectors = bio->bi_size >> 9;
3083
NeilBrown802ba062006-12-13 00:34:13 -08003084 if (bio_data_dir(bio) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003085 return biovec->bv_len; /* always allow writes to be mergeable */
3086
3087 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3088 if (max < 0) max = 0;
3089 if (max <= biovec->bv_len && bio_sectors == 0)
3090 return biovec->bv_len;
3091 else
3092 return max;
3093}
3094
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003095
3096static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3097{
3098 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3099 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3100 unsigned int bio_sectors = bio->bi_size >> 9;
3101
3102 return chunk_sectors >=
3103 ((sector & (chunk_sectors - 1)) + bio_sectors);
3104}
3105
3106/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003107 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3108 * later sampled by raid5d.
3109 */
3110static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3111{
3112 unsigned long flags;
3113
3114 spin_lock_irqsave(&conf->device_lock, flags);
3115
3116 bi->bi_next = conf->retry_read_aligned_list;
3117 conf->retry_read_aligned_list = bi;
3118
3119 spin_unlock_irqrestore(&conf->device_lock, flags);
3120 md_wakeup_thread(conf->mddev->thread);
3121}
3122
3123
3124static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3125{
3126 struct bio *bi;
3127
3128 bi = conf->retry_read_aligned;
3129 if (bi) {
3130 conf->retry_read_aligned = NULL;
3131 return bi;
3132 }
3133 bi = conf->retry_read_aligned_list;
3134 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003135 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003136 bi->bi_next = NULL;
3137 bi->bi_phys_segments = 1; /* biased count of active stripes */
3138 bi->bi_hw_segments = 0; /* count of processed stripes */
3139 }
3140
3141 return bi;
3142}
3143
3144
3145/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003146 * The "raid5_align_endio" should check if the read succeeded and if it
3147 * did, call bio_endio on the original bio (having bio_put the new bio
3148 * first).
3149 * If the read failed..
3150 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003151static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003152{
3153 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003154 mddev_t *mddev;
3155 raid5_conf_t *conf;
3156 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3157 mdk_rdev_t *rdev;
3158
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003159 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003160
3161 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3162 conf = mddev_to_conf(mddev);
3163 rdev = (void*)raid_bi->bi_next;
3164 raid_bi->bi_next = NULL;
3165
3166 rdev_dec_pending(rdev, conf->mddev);
3167
3168 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003169 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003170 if (atomic_dec_and_test(&conf->active_aligned_reads))
3171 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003172 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003173 }
3174
3175
Dan Williams45b42332007-07-09 11:56:43 -07003176 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003177
3178 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003179}
3180
Neil Brown387bb172007-02-08 14:20:29 -08003181static int bio_fits_rdev(struct bio *bi)
3182{
Jens Axboe165125e2007-07-24 09:28:11 +02003183 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003184
3185 if ((bi->bi_size>>9) > q->max_sectors)
3186 return 0;
3187 blk_recount_segments(q, bi);
3188 if (bi->bi_phys_segments > q->max_phys_segments ||
3189 bi->bi_hw_segments > q->max_hw_segments)
3190 return 0;
3191
3192 if (q->merge_bvec_fn)
3193 /* it's too hard to apply the merge_bvec_fn at this stage,
3194 * just just give up
3195 */
3196 return 0;
3197
3198 return 1;
3199}
3200
3201
Jens Axboe165125e2007-07-24 09:28:11 +02003202static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003203{
3204 mddev_t *mddev = q->queuedata;
3205 raid5_conf_t *conf = mddev_to_conf(mddev);
3206 const unsigned int raid_disks = conf->raid_disks;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003207 const unsigned int data_disks = raid_disks - conf->max_degraded;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003208 unsigned int dd_idx, pd_idx;
3209 struct bio* align_bi;
3210 mdk_rdev_t *rdev;
3211
3212 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003213 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003214 return 0;
3215 }
3216 /*
3217 * use bio_clone to make a copy of the bio
3218 */
3219 align_bi = bio_clone(raid_bio, GFP_NOIO);
3220 if (!align_bi)
3221 return 0;
3222 /*
3223 * set bi_end_io to a new function, and set bi_private to the
3224 * original bio.
3225 */
3226 align_bi->bi_end_io = raid5_align_endio;
3227 align_bi->bi_private = raid_bio;
3228 /*
3229 * compute position
3230 */
3231 align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
3232 raid_disks,
3233 data_disks,
3234 &dd_idx,
3235 &pd_idx,
3236 conf);
3237
3238 rcu_read_lock();
3239 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3240 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003241 atomic_inc(&rdev->nr_pending);
3242 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003243 raid_bio->bi_next = (void*)rdev;
3244 align_bi->bi_bdev = rdev->bdev;
3245 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3246 align_bi->bi_sector += rdev->data_offset;
3247
Neil Brown387bb172007-02-08 14:20:29 -08003248 if (!bio_fits_rdev(align_bi)) {
3249 /* too big in some way */
3250 bio_put(align_bi);
3251 rdev_dec_pending(rdev, mddev);
3252 return 0;
3253 }
3254
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003255 spin_lock_irq(&conf->device_lock);
3256 wait_event_lock_irq(conf->wait_for_stripe,
3257 conf->quiesce == 0,
3258 conf->device_lock, /* nothing */);
3259 atomic_inc(&conf->active_aligned_reads);
3260 spin_unlock_irq(&conf->device_lock);
3261
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003262 generic_make_request(align_bi);
3263 return 1;
3264 } else {
3265 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003266 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003267 return 0;
3268 }
3269}
3270
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003271/* __get_priority_stripe - get the next stripe to process
3272 *
3273 * Full stripe writes are allowed to pass preread active stripes up until
3274 * the bypass_threshold is exceeded. In general the bypass_count
3275 * increments when the handle_list is handled before the hold_list; however, it
3276 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3277 * stripe with in flight i/o. The bypass_count will be reset when the
3278 * head of the hold_list has changed, i.e. the head was promoted to the
3279 * handle_list.
3280 */
3281static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3282{
3283 struct stripe_head *sh;
3284
3285 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3286 __func__,
3287 list_empty(&conf->handle_list) ? "empty" : "busy",
3288 list_empty(&conf->hold_list) ? "empty" : "busy",
3289 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3290
3291 if (!list_empty(&conf->handle_list)) {
3292 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3293
3294 if (list_empty(&conf->hold_list))
3295 conf->bypass_count = 0;
3296 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3297 if (conf->hold_list.next == conf->last_hold)
3298 conf->bypass_count++;
3299 else {
3300 conf->last_hold = conf->hold_list.next;
3301 conf->bypass_count -= conf->bypass_threshold;
3302 if (conf->bypass_count < 0)
3303 conf->bypass_count = 0;
3304 }
3305 }
3306 } else if (!list_empty(&conf->hold_list) &&
3307 ((conf->bypass_threshold &&
3308 conf->bypass_count > conf->bypass_threshold) ||
3309 atomic_read(&conf->pending_full_writes) == 0)) {
3310 sh = list_entry(conf->hold_list.next,
3311 typeof(*sh), lru);
3312 conf->bypass_count -= conf->bypass_threshold;
3313 if (conf->bypass_count < 0)
3314 conf->bypass_count = 0;
3315 } else
3316 return NULL;
3317
3318 list_del_init(&sh->lru);
3319 atomic_inc(&sh->count);
3320 BUG_ON(atomic_read(&sh->count) != 1);
3321 return sh;
3322}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003323
Jens Axboe165125e2007-07-24 09:28:11 +02003324static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 mddev_t *mddev = q->queuedata;
3327 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 unsigned int dd_idx, pd_idx;
3329 sector_t new_sector;
3330 sector_t logical_sector, last_sector;
3331 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003332 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003333 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
NeilBrowne5dcdd82005-09-09 16:23:41 -07003335 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003336 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003337 return 0;
3338 }
3339
NeilBrown3d310eb2005-06-21 17:17:26 -07003340 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003341
Jens Axboea3623572005-11-01 09:26:16 +01003342 disk_stat_inc(mddev->gendisk, ios[rw]);
3343 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
NeilBrown802ba062006-12-13 00:34:13 -08003345 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003346 mddev->reshape_position == MaxSector &&
3347 chunk_aligned_read(q,bi))
3348 return 0;
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3351 last_sector = bi->bi_sector + (bi->bi_size>>9);
3352 bi->bi_next = NULL;
3353 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3356 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003357 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003358
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003359 retry:
NeilBrownb578d552006-03-27 01:18:12 -08003360 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003361 if (likely(conf->expand_progress == MaxSector))
3362 disks = conf->raid_disks;
3363 else {
NeilBrowndf8e7f762006-03-27 01:18:15 -08003364 /* spinlock is needed as expand_progress may be
3365 * 64bit on a 32bit platform, and so it might be
3366 * possible to see a half-updated value
3367 * Ofcourse expand_progress could change after
3368 * the lock is dropped, so once we get a reference
3369 * to the stripe that we think it is, we will have
3370 * to check again.
3371 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003372 spin_lock_irq(&conf->device_lock);
3373 disks = conf->raid_disks;
3374 if (logical_sector >= conf->expand_progress)
3375 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003376 else {
3377 if (logical_sector >= conf->expand_lo) {
3378 spin_unlock_irq(&conf->device_lock);
3379 schedule();
3380 goto retry;
3381 }
3382 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003383 spin_unlock_irq(&conf->device_lock);
3384 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003385 data_disks = disks - conf->max_degraded;
3386
3387 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003388 &dd_idx, &pd_idx, conf);
Dan Williams45b42332007-07-09 11:56:43 -07003389 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 (unsigned long long)new_sector,
3391 (unsigned long long)logical_sector);
3392
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003393 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003395 if (unlikely(conf->expand_progress != MaxSector)) {
3396 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003397 * stripe, so we must do the range check again.
3398 * Expansion could still move past after this
3399 * test, but as we are holding a reference to
3400 * 'sh', we know that if that happens,
3401 * STRIPE_EXPANDING will get set and the expansion
3402 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003403 */
3404 int must_retry = 0;
3405 spin_lock_irq(&conf->device_lock);
3406 if (logical_sector < conf->expand_progress &&
3407 disks == conf->previous_raid_disks)
3408 /* mismatch, need to try again */
3409 must_retry = 1;
3410 spin_unlock_irq(&conf->device_lock);
3411 if (must_retry) {
3412 release_stripe(sh);
3413 goto retry;
3414 }
3415 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003416 /* FIXME what if we get a false positive because these
3417 * are being updated.
3418 */
3419 if (logical_sector >= mddev->suspend_lo &&
3420 logical_sector < mddev->suspend_hi) {
3421 release_stripe(sh);
3422 schedule();
3423 goto retry;
3424 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003425
3426 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3427 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3428 /* Stripe is busy expanding or
3429 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 * and wait a while
3431 */
3432 raid5_unplug_device(mddev->queue);
3433 release_stripe(sh);
3434 schedule();
3435 goto retry;
3436 }
3437 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003438 set_bit(STRIPE_HANDLE, &sh->state);
3439 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 } else {
3442 /* cannot get stripe for read-ahead, just give-up */
3443 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3444 finish_wait(&conf->wait_for_overlap, &w);
3445 break;
3446 }
3447
3448 }
3449 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08003450 remaining = --bi->bi_phys_segments;
3451 spin_unlock_irq(&conf->device_lock);
3452 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
NeilBrown16a53ec2006-06-26 00:27:38 -07003454 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003456
Neil Brown0e13fe232008-06-28 08:31:20 +10003457 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 return 0;
3460}
3461
NeilBrown52c03292006-06-26 00:27:43 -07003462static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
NeilBrown52c03292006-06-26 00:27:43 -07003464 /* reshaping is quite different to recovery/resync so it is
3465 * handled quite separately ... here.
3466 *
3467 * On each call to sync_request, we gather one chunk worth of
3468 * destination stripes and flag them as expanding.
3469 * Then we find all the source stripes and request reads.
3470 * As the reads complete, handle_stripe will copy the data
3471 * into the destination stripe and release that stripe.
3472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3474 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003475 int pd_idx;
3476 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003477 int raid_disks = conf->previous_raid_disks;
3478 int data_disks = raid_disks - conf->max_degraded;
3479 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003480 int i;
3481 int dd_idx;
3482 sector_t writepos, safepos, gap;
3483
3484 if (sector_nr == 0 &&
3485 conf->expand_progress != 0) {
3486 /* restarting in the middle, skip the initial sectors */
3487 sector_nr = conf->expand_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003488 sector_div(sector_nr, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003489 *skipped = 1;
3490 return sector_nr;
3491 }
3492
3493 /* we update the metadata when there is more than 3Meg
3494 * in the block range (that is rather arbitrary, should
3495 * probably be time based) or when the data about to be
3496 * copied would over-write the source of the data at
3497 * the front of the range.
3498 * i.e. one new_stripe forward from expand_progress new_maps
3499 * to after where expand_lo old_maps to
3500 */
3501 writepos = conf->expand_progress +
NeilBrownf4168852007-02-28 20:11:53 -08003502 conf->chunk_size/512*(new_data_disks);
3503 sector_div(writepos, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003504 safepos = conf->expand_lo;
NeilBrownf4168852007-02-28 20:11:53 -08003505 sector_div(safepos, data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003506 gap = conf->expand_progress - conf->expand_lo;
3507
3508 if (writepos >= safepos ||
NeilBrownf4168852007-02-28 20:11:53 -08003509 gap > (new_data_disks)*3000*2 /*3Meg*/) {
NeilBrown52c03292006-06-26 00:27:43 -07003510 /* Cannot proceed until we've updated the superblock... */
3511 wait_event(conf->wait_for_overlap,
3512 atomic_read(&conf->reshape_stripes)==0);
3513 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07003514 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003515 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07003516 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003517 kthread_should_stop());
3518 spin_lock_irq(&conf->device_lock);
3519 conf->expand_lo = mddev->reshape_position;
3520 spin_unlock_irq(&conf->device_lock);
3521 wake_up(&conf->wait_for_overlap);
3522 }
3523
3524 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3525 int j;
3526 int skipped = 0;
3527 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
3528 sh = get_active_stripe(conf, sector_nr+i,
3529 conf->raid_disks, pd_idx, 0);
3530 set_bit(STRIPE_EXPANDING, &sh->state);
3531 atomic_inc(&conf->reshape_stripes);
3532 /* If any of this stripe is beyond the end of the old
3533 * array, then we need to zero those blocks
3534 */
3535 for (j=sh->disks; j--;) {
3536 sector_t s;
3537 if (j == sh->pd_idx)
3538 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003539 if (conf->level == 6 &&
3540 j == raid6_next_disk(sh->pd_idx, sh->disks))
3541 continue;
NeilBrown52c03292006-06-26 00:27:43 -07003542 s = compute_blocknr(sh, j);
Andre Nollf233ea52008-07-21 17:05:22 +10003543 if (s < mddev->array_sectors) {
NeilBrown52c03292006-06-26 00:27:43 -07003544 skipped = 1;
3545 continue;
3546 }
3547 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3548 set_bit(R5_Expanded, &sh->dev[j].flags);
3549 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3550 }
3551 if (!skipped) {
3552 set_bit(STRIPE_EXPAND_READY, &sh->state);
3553 set_bit(STRIPE_HANDLE, &sh->state);
3554 }
3555 release_stripe(sh);
3556 }
3557 spin_lock_irq(&conf->device_lock);
NeilBrown6d3baf22007-03-05 00:30:44 -08003558 conf->expand_progress = (sector_nr + i) * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003559 spin_unlock_irq(&conf->device_lock);
3560 /* Ok, those stripe are ready. We can start scheduling
3561 * reads on the source stripes.
3562 * The source stripes are determined by mapping the first and last
3563 * block on the destination stripes.
3564 */
NeilBrown52c03292006-06-26 00:27:43 -07003565 first_sector =
NeilBrownf4168852007-02-28 20:11:53 -08003566 raid5_compute_sector(sector_nr*(new_data_disks),
NeilBrown52c03292006-06-26 00:27:43 -07003567 raid_disks, data_disks,
3568 &dd_idx, &pd_idx, conf);
3569 last_sector =
3570 raid5_compute_sector((sector_nr+conf->chunk_size/512)
NeilBrownf4168852007-02-28 20:11:53 -08003571 *(new_data_disks) -1,
NeilBrown52c03292006-06-26 00:27:43 -07003572 raid_disks, data_disks,
3573 &dd_idx, &pd_idx, conf);
3574 if (last_sector >= (mddev->size<<1))
3575 last_sector = (mddev->size<<1)-1;
3576 while (first_sector <= last_sector) {
NeilBrownf4168852007-02-28 20:11:53 -08003577 pd_idx = stripe_to_pdidx(first_sector, conf,
3578 conf->previous_raid_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003579 sh = get_active_stripe(conf, first_sector,
3580 conf->previous_raid_disks, pd_idx, 0);
3581 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3582 set_bit(STRIPE_HANDLE, &sh->state);
3583 release_stripe(sh);
3584 first_sector += STRIPE_SECTORS;
3585 }
NeilBrownc6207272008-02-06 01:39:52 -08003586 /* If this takes us to the resync_max point where we have to pause,
3587 * then we need to write out the superblock.
3588 */
3589 sector_nr += conf->chunk_size>>9;
3590 if (sector_nr >= mddev->resync_max) {
3591 /* Cannot proceed until we've updated the superblock... */
3592 wait_event(conf->wait_for_overlap,
3593 atomic_read(&conf->reshape_stripes) == 0);
3594 mddev->reshape_position = conf->expand_progress;
3595 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3596 md_wakeup_thread(mddev->thread);
3597 wait_event(mddev->sb_wait,
3598 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3599 || kthread_should_stop());
3600 spin_lock_irq(&conf->device_lock);
3601 conf->expand_lo = mddev->reshape_position;
3602 spin_unlock_irq(&conf->device_lock);
3603 wake_up(&conf->wait_for_overlap);
3604 }
NeilBrown52c03292006-06-26 00:27:43 -07003605 return conf->chunk_size>>9;
3606}
3607
3608/* FIXME go_faster isn't used */
3609static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3610{
3611 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3612 struct stripe_head *sh;
3613 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07003615 sector_t max_sector = mddev->size << 1;
3616 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003617 int still_degraded = 0;
3618 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
NeilBrown72626682005-09-09 16:23:54 -07003620 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* just being told to finish up .. nothing much to do */
3622 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08003623 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3624 end_reshape(conf);
3625 return 0;
3626 }
NeilBrown72626682005-09-09 16:23:54 -07003627
3628 if (mddev->curr_resync < max_sector) /* aborted */
3629 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3630 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003631 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003632 conf->fullsync = 0;
3633 bitmap_close_sync(mddev->bitmap);
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 return 0;
3636 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003637
NeilBrown52c03292006-06-26 00:27:43 -07003638 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3639 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003640
NeilBrownc6207272008-02-06 01:39:52 -08003641 /* No need to check resync_max as we never do more than one
3642 * stripe, and as resync_max will always be on a chunk boundary,
3643 * if the check in md_do_sync didn't fire, there is no chance
3644 * of overstepping resync_max here
3645 */
3646
NeilBrown16a53ec2006-06-26 00:27:38 -07003647 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 * to resync, then assert that we are finished, because there is
3649 * nothing we can do.
3650 */
NeilBrown3285edf2006-06-26 00:27:55 -07003651 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003652 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07003653 sector_t rv = (mddev->size << 1) - sector_nr;
3654 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return rv;
3656 }
NeilBrown72626682005-09-09 16:23:54 -07003657 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003658 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003659 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3660 /* we can skip this block, and probably more */
3661 sync_blocks /= STRIPE_SECTORS;
3662 *skipped = 1;
3663 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
NeilBrownb47490c2008-02-06 01:39:50 -08003666
3667 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3668
NeilBrownccfcc3c2006-03-27 01:18:09 -08003669 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003670 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003672 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003674 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003676 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003678 /* Need to check if array will still be degraded after recovery/resync
3679 * We don't need to check the 'failed' flag as when that gets set,
3680 * recovery aborts.
3681 */
3682 for (i=0; i<mddev->raid_disks; i++)
3683 if (conf->disks[i].rdev == NULL)
3684 still_degraded = 1;
3685
3686 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3687
3688 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 set_bit(STRIPE_SYNCING, &sh->state);
3690 clear_bit(STRIPE_INSYNC, &sh->state);
3691 spin_unlock(&sh->lock);
3692
NeilBrown16a53ec2006-06-26 00:27:38 -07003693 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 release_stripe(sh);
3695
3696 return STRIPE_SECTORS;
3697}
3698
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003699static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3700{
3701 /* We may not be able to submit a whole bio at once as there
3702 * may not be enough stripe_heads available.
3703 * We cannot pre-allocate enough stripe_heads as we may need
3704 * more than exist in the cache (if we allow ever large chunks).
3705 * So we do one stripe head at a time and record in
3706 * ->bi_hw_segments how many have been done.
3707 *
3708 * We *know* that this entire raid_bio is in one chunk, so
3709 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3710 */
3711 struct stripe_head *sh;
3712 int dd_idx, pd_idx;
3713 sector_t sector, logical_sector, last_sector;
3714 int scnt = 0;
3715 int remaining;
3716 int handled = 0;
3717
3718 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3719 sector = raid5_compute_sector( logical_sector,
3720 conf->raid_disks,
3721 conf->raid_disks - conf->max_degraded,
3722 &dd_idx,
3723 &pd_idx,
3724 conf);
3725 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3726
3727 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08003728 logical_sector += STRIPE_SECTORS,
3729 sector += STRIPE_SECTORS,
3730 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003731
3732 if (scnt < raid_bio->bi_hw_segments)
3733 /* already done this stripe */
3734 continue;
3735
3736 sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
3737
3738 if (!sh) {
3739 /* failed to get a stripe - must wait */
3740 raid_bio->bi_hw_segments = scnt;
3741 conf->retry_read_aligned = raid_bio;
3742 return handled;
3743 }
3744
3745 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08003746 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
3747 release_stripe(sh);
3748 raid_bio->bi_hw_segments = scnt;
3749 conf->retry_read_aligned = raid_bio;
3750 return handled;
3751 }
3752
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003753 handle_stripe(sh, NULL);
3754 release_stripe(sh);
3755 handled++;
3756 }
3757 spin_lock_irq(&conf->device_lock);
3758 remaining = --raid_bio->bi_phys_segments;
3759 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10003760 if (remaining == 0)
3761 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003762 if (atomic_dec_and_test(&conf->active_aligned_reads))
3763 wake_up(&conf->wait_for_stripe);
3764 return handled;
3765}
3766
3767
3768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769/*
3770 * This is our raid5 kernel thread.
3771 *
3772 * We scan the hash table for stripes which can be handled now.
3773 * During the scan, completed stripes are saved for us by the interrupt
3774 * handler, so that they will not have to wait for our next wakeup.
3775 */
NeilBrown6ed30032008-02-06 01:40:00 -08003776static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 struct stripe_head *sh;
3779 raid5_conf_t *conf = mddev_to_conf(mddev);
3780 int handled;
3781
Dan Williams45b42332007-07-09 11:56:43 -07003782 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 handled = 0;
3787 spin_lock_irq(&conf->device_lock);
3788 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003789 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
NeilBrownae3c20c2006-07-10 04:44:17 -07003791 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07003792 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08003793 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003794 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08003795 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003796 conf->seq_write = seq;
3797 activate_bit_delay(conf);
3798 }
3799
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003800 while ((bio = remove_bio_from_retry(conf))) {
3801 int ok;
3802 spin_unlock_irq(&conf->device_lock);
3803 ok = retry_aligned_read(conf, bio);
3804 spin_lock_irq(&conf->device_lock);
3805 if (!ok)
3806 break;
3807 handled++;
3808 }
3809
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003810 sh = __get_priority_stripe(conf);
3811
3812 if (!sh) {
Dan Williamsd84e0f12007-01-02 13:52:30 -07003813 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 break;
Dan Williamsd84e0f12007-01-02 13:52:30 -07003815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 spin_unlock_irq(&conf->device_lock);
3817
3818 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003819 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 release_stripe(sh);
3821
3822 spin_lock_irq(&conf->device_lock);
3823 }
Dan Williams45b42332007-07-09 11:56:43 -07003824 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 spin_unlock_irq(&conf->device_lock);
3827
3828 unplug_slaves(mddev);
3829
Dan Williams45b42332007-07-09 11:56:43 -07003830 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
NeilBrown3f294f42005-11-08 21:39:25 -08003833static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003834raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003835{
NeilBrown007583c2005-11-08 21:39:30 -08003836 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003837 if (conf)
3838 return sprintf(page, "%d\n", conf->max_nr_stripes);
3839 else
3840 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003841}
3842
3843static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003844raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08003845{
NeilBrown007583c2005-11-08 21:39:30 -08003846 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07003847 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07003848 int err;
3849
NeilBrown3f294f42005-11-08 21:39:25 -08003850 if (len >= PAGE_SIZE)
3851 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08003852 if (!conf)
3853 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08003854
Dan Williams4ef197d82008-04-28 02:15:54 -07003855 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08003856 return -EINVAL;
3857 if (new <= 16 || new > 32768)
3858 return -EINVAL;
3859 while (new < conf->max_nr_stripes) {
3860 if (drop_one_stripe(conf))
3861 conf->max_nr_stripes--;
3862 else
3863 break;
3864 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07003865 err = md_allow_write(mddev);
3866 if (err)
3867 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08003868 while (new > conf->max_nr_stripes) {
3869 if (grow_one_stripe(conf))
3870 conf->max_nr_stripes++;
3871 else break;
3872 }
3873 return len;
3874}
NeilBrown007583c2005-11-08 21:39:30 -08003875
NeilBrown96de1e62005-11-08 21:39:39 -08003876static struct md_sysfs_entry
3877raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3878 raid5_show_stripe_cache_size,
3879 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003880
3881static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003882raid5_show_preread_threshold(mddev_t *mddev, char *page)
3883{
3884 raid5_conf_t *conf = mddev_to_conf(mddev);
3885 if (conf)
3886 return sprintf(page, "%d\n", conf->bypass_threshold);
3887 else
3888 return 0;
3889}
3890
3891static ssize_t
3892raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
3893{
3894 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07003895 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003896 if (len >= PAGE_SIZE)
3897 return -EINVAL;
3898 if (!conf)
3899 return -ENODEV;
3900
Dan Williams4ef197d82008-04-28 02:15:54 -07003901 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003902 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07003903 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003904 return -EINVAL;
3905 conf->bypass_threshold = new;
3906 return len;
3907}
3908
3909static struct md_sysfs_entry
3910raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
3911 S_IRUGO | S_IWUSR,
3912 raid5_show_preread_threshold,
3913 raid5_store_preread_threshold);
3914
3915static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003916stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003917{
NeilBrown007583c2005-11-08 21:39:30 -08003918 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003919 if (conf)
3920 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3921 else
3922 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003923}
3924
NeilBrown96de1e62005-11-08 21:39:39 -08003925static struct md_sysfs_entry
3926raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003927
NeilBrown007583c2005-11-08 21:39:30 -08003928static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003929 &raid5_stripecache_size.attr,
3930 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003931 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08003932 NULL,
3933};
NeilBrown007583c2005-11-08 21:39:30 -08003934static struct attribute_group raid5_attrs_group = {
3935 .name = NULL,
3936 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003937};
3938
NeilBrown72626682005-09-09 16:23:54 -07003939static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
3941 raid5_conf_t *conf;
3942 int raid_disk, memory;
3943 mdk_rdev_t *rdev;
3944 struct disk_info *disk;
3945 struct list_head *tmp;
NeilBrown02c2de82006-10-03 01:15:47 -07003946 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
NeilBrown16a53ec2006-06-26 00:27:38 -07003948 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3949 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003950 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 return -EIO;
3952 }
3953
NeilBrownf6705572006-03-27 01:18:11 -08003954 if (mddev->reshape_position != MaxSector) {
3955 /* Check that we can continue the reshape.
3956 * Currently only disks can change, it must
3957 * increase, and we must be past the point where
3958 * a stripe over-writes itself
3959 */
3960 sector_t here_new, here_old;
3961 int old_disks;
NeilBrownf4168852007-02-28 20:11:53 -08003962 int max_degraded = (mddev->level == 5 ? 1 : 2);
NeilBrownf6705572006-03-27 01:18:11 -08003963
3964 if (mddev->new_level != mddev->level ||
3965 mddev->new_layout != mddev->layout ||
3966 mddev->new_chunk != mddev->chunk_size) {
NeilBrownf4168852007-02-28 20:11:53 -08003967 printk(KERN_ERR "raid5: %s: unsupported reshape "
3968 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08003969 mdname(mddev));
3970 return -EINVAL;
3971 }
3972 if (mddev->delta_disks <= 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003973 printk(KERN_ERR "raid5: %s: unsupported reshape "
3974 "(reduce disks) required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08003975 mdname(mddev));
3976 return -EINVAL;
3977 }
3978 old_disks = mddev->raid_disks - mddev->delta_disks;
3979 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08003980 * further up in new geometry must map after here in old
3981 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08003982 */
3983 here_new = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08003984 if (sector_div(here_new, (mddev->chunk_size>>9)*
3985 (mddev->raid_disks - max_degraded))) {
3986 printk(KERN_ERR "raid5: reshape_position not "
3987 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08003988 return -EINVAL;
3989 }
3990 /* here_new is the stripe we will write to */
3991 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08003992 sector_div(here_old, (mddev->chunk_size>>9)*
3993 (old_disks-max_degraded));
3994 /* here_old is the first stripe that we might need to read
3995 * from */
NeilBrownf6705572006-03-27 01:18:11 -08003996 if (here_new >= here_old) {
3997 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08003998 printk(KERN_ERR "raid5: reshape_position too early for "
3999 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004000 return -EINVAL;
4001 }
4002 printk(KERN_INFO "raid5: reshape will continue\n");
4003 /* OK, we should be able to continue; */
4004 }
4005
4006
NeilBrownb55e6bf2006-03-27 01:18:06 -08004007 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 if ((conf = mddev->private) == NULL)
4009 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08004010 if (mddev->reshape_position == MaxSector) {
4011 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
4012 } else {
4013 conf->raid_disks = mddev->raid_disks;
4014 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4015 }
4016
4017 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08004018 GFP_KERNEL);
4019 if (!conf->disks)
4020 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08004021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 conf->mddev = mddev;
4023
NeilBrownfccddba2006-01-06 00:20:33 -08004024 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
NeilBrown16a53ec2006-06-26 00:27:38 -07004027 if (mddev->level == 6) {
4028 conf->spare_page = alloc_page(GFP_KERNEL);
4029 if (!conf->spare_page)
4030 goto abort;
4031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 spin_lock_init(&conf->device_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -07004033 mddev->queue->queue_lock = &conf->device_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 init_waitqueue_head(&conf->wait_for_stripe);
4035 init_waitqueue_head(&conf->wait_for_overlap);
4036 INIT_LIST_HEAD(&conf->handle_list);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004037 INIT_LIST_HEAD(&conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07004039 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 INIT_LIST_HEAD(&conf->inactive_list);
4041 atomic_set(&conf->active_stripes, 0);
4042 atomic_set(&conf->preread_active_stripes, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004043 atomic_set(&conf->active_aligned_reads, 0);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004044 conf->bypass_threshold = BYPASS_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Dan Williams45b42332007-07-09 11:56:43 -07004046 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
NeilBrownd089c6a2008-02-06 01:39:59 -08004048 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08004050 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 || raid_disk < 0)
4052 continue;
4053 disk = conf->disks + raid_disk;
4054
4055 disk->rdev = rdev;
4056
NeilBrownb2d444d2005-11-08 21:39:31 -08004057 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 char b[BDEVNAME_SIZE];
4059 printk(KERN_INFO "raid5: device %s operational as raid"
4060 " disk %d\n", bdevname(rdev->bdev,b),
4061 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07004062 working_disks++;
Neil Brown8c2e8702008-06-28 08:30:52 +10004063 } else
4064 /* Cannot rely on bitmap to complete recovery */
4065 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
4067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004069 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 */
NeilBrown02c2de82006-10-03 01:15:47 -07004071 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 conf->mddev = mddev;
4073 conf->chunk_size = mddev->chunk_size;
4074 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07004075 if (conf->level == 6)
4076 conf->max_degraded = 2;
4077 else
4078 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 conf->algorithm = mddev->layout;
4080 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08004081 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083 /* device size must be a multiple of chunk size */
4084 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07004085 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
NeilBrown16a53ec2006-06-26 00:27:38 -07004087 if (conf->level == 6 && conf->raid_disks < 4) {
4088 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4089 mdname(mddev), conf->raid_disks);
4090 goto abort;
4091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (!conf->chunk_size || conf->chunk_size % 4) {
4093 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4094 conf->chunk_size, mdname(mddev));
4095 goto abort;
4096 }
4097 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
4098 printk(KERN_ERR
4099 "raid5: unsupported parity algorithm %d for %s\n",
4100 conf->algorithm, mdname(mddev));
4101 goto abort;
4102 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004103 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 printk(KERN_ERR "raid5: not enough operational devices for %s"
4105 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004106 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 goto abort;
4108 }
4109
NeilBrown16a53ec2006-06-26 00:27:38 -07004110 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004112 if (mddev->ok_start_degraded)
4113 printk(KERN_WARNING
4114 "raid5: starting dirty degraded array: %s"
4115 "- data corruption possible.\n",
4116 mdname(mddev));
4117 else {
4118 printk(KERN_ERR
4119 "raid5: cannot start dirty degraded array for %s\n",
4120 mdname(mddev));
4121 goto abort;
4122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
4125 {
4126 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4127 if (!mddev->thread) {
4128 printk(KERN_ERR
4129 "raid5: couldn't allocate thread for %s\n",
4130 mdname(mddev));
4131 goto abort;
4132 }
4133 }
NeilBrown50368052005-12-12 02:39:17 -08004134 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4136 if (grow_stripes(conf, conf->max_nr_stripes)) {
4137 printk(KERN_ERR
4138 "raid5: couldn't allocate %dkB for buffers\n", memory);
4139 shrink_stripes(conf);
4140 md_unregister_thread(mddev->thread);
4141 goto abort;
4142 } else
4143 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4144 memory, mdname(mddev));
4145
4146 if (mddev->degraded == 0)
4147 printk("raid5: raid level %d set %s active with %d out of %d"
4148 " devices, algorithm %d\n", conf->level, mdname(mddev),
4149 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4150 conf->algorithm);
4151 else
4152 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4153 " out of %d devices, algorithm %d\n", conf->level,
4154 mdname(mddev), mddev->raid_disks - mddev->degraded,
4155 mddev->raid_disks, conf->algorithm);
4156
4157 print_raid5_conf(conf);
4158
NeilBrownf6705572006-03-27 01:18:11 -08004159 if (conf->expand_progress != MaxSector) {
4160 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08004161 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004162 atomic_set(&conf->reshape_stripes, 0);
4163 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4164 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4165 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4166 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4167 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4168 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004169 }
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004172 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 */
4174 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004175 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4176 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004177 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4179 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4180 }
4181
4182 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004183 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4184 printk(KERN_WARNING
4185 "raid5: failed to create sysfs attributes for %s\n",
4186 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004187
4188 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004189 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004190 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004191
Andre Nollf233ea52008-07-21 17:05:22 +10004192 mddev->array_sectors = 2 * mddev->size * (conf->previous_raid_disks -
NeilBrown16a53ec2006-06-26 00:27:38 -07004193 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07004194
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004195 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 return 0;
4198abort:
4199 if (conf) {
4200 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07004201 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004202 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08004203 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 kfree(conf);
4205 }
4206 mddev->private = NULL;
4207 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4208 return -EIO;
4209}
4210
4211
4212
NeilBrown3f294f42005-11-08 21:39:25 -08004213static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4216
4217 md_unregister_thread(mddev->thread);
4218 mddev->thread = NULL;
4219 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08004220 kfree(conf->stripe_hashtbl);
NeilBrown041ae522007-03-26 21:32:14 -08004221 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004223 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004224 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08004225 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 mddev->private = NULL;
4227 return 0;
4228}
4229
Dan Williams45b42332007-07-09 11:56:43 -07004230#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004231static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
4233 int i;
4234
NeilBrown16a53ec2006-06-26 00:27:38 -07004235 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4236 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4237 seq_printf(seq, "sh %llu, count %d.\n",
4238 (unsigned long long)sh->sector, atomic_read(&sh->count));
4239 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004240 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004241 seq_printf(seq, "(cache%d: %p %ld) ",
4242 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004244 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
NeilBrown16a53ec2006-06-26 00:27:38 -07004247static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004250 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 int i;
4252
4253 spin_lock_irq(&conf->device_lock);
4254 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004255 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if (sh->raid_conf != conf)
4257 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004258 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
4260 }
4261 spin_unlock_irq(&conf->device_lock);
4262}
4263#endif
4264
4265static void status (struct seq_file *seq, mddev_t *mddev)
4266{
4267 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4268 int i;
4269
4270 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004271 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 for (i = 0; i < conf->raid_disks; i++)
4273 seq_printf (seq, "%s",
4274 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004275 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004277#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004278 seq_printf (seq, "\n");
4279 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280#endif
4281}
4282
4283static void print_raid5_conf (raid5_conf_t *conf)
4284{
4285 int i;
4286 struct disk_info *tmp;
4287
4288 printk("RAID5 conf printout:\n");
4289 if (!conf) {
4290 printk("(conf==NULL)\n");
4291 return;
4292 }
NeilBrown02c2de82006-10-03 01:15:47 -07004293 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4294 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 for (i = 0; i < conf->raid_disks; i++) {
4297 char b[BDEVNAME_SIZE];
4298 tmp = conf->disks + i;
4299 if (tmp->rdev)
4300 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004301 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 bdevname(tmp->rdev->bdev,b));
4303 }
4304}
4305
4306static int raid5_spare_active(mddev_t *mddev)
4307{
4308 int i;
4309 raid5_conf_t *conf = mddev->private;
4310 struct disk_info *tmp;
4311
4312 for (i = 0; i < conf->raid_disks; i++) {
4313 tmp = conf->disks + i;
4314 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004315 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004316 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4317 unsigned long flags;
4318 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004320 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
4322 }
4323 print_raid5_conf(conf);
4324 return 0;
4325}
4326
4327static int raid5_remove_disk(mddev_t *mddev, int number)
4328{
4329 raid5_conf_t *conf = mddev->private;
4330 int err = 0;
4331 mdk_rdev_t *rdev;
4332 struct disk_info *p = conf->disks + number;
4333
4334 print_raid5_conf(conf);
4335 rdev = p->rdev;
4336 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004337 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 atomic_read(&rdev->nr_pending)) {
4339 err = -EBUSY;
4340 goto abort;
4341 }
NeilBrowndfc70642008-05-23 13:04:39 -07004342 /* Only remove non-faulty devices if recovery
4343 * isn't possible.
4344 */
4345 if (!test_bit(Faulty, &rdev->flags) &&
4346 mddev->degraded <= conf->max_degraded) {
4347 err = -EBUSY;
4348 goto abort;
4349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004351 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 if (atomic_read(&rdev->nr_pending)) {
4353 /* lost the race, try later */
4354 err = -EBUSY;
4355 p->rdev = rdev;
4356 }
4357 }
4358abort:
4359
4360 print_raid5_conf(conf);
4361 return err;
4362}
4363
4364static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4365{
4366 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004367 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int disk;
4369 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004370 int first = 0;
4371 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
Neil Brown6c2fce22008-06-28 08:31:31 +10004377 if (rdev->raid_disk >= 0)
4378 first = last = rdev->raid_disk;
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004381 * find the disk ... but prefer rdev->saved_raid_disk
4382 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004384 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004385 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004386 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4387 disk = rdev->saved_raid_disk;
4388 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004389 disk = first;
4390 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004392 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004394 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004395 if (rdev->saved_raid_disk != disk)
4396 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004397 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 break;
4399 }
4400 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
4404static int raid5_resize(mddev_t *mddev, sector_t sectors)
4405{
4406 /* no resync is happening, and there is enough space
4407 * on all devices, so we can resize.
4408 * We need to make sure resync covers any new space.
4409 * If the array is shrinking we should possibly wait until
4410 * any io in the removed space completes, but it hardly seems
4411 * worth it.
4412 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004413 raid5_conf_t *conf = mddev_to_conf(mddev);
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Andre Nollf233ea52008-07-21 17:05:22 +10004416 mddev->array_sectors = sectors * (mddev->raid_disks
4417 - conf->max_degraded);
4418 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004419 mddev->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
4421 mddev->recovery_cp = mddev->size << 1;
4422 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4423 }
4424 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07004425 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return 0;
4427}
4428
NeilBrown29269552006-03-27 01:18:10 -08004429#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004430static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08004431{
4432 raid5_conf_t *conf = mddev_to_conf(mddev);
4433 int err;
NeilBrown29269552006-03-27 01:18:10 -08004434
NeilBrown63c70c42006-03-27 01:18:13 -08004435 if (mddev->delta_disks < 0 ||
4436 mddev->new_level != mddev->level)
4437 return -EINVAL; /* Cannot shrink array or change level yet */
4438 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08004439 return 0; /* nothing to do */
4440
4441 /* Can only proceed if there are plenty of stripe_heads.
4442 * We need a minimum of one full stripe,, and for sensible progress
4443 * it is best to have about 4 times that.
4444 * If we require 4 times, then the default 256 4K stripe_heads will
4445 * allow for chunk sizes up to 256K, which is probably OK.
4446 * If the chunk size is greater, user-space should request more
4447 * stripe_heads first.
4448 */
NeilBrown63c70c42006-03-27 01:18:13 -08004449 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4450 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08004451 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
4452 (mddev->chunk_size / STRIPE_SIZE)*4);
4453 return -ENOSPC;
4454 }
4455
NeilBrown63c70c42006-03-27 01:18:13 -08004456 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
4457 if (err)
4458 return err;
4459
NeilBrownb4c4c7b2007-02-28 20:11:48 -08004460 if (mddev->degraded > conf->max_degraded)
4461 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004462 /* looks like we might be able to manage this */
4463 return 0;
4464}
4465
4466static int raid5_start_reshape(mddev_t *mddev)
4467{
4468 raid5_conf_t *conf = mddev_to_conf(mddev);
4469 mdk_rdev_t *rdev;
4470 struct list_head *rtmp;
4471 int spares = 0;
4472 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07004473 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08004474
NeilBrownf4168852007-02-28 20:11:53 -08004475 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08004476 return -EBUSY;
4477
NeilBrownd089c6a2008-02-06 01:39:59 -08004478 rdev_for_each(rdev, rtmp, mddev)
NeilBrown29269552006-03-27 01:18:10 -08004479 if (rdev->raid_disk < 0 &&
4480 !test_bit(Faulty, &rdev->flags))
4481 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08004482
NeilBrownf4168852007-02-28 20:11:53 -08004483 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08004484 /* Not enough devices even to make a degraded array
4485 * of that size
4486 */
4487 return -EINVAL;
4488
NeilBrownf6705572006-03-27 01:18:11 -08004489 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08004490 spin_lock_irq(&conf->device_lock);
4491 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08004492 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08004493 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004494 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08004495 spin_unlock_irq(&conf->device_lock);
4496
4497 /* Add some new drives, as many as will fit.
4498 * We know there are enough to make the newly sized array work.
4499 */
NeilBrownd089c6a2008-02-06 01:39:59 -08004500 rdev_for_each(rdev, rtmp, mddev)
NeilBrown29269552006-03-27 01:18:10 -08004501 if (rdev->raid_disk < 0 &&
4502 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10004503 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08004504 char nm[20];
4505 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08004506 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004507 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08004508 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004509 if (sysfs_create_link(&mddev->kobj,
4510 &rdev->kobj, nm))
4511 printk(KERN_WARNING
4512 "raid5: failed to create "
4513 " link %s for %s\n",
4514 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08004515 } else
4516 break;
4517 }
4518
NeilBrownc04be0a2006-10-03 01:15:53 -07004519 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004520 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07004521 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004522 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08004523 mddev->reshape_position = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004524 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08004525
NeilBrown29269552006-03-27 01:18:10 -08004526 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4527 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4528 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4529 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4530 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4531 "%s_reshape");
4532 if (!mddev->sync_thread) {
4533 mddev->recovery = 0;
4534 spin_lock_irq(&conf->device_lock);
4535 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
4536 conf->expand_progress = MaxSector;
4537 spin_unlock_irq(&conf->device_lock);
4538 return -EAGAIN;
4539 }
4540 md_wakeup_thread(mddev->sync_thread);
4541 md_new_event(mddev);
4542 return 0;
4543}
4544#endif
4545
4546static void end_reshape(raid5_conf_t *conf)
4547{
4548 struct block_device *bdev;
4549
NeilBrownf6705572006-03-27 01:18:11 -08004550 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Andre Nollf233ea52008-07-21 17:05:22 +10004551 conf->mddev->array_sectors = 2 * conf->mddev->size *
NeilBrownf4168852007-02-28 20:11:53 -08004552 (conf->raid_disks - conf->max_degraded);
Andre Nollf233ea52008-07-21 17:05:22 +10004553 set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004554 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08004555
NeilBrownf6705572006-03-27 01:18:11 -08004556 bdev = bdget_disk(conf->mddev->gendisk, 0);
4557 if (bdev) {
4558 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004559 i_size_write(bdev->bd_inode,
4560 (loff_t)conf->mddev->array_sectors << 9);
NeilBrownf6705572006-03-27 01:18:11 -08004561 mutex_unlock(&bdev->bd_inode->i_mutex);
4562 bdput(bdev);
4563 }
4564 spin_lock_irq(&conf->device_lock);
4565 conf->expand_progress = MaxSector;
4566 spin_unlock_irq(&conf->device_lock);
4567 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07004568
4569 /* read-ahead size must cover two whole stripes, which is
4570 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4571 */
4572 {
4573 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4574 int stripe = data_disks *
4575 (conf->mddev->chunk_size / PAGE_SIZE);
4576 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4577 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4578 }
NeilBrown29269552006-03-27 01:18:10 -08004579 }
NeilBrown29269552006-03-27 01:18:10 -08004580}
4581
NeilBrown72626682005-09-09 16:23:54 -07004582static void raid5_quiesce(mddev_t *mddev, int state)
4583{
4584 raid5_conf_t *conf = mddev_to_conf(mddev);
4585
4586 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08004587 case 2: /* resume for a suspend */
4588 wake_up(&conf->wait_for_overlap);
4589 break;
4590
NeilBrown72626682005-09-09 16:23:54 -07004591 case 1: /* stop all writes */
4592 spin_lock_irq(&conf->device_lock);
4593 conf->quiesce = 1;
4594 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004595 atomic_read(&conf->active_stripes) == 0 &&
4596 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07004597 conf->device_lock, /* nothing */);
4598 spin_unlock_irq(&conf->device_lock);
4599 break;
4600
4601 case 0: /* re-enable writes */
4602 spin_lock_irq(&conf->device_lock);
4603 conf->quiesce = 0;
4604 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08004605 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07004606 spin_unlock_irq(&conf->device_lock);
4607 break;
4608 }
NeilBrown72626682005-09-09 16:23:54 -07004609}
NeilBrownb15c2e52006-01-06 00:20:16 -08004610
NeilBrown16a53ec2006-06-26 00:27:38 -07004611static struct mdk_personality raid6_personality =
4612{
4613 .name = "raid6",
4614 .level = 6,
4615 .owner = THIS_MODULE,
4616 .make_request = make_request,
4617 .run = run,
4618 .stop = stop,
4619 .status = status,
4620 .error_handler = error,
4621 .hot_add_disk = raid5_add_disk,
4622 .hot_remove_disk= raid5_remove_disk,
4623 .spare_active = raid5_spare_active,
4624 .sync_request = sync_request,
4625 .resize = raid5_resize,
NeilBrownf4168852007-02-28 20:11:53 -08004626#ifdef CONFIG_MD_RAID5_RESHAPE
4627 .check_reshape = raid5_check_reshape,
4628 .start_reshape = raid5_start_reshape,
4629#endif
NeilBrown16a53ec2006-06-26 00:27:38 -07004630 .quiesce = raid5_quiesce,
4631};
NeilBrown2604b702006-01-06 00:20:36 -08004632static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
4634 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08004635 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 .owner = THIS_MODULE,
4637 .make_request = make_request,
4638 .run = run,
4639 .stop = stop,
4640 .status = status,
4641 .error_handler = error,
4642 .hot_add_disk = raid5_add_disk,
4643 .hot_remove_disk= raid5_remove_disk,
4644 .spare_active = raid5_spare_active,
4645 .sync_request = sync_request,
4646 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08004647#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004648 .check_reshape = raid5_check_reshape,
4649 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08004650#endif
NeilBrown72626682005-09-09 16:23:54 -07004651 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652};
4653
NeilBrown2604b702006-01-06 00:20:36 -08004654static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
NeilBrown2604b702006-01-06 00:20:36 -08004656 .name = "raid4",
4657 .level = 4,
4658 .owner = THIS_MODULE,
4659 .make_request = make_request,
4660 .run = run,
4661 .stop = stop,
4662 .status = status,
4663 .error_handler = error,
4664 .hot_add_disk = raid5_add_disk,
4665 .hot_remove_disk= raid5_remove_disk,
4666 .spare_active = raid5_spare_active,
4667 .sync_request = sync_request,
4668 .resize = raid5_resize,
NeilBrown3d378902007-03-26 21:32:13 -08004669#ifdef CONFIG_MD_RAID5_RESHAPE
4670 .check_reshape = raid5_check_reshape,
4671 .start_reshape = raid5_start_reshape,
4672#endif
NeilBrown2604b702006-01-06 00:20:36 -08004673 .quiesce = raid5_quiesce,
4674};
4675
4676static int __init raid5_init(void)
4677{
NeilBrown16a53ec2006-06-26 00:27:38 -07004678 int e;
4679
4680 e = raid6_select_algo();
4681 if ( e )
4682 return e;
4683 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004684 register_md_personality(&raid5_personality);
4685 register_md_personality(&raid4_personality);
4686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
NeilBrown2604b702006-01-06 00:20:36 -08004689static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
NeilBrown16a53ec2006-06-26 00:27:38 -07004691 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004692 unregister_md_personality(&raid5_personality);
4693 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696module_init(raid5_init);
4697module_exit(raid5_exit);
4698MODULE_LICENSE("GPL");
4699MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004700MODULE_ALIAS("md-raid5");
4701MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08004702MODULE_ALIAS("md-level-5");
4703MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07004704MODULE_ALIAS("md-personality-8"); /* RAID6 */
4705MODULE_ALIAS("md-raid6");
4706MODULE_ALIAS("md-level-6");
4707
4708/* This used to be two separate modules, they were: */
4709MODULE_ALIAS("raid5");
4710MODULE_ALIAS("raid6");