blob: b86ceba04f6f05f6ed44488238801c55f69e2c5b [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>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Stripe cache
58 */
59
60#define NR_STRIPES 256
61#define STRIPE_SIZE PAGE_SIZE
62#define STRIPE_SHIFT (PAGE_SHIFT - 9)
63#define STRIPE_SECTORS (STRIPE_SIZE>>9)
64#define IO_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080065#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define HASH_MASK (NR_HASH - 1)
67
NeilBrownfccddba2006-01-06 00:20:33 -080068#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
75 * be valid.
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
78 */
79#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
80/*
81 * The following can be used to debug the driver
82 */
83#define RAID5_DEBUG 0
84#define RAID5_PARANOIA 1
85#if RAID5_PARANOIA && defined(CONFIG_SMP)
86# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87#else
88# define CHECK_DEVLOCK()
89#endif
90
91#define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
92#if RAID5_DEBUG
93#define inline
94#define __inline__
95#endif
96
NeilBrown16a53ec2006-06-26 00:27:38 -070097#if !RAID6_USE_EMPTY_ZERO_PAGE
98/* In .bss so it's zeroed */
99const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
100#endif
101
102static inline int raid6_next_disk(int disk, int raid_disks)
103{
104 disk++;
105 return (disk < raid_disks) ? disk : 0;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static void print_raid5_conf (raid5_conf_t *conf);
108
Arjan van de Ven858119e2006-01-14 13:20:43 -0800109static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200112 BUG_ON(!list_empty(&sh->lru));
113 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700115 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700117 blk_plug_device(conf->mddev->queue);
118 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700119 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700120 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700121 blk_plug_device(conf->mddev->queue);
122 } else {
NeilBrown72626682005-09-09 16:23:54 -0700123 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 md_wakeup_thread(conf->mddev->thread);
127 } else {
128 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
129 atomic_dec(&conf->preread_active_stripes);
130 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
131 md_wakeup_thread(conf->mddev->thread);
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800134 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
135 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 wake_up(&conf->wait_for_stripe);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140}
141static void release_stripe(struct stripe_head *sh)
142{
143 raid5_conf_t *conf = sh->raid_conf;
144 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 spin_lock_irqsave(&conf->device_lock, flags);
147 __release_stripe(conf, sh);
148 spin_unlock_irqrestore(&conf->device_lock, flags);
149}
150
NeilBrownfccddba2006-01-06 00:20:33 -0800151static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
154
NeilBrownfccddba2006-01-06 00:20:33 -0800155 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
NeilBrown16a53ec2006-06-26 00:27:38 -0700158static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
NeilBrownfccddba2006-01-06 00:20:33 -0800160 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
163
164 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800165 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168
169/* find an idle stripe, make sure it is unhashed, and return it. */
170static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
171{
172 struct stripe_head *sh = NULL;
173 struct list_head *first;
174
175 CHECK_DEVLOCK();
176 if (list_empty(&conf->inactive_list))
177 goto out;
178 first = conf->inactive_list.next;
179 sh = list_entry(first, struct stripe_head, lru);
180 list_del_init(first);
181 remove_hash(sh);
182 atomic_inc(&conf->active_stripes);
183out:
184 return sh;
185}
186
187static void shrink_buffers(struct stripe_head *sh, int num)
188{
189 struct page *p;
190 int i;
191
192 for (i=0; i<num ; i++) {
193 p = sh->dev[i].page;
194 if (!p)
195 continue;
196 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800197 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
201static int grow_buffers(struct stripe_head *sh, int num)
202{
203 int i;
204
205 for (i=0; i<num; i++) {
206 struct page *page;
207
208 if (!(page = alloc_page(GFP_KERNEL))) {
209 return 1;
210 }
211 sh->dev[i].page = page;
212 }
213 return 0;
214}
215
216static void raid5_build_block (struct stripe_head *sh, int i);
217
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800218static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200223 BUG_ON(atomic_read(&sh->count) != 0);
224 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 CHECK_DEVLOCK();
227 PRINTK("init_stripe called, stripe %llu\n",
228 (unsigned long long)sh->sector);
229
230 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sh->sector = sector;
233 sh->pd_idx = pd_idx;
234 sh->state = 0;
235
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800236 sh->disks = disks;
237
238 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct r5dev *dev = &sh->dev[i];
240
241 if (dev->toread || dev->towrite || dev->written ||
242 test_bit(R5_LOCKED, &dev->flags)) {
243 printk("sector=%llx i=%d %p %p %p %d\n",
244 (unsigned long long)sh->sector, i, dev->toread,
245 dev->towrite, dev->written,
246 test_bit(R5_LOCKED, &dev->flags));
247 BUG();
248 }
249 dev->flags = 0;
250 raid5_build_block(sh, i);
251 }
252 insert_hash(conf, sh);
253}
254
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800255static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800258 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 CHECK_DEVLOCK();
261 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800262 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800263 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return sh;
265 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
266 return NULL;
267}
268
269static void unplug_slaves(mddev_t *mddev);
270static void raid5_unplug_device(request_queue_t *q);
271
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800272static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
273 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct stripe_head *sh;
276
277 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
278
279 spin_lock_irq(&conf->device_lock);
280
281 do {
NeilBrown72626682005-09-09 16:23:54 -0700282 wait_event_lock_irq(conf->wait_for_stripe,
283 conf->quiesce == 0,
284 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800285 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!sh) {
287 if (!conf->inactive_blocked)
288 sh = get_free_stripe(conf);
289 if (noblock && sh == NULL)
290 break;
291 if (!sh) {
292 conf->inactive_blocked = 1;
293 wait_event_lock_irq(conf->wait_for_stripe,
294 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800295 (atomic_read(&conf->active_stripes)
296 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 || !conf->inactive_blocked),
298 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700299 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 );
301 conf->inactive_blocked = 0;
302 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800303 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else {
305 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200306 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 } else {
308 if (!test_bit(STRIPE_HANDLE, &sh->state))
309 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700310 if (list_empty(&sh->lru) &&
311 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700312 BUG();
313 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 }
316 } while (sh == NULL);
317
318 if (sh)
319 atomic_inc(&sh->count);
320
321 spin_unlock_irq(&conf->device_lock);
322 return sh;
323}
324
NeilBrown3f294f42005-11-08 21:39:25 -0800325static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800328 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
329 if (!sh)
330 return 0;
331 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
332 sh->raid_conf = conf;
333 spin_lock_init(&sh->lock);
334
335 if (grow_buffers(sh, conf->raid_disks)) {
336 shrink_buffers(sh, conf->raid_disks);
337 kmem_cache_free(conf->slab_cache, sh);
338 return 0;
339 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800340 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800341 /* we just created an active stripe so... */
342 atomic_set(&sh->count, 1);
343 atomic_inc(&conf->active_stripes);
344 INIT_LIST_HEAD(&sh->lru);
345 release_stripe(sh);
346 return 1;
347}
348
349static int grow_stripes(raid5_conf_t *conf, int num)
350{
Christoph Lametere18b8902006-12-06 20:33:20 -0800351 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int devs = conf->raid_disks;
353
NeilBrownad01c9e2006-03-27 01:18:07 -0800354 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
355 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
356 conf->active_name = 0;
357 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
359 0, 0, NULL, NULL);
360 if (!sc)
361 return 1;
362 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800363 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700364 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800365 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
NeilBrown29269552006-03-27 01:18:10 -0800369
370#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800371static int resize_stripes(raid5_conf_t *conf, int newsize)
372{
373 /* Make all the stripes able to hold 'newsize' devices.
374 * New slots in each stripe get 'page' set to a new page.
375 *
376 * This happens in stages:
377 * 1/ create a new kmem_cache and allocate the required number of
378 * stripe_heads.
379 * 2/ gather all the old stripe_heads and tranfer the pages across
380 * to the new stripe_heads. This will have the side effect of
381 * freezing the array as once all stripe_heads have been collected,
382 * no IO will be possible. Old stripe heads are freed once their
383 * pages have been transferred over, and the old kmem_cache is
384 * freed when all stripes are done.
385 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
386 * we simple return a failre status - no need to clean anything up.
387 * 4/ allocate new pages for the new slots in the new stripe_heads.
388 * If this fails, we don't bother trying the shrink the
389 * stripe_heads down again, we just leave them as they are.
390 * As each stripe_head is processed the new one is released into
391 * active service.
392 *
393 * Once step2 is started, we cannot afford to wait for a write,
394 * so we use GFP_NOIO allocations.
395 */
396 struct stripe_head *osh, *nsh;
397 LIST_HEAD(newstripes);
398 struct disk_info *ndisks;
399 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -0800400 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800401 int i;
402
403 if (newsize <= conf->pool_size)
404 return 0; /* never bother to shrink */
405
406 /* Step 1 */
407 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
408 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
409 0, 0, NULL, NULL);
410 if (!sc)
411 return -ENOMEM;
412
413 for (i = conf->max_nr_stripes; i; i--) {
414 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
415 if (!nsh)
416 break;
417
418 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
419
420 nsh->raid_conf = conf;
421 spin_lock_init(&nsh->lock);
422
423 list_add(&nsh->lru, &newstripes);
424 }
425 if (i) {
426 /* didn't get enough, give up */
427 while (!list_empty(&newstripes)) {
428 nsh = list_entry(newstripes.next, struct stripe_head, lru);
429 list_del(&nsh->lru);
430 kmem_cache_free(sc, nsh);
431 }
432 kmem_cache_destroy(sc);
433 return -ENOMEM;
434 }
435 /* Step 2 - Must use GFP_NOIO now.
436 * OK, we have enough stripes, start collecting inactive
437 * stripes and copying them over
438 */
439 list_for_each_entry(nsh, &newstripes, lru) {
440 spin_lock_irq(&conf->device_lock);
441 wait_event_lock_irq(conf->wait_for_stripe,
442 !list_empty(&conf->inactive_list),
443 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800444 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800445 );
446 osh = get_free_stripe(conf);
447 spin_unlock_irq(&conf->device_lock);
448 atomic_set(&nsh->count, 1);
449 for(i=0; i<conf->pool_size; i++)
450 nsh->dev[i].page = osh->dev[i].page;
451 for( ; i<newsize; i++)
452 nsh->dev[i].page = NULL;
453 kmem_cache_free(conf->slab_cache, osh);
454 }
455 kmem_cache_destroy(conf->slab_cache);
456
457 /* Step 3.
458 * At this point, we are holding all the stripes so the array
459 * is completely stalled, so now is a good time to resize
460 * conf->disks.
461 */
462 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
463 if (ndisks) {
464 for (i=0; i<conf->raid_disks; i++)
465 ndisks[i] = conf->disks[i];
466 kfree(conf->disks);
467 conf->disks = ndisks;
468 } else
469 err = -ENOMEM;
470
471 /* Step 4, return new stripes to service */
472 while(!list_empty(&newstripes)) {
473 nsh = list_entry(newstripes.next, struct stripe_head, lru);
474 list_del_init(&nsh->lru);
475 for (i=conf->raid_disks; i < newsize; i++)
476 if (nsh->dev[i].page == NULL) {
477 struct page *p = alloc_page(GFP_NOIO);
478 nsh->dev[i].page = p;
479 if (!p)
480 err = -ENOMEM;
481 }
482 release_stripe(nsh);
483 }
484 /* critical section pass, GFP_NOIO no longer needed */
485
486 conf->slab_cache = sc;
487 conf->active_name = 1-conf->active_name;
488 conf->pool_size = newsize;
489 return err;
490}
NeilBrown29269552006-03-27 01:18:10 -0800491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
NeilBrown3f294f42005-11-08 21:39:25 -0800493static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct stripe_head *sh;
496
NeilBrown3f294f42005-11-08 21:39:25 -0800497 spin_lock_irq(&conf->device_lock);
498 sh = get_free_stripe(conf);
499 spin_unlock_irq(&conf->device_lock);
500 if (!sh)
501 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200502 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -0800503 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800504 kmem_cache_free(conf->slab_cache, sh);
505 atomic_dec(&conf->active_stripes);
506 return 1;
507}
508
509static void shrink_stripes(raid5_conf_t *conf)
510{
511 while (drop_one_stripe(conf))
512 ;
513
NeilBrown29fc7e32006-02-03 03:03:41 -0800514 if (conf->slab_cache)
515 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 conf->slab_cache = NULL;
517}
518
NeilBrown4e5314b2005-11-08 21:39:22 -0800519static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int error)
521{
522 struct stripe_head *sh = bi->bi_private;
523 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800524 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -0700526 char b[BDEVNAME_SIZE];
527 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (bi->bi_size)
530 return 1;
531
532 for (i=0 ; i<disks; i++)
533 if (bi == &sh->dev[i].req)
534 break;
535
536 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
537 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
538 uptodate);
539 if (i == disks) {
540 BUG();
541 return 0;
542 }
543
544 if (uptodate) {
545#if 0
546 struct bio *bio;
547 unsigned long flags;
548 spin_lock_irqsave(&conf->device_lock, flags);
549 /* we can return a buffer if we bypassed the cache or
550 * if the top buffer is not in highmem. If there are
551 * multiple buffers, leave the extra work to
552 * handle_stripe
553 */
554 buffer = sh->bh_read[i];
555 if (buffer &&
556 (!PageHighMem(buffer->b_page)
557 || buffer->b_page == bh->b_page )
558 ) {
559 sh->bh_read[i] = buffer->b_reqnext;
560 buffer->b_reqnext = NULL;
561 } else
562 buffer = NULL;
563 spin_unlock_irqrestore(&conf->device_lock, flags);
564 if (sh->bh_page[i]==bh->b_page)
565 set_buffer_uptodate(bh);
566 if (buffer) {
567 if (buffer->b_page != bh->b_page)
568 memcpy(buffer->b_data, bh->b_data, bh->b_size);
569 buffer->b_end_io(buffer, 1);
570 }
571#else
572 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800573#endif
574 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -0700575 rdev = conf->disks[i].rdev;
576 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
577 mdname(conf->mddev), STRIPE_SECTORS,
578 (unsigned long long)sh->sector + rdev->data_offset,
579 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -0800580 clear_bit(R5_ReadError, &sh->dev[i].flags);
581 clear_bit(R5_ReWrite, &sh->dev[i].flags);
582 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800583 if (atomic_read(&conf->disks[i].rdev->read_errors))
584 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
NeilBrownd6950432006-07-10 04:44:20 -0700586 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -0800587 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -0700588 rdev = conf->disks[i].rdev;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700591 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -0800592 if (conf->mddev->degraded)
NeilBrownd6950432006-07-10 04:44:20 -0700593 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
594 mdname(conf->mddev),
595 (unsigned long long)sh->sector + rdev->data_offset,
596 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800597 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800598 /* Oh, no!!! */
NeilBrownd6950432006-07-10 04:44:20 -0700599 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
600 mdname(conf->mddev),
601 (unsigned long long)sh->sector + rdev->data_offset,
602 bdn);
603 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -0800604 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800605 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -0700606 "raid5:%s: Too many read errors, failing device %s.\n",
607 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800608 else
609 retry = 1;
610 if (retry)
611 set_bit(R5_ReadError, &sh->dev[i].flags);
612 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800613 clear_bit(R5_ReadError, &sh->dev[i].flags);
614 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700615 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
619#if 0
620 /* must restore b_page before unlocking buffer... */
621 if (sh->bh_page[i] != bh->b_page) {
622 bh->b_page = sh->bh_page[i];
623 bh->b_data = page_address(bh->b_page);
624 clear_buffer_uptodate(bh);
625 }
626#endif
627 clear_bit(R5_LOCKED, &sh->dev[i].flags);
628 set_bit(STRIPE_HANDLE, &sh->state);
629 release_stripe(sh);
630 return 0;
631}
632
633static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
634 int error)
635{
636 struct stripe_head *sh = bi->bi_private;
637 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800638 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
640
641 if (bi->bi_size)
642 return 1;
643
644 for (i=0 ; i<disks; i++)
645 if (bi == &sh->dev[i].req)
646 break;
647
648 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
649 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
650 uptodate);
651 if (i == disks) {
652 BUG();
653 return 0;
654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!uptodate)
657 md_error(conf->mddev, conf->disks[i].rdev);
658
659 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
660
661 clear_bit(R5_LOCKED, &sh->dev[i].flags);
662 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -0700663 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666
667
668static sector_t compute_blocknr(struct stripe_head *sh, int i);
669
670static void raid5_build_block (struct stripe_head *sh, int i)
671{
672 struct r5dev *dev = &sh->dev[i];
673
674 bio_init(&dev->req);
675 dev->req.bi_io_vec = &dev->vec;
676 dev->req.bi_vcnt++;
677 dev->req.bi_max_vecs++;
678 dev->vec.bv_page = dev->page;
679 dev->vec.bv_len = STRIPE_SIZE;
680 dev->vec.bv_offset = 0;
681
682 dev->req.bi_sector = sh->sector;
683 dev->req.bi_private = sh;
684
685 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -0700686 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void error(mddev_t *mddev, mdk_rdev_t *rdev)
690{
691 char b[BDEVNAME_SIZE];
692 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
693 PRINTK("raid5: error called\n");
694
NeilBrownb2d444d2005-11-08 21:39:31 -0800695 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -0700696 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -0700697 if (test_and_clear_bit(In_sync, &rdev->flags)) {
698 unsigned long flags;
699 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700701 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * if recovery was running, make sure it aborts.
704 */
705 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
706 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800707 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk (KERN_ALERT
709 "raid5: Disk failure on %s, disabling device."
710 " Operation continuing on %d devices\n",
NeilBrown02c2de82006-10-03 01:15:47 -0700711 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
NeilBrown16a53ec2006-06-26 00:27:38 -0700713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715/*
716 * Input: a 'big' sector number,
717 * Output: index of the data and parity disk, and the sector # in them.
718 */
719static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
720 unsigned int data_disks, unsigned int * dd_idx,
721 unsigned int * pd_idx, raid5_conf_t *conf)
722{
723 long stripe;
724 unsigned long chunk_number;
725 unsigned int chunk_offset;
726 sector_t new_sector;
727 int sectors_per_chunk = conf->chunk_size >> 9;
728
729 /* First compute the information on this sector */
730
731 /*
732 * Compute the chunk number and the sector offset inside the chunk
733 */
734 chunk_offset = sector_div(r_sector, sectors_per_chunk);
735 chunk_number = r_sector;
736 BUG_ON(r_sector != chunk_number);
737
738 /*
739 * Compute the stripe number
740 */
741 stripe = chunk_number / data_disks;
742
743 /*
744 * Compute the data disk and parity disk indexes inside the stripe
745 */
746 *dd_idx = chunk_number % data_disks;
747
748 /*
749 * Select the parity disk based on the user selected algorithm.
750 */
NeilBrown16a53ec2006-06-26 00:27:38 -0700751 switch(conf->level) {
752 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -0700754 break;
755 case 5:
756 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case ALGORITHM_LEFT_ASYMMETRIC:
758 *pd_idx = data_disks - stripe % raid_disks;
759 if (*dd_idx >= *pd_idx)
760 (*dd_idx)++;
761 break;
762 case ALGORITHM_RIGHT_ASYMMETRIC:
763 *pd_idx = stripe % raid_disks;
764 if (*dd_idx >= *pd_idx)
765 (*dd_idx)++;
766 break;
767 case ALGORITHM_LEFT_SYMMETRIC:
768 *pd_idx = data_disks - stripe % raid_disks;
769 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
770 break;
771 case ALGORITHM_RIGHT_SYMMETRIC:
772 *pd_idx = stripe % raid_disks;
773 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
774 break;
775 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800776 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700778 }
779 break;
780 case 6:
781
782 /**** FIX THIS ****/
783 switch (conf->algorithm) {
784 case ALGORITHM_LEFT_ASYMMETRIC:
785 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
786 if (*pd_idx == raid_disks-1)
787 (*dd_idx)++; /* Q D D D P */
788 else if (*dd_idx >= *pd_idx)
789 (*dd_idx) += 2; /* D D P Q D */
790 break;
791 case ALGORITHM_RIGHT_ASYMMETRIC:
792 *pd_idx = stripe % raid_disks;
793 if (*pd_idx == raid_disks-1)
794 (*dd_idx)++; /* Q D D D P */
795 else if (*dd_idx >= *pd_idx)
796 (*dd_idx) += 2; /* D D P Q D */
797 break;
798 case ALGORITHM_LEFT_SYMMETRIC:
799 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
800 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
801 break;
802 case ALGORITHM_RIGHT_SYMMETRIC:
803 *pd_idx = stripe % raid_disks;
804 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
805 break;
806 default:
807 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
808 conf->algorithm);
809 }
810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 /*
814 * Finally, compute the new sector number
815 */
816 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
817 return new_sector;
818}
819
820
821static sector_t compute_blocknr(struct stripe_head *sh, int i)
822{
823 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800824 int raid_disks = sh->disks, data_disks = raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 sector_t new_sector = sh->sector, check;
826 int sectors_per_chunk = conf->chunk_size >> 9;
827 sector_t stripe;
828 int chunk_offset;
829 int chunk_number, dummy1, dummy2, dd_idx = i;
830 sector_t r_sector;
831
NeilBrown16a53ec2006-06-26 00:27:38 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 chunk_offset = sector_div(new_sector, sectors_per_chunk);
834 stripe = new_sector;
835 BUG_ON(new_sector != stripe);
836
NeilBrown16a53ec2006-06-26 00:27:38 -0700837 if (i == sh->pd_idx)
838 return 0;
839 switch(conf->level) {
840 case 4: break;
841 case 5:
842 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case ALGORITHM_LEFT_ASYMMETRIC:
844 case ALGORITHM_RIGHT_ASYMMETRIC:
845 if (i > sh->pd_idx)
846 i--;
847 break;
848 case ALGORITHM_LEFT_SYMMETRIC:
849 case ALGORITHM_RIGHT_SYMMETRIC:
850 if (i < sh->pd_idx)
851 i += raid_disks;
852 i -= (sh->pd_idx + 1);
853 break;
854 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800855 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -0700856 conf->algorithm);
857 }
858 break;
859 case 6:
860 data_disks = raid_disks - 2;
861 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
862 return 0; /* It is the Q disk */
863 switch (conf->algorithm) {
864 case ALGORITHM_LEFT_ASYMMETRIC:
865 case ALGORITHM_RIGHT_ASYMMETRIC:
866 if (sh->pd_idx == raid_disks-1)
867 i--; /* Q D D D P */
868 else if (i > sh->pd_idx)
869 i -= 2; /* D D P Q D */
870 break;
871 case ALGORITHM_LEFT_SYMMETRIC:
872 case ALGORITHM_RIGHT_SYMMETRIC:
873 if (sh->pd_idx == raid_disks-1)
874 i--; /* Q D D D P */
875 else {
876 /* D D P Q D */
877 if (i < sh->pd_idx)
878 i += raid_disks;
879 i -= (sh->pd_idx + 2);
880 }
881 break;
882 default:
883 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700885 }
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889 chunk_number = stripe * data_disks + i;
890 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
891
892 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
893 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800894 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896 }
897 return r_sector;
898}
899
900
901
902/*
NeilBrown16a53ec2006-06-26 00:27:38 -0700903 * Copy data between a page in the stripe cache, and one or more bion
904 * The page could align with the middle of the bio, or there could be
905 * several bion, each with several bio_vecs, which cover part of the page
906 * Multiple bion are linked together on bi_next. There may be extras
907 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909static void copy_data(int frombio, struct bio *bio,
910 struct page *page,
911 sector_t sector)
912{
913 char *pa = page_address(page);
914 struct bio_vec *bvl;
915 int i;
916 int page_offset;
917
918 if (bio->bi_sector >= sector)
919 page_offset = (signed)(bio->bi_sector - sector) * 512;
920 else
921 page_offset = (signed)(sector - bio->bi_sector) * -512;
922 bio_for_each_segment(bvl, bio, i) {
923 int len = bio_iovec_idx(bio,i)->bv_len;
924 int clen;
925 int b_offset = 0;
926
927 if (page_offset < 0) {
928 b_offset = -page_offset;
929 page_offset += b_offset;
930 len -= b_offset;
931 }
932
933 if (len > 0 && page_offset + len > STRIPE_SIZE)
934 clen = STRIPE_SIZE - page_offset;
935 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (clen > 0) {
938 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
939 if (frombio)
940 memcpy(pa+page_offset, ba+b_offset, clen);
941 else
942 memcpy(ba+b_offset, pa+page_offset, clen);
943 __bio_kunmap_atomic(ba, KM_USER0);
944 }
945 if (clen < len) /* hit end of page */
946 break;
947 page_offset += len;
948 }
949}
950
951#define check_xor() do { \
952 if (count == MAX_XOR_BLOCKS) { \
953 xor_block(count, STRIPE_SIZE, ptr); \
954 count = 1; \
955 } \
956 } while(0)
957
958
959static void compute_block(struct stripe_head *sh, int dd_idx)
960{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800961 int i, count, disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 void *ptr[MAX_XOR_BLOCKS], *p;
963
964 PRINTK("compute_block, stripe %llu, idx %d\n",
965 (unsigned long long)sh->sector, dd_idx);
966
967 ptr[0] = page_address(sh->dev[dd_idx].page);
968 memset(ptr[0], 0, STRIPE_SIZE);
969 count = 1;
970 for (i = disks ; i--; ) {
971 if (i == dd_idx)
972 continue;
973 p = page_address(sh->dev[i].page);
974 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
975 ptr[count++] = p;
976 else
NeilBrown14f8d262006-01-06 00:20:14 -0800977 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 " not present\n", dd_idx,
979 (unsigned long long)sh->sector, i);
980
981 check_xor();
982 }
983 if (count != 1)
984 xor_block(count, STRIPE_SIZE, ptr);
985 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
986}
987
NeilBrown16a53ec2006-06-26 00:27:38 -0700988static void compute_parity5(struct stripe_head *sh, int method)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800991 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 void *ptr[MAX_XOR_BLOCKS];
993 struct bio *chosen;
994
NeilBrown16a53ec2006-06-26 00:27:38 -0700995 PRINTK("compute_parity5, stripe %llu, method %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 (unsigned long long)sh->sector, method);
997
998 count = 1;
999 ptr[0] = page_address(sh->dev[pd_idx].page);
1000 switch(method) {
1001 case READ_MODIFY_WRITE:
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001002 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 for (i=disks ; i-- ;) {
1004 if (i==pd_idx)
1005 continue;
1006 if (sh->dev[i].towrite &&
1007 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1008 ptr[count++] = page_address(sh->dev[i].page);
1009 chosen = sh->dev[i].towrite;
1010 sh->dev[i].towrite = NULL;
1011
1012 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1013 wake_up(&conf->wait_for_overlap);
1014
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001015 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 sh->dev[i].written = chosen;
1017 check_xor();
1018 }
1019 }
1020 break;
1021 case RECONSTRUCT_WRITE:
1022 memset(ptr[0], 0, STRIPE_SIZE);
1023 for (i= disks; i-- ;)
1024 if (i!=pd_idx && sh->dev[i].towrite) {
1025 chosen = sh->dev[i].towrite;
1026 sh->dev[i].towrite = NULL;
1027
1028 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1029 wake_up(&conf->wait_for_overlap);
1030
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001031 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sh->dev[i].written = chosen;
1033 }
1034 break;
1035 case CHECK_PARITY:
1036 break;
1037 }
1038 if (count>1) {
1039 xor_block(count, STRIPE_SIZE, ptr);
1040 count = 1;
1041 }
1042
1043 for (i = disks; i--;)
1044 if (sh->dev[i].written) {
1045 sector_t sector = sh->dev[i].sector;
1046 struct bio *wbi = sh->dev[i].written;
1047 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1048 copy_data(1, wbi, sh->dev[i].page, sector);
1049 wbi = r5_next_bio(wbi, sector);
1050 }
1051
1052 set_bit(R5_LOCKED, &sh->dev[i].flags);
1053 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1054 }
1055
1056 switch(method) {
1057 case RECONSTRUCT_WRITE:
1058 case CHECK_PARITY:
1059 for (i=disks; i--;)
1060 if (i != pd_idx) {
1061 ptr[count++] = page_address(sh->dev[i].page);
1062 check_xor();
1063 }
1064 break;
1065 case READ_MODIFY_WRITE:
1066 for (i = disks; i--;)
1067 if (sh->dev[i].written) {
1068 ptr[count++] = page_address(sh->dev[i].page);
1069 check_xor();
1070 }
1071 }
1072 if (count != 1)
1073 xor_block(count, STRIPE_SIZE, ptr);
1074
1075 if (method != CHECK_PARITY) {
1076 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1077 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1078 } else
1079 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1080}
1081
NeilBrown16a53ec2006-06-26 00:27:38 -07001082static void compute_parity6(struct stripe_head *sh, int method)
1083{
1084 raid6_conf_t *conf = sh->raid_conf;
1085 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1086 struct bio *chosen;
1087 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1088 void *ptrs[disks];
1089
1090 qd_idx = raid6_next_disk(pd_idx, disks);
1091 d0_idx = raid6_next_disk(qd_idx, disks);
1092
1093 PRINTK("compute_parity, stripe %llu, method %d\n",
1094 (unsigned long long)sh->sector, method);
1095
1096 switch(method) {
1097 case READ_MODIFY_WRITE:
1098 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1099 case RECONSTRUCT_WRITE:
1100 for (i= disks; i-- ;)
1101 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1102 chosen = sh->dev[i].towrite;
1103 sh->dev[i].towrite = NULL;
1104
1105 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1106 wake_up(&conf->wait_for_overlap);
1107
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001108 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001109 sh->dev[i].written = chosen;
1110 }
1111 break;
1112 case CHECK_PARITY:
1113 BUG(); /* Not implemented yet */
1114 }
1115
1116 for (i = disks; i--;)
1117 if (sh->dev[i].written) {
1118 sector_t sector = sh->dev[i].sector;
1119 struct bio *wbi = sh->dev[i].written;
1120 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1121 copy_data(1, wbi, sh->dev[i].page, sector);
1122 wbi = r5_next_bio(wbi, sector);
1123 }
1124
1125 set_bit(R5_LOCKED, &sh->dev[i].flags);
1126 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1127 }
1128
1129// switch(method) {
1130// case RECONSTRUCT_WRITE:
1131// case CHECK_PARITY:
1132// case UPDATE_PARITY:
1133 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1134 /* FIX: Is this ordering of drives even remotely optimal? */
1135 count = 0;
1136 i = d0_idx;
1137 do {
1138 ptrs[count++] = page_address(sh->dev[i].page);
1139 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1140 printk("block %d/%d not uptodate on parity calc\n", i,count);
1141 i = raid6_next_disk(i, disks);
1142 } while ( i != d0_idx );
1143// break;
1144// }
1145
1146 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1147
1148 switch(method) {
1149 case RECONSTRUCT_WRITE:
1150 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1151 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1152 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1153 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1154 break;
1155 case UPDATE_PARITY:
1156 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1157 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1158 break;
1159 }
1160}
1161
1162
1163/* Compute one missing block */
1164static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1165{
1166 raid6_conf_t *conf = sh->raid_conf;
1167 int i, count, disks = conf->raid_disks;
1168 void *ptr[MAX_XOR_BLOCKS], *p;
1169 int pd_idx = sh->pd_idx;
1170 int qd_idx = raid6_next_disk(pd_idx, disks);
1171
1172 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1173 (unsigned long long)sh->sector, dd_idx);
1174
1175 if ( dd_idx == qd_idx ) {
1176 /* We're actually computing the Q drive */
1177 compute_parity6(sh, UPDATE_PARITY);
1178 } else {
1179 ptr[0] = page_address(sh->dev[dd_idx].page);
1180 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1181 count = 1;
1182 for (i = disks ; i--; ) {
1183 if (i == dd_idx || i == qd_idx)
1184 continue;
1185 p = page_address(sh->dev[i].page);
1186 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1187 ptr[count++] = p;
1188 else
1189 printk("compute_block() %d, stripe %llu, %d"
1190 " not present\n", dd_idx,
1191 (unsigned long long)sh->sector, i);
1192
1193 check_xor();
1194 }
1195 if (count != 1)
1196 xor_block(count, STRIPE_SIZE, ptr);
1197 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1198 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1199 }
1200}
1201
1202/* Compute two missing blocks */
1203static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1204{
1205 raid6_conf_t *conf = sh->raid_conf;
1206 int i, count, disks = conf->raid_disks;
1207 int pd_idx = sh->pd_idx;
1208 int qd_idx = raid6_next_disk(pd_idx, disks);
1209 int d0_idx = raid6_next_disk(qd_idx, disks);
1210 int faila, failb;
1211
1212 /* faila and failb are disk numbers relative to d0_idx */
1213 /* pd_idx become disks-2 and qd_idx become disks-1 */
1214 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1215 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1216
1217 BUG_ON(faila == failb);
1218 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1219
1220 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1221 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1222
1223 if ( failb == disks-1 ) {
1224 /* Q disk is one of the missing disks */
1225 if ( faila == disks-2 ) {
1226 /* Missing P+Q, just recompute */
1227 compute_parity6(sh, UPDATE_PARITY);
1228 return;
1229 } else {
1230 /* We're missing D+Q; recompute D from P */
1231 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1232 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1233 return;
1234 }
1235 }
1236
1237 /* We're missing D+P or D+D; build pointer table */
1238 {
1239 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1240 void *ptrs[disks];
1241
1242 count = 0;
1243 i = d0_idx;
1244 do {
1245 ptrs[count++] = page_address(sh->dev[i].page);
1246 i = raid6_next_disk(i, disks);
1247 if (i != dd_idx1 && i != dd_idx2 &&
1248 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1249 printk("compute_2 with missing block %d/%d\n", count, i);
1250 } while ( i != d0_idx );
1251
1252 if ( failb == disks-2 ) {
1253 /* We're missing D+P. */
1254 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1255 } else {
1256 /* We're missing D+D. */
1257 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1258 }
1259
1260 /* Both the above update both missing blocks */
1261 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1262 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1263 }
1264}
1265
1266
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001270 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * The bi_next chain must be in order.
1272 */
1273static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1274{
1275 struct bio **bip;
1276 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001277 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1280 (unsigned long long)bi->bi_sector,
1281 (unsigned long long)sh->sector);
1282
1283
1284 spin_lock(&sh->lock);
1285 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001286 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001288 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1289 firstwrite = 1;
1290 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bip = &sh->dev[dd_idx].toread;
1292 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1293 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1294 goto overlap;
1295 bip = & (*bip)->bi_next;
1296 }
1297 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1298 goto overlap;
1299
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001300 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (*bip)
1302 bi->bi_next = *bip;
1303 *bip = bi;
1304 bi->bi_phys_segments ++;
1305 spin_unlock_irq(&conf->device_lock);
1306 spin_unlock(&sh->lock);
1307
1308 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1309 (unsigned long long)bi->bi_sector,
1310 (unsigned long long)sh->sector, dd_idx);
1311
NeilBrown72626682005-09-09 16:23:54 -07001312 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001313 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1314 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001315 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001316 set_bit(STRIPE_BIT_DELAY, &sh->state);
1317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (forwrite) {
1320 /* check if page is covered */
1321 sector_t sector = sh->dev[dd_idx].sector;
1322 for (bi=sh->dev[dd_idx].towrite;
1323 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1324 bi && bi->bi_sector <= sector;
1325 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1326 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1327 sector = bi->bi_sector + (bi->bi_size>>9);
1328 }
1329 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1330 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1331 }
1332 return 1;
1333
1334 overlap:
1335 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1336 spin_unlock_irq(&conf->device_lock);
1337 spin_unlock(&sh->lock);
1338 return 0;
1339}
1340
NeilBrown29269552006-03-27 01:18:10 -08001341static void end_reshape(raid5_conf_t *conf);
1342
NeilBrown16a53ec2006-06-26 00:27:38 -07001343static int page_is_zero(struct page *p)
1344{
1345 char *a = page_address(p);
1346 return ((*(u32*)a) == 0 &&
1347 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1348}
1349
NeilBrownccfcc3c2006-03-27 01:18:09 -08001350static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1351{
1352 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001353 int pd_idx, dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001354 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1355
NeilBrownccfcc3c2006-03-27 01:18:09 -08001356 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1357 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1358 return pd_idx;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/*
1363 * handle_stripe - do things to a stripe.
1364 *
1365 * We lock the stripe and then examine the state of various bits
1366 * to see what needs to be done.
1367 * Possible results:
1368 * return some read request which now have data
1369 * return some write requests which are safely on disc
1370 * schedule a read on some buffers
1371 * schedule a write of some buffers
1372 * return confirmation of parity correctness
1373 *
1374 * Parity calculations are done inside the stripe lock
1375 * buffers are taken off read_list or write_list, and bh_cache buffers
1376 * get BH_Lock set before the stripe lock is released.
1377 *
1378 */
1379
NeilBrown16a53ec2006-06-26 00:27:38 -07001380static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001383 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct bio *return_bi= NULL;
1385 struct bio *bi;
1386 int i;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001387 int syncing, expanding, expanded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1389 int non_overwrite = 0;
1390 int failed_num=0;
1391 struct r5dev *dev;
1392
1393 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1394 (unsigned long long)sh->sector, atomic_read(&sh->count),
1395 sh->pd_idx);
1396
1397 spin_lock(&sh->lock);
1398 clear_bit(STRIPE_HANDLE, &sh->state);
1399 clear_bit(STRIPE_DELAYED, &sh->state);
1400
1401 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001402 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1403 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Now to look around and see what can be done */
1405
NeilBrown9910f162006-01-06 00:20:24 -08001406 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 for (i=disks; i--; ) {
1408 mdk_rdev_t *rdev;
1409 dev = &sh->dev[i];
1410 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1413 i, dev->flags, dev->toread, dev->towrite, dev->written);
1414 /* maybe we can reply to a read */
1415 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1416 struct bio *rbi, *rbi2;
1417 PRINTK("Return read for disc %d\n", i);
1418 spin_lock_irq(&conf->device_lock);
1419 rbi = dev->toread;
1420 dev->toread = NULL;
1421 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1422 wake_up(&conf->wait_for_overlap);
1423 spin_unlock_irq(&conf->device_lock);
1424 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1425 copy_data(0, rbi, dev->page, dev->sector);
1426 rbi2 = r5_next_bio(rbi, dev->sector);
1427 spin_lock_irq(&conf->device_lock);
1428 if (--rbi->bi_phys_segments == 0) {
1429 rbi->bi_next = return_bi;
1430 return_bi = rbi;
1431 }
1432 spin_unlock_irq(&conf->device_lock);
1433 rbi = rbi2;
1434 }
1435 }
1436
1437 /* now count some things */
1438 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1439 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1440
1441
1442 if (dev->toread) to_read++;
1443 if (dev->towrite) {
1444 to_write++;
1445 if (!test_bit(R5_OVERWRITE, &dev->flags))
1446 non_overwrite++;
1447 }
1448 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001449 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001450 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001451 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001452 clear_bit(R5_ReadError, &dev->flags);
1453 clear_bit(R5_ReWrite, &dev->flags);
1454 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001455 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001456 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 failed++;
1458 failed_num = i;
1459 } else
1460 set_bit(R5_Insync, &dev->flags);
1461 }
NeilBrown9910f162006-01-06 00:20:24 -08001462 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 PRINTK("locked=%d uptodate=%d to_read=%d"
1464 " to_write=%d failed=%d failed_num=%d\n",
1465 locked, uptodate, to_read, to_write, failed, failed_num);
1466 /* check if the array has lost two devices and, if so, some requests might
1467 * need to be failed
1468 */
1469 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001471 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001472
1473 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001474 mdk_rdev_t *rdev;
1475 rcu_read_lock();
1476 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001477 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001478 /* multiple read failures in one stripe */
1479 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001480 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001481 }
1482
NeilBrown72626682005-09-09 16:23:54 -07001483 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* fail all writes first */
1485 bi = sh->dev[i].towrite;
1486 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001487 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1490 wake_up(&conf->wait_for_overlap);
1491
1492 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1493 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1494 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1495 if (--bi->bi_phys_segments == 0) {
1496 md_write_end(conf->mddev);
1497 bi->bi_next = return_bi;
1498 return_bi = bi;
1499 }
1500 bi = nextbi;
1501 }
1502 /* and fail all 'written' */
1503 bi = sh->dev[i].written;
1504 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001505 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1507 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1508 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1509 if (--bi->bi_phys_segments == 0) {
1510 md_write_end(conf->mddev);
1511 bi->bi_next = return_bi;
1512 return_bi = bi;
1513 }
1514 bi = bi2;
1515 }
1516
1517 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001518 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1519 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 bi = sh->dev[i].toread;
1521 sh->dev[i].toread = NULL;
1522 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1523 wake_up(&conf->wait_for_overlap);
1524 if (bi) to_read--;
1525 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1526 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1527 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1528 if (--bi->bi_phys_segments == 0) {
1529 bi->bi_next = return_bi;
1530 return_bi = bi;
1531 }
1532 bi = nextbi;
1533 }
1534 }
NeilBrown72626682005-09-09 16:23:54 -07001535 spin_unlock_irq(&conf->device_lock);
1536 if (bitmap_end)
1537 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1538 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541 if (failed > 1 && syncing) {
1542 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1543 clear_bit(STRIPE_SYNCING, &sh->state);
1544 syncing = 0;
1545 }
1546
1547 /* might be able to return some write requests if the parity block
1548 * is safe, or on a failed drive
1549 */
1550 dev = &sh->dev[sh->pd_idx];
1551 if ( written &&
1552 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1553 test_bit(R5_UPTODATE, &dev->flags))
1554 || (failed == 1 && failed_num == sh->pd_idx))
1555 ) {
1556 /* any written block on an uptodate or failed drive can be returned.
1557 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1558 * never LOCKED, so we don't need to test 'failed' directly.
1559 */
1560 for (i=disks; i--; )
1561 if (sh->dev[i].written) {
1562 dev = &sh->dev[i];
1563 if (!test_bit(R5_LOCKED, &dev->flags) &&
1564 test_bit(R5_UPTODATE, &dev->flags) ) {
1565 /* We can return any write requests */
1566 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001567 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 PRINTK("Return write for disc %d\n", i);
1569 spin_lock_irq(&conf->device_lock);
1570 wbi = dev->written;
1571 dev->written = NULL;
1572 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1573 wbi2 = r5_next_bio(wbi, dev->sector);
1574 if (--wbi->bi_phys_segments == 0) {
1575 md_write_end(conf->mddev);
1576 wbi->bi_next = return_bi;
1577 return_bi = wbi;
1578 }
1579 wbi = wbi2;
1580 }
NeilBrown72626682005-09-09 16:23:54 -07001581 if (dev->towrite == NULL)
1582 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001584 if (bitmap_end)
1585 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1586 STRIPE_SECTORS,
1587 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 }
1590 }
1591
1592 /* Now we might consider reading some blocks, either to check/generate
1593 * parity, or to satisfy requests
1594 * or to load a block that is being partially written.
1595 */
NeilBrownccfcc3c2006-03-27 01:18:09 -08001596 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 for (i=disks; i--;) {
1598 dev = &sh->dev[i];
1599 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1600 (dev->toread ||
1601 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1602 syncing ||
NeilBrownccfcc3c2006-03-27 01:18:09 -08001603 expanding ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 (failed && (sh->dev[failed_num].toread ||
1605 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1606 )
1607 ) {
1608 /* we would like to get this block, possibly
1609 * by computing it, but we might not be able to
1610 */
1611 if (uptodate == disks-1) {
1612 PRINTK("Computing block %d\n", i);
1613 compute_block(sh, i);
1614 uptodate++;
1615 } else if (test_bit(R5_Insync, &dev->flags)) {
1616 set_bit(R5_LOCKED, &dev->flags);
1617 set_bit(R5_Wantread, &dev->flags);
1618#if 0
1619 /* if I am just reading this block and we don't have
1620 a failed drive, or any pending writes then sidestep the cache */
1621 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1622 ! syncing && !failed && !to_write) {
1623 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1624 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1625 }
1626#endif
1627 locked++;
1628 PRINTK("Reading block %d (sync=%d)\n",
1629 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 }
1632 }
1633 set_bit(STRIPE_HANDLE, &sh->state);
1634 }
1635
1636 /* now to consider writing and what else, if anything should be read */
1637 if (to_write) {
1638 int rmw=0, rcw=0;
1639 for (i=disks ; i--;) {
1640 /* would I have to read this buffer for read_modify_write */
1641 dev = &sh->dev[i];
1642 if ((dev->towrite || i == sh->pd_idx) &&
1643 (!test_bit(R5_LOCKED, &dev->flags)
1644#if 0
1645|| sh->bh_page[i]!=bh->b_page
1646#endif
1647 ) &&
1648 !test_bit(R5_UPTODATE, &dev->flags)) {
1649 if (test_bit(R5_Insync, &dev->flags)
1650/* && !(!mddev->insync && i == sh->pd_idx) */
1651 )
1652 rmw++;
1653 else rmw += 2*disks; /* cannot read it */
1654 }
1655 /* Would I have to read this buffer for reconstruct_write */
1656 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1657 (!test_bit(R5_LOCKED, &dev->flags)
1658#if 0
1659|| sh->bh_page[i] != bh->b_page
1660#endif
1661 ) &&
1662 !test_bit(R5_UPTODATE, &dev->flags)) {
1663 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1664 else rcw += 2*disks;
1665 }
1666 }
1667 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1668 (unsigned long long)sh->sector, rmw, rcw);
1669 set_bit(STRIPE_HANDLE, &sh->state);
1670 if (rmw < rcw && rmw > 0)
1671 /* prefer read-modify-write, but need to get some data */
1672 for (i=disks; i--;) {
1673 dev = &sh->dev[i];
1674 if ((dev->towrite || i == sh->pd_idx) &&
1675 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1676 test_bit(R5_Insync, &dev->flags)) {
1677 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1678 {
1679 PRINTK("Read_old block %d for r-m-w\n", i);
1680 set_bit(R5_LOCKED, &dev->flags);
1681 set_bit(R5_Wantread, &dev->flags);
1682 locked++;
1683 } else {
1684 set_bit(STRIPE_DELAYED, &sh->state);
1685 set_bit(STRIPE_HANDLE, &sh->state);
1686 }
1687 }
1688 }
1689 if (rcw <= rmw && rcw > 0)
1690 /* want reconstruct write, but need to get some data */
1691 for (i=disks; i--;) {
1692 dev = &sh->dev[i];
1693 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1694 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1695 test_bit(R5_Insync, &dev->flags)) {
1696 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1697 {
1698 PRINTK("Read_old block %d for Reconstruct\n", i);
1699 set_bit(R5_LOCKED, &dev->flags);
1700 set_bit(R5_Wantread, &dev->flags);
1701 locked++;
1702 } else {
1703 set_bit(STRIPE_DELAYED, &sh->state);
1704 set_bit(STRIPE_HANDLE, &sh->state);
1705 }
1706 }
1707 }
1708 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001709 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1710 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 PRINTK("Computing parity...\n");
NeilBrown16a53ec2006-06-26 00:27:38 -07001712 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /* now every locked buffer is ready to be written */
1714 for (i=disks; i--;)
1715 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1716 PRINTK("Writing block %d\n", i);
1717 locked++;
1718 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1719 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1720 || (i==sh->pd_idx && failed == 0))
1721 set_bit(STRIPE_INSYNC, &sh->state);
1722 }
1723 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1724 atomic_dec(&conf->preread_active_stripes);
1725 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1726 md_wakeup_thread(conf->mddev->thread);
1727 }
1728 }
1729 }
1730
1731 /* maybe we need to check and possibly fix the parity for this stripe
1732 * Any reads will already have been scheduled, so we just see if enough data
1733 * is available
1734 */
1735 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001736 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 set_bit(STRIPE_HANDLE, &sh->state);
1738 if (failed == 0) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001739 BUG_ON(uptodate != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001740 compute_parity5(sh, CHECK_PARITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 uptodate--;
NeilBrown16a53ec2006-06-26 00:27:38 -07001742 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* parity is correct (on disc, not in buffer any more) */
1744 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001745 } else {
1746 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1747 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1748 /* don't try to repair!! */
1749 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001750 else {
1751 compute_block(sh, sh->pd_idx);
1752 uptodate++;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
1756 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001757 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (failed==0)
1759 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001761 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1762 BUG_ON(uptodate != disks);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 set_bit(R5_LOCKED, &dev->flags);
1765 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001766 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 locked++;
1768 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
1771 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1772 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1773 clear_bit(STRIPE_SYNCING, &sh->state);
1774 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001775
1776 /* If the failed drive is just a ReadError, then we might need to progress
1777 * the repair/check process
1778 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001779 if (failed == 1 && ! conf->mddev->ro &&
1780 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001781 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1782 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1783 ) {
1784 dev = &sh->dev[failed_num];
1785 if (!test_bit(R5_ReWrite, &dev->flags)) {
1786 set_bit(R5_Wantwrite, &dev->flags);
1787 set_bit(R5_ReWrite, &dev->flags);
1788 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001789 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001790 } else {
1791 /* let's read it back */
1792 set_bit(R5_Wantread, &dev->flags);
1793 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001794 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001795 }
1796 }
1797
NeilBrownccfcc3c2006-03-27 01:18:09 -08001798 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1799 /* Need to write out all blocks after computing parity */
1800 sh->disks = conf->raid_disks;
1801 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001802 compute_parity5(sh, RECONSTRUCT_WRITE);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001803 for (i= conf->raid_disks; i--;) {
1804 set_bit(R5_LOCKED, &sh->dev[i].flags);
1805 locked++;
1806 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1807 }
1808 clear_bit(STRIPE_EXPANDING, &sh->state);
1809 } else if (expanded) {
1810 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08001811 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001812 wake_up(&conf->wait_for_overlap);
1813 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1814 }
1815
1816 if (expanding && locked == 0) {
1817 /* We have read all the blocks in this stripe and now we need to
1818 * copy some of them into a target stripe for expand.
1819 */
1820 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1821 for (i=0; i< sh->disks; i++)
1822 if (i != sh->pd_idx) {
1823 int dd_idx, pd_idx, j;
1824 struct stripe_head *sh2;
1825
1826 sector_t bn = compute_blocknr(sh, i);
1827 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1828 conf->raid_disks-1,
1829 &dd_idx, &pd_idx, conf);
1830 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1831 if (sh2 == NULL)
1832 /* so far only the early blocks of this stripe
1833 * have been requested. When later blocks
1834 * get requested, we will try again
1835 */
1836 continue;
1837 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1838 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1839 /* must have already done this block */
1840 release_stripe(sh2);
1841 continue;
1842 }
1843 memcpy(page_address(sh2->dev[dd_idx].page),
1844 page_address(sh->dev[i].page),
1845 STRIPE_SIZE);
1846 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1847 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1848 for (j=0; j<conf->raid_disks; j++)
1849 if (j != sh2->pd_idx &&
1850 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1851 break;
1852 if (j == conf->raid_disks) {
1853 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1854 set_bit(STRIPE_HANDLE, &sh2->state);
1855 }
1856 release_stripe(sh2);
1857 }
1858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 spin_unlock(&sh->lock);
1861
1862 while ((bi=return_bi)) {
1863 int bytes = bi->bi_size;
1864
1865 return_bi = bi->bi_next;
1866 bi->bi_next = NULL;
1867 bi->bi_size = 0;
1868 bi->bi_end_io(bi, bytes, 0);
1869 }
1870 for (i=disks; i-- ;) {
1871 int rw;
1872 struct bio *bi;
1873 mdk_rdev_t *rdev;
1874 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1875 rw = 1;
1876 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1877 rw = 0;
1878 else
1879 continue;
1880
1881 bi = &sh->dev[i].req;
1882
1883 bi->bi_rw = rw;
1884 if (rw)
1885 bi->bi_end_io = raid5_end_write_request;
1886 else
1887 bi->bi_end_io = raid5_end_read_request;
1888
1889 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001890 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001891 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rdev = NULL;
1893 if (rdev)
1894 atomic_inc(&rdev->nr_pending);
1895 rcu_read_unlock();
1896
1897 if (rdev) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08001898 if (syncing || expanding || expanded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1900
1901 bi->bi_bdev = rdev->bdev;
1902 PRINTK("for %llu schedule op %ld on disc %d\n",
1903 (unsigned long long)sh->sector, bi->bi_rw, i);
1904 atomic_inc(&sh->count);
1905 bi->bi_sector = sh->sector + rdev->data_offset;
1906 bi->bi_flags = 1 << BIO_UPTODATE;
1907 bi->bi_vcnt = 1;
1908 bi->bi_max_vecs = 1;
1909 bi->bi_idx = 0;
1910 bi->bi_io_vec = &sh->dev[i].vec;
1911 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1912 bi->bi_io_vec[0].bv_offset = 0;
1913 bi->bi_size = STRIPE_SIZE;
1914 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001915 if (rw == WRITE &&
1916 test_bit(R5_ReWrite, &sh->dev[i].flags))
1917 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 generic_make_request(bi);
1919 } else {
NeilBrown72626682005-09-09 16:23:54 -07001920 if (rw == 1)
1921 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 PRINTK("skip op %ld on disc %d for sector %llu\n",
1923 bi->bi_rw, i, (unsigned long long)sh->sector);
1924 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1925 set_bit(STRIPE_HANDLE, &sh->state);
1926 }
1927 }
1928}
1929
NeilBrown16a53ec2006-06-26 00:27:38 -07001930static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1931{
1932 raid6_conf_t *conf = sh->raid_conf;
1933 int disks = conf->raid_disks;
1934 struct bio *return_bi= NULL;
1935 struct bio *bi;
1936 int i;
1937 int syncing;
1938 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1939 int non_overwrite = 0;
1940 int failed_num[2] = {0, 0};
1941 struct r5dev *dev, *pdev, *qdev;
1942 int pd_idx = sh->pd_idx;
1943 int qd_idx = raid6_next_disk(pd_idx, disks);
1944 int p_failed, q_failed;
1945
1946 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1947 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1948 pd_idx, qd_idx);
1949
1950 spin_lock(&sh->lock);
1951 clear_bit(STRIPE_HANDLE, &sh->state);
1952 clear_bit(STRIPE_DELAYED, &sh->state);
1953
1954 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1955 /* Now to look around and see what can be done */
1956
1957 rcu_read_lock();
1958 for (i=disks; i--; ) {
1959 mdk_rdev_t *rdev;
1960 dev = &sh->dev[i];
1961 clear_bit(R5_Insync, &dev->flags);
1962
1963 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1964 i, dev->flags, dev->toread, dev->towrite, dev->written);
1965 /* maybe we can reply to a read */
1966 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1967 struct bio *rbi, *rbi2;
1968 PRINTK("Return read for disc %d\n", i);
1969 spin_lock_irq(&conf->device_lock);
1970 rbi = dev->toread;
1971 dev->toread = NULL;
1972 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1973 wake_up(&conf->wait_for_overlap);
1974 spin_unlock_irq(&conf->device_lock);
1975 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1976 copy_data(0, rbi, dev->page, dev->sector);
1977 rbi2 = r5_next_bio(rbi, dev->sector);
1978 spin_lock_irq(&conf->device_lock);
1979 if (--rbi->bi_phys_segments == 0) {
1980 rbi->bi_next = return_bi;
1981 return_bi = rbi;
1982 }
1983 spin_unlock_irq(&conf->device_lock);
1984 rbi = rbi2;
1985 }
1986 }
1987
1988 /* now count some things */
1989 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1990 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1991
1992
1993 if (dev->toread) to_read++;
1994 if (dev->towrite) {
1995 to_write++;
1996 if (!test_bit(R5_OVERWRITE, &dev->flags))
1997 non_overwrite++;
1998 }
1999 if (dev->written) written++;
2000 rdev = rcu_dereference(conf->disks[i].rdev);
2001 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2002 /* The ReadError flag will just be confusing now */
2003 clear_bit(R5_ReadError, &dev->flags);
2004 clear_bit(R5_ReWrite, &dev->flags);
2005 }
2006 if (!rdev || !test_bit(In_sync, &rdev->flags)
2007 || test_bit(R5_ReadError, &dev->flags)) {
2008 if ( failed < 2 )
2009 failed_num[failed] = i;
2010 failed++;
2011 } else
2012 set_bit(R5_Insync, &dev->flags);
2013 }
2014 rcu_read_unlock();
2015 PRINTK("locked=%d uptodate=%d to_read=%d"
2016 " to_write=%d failed=%d failed_num=%d,%d\n",
2017 locked, uptodate, to_read, to_write, failed,
2018 failed_num[0], failed_num[1]);
2019 /* check if the array has lost >2 devices and, if so, some requests might
2020 * need to be failed
2021 */
2022 if (failed > 2 && to_read+to_write+written) {
2023 for (i=disks; i--; ) {
2024 int bitmap_end = 0;
2025
2026 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2027 mdk_rdev_t *rdev;
2028 rcu_read_lock();
2029 rdev = rcu_dereference(conf->disks[i].rdev);
2030 if (rdev && test_bit(In_sync, &rdev->flags))
2031 /* multiple read failures in one stripe */
2032 md_error(conf->mddev, rdev);
2033 rcu_read_unlock();
2034 }
2035
2036 spin_lock_irq(&conf->device_lock);
2037 /* fail all writes first */
2038 bi = sh->dev[i].towrite;
2039 sh->dev[i].towrite = NULL;
2040 if (bi) { to_write--; bitmap_end = 1; }
2041
2042 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2043 wake_up(&conf->wait_for_overlap);
2044
2045 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2046 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2047 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2048 if (--bi->bi_phys_segments == 0) {
2049 md_write_end(conf->mddev);
2050 bi->bi_next = return_bi;
2051 return_bi = bi;
2052 }
2053 bi = nextbi;
2054 }
2055 /* and fail all 'written' */
2056 bi = sh->dev[i].written;
2057 sh->dev[i].written = NULL;
2058 if (bi) bitmap_end = 1;
2059 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2060 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2061 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2062 if (--bi->bi_phys_segments == 0) {
2063 md_write_end(conf->mddev);
2064 bi->bi_next = return_bi;
2065 return_bi = bi;
2066 }
2067 bi = bi2;
2068 }
2069
2070 /* fail any reads if this device is non-operational */
2071 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2072 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2073 bi = sh->dev[i].toread;
2074 sh->dev[i].toread = NULL;
2075 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2076 wake_up(&conf->wait_for_overlap);
2077 if (bi) to_read--;
2078 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2079 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2080 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2081 if (--bi->bi_phys_segments == 0) {
2082 bi->bi_next = return_bi;
2083 return_bi = bi;
2084 }
2085 bi = nextbi;
2086 }
2087 }
2088 spin_unlock_irq(&conf->device_lock);
2089 if (bitmap_end)
2090 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2091 STRIPE_SECTORS, 0, 0);
2092 }
2093 }
2094 if (failed > 2 && syncing) {
2095 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2096 clear_bit(STRIPE_SYNCING, &sh->state);
2097 syncing = 0;
2098 }
2099
2100 /*
2101 * might be able to return some write requests if the parity blocks
2102 * are safe, or on a failed drive
2103 */
2104 pdev = &sh->dev[pd_idx];
2105 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2106 || (failed >= 2 && failed_num[1] == pd_idx);
2107 qdev = &sh->dev[qd_idx];
2108 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2109 || (failed >= 2 && failed_num[1] == qd_idx);
2110
2111 if ( written &&
2112 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2113 && !test_bit(R5_LOCKED, &pdev->flags)
2114 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2115 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2116 && !test_bit(R5_LOCKED, &qdev->flags)
2117 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2118 /* any written block on an uptodate or failed drive can be
2119 * returned. Note that if we 'wrote' to a failed drive,
2120 * it will be UPTODATE, but never LOCKED, so we don't need
2121 * to test 'failed' directly.
2122 */
2123 for (i=disks; i--; )
2124 if (sh->dev[i].written) {
2125 dev = &sh->dev[i];
2126 if (!test_bit(R5_LOCKED, &dev->flags) &&
2127 test_bit(R5_UPTODATE, &dev->flags) ) {
2128 /* We can return any write requests */
2129 int bitmap_end = 0;
2130 struct bio *wbi, *wbi2;
2131 PRINTK("Return write for stripe %llu disc %d\n",
2132 (unsigned long long)sh->sector, i);
2133 spin_lock_irq(&conf->device_lock);
2134 wbi = dev->written;
2135 dev->written = NULL;
2136 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2137 wbi2 = r5_next_bio(wbi, dev->sector);
2138 if (--wbi->bi_phys_segments == 0) {
2139 md_write_end(conf->mddev);
2140 wbi->bi_next = return_bi;
2141 return_bi = wbi;
2142 }
2143 wbi = wbi2;
2144 }
2145 if (dev->towrite == NULL)
2146 bitmap_end = 1;
2147 spin_unlock_irq(&conf->device_lock);
2148 if (bitmap_end)
2149 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2150 STRIPE_SECTORS,
2151 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2152 }
2153 }
2154 }
2155
2156 /* Now we might consider reading some blocks, either to check/generate
2157 * parity, or to satisfy requests
2158 * or to load a block that is being partially written.
2159 */
2160 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2161 for (i=disks; i--;) {
2162 dev = &sh->dev[i];
2163 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2164 (dev->toread ||
2165 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2166 syncing ||
2167 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2168 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2169 )
2170 ) {
2171 /* we would like to get this block, possibly
2172 * by computing it, but we might not be able to
2173 */
2174 if (uptodate == disks-1) {
2175 PRINTK("Computing stripe %llu block %d\n",
2176 (unsigned long long)sh->sector, i);
2177 compute_block_1(sh, i, 0);
2178 uptodate++;
2179 } else if ( uptodate == disks-2 && failed >= 2 ) {
2180 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2181 int other;
2182 for (other=disks; other--;) {
2183 if ( other == i )
2184 continue;
2185 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2186 break;
2187 }
2188 BUG_ON(other < 0);
2189 PRINTK("Computing stripe %llu blocks %d,%d\n",
2190 (unsigned long long)sh->sector, i, other);
2191 compute_block_2(sh, i, other);
2192 uptodate += 2;
2193 } else if (test_bit(R5_Insync, &dev->flags)) {
2194 set_bit(R5_LOCKED, &dev->flags);
2195 set_bit(R5_Wantread, &dev->flags);
2196#if 0
2197 /* if I am just reading this block and we don't have
2198 a failed drive, or any pending writes then sidestep the cache */
2199 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
2200 ! syncing && !failed && !to_write) {
2201 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
2202 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
2203 }
2204#endif
2205 locked++;
2206 PRINTK("Reading block %d (sync=%d)\n",
2207 i, syncing);
2208 }
2209 }
2210 }
2211 set_bit(STRIPE_HANDLE, &sh->state);
2212 }
2213
2214 /* now to consider writing and what else, if anything should be read */
2215 if (to_write) {
2216 int rcw=0, must_compute=0;
2217 for (i=disks ; i--;) {
2218 dev = &sh->dev[i];
2219 /* Would I have to read this buffer for reconstruct_write */
2220 if (!test_bit(R5_OVERWRITE, &dev->flags)
2221 && i != pd_idx && i != qd_idx
2222 && (!test_bit(R5_LOCKED, &dev->flags)
2223#if 0
2224 || sh->bh_page[i] != bh->b_page
2225#endif
2226 ) &&
2227 !test_bit(R5_UPTODATE, &dev->flags)) {
2228 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2229 else {
2230 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2231 must_compute++;
2232 }
2233 }
2234 }
2235 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2236 (unsigned long long)sh->sector, rcw, must_compute);
2237 set_bit(STRIPE_HANDLE, &sh->state);
2238
2239 if (rcw > 0)
2240 /* want reconstruct write, but need to get some data */
2241 for (i=disks; i--;) {
2242 dev = &sh->dev[i];
2243 if (!test_bit(R5_OVERWRITE, &dev->flags)
2244 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2245 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2246 test_bit(R5_Insync, &dev->flags)) {
2247 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2248 {
2249 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2250 (unsigned long long)sh->sector, i);
2251 set_bit(R5_LOCKED, &dev->flags);
2252 set_bit(R5_Wantread, &dev->flags);
2253 locked++;
2254 } else {
2255 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2256 (unsigned long long)sh->sector, i);
2257 set_bit(STRIPE_DELAYED, &sh->state);
2258 set_bit(STRIPE_HANDLE, &sh->state);
2259 }
2260 }
2261 }
2262 /* now if nothing is locked, and if we have enough data, we can start a write request */
2263 if (locked == 0 && rcw == 0 &&
2264 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2265 if ( must_compute > 0 ) {
2266 /* We have failed blocks and need to compute them */
2267 switch ( failed ) {
2268 case 0: BUG();
2269 case 1: compute_block_1(sh, failed_num[0], 0); break;
2270 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2271 default: BUG(); /* This request should have been failed? */
2272 }
2273 }
2274
2275 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2276 compute_parity6(sh, RECONSTRUCT_WRITE);
2277 /* now every locked buffer is ready to be written */
2278 for (i=disks; i--;)
2279 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2280 PRINTK("Writing stripe %llu block %d\n",
2281 (unsigned long long)sh->sector, i);
2282 locked++;
2283 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2284 }
2285 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2286 set_bit(STRIPE_INSYNC, &sh->state);
2287
2288 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2289 atomic_dec(&conf->preread_active_stripes);
2290 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2291 md_wakeup_thread(conf->mddev->thread);
2292 }
2293 }
2294 }
2295
2296 /* maybe we need to check and possibly fix the parity for this stripe
2297 * Any reads will already have been scheduled, so we just see if enough data
2298 * is available
2299 */
2300 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2301 int update_p = 0, update_q = 0;
2302 struct r5dev *dev;
2303
2304 set_bit(STRIPE_HANDLE, &sh->state);
2305
2306 BUG_ON(failed>2);
2307 BUG_ON(uptodate < disks);
2308 /* Want to check and possibly repair P and Q.
2309 * However there could be one 'failed' device, in which
2310 * case we can only check one of them, possibly using the
2311 * other to generate missing data
2312 */
2313
2314 /* If !tmp_page, we cannot do the calculations,
2315 * but as we have set STRIPE_HANDLE, we will soon be called
2316 * by stripe_handle with a tmp_page - just wait until then.
2317 */
2318 if (tmp_page) {
2319 if (failed == q_failed) {
2320 /* The only possible failed device holds 'Q', so it makes
2321 * sense to check P (If anything else were failed, we would
2322 * have used P to recreate it).
2323 */
2324 compute_block_1(sh, pd_idx, 1);
2325 if (!page_is_zero(sh->dev[pd_idx].page)) {
2326 compute_block_1(sh,pd_idx,0);
2327 update_p = 1;
2328 }
2329 }
2330 if (!q_failed && failed < 2) {
2331 /* q is not failed, and we didn't use it to generate
2332 * anything, so it makes sense to check it
2333 */
2334 memcpy(page_address(tmp_page),
2335 page_address(sh->dev[qd_idx].page),
2336 STRIPE_SIZE);
2337 compute_parity6(sh, UPDATE_PARITY);
2338 if (memcmp(page_address(tmp_page),
2339 page_address(sh->dev[qd_idx].page),
2340 STRIPE_SIZE)!= 0) {
2341 clear_bit(STRIPE_INSYNC, &sh->state);
2342 update_q = 1;
2343 }
2344 }
2345 if (update_p || update_q) {
2346 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2347 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2348 /* don't try to repair!! */
2349 update_p = update_q = 0;
2350 }
2351
2352 /* now write out any block on a failed drive,
2353 * or P or Q if they need it
2354 */
2355
2356 if (failed == 2) {
2357 dev = &sh->dev[failed_num[1]];
2358 locked++;
2359 set_bit(R5_LOCKED, &dev->flags);
2360 set_bit(R5_Wantwrite, &dev->flags);
2361 }
2362 if (failed >= 1) {
2363 dev = &sh->dev[failed_num[0]];
2364 locked++;
2365 set_bit(R5_LOCKED, &dev->flags);
2366 set_bit(R5_Wantwrite, &dev->flags);
2367 }
2368
2369 if (update_p) {
2370 dev = &sh->dev[pd_idx];
2371 locked ++;
2372 set_bit(R5_LOCKED, &dev->flags);
2373 set_bit(R5_Wantwrite, &dev->flags);
2374 }
2375 if (update_q) {
2376 dev = &sh->dev[qd_idx];
2377 locked++;
2378 set_bit(R5_LOCKED, &dev->flags);
2379 set_bit(R5_Wantwrite, &dev->flags);
2380 }
2381 clear_bit(STRIPE_DEGRADED, &sh->state);
2382
2383 set_bit(STRIPE_INSYNC, &sh->state);
2384 }
2385 }
2386
2387 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2388 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2389 clear_bit(STRIPE_SYNCING, &sh->state);
2390 }
2391
2392 /* If the failed drives are just a ReadError, then we might need
2393 * to progress the repair/check process
2394 */
2395 if (failed <= 2 && ! conf->mddev->ro)
2396 for (i=0; i<failed;i++) {
2397 dev = &sh->dev[failed_num[i]];
2398 if (test_bit(R5_ReadError, &dev->flags)
2399 && !test_bit(R5_LOCKED, &dev->flags)
2400 && test_bit(R5_UPTODATE, &dev->flags)
2401 ) {
2402 if (!test_bit(R5_ReWrite, &dev->flags)) {
2403 set_bit(R5_Wantwrite, &dev->flags);
2404 set_bit(R5_ReWrite, &dev->flags);
2405 set_bit(R5_LOCKED, &dev->flags);
2406 } else {
2407 /* let's read it back */
2408 set_bit(R5_Wantread, &dev->flags);
2409 set_bit(R5_LOCKED, &dev->flags);
2410 }
2411 }
2412 }
2413 spin_unlock(&sh->lock);
2414
2415 while ((bi=return_bi)) {
2416 int bytes = bi->bi_size;
2417
2418 return_bi = bi->bi_next;
2419 bi->bi_next = NULL;
2420 bi->bi_size = 0;
2421 bi->bi_end_io(bi, bytes, 0);
2422 }
2423 for (i=disks; i-- ;) {
2424 int rw;
2425 struct bio *bi;
2426 mdk_rdev_t *rdev;
2427 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2428 rw = 1;
2429 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2430 rw = 0;
2431 else
2432 continue;
2433
2434 bi = &sh->dev[i].req;
2435
2436 bi->bi_rw = rw;
2437 if (rw)
2438 bi->bi_end_io = raid5_end_write_request;
2439 else
2440 bi->bi_end_io = raid5_end_read_request;
2441
2442 rcu_read_lock();
2443 rdev = rcu_dereference(conf->disks[i].rdev);
2444 if (rdev && test_bit(Faulty, &rdev->flags))
2445 rdev = NULL;
2446 if (rdev)
2447 atomic_inc(&rdev->nr_pending);
2448 rcu_read_unlock();
2449
2450 if (rdev) {
2451 if (syncing)
2452 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2453
2454 bi->bi_bdev = rdev->bdev;
2455 PRINTK("for %llu schedule op %ld on disc %d\n",
2456 (unsigned long long)sh->sector, bi->bi_rw, i);
2457 atomic_inc(&sh->count);
2458 bi->bi_sector = sh->sector + rdev->data_offset;
2459 bi->bi_flags = 1 << BIO_UPTODATE;
2460 bi->bi_vcnt = 1;
2461 bi->bi_max_vecs = 1;
2462 bi->bi_idx = 0;
2463 bi->bi_io_vec = &sh->dev[i].vec;
2464 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2465 bi->bi_io_vec[0].bv_offset = 0;
2466 bi->bi_size = STRIPE_SIZE;
2467 bi->bi_next = NULL;
2468 if (rw == WRITE &&
2469 test_bit(R5_ReWrite, &sh->dev[i].flags))
2470 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2471 generic_make_request(bi);
2472 } else {
2473 if (rw == 1)
2474 set_bit(STRIPE_DEGRADED, &sh->state);
2475 PRINTK("skip op %ld on disc %d for sector %llu\n",
2476 bi->bi_rw, i, (unsigned long long)sh->sector);
2477 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2478 set_bit(STRIPE_HANDLE, &sh->state);
2479 }
2480 }
2481}
2482
2483static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2484{
2485 if (sh->raid_conf->level == 6)
2486 handle_stripe6(sh, tmp_page);
2487 else
2488 handle_stripe5(sh);
2489}
2490
2491
2492
Arjan van de Ven858119e2006-01-14 13:20:43 -08002493static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
2495 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2496 while (!list_empty(&conf->delayed_list)) {
2497 struct list_head *l = conf->delayed_list.next;
2498 struct stripe_head *sh;
2499 sh = list_entry(l, struct stripe_head, lru);
2500 list_del_init(l);
2501 clear_bit(STRIPE_DELAYED, &sh->state);
2502 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2503 atomic_inc(&conf->preread_active_stripes);
2504 list_add_tail(&sh->lru, &conf->handle_list);
2505 }
2506 }
2507}
2508
Arjan van de Ven858119e2006-01-14 13:20:43 -08002509static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07002510{
2511 /* device_lock is held */
2512 struct list_head head;
2513 list_add(&head, &conf->bitmap_list);
2514 list_del_init(&conf->bitmap_list);
2515 while (!list_empty(&head)) {
2516 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2517 list_del_init(&sh->lru);
2518 atomic_inc(&sh->count);
2519 __release_stripe(conf, sh);
2520 }
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523static void unplug_slaves(mddev_t *mddev)
2524{
2525 raid5_conf_t *conf = mddev_to_conf(mddev);
2526 int i;
2527
2528 rcu_read_lock();
2529 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002530 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002531 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2533
2534 atomic_inc(&rdev->nr_pending);
2535 rcu_read_unlock();
2536
2537 if (r_queue->unplug_fn)
2538 r_queue->unplug_fn(r_queue);
2539
2540 rdev_dec_pending(rdev, mddev);
2541 rcu_read_lock();
2542 }
2543 }
2544 rcu_read_unlock();
2545}
2546
2547static void raid5_unplug_device(request_queue_t *q)
2548{
2549 mddev_t *mddev = q->queuedata;
2550 raid5_conf_t *conf = mddev_to_conf(mddev);
2551 unsigned long flags;
2552
2553 spin_lock_irqsave(&conf->device_lock, flags);
2554
NeilBrown72626682005-09-09 16:23:54 -07002555 if (blk_remove_plug(q)) {
2556 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 md_wakeup_thread(mddev->thread);
2560
2561 spin_unlock_irqrestore(&conf->device_lock, flags);
2562
2563 unplug_slaves(mddev);
2564}
2565
2566static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2567 sector_t *error_sector)
2568{
2569 mddev_t *mddev = q->queuedata;
2570 raid5_conf_t *conf = mddev_to_conf(mddev);
2571 int i, ret = 0;
2572
2573 rcu_read_lock();
2574 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002575 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002576 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct block_device *bdev = rdev->bdev;
2578 request_queue_t *r_queue = bdev_get_queue(bdev);
2579
2580 if (!r_queue->issue_flush_fn)
2581 ret = -EOPNOTSUPP;
2582 else {
2583 atomic_inc(&rdev->nr_pending);
2584 rcu_read_unlock();
2585 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2586 error_sector);
2587 rdev_dec_pending(rdev, mddev);
2588 rcu_read_lock();
2589 }
2590 }
2591 }
2592 rcu_read_unlock();
2593 return ret;
2594}
2595
NeilBrownf022b2f2006-10-03 01:15:56 -07002596static int raid5_congested(void *data, int bits)
2597{
2598 mddev_t *mddev = data;
2599 raid5_conf_t *conf = mddev_to_conf(mddev);
2600
2601 /* No difference between reads and writes. Just check
2602 * how busy the stripe_cache is
2603 */
2604 if (conf->inactive_blocked)
2605 return 1;
2606 if (conf->quiesce)
2607 return 1;
2608 if (list_empty_careful(&conf->inactive_list))
2609 return 1;
2610
2611 return 0;
2612}
2613
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08002614/* We want read requests to align with chunks where possible,
2615 * but write requests don't need to.
2616 */
2617static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
2618{
2619 mddev_t *mddev = q->queuedata;
2620 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2621 int max;
2622 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2623 unsigned int bio_sectors = bio->bi_size >> 9;
2624
2625 if (bio_data_dir(bio))
2626 return biovec->bv_len; /* always allow writes to be mergeable */
2627
2628 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
2629 if (max < 0) max = 0;
2630 if (max <= biovec->bv_len && bio_sectors == 0)
2631 return biovec->bv_len;
2632 else
2633 return max;
2634}
2635
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002636static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 mddev_t *mddev = q->queuedata;
2639 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 unsigned int dd_idx, pd_idx;
2641 sector_t new_sector;
2642 sector_t logical_sector, last_sector;
2643 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01002644 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08002645 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
NeilBrowne5dcdd82005-09-09 16:23:41 -07002647 if (unlikely(bio_barrier(bi))) {
2648 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2649 return 0;
2650 }
2651
NeilBrown3d310eb2005-06-21 17:17:26 -07002652 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07002653
Jens Axboea3623572005-11-01 09:26:16 +01002654 disk_stat_inc(mddev->gendisk, ios[rw]);
2655 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2658 last_sector = bi->bi_sector + (bi->bi_size>>9);
2659 bi->bi_next = NULL;
2660 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2663 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002664 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002665
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002666 retry:
NeilBrownb578d552006-03-27 01:18:12 -08002667 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002668 if (likely(conf->expand_progress == MaxSector))
2669 disks = conf->raid_disks;
2670 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08002671 /* spinlock is needed as expand_progress may be
2672 * 64bit on a 32bit platform, and so it might be
2673 * possible to see a half-updated value
2674 * Ofcourse expand_progress could change after
2675 * the lock is dropped, so once we get a reference
2676 * to the stripe that we think it is, we will have
2677 * to check again.
2678 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002679 spin_lock_irq(&conf->device_lock);
2680 disks = conf->raid_disks;
2681 if (logical_sector >= conf->expand_progress)
2682 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002683 else {
2684 if (logical_sector >= conf->expand_lo) {
2685 spin_unlock_irq(&conf->device_lock);
2686 schedule();
2687 goto retry;
2688 }
2689 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002690 spin_unlock_irq(&conf->device_lock);
2691 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002692 data_disks = disks - conf->max_degraded;
2693
2694 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002695 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2697 (unsigned long long)new_sector,
2698 (unsigned long long)logical_sector);
2699
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002700 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002702 if (unlikely(conf->expand_progress != MaxSector)) {
2703 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08002704 * stripe, so we must do the range check again.
2705 * Expansion could still move past after this
2706 * test, but as we are holding a reference to
2707 * 'sh', we know that if that happens,
2708 * STRIPE_EXPANDING will get set and the expansion
2709 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002710 */
2711 int must_retry = 0;
2712 spin_lock_irq(&conf->device_lock);
2713 if (logical_sector < conf->expand_progress &&
2714 disks == conf->previous_raid_disks)
2715 /* mismatch, need to try again */
2716 must_retry = 1;
2717 spin_unlock_irq(&conf->device_lock);
2718 if (must_retry) {
2719 release_stripe(sh);
2720 goto retry;
2721 }
2722 }
NeilBrowne464eaf2006-03-27 01:18:14 -08002723 /* FIXME what if we get a false positive because these
2724 * are being updated.
2725 */
2726 if (logical_sector >= mddev->suspend_lo &&
2727 logical_sector < mddev->suspend_hi) {
2728 release_stripe(sh);
2729 schedule();
2730 goto retry;
2731 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002732
2733 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2734 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2735 /* Stripe is busy expanding or
2736 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 * and wait a while
2738 */
2739 raid5_unplug_device(mddev->queue);
2740 release_stripe(sh);
2741 schedule();
2742 goto retry;
2743 }
2744 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002745 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 } else {
2748 /* cannot get stripe for read-ahead, just give-up */
2749 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2750 finish_wait(&conf->wait_for_overlap, &w);
2751 break;
2752 }
2753
2754 }
2755 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08002756 remaining = --bi->bi_phys_segments;
2757 spin_unlock_irq(&conf->device_lock);
2758 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 int bytes = bi->bi_size;
2760
NeilBrown16a53ec2006-06-26 00:27:38 -07002761 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 md_write_end(mddev);
2763 bi->bi_size = 0;
2764 bi->bi_end_io(bi, bytes, 0);
2765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return 0;
2767}
2768
NeilBrown52c03292006-06-26 00:27:43 -07002769static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
NeilBrown52c03292006-06-26 00:27:43 -07002771 /* reshaping is quite different to recovery/resync so it is
2772 * handled quite separately ... here.
2773 *
2774 * On each call to sync_request, we gather one chunk worth of
2775 * destination stripes and flag them as expanding.
2776 * Then we find all the source stripes and request reads.
2777 * As the reads complete, handle_stripe will copy the data
2778 * into the destination stripe and release that stripe.
2779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2781 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08002782 int pd_idx;
2783 sector_t first_sector, last_sector;
NeilBrown52c03292006-06-26 00:27:43 -07002784 int raid_disks;
2785 int data_disks;
2786 int i;
2787 int dd_idx;
2788 sector_t writepos, safepos, gap;
2789
2790 if (sector_nr == 0 &&
2791 conf->expand_progress != 0) {
2792 /* restarting in the middle, skip the initial sectors */
2793 sector_nr = conf->expand_progress;
2794 sector_div(sector_nr, conf->raid_disks-1);
2795 *skipped = 1;
2796 return sector_nr;
2797 }
2798
2799 /* we update the metadata when there is more than 3Meg
2800 * in the block range (that is rather arbitrary, should
2801 * probably be time based) or when the data about to be
2802 * copied would over-write the source of the data at
2803 * the front of the range.
2804 * i.e. one new_stripe forward from expand_progress new_maps
2805 * to after where expand_lo old_maps to
2806 */
2807 writepos = conf->expand_progress +
2808 conf->chunk_size/512*(conf->raid_disks-1);
2809 sector_div(writepos, conf->raid_disks-1);
2810 safepos = conf->expand_lo;
2811 sector_div(safepos, conf->previous_raid_disks-1);
2812 gap = conf->expand_progress - conf->expand_lo;
2813
2814 if (writepos >= safepos ||
2815 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2816 /* Cannot proceed until we've updated the superblock... */
2817 wait_event(conf->wait_for_overlap,
2818 atomic_read(&conf->reshape_stripes)==0);
2819 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07002820 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07002821 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07002822 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07002823 kthread_should_stop());
2824 spin_lock_irq(&conf->device_lock);
2825 conf->expand_lo = mddev->reshape_position;
2826 spin_unlock_irq(&conf->device_lock);
2827 wake_up(&conf->wait_for_overlap);
2828 }
2829
2830 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2831 int j;
2832 int skipped = 0;
2833 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2834 sh = get_active_stripe(conf, sector_nr+i,
2835 conf->raid_disks, pd_idx, 0);
2836 set_bit(STRIPE_EXPANDING, &sh->state);
2837 atomic_inc(&conf->reshape_stripes);
2838 /* If any of this stripe is beyond the end of the old
2839 * array, then we need to zero those blocks
2840 */
2841 for (j=sh->disks; j--;) {
2842 sector_t s;
2843 if (j == sh->pd_idx)
2844 continue;
2845 s = compute_blocknr(sh, j);
2846 if (s < (mddev->array_size<<1)) {
2847 skipped = 1;
2848 continue;
2849 }
2850 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2851 set_bit(R5_Expanded, &sh->dev[j].flags);
2852 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2853 }
2854 if (!skipped) {
2855 set_bit(STRIPE_EXPAND_READY, &sh->state);
2856 set_bit(STRIPE_HANDLE, &sh->state);
2857 }
2858 release_stripe(sh);
2859 }
2860 spin_lock_irq(&conf->device_lock);
2861 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2862 spin_unlock_irq(&conf->device_lock);
2863 /* Ok, those stripe are ready. We can start scheduling
2864 * reads on the source stripes.
2865 * The source stripes are determined by mapping the first and last
2866 * block on the destination stripes.
2867 */
2868 raid_disks = conf->previous_raid_disks;
2869 data_disks = raid_disks - 1;
2870 first_sector =
2871 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2872 raid_disks, data_disks,
2873 &dd_idx, &pd_idx, conf);
2874 last_sector =
2875 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2876 *(conf->raid_disks-1) -1,
2877 raid_disks, data_disks,
2878 &dd_idx, &pd_idx, conf);
2879 if (last_sector >= (mddev->size<<1))
2880 last_sector = (mddev->size<<1)-1;
2881 while (first_sector <= last_sector) {
2882 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2883 sh = get_active_stripe(conf, first_sector,
2884 conf->previous_raid_disks, pd_idx, 0);
2885 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2886 set_bit(STRIPE_HANDLE, &sh->state);
2887 release_stripe(sh);
2888 first_sector += STRIPE_SECTORS;
2889 }
2890 return conf->chunk_size>>9;
2891}
2892
2893/* FIXME go_faster isn't used */
2894static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2895{
2896 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2897 struct stripe_head *sh;
2898 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07002900 sector_t max_sector = mddev->size << 1;
2901 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002902 int still_degraded = 0;
2903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
NeilBrown72626682005-09-09 16:23:54 -07002905 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /* just being told to finish up .. nothing much to do */
2907 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08002908 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2909 end_reshape(conf);
2910 return 0;
2911 }
NeilBrown72626682005-09-09 16:23:54 -07002912
2913 if (mddev->curr_resync < max_sector) /* aborted */
2914 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2915 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07002916 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07002917 conf->fullsync = 0;
2918 bitmap_close_sync(mddev->bitmap);
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08002922
NeilBrown52c03292006-06-26 00:27:43 -07002923 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2924 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08002925
NeilBrown16a53ec2006-06-26 00:27:38 -07002926 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 * to resync, then assert that we are finished, because there is
2928 * nothing we can do.
2929 */
NeilBrown3285edf2006-06-26 00:27:55 -07002930 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07002931 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07002932 sector_t rv = (mddev->size << 1) - sector_nr;
2933 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return rv;
2935 }
NeilBrown72626682005-09-09 16:23:54 -07002936 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08002937 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07002938 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
2939 /* we can skip this block, and probably more */
2940 sync_blocks /= STRIPE_SECTORS;
2941 *skipped = 1;
2942 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
NeilBrownccfcc3c2006-03-27 01:18:09 -08002945 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002946 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002948 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07002950 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08002952 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002954 /* Need to check if array will still be degraded after recovery/resync
2955 * We don't need to check the 'failed' flag as when that gets set,
2956 * recovery aborts.
2957 */
2958 for (i=0; i<mddev->raid_disks; i++)
2959 if (conf->disks[i].rdev == NULL)
2960 still_degraded = 1;
2961
2962 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
2963
2964 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 set_bit(STRIPE_SYNCING, &sh->state);
2966 clear_bit(STRIPE_INSYNC, &sh->state);
2967 spin_unlock(&sh->lock);
2968
NeilBrown16a53ec2006-06-26 00:27:38 -07002969 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 release_stripe(sh);
2971
2972 return STRIPE_SECTORS;
2973}
2974
2975/*
2976 * This is our raid5 kernel thread.
2977 *
2978 * We scan the hash table for stripes which can be handled now.
2979 * During the scan, completed stripes are saved for us by the interrupt
2980 * handler, so that they will not have to wait for our next wakeup.
2981 */
2982static void raid5d (mddev_t *mddev)
2983{
2984 struct stripe_head *sh;
2985 raid5_conf_t *conf = mddev_to_conf(mddev);
2986 int handled;
2987
2988 PRINTK("+++ raid5d active\n");
2989
2990 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 handled = 0;
2993 spin_lock_irq(&conf->device_lock);
2994 while (1) {
2995 struct list_head *first;
2996
NeilBrownae3c20c2006-07-10 04:44:17 -07002997 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07002998 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08002999 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003000 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08003001 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003002 conf->seq_write = seq;
3003 activate_bit_delay(conf);
3004 }
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (list_empty(&conf->handle_list) &&
3007 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
3008 !blk_queue_plugged(mddev->queue) &&
3009 !list_empty(&conf->delayed_list))
3010 raid5_activate_delayed(conf);
3011
3012 if (list_empty(&conf->handle_list))
3013 break;
3014
3015 first = conf->handle_list.next;
3016 sh = list_entry(first, struct stripe_head, lru);
3017
3018 list_del_init(first);
3019 atomic_inc(&sh->count);
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003020 BUG_ON(atomic_read(&sh->count)!= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 spin_unlock_irq(&conf->device_lock);
3022
3023 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003024 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 release_stripe(sh);
3026
3027 spin_lock_irq(&conf->device_lock);
3028 }
3029 PRINTK("%d stripes handled\n", handled);
3030
3031 spin_unlock_irq(&conf->device_lock);
3032
3033 unplug_slaves(mddev);
3034
3035 PRINTK("--- raid5d inactive\n");
3036}
3037
NeilBrown3f294f42005-11-08 21:39:25 -08003038static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003039raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003040{
NeilBrown007583c2005-11-08 21:39:30 -08003041 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003042 if (conf)
3043 return sprintf(page, "%d\n", conf->max_nr_stripes);
3044 else
3045 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003046}
3047
3048static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003049raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08003050{
NeilBrown007583c2005-11-08 21:39:30 -08003051 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08003052 char *end;
3053 int new;
3054 if (len >= PAGE_SIZE)
3055 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08003056 if (!conf)
3057 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08003058
3059 new = simple_strtoul(page, &end, 10);
3060 if (!*page || (*end && *end != '\n') )
3061 return -EINVAL;
3062 if (new <= 16 || new > 32768)
3063 return -EINVAL;
3064 while (new < conf->max_nr_stripes) {
3065 if (drop_one_stripe(conf))
3066 conf->max_nr_stripes--;
3067 else
3068 break;
3069 }
3070 while (new > conf->max_nr_stripes) {
3071 if (grow_one_stripe(conf))
3072 conf->max_nr_stripes++;
3073 else break;
3074 }
3075 return len;
3076}
NeilBrown007583c2005-11-08 21:39:30 -08003077
NeilBrown96de1e62005-11-08 21:39:39 -08003078static struct md_sysfs_entry
3079raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3080 raid5_show_stripe_cache_size,
3081 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003082
3083static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003084stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003085{
NeilBrown007583c2005-11-08 21:39:30 -08003086 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003087 if (conf)
3088 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3089 else
3090 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003091}
3092
NeilBrown96de1e62005-11-08 21:39:39 -08003093static struct md_sysfs_entry
3094raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003095
NeilBrown007583c2005-11-08 21:39:30 -08003096static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003097 &raid5_stripecache_size.attr,
3098 &raid5_stripecache_active.attr,
3099 NULL,
3100};
NeilBrown007583c2005-11-08 21:39:30 -08003101static struct attribute_group raid5_attrs_group = {
3102 .name = NULL,
3103 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003104};
3105
NeilBrown72626682005-09-09 16:23:54 -07003106static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 raid5_conf_t *conf;
3109 int raid_disk, memory;
3110 mdk_rdev_t *rdev;
3111 struct disk_info *disk;
3112 struct list_head *tmp;
NeilBrown02c2de82006-10-03 01:15:47 -07003113 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
NeilBrown16a53ec2006-06-26 00:27:38 -07003115 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3116 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003117 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return -EIO;
3119 }
3120
NeilBrownf6705572006-03-27 01:18:11 -08003121 if (mddev->reshape_position != MaxSector) {
3122 /* Check that we can continue the reshape.
3123 * Currently only disks can change, it must
3124 * increase, and we must be past the point where
3125 * a stripe over-writes itself
3126 */
3127 sector_t here_new, here_old;
3128 int old_disks;
3129
3130 if (mddev->new_level != mddev->level ||
3131 mddev->new_layout != mddev->layout ||
3132 mddev->new_chunk != mddev->chunk_size) {
3133 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3134 mdname(mddev));
3135 return -EINVAL;
3136 }
3137 if (mddev->delta_disks <= 0) {
3138 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3139 mdname(mddev));
3140 return -EINVAL;
3141 }
3142 old_disks = mddev->raid_disks - mddev->delta_disks;
3143 /* reshape_position must be on a new-stripe boundary, and one
3144 * further up in new geometry must map after here in old geometry.
3145 */
3146 here_new = mddev->reshape_position;
3147 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3148 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3149 return -EINVAL;
3150 }
3151 /* here_new is the stripe we will write to */
3152 here_old = mddev->reshape_position;
3153 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3154 /* here_old is the first stripe that we might need to read from */
3155 if (here_new >= here_old) {
3156 /* Reading from the same stripe as writing to - bad */
3157 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3158 return -EINVAL;
3159 }
3160 printk(KERN_INFO "raid5: reshape will continue\n");
3161 /* OK, we should be able to continue; */
3162 }
3163
3164
NeilBrownb55e6bf2006-03-27 01:18:06 -08003165 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if ((conf = mddev->private) == NULL)
3167 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08003168 if (mddev->reshape_position == MaxSector) {
3169 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3170 } else {
3171 conf->raid_disks = mddev->raid_disks;
3172 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3173 }
3174
3175 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08003176 GFP_KERNEL);
3177 if (!conf->disks)
3178 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 conf->mddev = mddev;
3181
NeilBrownfccddba2006-01-06 00:20:33 -08003182 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
NeilBrown16a53ec2006-06-26 00:27:38 -07003185 if (mddev->level == 6) {
3186 conf->spare_page = alloc_page(GFP_KERNEL);
3187 if (!conf->spare_page)
3188 goto abort;
3189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 spin_lock_init(&conf->device_lock);
3191 init_waitqueue_head(&conf->wait_for_stripe);
3192 init_waitqueue_head(&conf->wait_for_overlap);
3193 INIT_LIST_HEAD(&conf->handle_list);
3194 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07003195 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 INIT_LIST_HEAD(&conf->inactive_list);
3197 atomic_set(&conf->active_stripes, 0);
3198 atomic_set(&conf->preread_active_stripes, 0);
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3201
3202 ITERATE_RDEV(mddev,rdev,tmp) {
3203 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08003204 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 || raid_disk < 0)
3206 continue;
3207 disk = conf->disks + raid_disk;
3208
3209 disk->rdev = rdev;
3210
NeilBrownb2d444d2005-11-08 21:39:31 -08003211 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 char b[BDEVNAME_SIZE];
3213 printk(KERN_INFO "raid5: device %s operational as raid"
3214 " disk %d\n", bdevname(rdev->bdev,b),
3215 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07003216 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218 }
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003221 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 */
NeilBrown02c2de82006-10-03 01:15:47 -07003223 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 conf->mddev = mddev;
3225 conf->chunk_size = mddev->chunk_size;
3226 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003227 if (conf->level == 6)
3228 conf->max_degraded = 2;
3229 else
3230 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 conf->algorithm = mddev->layout;
3232 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08003233 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 /* device size must be a multiple of chunk size */
3236 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07003237 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
NeilBrown16a53ec2006-06-26 00:27:38 -07003239 if (conf->level == 6 && conf->raid_disks < 4) {
3240 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3241 mdname(mddev), conf->raid_disks);
3242 goto abort;
3243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (!conf->chunk_size || conf->chunk_size % 4) {
3245 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3246 conf->chunk_size, mdname(mddev));
3247 goto abort;
3248 }
3249 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3250 printk(KERN_ERR
3251 "raid5: unsupported parity algorithm %d for %s\n",
3252 conf->algorithm, mdname(mddev));
3253 goto abort;
3254 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003255 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 printk(KERN_ERR "raid5: not enough operational devices for %s"
3257 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07003258 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto abort;
3260 }
3261
NeilBrown16a53ec2006-06-26 00:27:38 -07003262 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003264 if (mddev->ok_start_degraded)
3265 printk(KERN_WARNING
3266 "raid5: starting dirty degraded array: %s"
3267 "- data corruption possible.\n",
3268 mdname(mddev));
3269 else {
3270 printk(KERN_ERR
3271 "raid5: cannot start dirty degraded array for %s\n",
3272 mdname(mddev));
3273 goto abort;
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
3276
3277 {
3278 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3279 if (!mddev->thread) {
3280 printk(KERN_ERR
3281 "raid5: couldn't allocate thread for %s\n",
3282 mdname(mddev));
3283 goto abort;
3284 }
3285 }
NeilBrown50368052005-12-12 02:39:17 -08003286 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3288 if (grow_stripes(conf, conf->max_nr_stripes)) {
3289 printk(KERN_ERR
3290 "raid5: couldn't allocate %dkB for buffers\n", memory);
3291 shrink_stripes(conf);
3292 md_unregister_thread(mddev->thread);
3293 goto abort;
3294 } else
3295 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3296 memory, mdname(mddev));
3297
3298 if (mddev->degraded == 0)
3299 printk("raid5: raid level %d set %s active with %d out of %d"
3300 " devices, algorithm %d\n", conf->level, mdname(mddev),
3301 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3302 conf->algorithm);
3303 else
3304 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3305 " out of %d devices, algorithm %d\n", conf->level,
3306 mdname(mddev), mddev->raid_disks - mddev->degraded,
3307 mddev->raid_disks, conf->algorithm);
3308
3309 print_raid5_conf(conf);
3310
NeilBrownf6705572006-03-27 01:18:11 -08003311 if (conf->expand_progress != MaxSector) {
3312 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08003313 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08003314 atomic_set(&conf->reshape_stripes, 0);
3315 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3316 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3317 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3318 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3319 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3320 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08003321 }
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07003324 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 */
3326 {
NeilBrown16a53ec2006-06-26 00:27:38 -07003327 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3328 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07003329 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3331 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3332 }
3333
3334 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08003335 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07003336
3337 mddev->queue->unplug_fn = raid5_unplug_device;
3338 mddev->queue->issue_flush_fn = raid5_issue_flush;
NeilBrownf022b2f2006-10-03 01:15:56 -07003339 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
3340 mddev->queue->backing_dev_info.congested_data = mddev;
3341
NeilBrown16a53ec2006-06-26 00:27:38 -07003342 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3343 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07003344
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003345 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return 0;
3348abort:
3349 if (conf) {
3350 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07003351 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003352 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08003353 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 kfree(conf);
3355 }
3356 mddev->private = NULL;
3357 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3358 return -EIO;
3359}
3360
3361
3362
NeilBrown3f294f42005-11-08 21:39:25 -08003363static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
3365 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3366
3367 md_unregister_thread(mddev->thread);
3368 mddev->thread = NULL;
3369 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08003370 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08003372 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003373 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08003374 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 mddev->private = NULL;
3376 return 0;
3377}
3378
3379#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003380static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 int i;
3383
NeilBrown16a53ec2006-06-26 00:27:38 -07003384 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3385 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3386 seq_printf(seq, "sh %llu, count %d.\n",
3387 (unsigned long long)sh->sector, atomic_read(&sh->count));
3388 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003389 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003390 seq_printf(seq, "(cache%d: %p %ld) ",
3391 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003393 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
NeilBrown16a53ec2006-06-26 00:27:38 -07003396static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08003399 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int i;
3401
3402 spin_lock_irq(&conf->device_lock);
3403 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08003404 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 if (sh->raid_conf != conf)
3406 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07003407 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409 }
3410 spin_unlock_irq(&conf->device_lock);
3411}
3412#endif
3413
3414static void status (struct seq_file *seq, mddev_t *mddev)
3415{
3416 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3417 int i;
3418
3419 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07003420 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 for (i = 0; i < conf->raid_disks; i++)
3422 seq_printf (seq, "%s",
3423 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08003424 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 seq_printf (seq, "]");
3426#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003427 seq_printf (seq, "\n");
3428 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429#endif
3430}
3431
3432static void print_raid5_conf (raid5_conf_t *conf)
3433{
3434 int i;
3435 struct disk_info *tmp;
3436
3437 printk("RAID5 conf printout:\n");
3438 if (!conf) {
3439 printk("(conf==NULL)\n");
3440 return;
3441 }
NeilBrown02c2de82006-10-03 01:15:47 -07003442 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3443 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 for (i = 0; i < conf->raid_disks; i++) {
3446 char b[BDEVNAME_SIZE];
3447 tmp = conf->disks + i;
3448 if (tmp->rdev)
3449 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08003450 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 bdevname(tmp->rdev->bdev,b));
3452 }
3453}
3454
3455static int raid5_spare_active(mddev_t *mddev)
3456{
3457 int i;
3458 raid5_conf_t *conf = mddev->private;
3459 struct disk_info *tmp;
3460
3461 for (i = 0; i < conf->raid_disks; i++) {
3462 tmp = conf->disks + i;
3463 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08003464 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07003465 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3466 unsigned long flags;
3467 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07003469 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
3471 }
3472 print_raid5_conf(conf);
3473 return 0;
3474}
3475
3476static int raid5_remove_disk(mddev_t *mddev, int number)
3477{
3478 raid5_conf_t *conf = mddev->private;
3479 int err = 0;
3480 mdk_rdev_t *rdev;
3481 struct disk_info *p = conf->disks + number;
3482
3483 print_raid5_conf(conf);
3484 rdev = p->rdev;
3485 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003486 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 atomic_read(&rdev->nr_pending)) {
3488 err = -EBUSY;
3489 goto abort;
3490 }
3491 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003492 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (atomic_read(&rdev->nr_pending)) {
3494 /* lost the race, try later */
3495 err = -EBUSY;
3496 p->rdev = rdev;
3497 }
3498 }
3499abort:
3500
3501 print_raid5_conf(conf);
3502 return err;
3503}
3504
3505static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3506{
3507 raid5_conf_t *conf = mddev->private;
3508 int found = 0;
3509 int disk;
3510 struct disk_info *p;
3511
NeilBrown16a53ec2006-06-26 00:27:38 -07003512 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 /* no point adding a device */
3514 return 0;
3515
3516 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003517 * find the disk ... but prefer rdev->saved_raid_disk
3518 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003520 if (rdev->saved_raid_disk >= 0 &&
3521 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3522 disk = rdev->saved_raid_disk;
3523 else
3524 disk = 0;
3525 for ( ; disk < conf->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003527 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 rdev->raid_disk = disk;
3529 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07003530 if (rdev->saved_raid_disk != disk)
3531 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08003532 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 break;
3534 }
3535 print_raid5_conf(conf);
3536 return found;
3537}
3538
3539static int raid5_resize(mddev_t *mddev, sector_t sectors)
3540{
3541 /* no resync is happening, and there is enough space
3542 * on all devices, so we can resize.
3543 * We need to make sure resync covers any new space.
3544 * If the array is shrinking we should possibly wait until
3545 * any io in the removed space completes, but it hardly seems
3546 * worth it.
3547 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003548 raid5_conf_t *conf = mddev_to_conf(mddev);
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003551 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 set_capacity(mddev->gendisk, mddev->array_size << 1);
3553 mddev->changed = 1;
3554 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3555 mddev->recovery_cp = mddev->size << 1;
3556 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3557 }
3558 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003559 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return 0;
3561}
3562
NeilBrown29269552006-03-27 01:18:10 -08003563#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003564static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08003565{
3566 raid5_conf_t *conf = mddev_to_conf(mddev);
3567 int err;
NeilBrown29269552006-03-27 01:18:10 -08003568
NeilBrown63c70c42006-03-27 01:18:13 -08003569 if (mddev->delta_disks < 0 ||
3570 mddev->new_level != mddev->level)
3571 return -EINVAL; /* Cannot shrink array or change level yet */
3572 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08003573 return 0; /* nothing to do */
3574
3575 /* Can only proceed if there are plenty of stripe_heads.
3576 * We need a minimum of one full stripe,, and for sensible progress
3577 * it is best to have about 4 times that.
3578 * If we require 4 times, then the default 256 4K stripe_heads will
3579 * allow for chunk sizes up to 256K, which is probably OK.
3580 * If the chunk size is greater, user-space should request more
3581 * stripe_heads first.
3582 */
NeilBrown63c70c42006-03-27 01:18:13 -08003583 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3584 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08003585 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3586 (mddev->chunk_size / STRIPE_SIZE)*4);
3587 return -ENOSPC;
3588 }
3589
NeilBrown63c70c42006-03-27 01:18:13 -08003590 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3591 if (err)
3592 return err;
3593
3594 /* looks like we might be able to manage this */
3595 return 0;
3596}
3597
3598static int raid5_start_reshape(mddev_t *mddev)
3599{
3600 raid5_conf_t *conf = mddev_to_conf(mddev);
3601 mdk_rdev_t *rdev;
3602 struct list_head *rtmp;
3603 int spares = 0;
3604 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07003605 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08003606
3607 if (mddev->degraded ||
3608 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3609 return -EBUSY;
3610
NeilBrown29269552006-03-27 01:18:10 -08003611 ITERATE_RDEV(mddev, rdev, rtmp)
3612 if (rdev->raid_disk < 0 &&
3613 !test_bit(Faulty, &rdev->flags))
3614 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08003615
3616 if (spares < mddev->delta_disks-1)
NeilBrown29269552006-03-27 01:18:10 -08003617 /* Not enough devices even to make a degraded array
3618 * of that size
3619 */
3620 return -EINVAL;
3621
NeilBrownf6705572006-03-27 01:18:11 -08003622 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08003623 spin_lock_irq(&conf->device_lock);
3624 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003625 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08003626 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003627 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08003628 spin_unlock_irq(&conf->device_lock);
3629
3630 /* Add some new drives, as many as will fit.
3631 * We know there are enough to make the newly sized array work.
3632 */
3633 ITERATE_RDEV(mddev, rdev, rtmp)
3634 if (rdev->raid_disk < 0 &&
3635 !test_bit(Faulty, &rdev->flags)) {
3636 if (raid5_add_disk(mddev, rdev)) {
3637 char nm[20];
3638 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08003639 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003640 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08003641 sprintf(nm, "rd%d", rdev->raid_disk);
3642 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3643 } else
3644 break;
3645 }
3646
NeilBrownc04be0a2006-10-03 01:15:53 -07003647 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003648 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07003649 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003650 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08003651 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07003652 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08003653
NeilBrown29269552006-03-27 01:18:10 -08003654 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3655 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3656 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3657 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3658 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3659 "%s_reshape");
3660 if (!mddev->sync_thread) {
3661 mddev->recovery = 0;
3662 spin_lock_irq(&conf->device_lock);
3663 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3664 conf->expand_progress = MaxSector;
3665 spin_unlock_irq(&conf->device_lock);
3666 return -EAGAIN;
3667 }
3668 md_wakeup_thread(mddev->sync_thread);
3669 md_new_event(mddev);
3670 return 0;
3671}
3672#endif
3673
3674static void end_reshape(raid5_conf_t *conf)
3675{
3676 struct block_device *bdev;
3677
NeilBrownf6705572006-03-27 01:18:11 -08003678 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3679 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3680 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3681 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08003682
NeilBrownf6705572006-03-27 01:18:11 -08003683 bdev = bdget_disk(conf->mddev->gendisk, 0);
3684 if (bdev) {
3685 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown0692c6b2006-11-08 17:44:48 -08003686 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
NeilBrownf6705572006-03-27 01:18:11 -08003687 mutex_unlock(&bdev->bd_inode->i_mutex);
3688 bdput(bdev);
3689 }
3690 spin_lock_irq(&conf->device_lock);
3691 conf->expand_progress = MaxSector;
3692 spin_unlock_irq(&conf->device_lock);
3693 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07003694
3695 /* read-ahead size must cover two whole stripes, which is
3696 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3697 */
3698 {
3699 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3700 int stripe = data_disks *
3701 (conf->mddev->chunk_size / PAGE_SIZE);
3702 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3703 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3704 }
NeilBrown29269552006-03-27 01:18:10 -08003705 }
NeilBrown29269552006-03-27 01:18:10 -08003706}
3707
NeilBrown72626682005-09-09 16:23:54 -07003708static void raid5_quiesce(mddev_t *mddev, int state)
3709{
3710 raid5_conf_t *conf = mddev_to_conf(mddev);
3711
3712 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08003713 case 2: /* resume for a suspend */
3714 wake_up(&conf->wait_for_overlap);
3715 break;
3716
NeilBrown72626682005-09-09 16:23:54 -07003717 case 1: /* stop all writes */
3718 spin_lock_irq(&conf->device_lock);
3719 conf->quiesce = 1;
3720 wait_event_lock_irq(conf->wait_for_stripe,
3721 atomic_read(&conf->active_stripes) == 0,
3722 conf->device_lock, /* nothing */);
3723 spin_unlock_irq(&conf->device_lock);
3724 break;
3725
3726 case 0: /* re-enable writes */
3727 spin_lock_irq(&conf->device_lock);
3728 conf->quiesce = 0;
3729 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08003730 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07003731 spin_unlock_irq(&conf->device_lock);
3732 break;
3733 }
NeilBrown72626682005-09-09 16:23:54 -07003734}
NeilBrownb15c2e52006-01-06 00:20:16 -08003735
NeilBrown16a53ec2006-06-26 00:27:38 -07003736static struct mdk_personality raid6_personality =
3737{
3738 .name = "raid6",
3739 .level = 6,
3740 .owner = THIS_MODULE,
3741 .make_request = make_request,
3742 .run = run,
3743 .stop = stop,
3744 .status = status,
3745 .error_handler = error,
3746 .hot_add_disk = raid5_add_disk,
3747 .hot_remove_disk= raid5_remove_disk,
3748 .spare_active = raid5_spare_active,
3749 .sync_request = sync_request,
3750 .resize = raid5_resize,
3751 .quiesce = raid5_quiesce,
3752};
NeilBrown2604b702006-01-06 00:20:36 -08003753static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
3755 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08003756 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 .owner = THIS_MODULE,
3758 .make_request = make_request,
3759 .run = run,
3760 .stop = stop,
3761 .status = status,
3762 .error_handler = error,
3763 .hot_add_disk = raid5_add_disk,
3764 .hot_remove_disk= raid5_remove_disk,
3765 .spare_active = raid5_spare_active,
3766 .sync_request = sync_request,
3767 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08003768#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003769 .check_reshape = raid5_check_reshape,
3770 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08003771#endif
NeilBrown72626682005-09-09 16:23:54 -07003772 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773};
3774
NeilBrown2604b702006-01-06 00:20:36 -08003775static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
NeilBrown2604b702006-01-06 00:20:36 -08003777 .name = "raid4",
3778 .level = 4,
3779 .owner = THIS_MODULE,
3780 .make_request = make_request,
3781 .run = run,
3782 .stop = stop,
3783 .status = status,
3784 .error_handler = error,
3785 .hot_add_disk = raid5_add_disk,
3786 .hot_remove_disk= raid5_remove_disk,
3787 .spare_active = raid5_spare_active,
3788 .sync_request = sync_request,
3789 .resize = raid5_resize,
3790 .quiesce = raid5_quiesce,
3791};
3792
3793static int __init raid5_init(void)
3794{
NeilBrown16a53ec2006-06-26 00:27:38 -07003795 int e;
3796
3797 e = raid6_select_algo();
3798 if ( e )
3799 return e;
3800 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003801 register_md_personality(&raid5_personality);
3802 register_md_personality(&raid4_personality);
3803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
NeilBrown2604b702006-01-06 00:20:36 -08003806static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
NeilBrown16a53ec2006-06-26 00:27:38 -07003808 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003809 unregister_md_personality(&raid5_personality);
3810 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
3813module_init(raid5_init);
3814module_exit(raid5_exit);
3815MODULE_LICENSE("GPL");
3816MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003817MODULE_ALIAS("md-raid5");
3818MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08003819MODULE_ALIAS("md-level-5");
3820MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07003821MODULE_ALIAS("md-personality-8"); /* RAID6 */
3822MODULE_ALIAS("md-raid6");
3823MODULE_ALIAS("md-level-6");
3824
3825/* This used to be two separate modules, they were: */
3826MODULE_ALIAS("raid5");
3827MODULE_ALIAS("raid6");