[PATCH] md: remove some old ifdefed-out code from raid5.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / md / raid5.c
CommitLineData
1da177e4
LT
1/*
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
16a53ecc 5 * Copyright (C) 2002, 2003 H. Peter Anvin
1da177e4 6 *
16a53ecc
N
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
1da177e4
LT
10 *
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
ae3c20cc
N
21/*
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 */
1da177e4 45
1da177e4
LT
46#include <linux/module.h>
47#include <linux/slab.h>
1da177e4
LT
48#include <linux/highmem.h>
49#include <linux/bitops.h>
f6705578 50#include <linux/kthread.h>
1da177e4 51#include <asm/atomic.h>
16a53ecc 52#include "raid6.h"
1da177e4 53
72626685
N
54#include <linux/raid/bitmap.h>
55
1da177e4
LT
56/*
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
fccddba0 65#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
1da177e4
LT
66#define HASH_MASK (NR_HASH - 1)
67
fccddba0 68#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
1da177e4
LT
69
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
16a53ecc
N
97#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}
1da177e4
LT
107static void print_raid5_conf (raid5_conf_t *conf);
108
858119e1 109static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
1da177e4
LT
110{
111 if (atomic_dec_and_test(&sh->count)) {
78bafebd
ES
112 BUG_ON(!list_empty(&sh->lru));
113 BUG_ON(atomic_read(&conf->active_stripes)==0);
1da177e4 114 if (test_bit(STRIPE_HANDLE, &sh->state)) {
7c785b7a 115 if (test_bit(STRIPE_DELAYED, &sh->state)) {
1da177e4 116 list_add_tail(&sh->lru, &conf->delayed_list);
7c785b7a
N
117 blk_plug_device(conf->mddev->queue);
118 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
ae3c20cc 119 sh->bm_seq - conf->seq_write > 0) {
72626685 120 list_add_tail(&sh->lru, &conf->bitmap_list);
7c785b7a
N
121 blk_plug_device(conf->mddev->queue);
122 } else {
72626685 123 clear_bit(STRIPE_BIT_DELAY, &sh->state);
1da177e4 124 list_add_tail(&sh->lru, &conf->handle_list);
72626685 125 }
1da177e4
LT
126 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 }
1da177e4 133 atomic_dec(&conf->active_stripes);
ccfcc3c1
N
134 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
135 list_add_tail(&sh->lru, &conf->inactive_list);
1da177e4 136 wake_up(&conf->wait_for_stripe);
46031f9a
RBJ
137 if (conf->retry_read_aligned)
138 md_wakeup_thread(conf->mddev->thread);
ccfcc3c1 139 }
1da177e4
LT
140 }
141 }
142}
143static void release_stripe(struct stripe_head *sh)
144{
145 raid5_conf_t *conf = sh->raid_conf;
146 unsigned long flags;
16a53ecc 147
1da177e4
LT
148 spin_lock_irqsave(&conf->device_lock, flags);
149 __release_stripe(conf, sh);
150 spin_unlock_irqrestore(&conf->device_lock, flags);
151}
152
fccddba0 153static inline void remove_hash(struct stripe_head *sh)
1da177e4
LT
154{
155 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
156
fccddba0 157 hlist_del_init(&sh->hash);
1da177e4
LT
158}
159
16a53ecc 160static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
1da177e4 161{
fccddba0 162 struct hlist_head *hp = stripe_hash(conf, sh->sector);
1da177e4
LT
163
164 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
165
166 CHECK_DEVLOCK();
fccddba0 167 hlist_add_head(&sh->hash, hp);
1da177e4
LT
168}
169
170
171/* find an idle stripe, make sure it is unhashed, and return it. */
172static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
173{
174 struct stripe_head *sh = NULL;
175 struct list_head *first;
176
177 CHECK_DEVLOCK();
178 if (list_empty(&conf->inactive_list))
179 goto out;
180 first = conf->inactive_list.next;
181 sh = list_entry(first, struct stripe_head, lru);
182 list_del_init(first);
183 remove_hash(sh);
184 atomic_inc(&conf->active_stripes);
185out:
186 return sh;
187}
188
189static void shrink_buffers(struct stripe_head *sh, int num)
190{
191 struct page *p;
192 int i;
193
194 for (i=0; i<num ; i++) {
195 p = sh->dev[i].page;
196 if (!p)
197 continue;
198 sh->dev[i].page = NULL;
2d1f3b5d 199 put_page(p);
1da177e4
LT
200 }
201}
202
203static int grow_buffers(struct stripe_head *sh, int num)
204{
205 int i;
206
207 for (i=0; i<num; i++) {
208 struct page *page;
209
210 if (!(page = alloc_page(GFP_KERNEL))) {
211 return 1;
212 }
213 sh->dev[i].page = page;
214 }
215 return 0;
216}
217
218static void raid5_build_block (struct stripe_head *sh, int i);
219
7ecaa1e6 220static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
1da177e4
LT
221{
222 raid5_conf_t *conf = sh->raid_conf;
7ecaa1e6 223 int i;
1da177e4 224
78bafebd
ES
225 BUG_ON(atomic_read(&sh->count) != 0);
226 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1da177e4
LT
227
228 CHECK_DEVLOCK();
229 PRINTK("init_stripe called, stripe %llu\n",
230 (unsigned long long)sh->sector);
231
232 remove_hash(sh);
16a53ecc 233
1da177e4
LT
234 sh->sector = sector;
235 sh->pd_idx = pd_idx;
236 sh->state = 0;
237
7ecaa1e6
N
238 sh->disks = disks;
239
240 for (i = sh->disks; i--; ) {
1da177e4
LT
241 struct r5dev *dev = &sh->dev[i];
242
243 if (dev->toread || dev->towrite || dev->written ||
244 test_bit(R5_LOCKED, &dev->flags)) {
245 printk("sector=%llx i=%d %p %p %p %d\n",
246 (unsigned long long)sh->sector, i, dev->toread,
247 dev->towrite, dev->written,
248 test_bit(R5_LOCKED, &dev->flags));
249 BUG();
250 }
251 dev->flags = 0;
252 raid5_build_block(sh, i);
253 }
254 insert_hash(conf, sh);
255}
256
7ecaa1e6 257static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
1da177e4
LT
258{
259 struct stripe_head *sh;
fccddba0 260 struct hlist_node *hn;
1da177e4
LT
261
262 CHECK_DEVLOCK();
263 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
fccddba0 264 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
7ecaa1e6 265 if (sh->sector == sector && sh->disks == disks)
1da177e4
LT
266 return sh;
267 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
268 return NULL;
269}
270
271static void unplug_slaves(mddev_t *mddev);
272static void raid5_unplug_device(request_queue_t *q);
273
7ecaa1e6
N
274static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
275 int pd_idx, int noblock)
1da177e4
LT
276{
277 struct stripe_head *sh;
278
279 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
280
281 spin_lock_irq(&conf->device_lock);
282
283 do {
72626685
N
284 wait_event_lock_irq(conf->wait_for_stripe,
285 conf->quiesce == 0,
286 conf->device_lock, /* nothing */);
7ecaa1e6 287 sh = __find_stripe(conf, sector, disks);
1da177e4
LT
288 if (!sh) {
289 if (!conf->inactive_blocked)
290 sh = get_free_stripe(conf);
291 if (noblock && sh == NULL)
292 break;
293 if (!sh) {
294 conf->inactive_blocked = 1;
295 wait_event_lock_irq(conf->wait_for_stripe,
296 !list_empty(&conf->inactive_list) &&
5036805b
N
297 (atomic_read(&conf->active_stripes)
298 < (conf->max_nr_stripes *3/4)
1da177e4
LT
299 || !conf->inactive_blocked),
300 conf->device_lock,
f4370781 301 raid5_unplug_device(conf->mddev->queue)
1da177e4
LT
302 );
303 conf->inactive_blocked = 0;
304 } else
7ecaa1e6 305 init_stripe(sh, sector, pd_idx, disks);
1da177e4
LT
306 } else {
307 if (atomic_read(&sh->count)) {
78bafebd 308 BUG_ON(!list_empty(&sh->lru));
1da177e4
LT
309 } else {
310 if (!test_bit(STRIPE_HANDLE, &sh->state))
311 atomic_inc(&conf->active_stripes);
ff4e8d9a
N
312 if (list_empty(&sh->lru) &&
313 !test_bit(STRIPE_EXPANDING, &sh->state))
16a53ecc
N
314 BUG();
315 list_del_init(&sh->lru);
1da177e4
LT
316 }
317 }
318 } while (sh == NULL);
319
320 if (sh)
321 atomic_inc(&sh->count);
322
323 spin_unlock_irq(&conf->device_lock);
324 return sh;
325}
326
3f294f4f 327static int grow_one_stripe(raid5_conf_t *conf)
1da177e4
LT
328{
329 struct stripe_head *sh;
3f294f4f
N
330 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
331 if (!sh)
332 return 0;
333 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
334 sh->raid_conf = conf;
335 spin_lock_init(&sh->lock);
336
337 if (grow_buffers(sh, conf->raid_disks)) {
338 shrink_buffers(sh, conf->raid_disks);
339 kmem_cache_free(conf->slab_cache, sh);
340 return 0;
341 }
7ecaa1e6 342 sh->disks = conf->raid_disks;
3f294f4f
N
343 /* we just created an active stripe so... */
344 atomic_set(&sh->count, 1);
345 atomic_inc(&conf->active_stripes);
346 INIT_LIST_HEAD(&sh->lru);
347 release_stripe(sh);
348 return 1;
349}
350
351static int grow_stripes(raid5_conf_t *conf, int num)
352{
e18b890b 353 struct kmem_cache *sc;
1da177e4
LT
354 int devs = conf->raid_disks;
355
ad01c9e3
N
356 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
357 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
358 conf->active_name = 0;
359 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1da177e4
LT
360 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
361 0, 0, NULL, NULL);
362 if (!sc)
363 return 1;
364 conf->slab_cache = sc;
ad01c9e3 365 conf->pool_size = devs;
16a53ecc 366 while (num--)
3f294f4f 367 if (!grow_one_stripe(conf))
1da177e4 368 return 1;
1da177e4
LT
369 return 0;
370}
29269553
N
371
372#ifdef CONFIG_MD_RAID5_RESHAPE
ad01c9e3
N
373static int resize_stripes(raid5_conf_t *conf, int newsize)
374{
375 /* Make all the stripes able to hold 'newsize' devices.
376 * New slots in each stripe get 'page' set to a new page.
377 *
378 * This happens in stages:
379 * 1/ create a new kmem_cache and allocate the required number of
380 * stripe_heads.
381 * 2/ gather all the old stripe_heads and tranfer the pages across
382 * to the new stripe_heads. This will have the side effect of
383 * freezing the array as once all stripe_heads have been collected,
384 * no IO will be possible. Old stripe heads are freed once their
385 * pages have been transferred over, and the old kmem_cache is
386 * freed when all stripes are done.
387 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
388 * we simple return a failre status - no need to clean anything up.
389 * 4/ allocate new pages for the new slots in the new stripe_heads.
390 * If this fails, we don't bother trying the shrink the
391 * stripe_heads down again, we just leave them as they are.
392 * As each stripe_head is processed the new one is released into
393 * active service.
394 *
395 * Once step2 is started, we cannot afford to wait for a write,
396 * so we use GFP_NOIO allocations.
397 */
398 struct stripe_head *osh, *nsh;
399 LIST_HEAD(newstripes);
400 struct disk_info *ndisks;
401 int err = 0;
e18b890b 402 struct kmem_cache *sc;
ad01c9e3
N
403 int i;
404
405 if (newsize <= conf->pool_size)
406 return 0; /* never bother to shrink */
407
408 /* Step 1 */
409 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
410 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
411 0, 0, NULL, NULL);
412 if (!sc)
413 return -ENOMEM;
414
415 for (i = conf->max_nr_stripes; i; i--) {
416 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
417 if (!nsh)
418 break;
419
420 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
421
422 nsh->raid_conf = conf;
423 spin_lock_init(&nsh->lock);
424
425 list_add(&nsh->lru, &newstripes);
426 }
427 if (i) {
428 /* didn't get enough, give up */
429 while (!list_empty(&newstripes)) {
430 nsh = list_entry(newstripes.next, struct stripe_head, lru);
431 list_del(&nsh->lru);
432 kmem_cache_free(sc, nsh);
433 }
434 kmem_cache_destroy(sc);
435 return -ENOMEM;
436 }
437 /* Step 2 - Must use GFP_NOIO now.
438 * OK, we have enough stripes, start collecting inactive
439 * stripes and copying them over
440 */
441 list_for_each_entry(nsh, &newstripes, lru) {
442 spin_lock_irq(&conf->device_lock);
443 wait_event_lock_irq(conf->wait_for_stripe,
444 !list_empty(&conf->inactive_list),
445 conf->device_lock,
b3b46be3 446 unplug_slaves(conf->mddev)
ad01c9e3
N
447 );
448 osh = get_free_stripe(conf);
449 spin_unlock_irq(&conf->device_lock);
450 atomic_set(&nsh->count, 1);
451 for(i=0; i<conf->pool_size; i++)
452 nsh->dev[i].page = osh->dev[i].page;
453 for( ; i<newsize; i++)
454 nsh->dev[i].page = NULL;
455 kmem_cache_free(conf->slab_cache, osh);
456 }
457 kmem_cache_destroy(conf->slab_cache);
458
459 /* Step 3.
460 * At this point, we are holding all the stripes so the array
461 * is completely stalled, so now is a good time to resize
462 * conf->disks.
463 */
464 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
465 if (ndisks) {
466 for (i=0; i<conf->raid_disks; i++)
467 ndisks[i] = conf->disks[i];
468 kfree(conf->disks);
469 conf->disks = ndisks;
470 } else
471 err = -ENOMEM;
472
473 /* Step 4, return new stripes to service */
474 while(!list_empty(&newstripes)) {
475 nsh = list_entry(newstripes.next, struct stripe_head, lru);
476 list_del_init(&nsh->lru);
477 for (i=conf->raid_disks; i < newsize; i++)
478 if (nsh->dev[i].page == NULL) {
479 struct page *p = alloc_page(GFP_NOIO);
480 nsh->dev[i].page = p;
481 if (!p)
482 err = -ENOMEM;
483 }
484 release_stripe(nsh);
485 }
486 /* critical section pass, GFP_NOIO no longer needed */
487
488 conf->slab_cache = sc;
489 conf->active_name = 1-conf->active_name;
490 conf->pool_size = newsize;
491 return err;
492}
29269553 493#endif
1da177e4 494
3f294f4f 495static int drop_one_stripe(raid5_conf_t *conf)
1da177e4
LT
496{
497 struct stripe_head *sh;
498
3f294f4f
N
499 spin_lock_irq(&conf->device_lock);
500 sh = get_free_stripe(conf);
501 spin_unlock_irq(&conf->device_lock);
502 if (!sh)
503 return 0;
78bafebd 504 BUG_ON(atomic_read(&sh->count));
ad01c9e3 505 shrink_buffers(sh, conf->pool_size);
3f294f4f
N
506 kmem_cache_free(conf->slab_cache, sh);
507 atomic_dec(&conf->active_stripes);
508 return 1;
509}
510
511static void shrink_stripes(raid5_conf_t *conf)
512{
513 while (drop_one_stripe(conf))
514 ;
515
29fc7e3e
N
516 if (conf->slab_cache)
517 kmem_cache_destroy(conf->slab_cache);
1da177e4
LT
518 conf->slab_cache = NULL;
519}
520
4e5314b5 521static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
1da177e4
LT
522 int error)
523{
524 struct stripe_head *sh = bi->bi_private;
525 raid5_conf_t *conf = sh->raid_conf;
7ecaa1e6 526 int disks = sh->disks, i;
1da177e4 527 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
d6950432
N
528 char b[BDEVNAME_SIZE];
529 mdk_rdev_t *rdev;
1da177e4
LT
530
531 if (bi->bi_size)
532 return 1;
533
534 for (i=0 ; i<disks; i++)
535 if (bi == &sh->dev[i].req)
536 break;
537
538 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
539 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
540 uptodate);
541 if (i == disks) {
542 BUG();
543 return 0;
544 }
545
546 if (uptodate) {
1da177e4 547 set_bit(R5_UPTODATE, &sh->dev[i].flags);
4e5314b5 548 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
d6950432
N
549 rdev = conf->disks[i].rdev;
550 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
551 mdname(conf->mddev), STRIPE_SECTORS,
552 (unsigned long long)sh->sector + rdev->data_offset,
553 bdevname(rdev->bdev, b));
4e5314b5
N
554 clear_bit(R5_ReadError, &sh->dev[i].flags);
555 clear_bit(R5_ReWrite, &sh->dev[i].flags);
556 }
ba22dcbf
N
557 if (atomic_read(&conf->disks[i].rdev->read_errors))
558 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1da177e4 559 } else {
d6950432 560 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
ba22dcbf 561 int retry = 0;
d6950432
N
562 rdev = conf->disks[i].rdev;
563
1da177e4 564 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
d6950432 565 atomic_inc(&rdev->read_errors);
ba22dcbf 566 if (conf->mddev->degraded)
d6950432
N
567 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
568 mdname(conf->mddev),
569 (unsigned long long)sh->sector + rdev->data_offset,
570 bdn);
ba22dcbf 571 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
4e5314b5 572 /* Oh, no!!! */
d6950432
N
573 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
574 mdname(conf->mddev),
575 (unsigned long long)sh->sector + rdev->data_offset,
576 bdn);
577 else if (atomic_read(&rdev->read_errors)
ba22dcbf 578 > conf->max_nr_stripes)
14f8d26b 579 printk(KERN_WARNING
d6950432
N
580 "raid5:%s: Too many read errors, failing device %s.\n",
581 mdname(conf->mddev), bdn);
ba22dcbf
N
582 else
583 retry = 1;
584 if (retry)
585 set_bit(R5_ReadError, &sh->dev[i].flags);
586 else {
4e5314b5
N
587 clear_bit(R5_ReadError, &sh->dev[i].flags);
588 clear_bit(R5_ReWrite, &sh->dev[i].flags);
d6950432 589 md_error(conf->mddev, rdev);
ba22dcbf 590 }
1da177e4
LT
591 }
592 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1da177e4
LT
593 clear_bit(R5_LOCKED, &sh->dev[i].flags);
594 set_bit(STRIPE_HANDLE, &sh->state);
595 release_stripe(sh);
596 return 0;
597}
598
599static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
600 int error)
601{
602 struct stripe_head *sh = bi->bi_private;
603 raid5_conf_t *conf = sh->raid_conf;
7ecaa1e6 604 int disks = sh->disks, i;
1da177e4
LT
605 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
606
607 if (bi->bi_size)
608 return 1;
609
610 for (i=0 ; i<disks; i++)
611 if (bi == &sh->dev[i].req)
612 break;
613
614 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
615 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
616 uptodate);
617 if (i == disks) {
618 BUG();
619 return 0;
620 }
621
1da177e4
LT
622 if (!uptodate)
623 md_error(conf->mddev, conf->disks[i].rdev);
624
625 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
626
627 clear_bit(R5_LOCKED, &sh->dev[i].flags);
628 set_bit(STRIPE_HANDLE, &sh->state);
c04be0aa 629 release_stripe(sh);
1da177e4
LT
630 return 0;
631}
632
633
634static sector_t compute_blocknr(struct stripe_head *sh, int i);
635
636static void raid5_build_block (struct stripe_head *sh, int i)
637{
638 struct r5dev *dev = &sh->dev[i];
639
640 bio_init(&dev->req);
641 dev->req.bi_io_vec = &dev->vec;
642 dev->req.bi_vcnt++;
643 dev->req.bi_max_vecs++;
644 dev->vec.bv_page = dev->page;
645 dev->vec.bv_len = STRIPE_SIZE;
646 dev->vec.bv_offset = 0;
647
648 dev->req.bi_sector = sh->sector;
649 dev->req.bi_private = sh;
650
651 dev->flags = 0;
16a53ecc 652 dev->sector = compute_blocknr(sh, i);
1da177e4
LT
653}
654
655static void error(mddev_t *mddev, mdk_rdev_t *rdev)
656{
657 char b[BDEVNAME_SIZE];
658 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
659 PRINTK("raid5: error called\n");
660
b2d444d7 661 if (!test_bit(Faulty, &rdev->flags)) {
850b2b42 662 set_bit(MD_CHANGE_DEVS, &mddev->flags);
c04be0aa
N
663 if (test_and_clear_bit(In_sync, &rdev->flags)) {
664 unsigned long flags;
665 spin_lock_irqsave(&conf->device_lock, flags);
1da177e4 666 mddev->degraded++;
c04be0aa 667 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
668 /*
669 * if recovery was running, make sure it aborts.
670 */
671 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
672 }
b2d444d7 673 set_bit(Faulty, &rdev->flags);
1da177e4
LT
674 printk (KERN_ALERT
675 "raid5: Disk failure on %s, disabling device."
676 " Operation continuing on %d devices\n",
02c2de8c 677 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1da177e4 678 }
16a53ecc 679}
1da177e4
LT
680
681/*
682 * Input: a 'big' sector number,
683 * Output: index of the data and parity disk, and the sector # in them.
684 */
685static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
686 unsigned int data_disks, unsigned int * dd_idx,
687 unsigned int * pd_idx, raid5_conf_t *conf)
688{
689 long stripe;
690 unsigned long chunk_number;
691 unsigned int chunk_offset;
692 sector_t new_sector;
693 int sectors_per_chunk = conf->chunk_size >> 9;
694
695 /* First compute the information on this sector */
696
697 /*
698 * Compute the chunk number and the sector offset inside the chunk
699 */
700 chunk_offset = sector_div(r_sector, sectors_per_chunk);
701 chunk_number = r_sector;
702 BUG_ON(r_sector != chunk_number);
703
704 /*
705 * Compute the stripe number
706 */
707 stripe = chunk_number / data_disks;
708
709 /*
710 * Compute the data disk and parity disk indexes inside the stripe
711 */
712 *dd_idx = chunk_number % data_disks;
713
714 /*
715 * Select the parity disk based on the user selected algorithm.
716 */
16a53ecc
N
717 switch(conf->level) {
718 case 4:
1da177e4 719 *pd_idx = data_disks;
16a53ecc
N
720 break;
721 case 5:
722 switch (conf->algorithm) {
1da177e4
LT
723 case ALGORITHM_LEFT_ASYMMETRIC:
724 *pd_idx = data_disks - stripe % raid_disks;
725 if (*dd_idx >= *pd_idx)
726 (*dd_idx)++;
727 break;
728 case ALGORITHM_RIGHT_ASYMMETRIC:
729 *pd_idx = stripe % raid_disks;
730 if (*dd_idx >= *pd_idx)
731 (*dd_idx)++;
732 break;
733 case ALGORITHM_LEFT_SYMMETRIC:
734 *pd_idx = data_disks - stripe % raid_disks;
735 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
736 break;
737 case ALGORITHM_RIGHT_SYMMETRIC:
738 *pd_idx = stripe % raid_disks;
739 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
740 break;
741 default:
14f8d26b 742 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
1da177e4 743 conf->algorithm);
16a53ecc
N
744 }
745 break;
746 case 6:
747
748 /**** FIX THIS ****/
749 switch (conf->algorithm) {
750 case ALGORITHM_LEFT_ASYMMETRIC:
751 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
752 if (*pd_idx == raid_disks-1)
753 (*dd_idx)++; /* Q D D D P */
754 else if (*dd_idx >= *pd_idx)
755 (*dd_idx) += 2; /* D D P Q D */
756 break;
757 case ALGORITHM_RIGHT_ASYMMETRIC:
758 *pd_idx = stripe % raid_disks;
759 if (*pd_idx == raid_disks-1)
760 (*dd_idx)++; /* Q D D D P */
761 else if (*dd_idx >= *pd_idx)
762 (*dd_idx) += 2; /* D D P Q D */
763 break;
764 case ALGORITHM_LEFT_SYMMETRIC:
765 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
766 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
767 break;
768 case ALGORITHM_RIGHT_SYMMETRIC:
769 *pd_idx = stripe % raid_disks;
770 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
771 break;
772 default:
773 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
774 conf->algorithm);
775 }
776 break;
1da177e4
LT
777 }
778
779 /*
780 * Finally, compute the new sector number
781 */
782 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
783 return new_sector;
784}
785
786
787static sector_t compute_blocknr(struct stripe_head *sh, int i)
788{
789 raid5_conf_t *conf = sh->raid_conf;
b875e531
N
790 int raid_disks = sh->disks;
791 int data_disks = raid_disks - conf->max_degraded;
1da177e4
LT
792 sector_t new_sector = sh->sector, check;
793 int sectors_per_chunk = conf->chunk_size >> 9;
794 sector_t stripe;
795 int chunk_offset;
796 int chunk_number, dummy1, dummy2, dd_idx = i;
797 sector_t r_sector;
798
16a53ecc 799
1da177e4
LT
800 chunk_offset = sector_div(new_sector, sectors_per_chunk);
801 stripe = new_sector;
802 BUG_ON(new_sector != stripe);
803
16a53ecc
N
804 if (i == sh->pd_idx)
805 return 0;
806 switch(conf->level) {
807 case 4: break;
808 case 5:
809 switch (conf->algorithm) {
1da177e4
LT
810 case ALGORITHM_LEFT_ASYMMETRIC:
811 case ALGORITHM_RIGHT_ASYMMETRIC:
812 if (i > sh->pd_idx)
813 i--;
814 break;
815 case ALGORITHM_LEFT_SYMMETRIC:
816 case ALGORITHM_RIGHT_SYMMETRIC:
817 if (i < sh->pd_idx)
818 i += raid_disks;
819 i -= (sh->pd_idx + 1);
820 break;
821 default:
14f8d26b 822 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
16a53ecc
N
823 conf->algorithm);
824 }
825 break;
826 case 6:
16a53ecc
N
827 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
828 return 0; /* It is the Q disk */
829 switch (conf->algorithm) {
830 case ALGORITHM_LEFT_ASYMMETRIC:
831 case ALGORITHM_RIGHT_ASYMMETRIC:
832 if (sh->pd_idx == raid_disks-1)
833 i--; /* Q D D D P */
834 else if (i > sh->pd_idx)
835 i -= 2; /* D D P Q D */
836 break;
837 case ALGORITHM_LEFT_SYMMETRIC:
838 case ALGORITHM_RIGHT_SYMMETRIC:
839 if (sh->pd_idx == raid_disks-1)
840 i--; /* Q D D D P */
841 else {
842 /* D D P Q D */
843 if (i < sh->pd_idx)
844 i += raid_disks;
845 i -= (sh->pd_idx + 2);
846 }
847 break;
848 default:
849 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
1da177e4 850 conf->algorithm);
16a53ecc
N
851 }
852 break;
1da177e4
LT
853 }
854
855 chunk_number = stripe * data_disks + i;
856 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
857
858 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
859 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
14f8d26b 860 printk(KERN_ERR "compute_blocknr: map not correct\n");
1da177e4
LT
861 return 0;
862 }
863 return r_sector;
864}
865
866
867
868/*
16a53ecc
N
869 * Copy data between a page in the stripe cache, and one or more bion
870 * The page could align with the middle of the bio, or there could be
871 * several bion, each with several bio_vecs, which cover part of the page
872 * Multiple bion are linked together on bi_next. There may be extras
873 * at the end of this list. We ignore them.
1da177e4
LT
874 */
875static void copy_data(int frombio, struct bio *bio,
876 struct page *page,
877 sector_t sector)
878{
879 char *pa = page_address(page);
880 struct bio_vec *bvl;
881 int i;
882 int page_offset;
883
884 if (bio->bi_sector >= sector)
885 page_offset = (signed)(bio->bi_sector - sector) * 512;
886 else
887 page_offset = (signed)(sector - bio->bi_sector) * -512;
888 bio_for_each_segment(bvl, bio, i) {
889 int len = bio_iovec_idx(bio,i)->bv_len;
890 int clen;
891 int b_offset = 0;
892
893 if (page_offset < 0) {
894 b_offset = -page_offset;
895 page_offset += b_offset;
896 len -= b_offset;
897 }
898
899 if (len > 0 && page_offset + len > STRIPE_SIZE)
900 clen = STRIPE_SIZE - page_offset;
901 else clen = len;
16a53ecc 902
1da177e4
LT
903 if (clen > 0) {
904 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
905 if (frombio)
906 memcpy(pa+page_offset, ba+b_offset, clen);
907 else
908 memcpy(ba+b_offset, pa+page_offset, clen);
909 __bio_kunmap_atomic(ba, KM_USER0);
910 }
911 if (clen < len) /* hit end of page */
912 break;
913 page_offset += len;
914 }
915}
916
917#define check_xor() do { \
918 if (count == MAX_XOR_BLOCKS) { \
919 xor_block(count, STRIPE_SIZE, ptr); \
920 count = 1; \
921 } \
922 } while(0)
923
924
925static void compute_block(struct stripe_head *sh, int dd_idx)
926{
7ecaa1e6 927 int i, count, disks = sh->disks;
1da177e4
LT
928 void *ptr[MAX_XOR_BLOCKS], *p;
929
930 PRINTK("compute_block, stripe %llu, idx %d\n",
931 (unsigned long long)sh->sector, dd_idx);
932
933 ptr[0] = page_address(sh->dev[dd_idx].page);
934 memset(ptr[0], 0, STRIPE_SIZE);
935 count = 1;
936 for (i = disks ; i--; ) {
937 if (i == dd_idx)
938 continue;
939 p = page_address(sh->dev[i].page);
940 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
941 ptr[count++] = p;
942 else
14f8d26b 943 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
1da177e4
LT
944 " not present\n", dd_idx,
945 (unsigned long long)sh->sector, i);
946
947 check_xor();
948 }
949 if (count != 1)
950 xor_block(count, STRIPE_SIZE, ptr);
951 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
952}
953
16a53ecc 954static void compute_parity5(struct stripe_head *sh, int method)
1da177e4
LT
955{
956 raid5_conf_t *conf = sh->raid_conf;
7ecaa1e6 957 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
1da177e4
LT
958 void *ptr[MAX_XOR_BLOCKS];
959 struct bio *chosen;
960
16a53ecc 961 PRINTK("compute_parity5, stripe %llu, method %d\n",
1da177e4
LT
962 (unsigned long long)sh->sector, method);
963
964 count = 1;
965 ptr[0] = page_address(sh->dev[pd_idx].page);
966 switch(method) {
967 case READ_MODIFY_WRITE:
78bafebd 968 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
1da177e4
LT
969 for (i=disks ; i-- ;) {
970 if (i==pd_idx)
971 continue;
972 if (sh->dev[i].towrite &&
973 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
974 ptr[count++] = page_address(sh->dev[i].page);
975 chosen = sh->dev[i].towrite;
976 sh->dev[i].towrite = NULL;
977
978 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
979 wake_up(&conf->wait_for_overlap);
980
78bafebd 981 BUG_ON(sh->dev[i].written);
1da177e4
LT
982 sh->dev[i].written = chosen;
983 check_xor();
984 }
985 }
986 break;
987 case RECONSTRUCT_WRITE:
988 memset(ptr[0], 0, STRIPE_SIZE);
989 for (i= disks; i-- ;)
990 if (i!=pd_idx && sh->dev[i].towrite) {
991 chosen = sh->dev[i].towrite;
992 sh->dev[i].towrite = NULL;
993
994 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
995 wake_up(&conf->wait_for_overlap);
996
78bafebd 997 BUG_ON(sh->dev[i].written);
1da177e4
LT
998 sh->dev[i].written = chosen;
999 }
1000 break;
1001 case CHECK_PARITY:
1002 break;
1003 }
1004 if (count>1) {
1005 xor_block(count, STRIPE_SIZE, ptr);
1006 count = 1;
1007 }
1008
1009 for (i = disks; i--;)
1010 if (sh->dev[i].written) {
1011 sector_t sector = sh->dev[i].sector;
1012 struct bio *wbi = sh->dev[i].written;
1013 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1014 copy_data(1, wbi, sh->dev[i].page, sector);
1015 wbi = r5_next_bio(wbi, sector);
1016 }
1017
1018 set_bit(R5_LOCKED, &sh->dev[i].flags);
1019 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1020 }
1021
1022 switch(method) {
1023 case RECONSTRUCT_WRITE:
1024 case CHECK_PARITY:
1025 for (i=disks; i--;)
1026 if (i != pd_idx) {
1027 ptr[count++] = page_address(sh->dev[i].page);
1028 check_xor();
1029 }
1030 break;
1031 case READ_MODIFY_WRITE:
1032 for (i = disks; i--;)
1033 if (sh->dev[i].written) {
1034 ptr[count++] = page_address(sh->dev[i].page);
1035 check_xor();
1036 }
1037 }
1038 if (count != 1)
1039 xor_block(count, STRIPE_SIZE, ptr);
1040
1041 if (method != CHECK_PARITY) {
1042 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1043 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1044 } else
1045 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1046}
1047
16a53ecc
N
1048static void compute_parity6(struct stripe_head *sh, int method)
1049{
1050 raid6_conf_t *conf = sh->raid_conf;
1051 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1052 struct bio *chosen;
1053 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1054 void *ptrs[disks];
1055
1056 qd_idx = raid6_next_disk(pd_idx, disks);
1057 d0_idx = raid6_next_disk(qd_idx, disks);
1058
1059 PRINTK("compute_parity, stripe %llu, method %d\n",
1060 (unsigned long long)sh->sector, method);
1061
1062 switch(method) {
1063 case READ_MODIFY_WRITE:
1064 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1065 case RECONSTRUCT_WRITE:
1066 for (i= disks; i-- ;)
1067 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1068 chosen = sh->dev[i].towrite;
1069 sh->dev[i].towrite = NULL;
1070
1071 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1072 wake_up(&conf->wait_for_overlap);
1073
52e5f9d1 1074 BUG_ON(sh->dev[i].written);
16a53ecc
N
1075 sh->dev[i].written = chosen;
1076 }
1077 break;
1078 case CHECK_PARITY:
1079 BUG(); /* Not implemented yet */
1080 }
1081
1082 for (i = disks; i--;)
1083 if (sh->dev[i].written) {
1084 sector_t sector = sh->dev[i].sector;
1085 struct bio *wbi = sh->dev[i].written;
1086 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1087 copy_data(1, wbi, sh->dev[i].page, sector);
1088 wbi = r5_next_bio(wbi, sector);
1089 }
1090
1091 set_bit(R5_LOCKED, &sh->dev[i].flags);
1092 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1093 }
1094
1095// switch(method) {
1096// case RECONSTRUCT_WRITE:
1097// case CHECK_PARITY:
1098// case UPDATE_PARITY:
1099 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1100 /* FIX: Is this ordering of drives even remotely optimal? */
1101 count = 0;
1102 i = d0_idx;
1103 do {
1104 ptrs[count++] = page_address(sh->dev[i].page);
1105 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1106 printk("block %d/%d not uptodate on parity calc\n", i,count);
1107 i = raid6_next_disk(i, disks);
1108 } while ( i != d0_idx );
1109// break;
1110// }
1111
1112 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1113
1114 switch(method) {
1115 case RECONSTRUCT_WRITE:
1116 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1117 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1118 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1119 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1120 break;
1121 case UPDATE_PARITY:
1122 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1123 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1124 break;
1125 }
1126}
1127
1128
1129/* Compute one missing block */
1130static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1131{
1132 raid6_conf_t *conf = sh->raid_conf;
1133 int i, count, disks = conf->raid_disks;
1134 void *ptr[MAX_XOR_BLOCKS], *p;
1135 int pd_idx = sh->pd_idx;
1136 int qd_idx = raid6_next_disk(pd_idx, disks);
1137
1138 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1139 (unsigned long long)sh->sector, dd_idx);
1140
1141 if ( dd_idx == qd_idx ) {
1142 /* We're actually computing the Q drive */
1143 compute_parity6(sh, UPDATE_PARITY);
1144 } else {
1145 ptr[0] = page_address(sh->dev[dd_idx].page);
1146 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1147 count = 1;
1148 for (i = disks ; i--; ) {
1149 if (i == dd_idx || i == qd_idx)
1150 continue;
1151 p = page_address(sh->dev[i].page);
1152 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1153 ptr[count++] = p;
1154 else
1155 printk("compute_block() %d, stripe %llu, %d"
1156 " not present\n", dd_idx,
1157 (unsigned long long)sh->sector, i);
1158
1159 check_xor();
1160 }
1161 if (count != 1)
1162 xor_block(count, STRIPE_SIZE, ptr);
1163 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1164 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1165 }
1166}
1167
1168/* Compute two missing blocks */
1169static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1170{
1171 raid6_conf_t *conf = sh->raid_conf;
1172 int i, count, disks = conf->raid_disks;
1173 int pd_idx = sh->pd_idx;
1174 int qd_idx = raid6_next_disk(pd_idx, disks);
1175 int d0_idx = raid6_next_disk(qd_idx, disks);
1176 int faila, failb;
1177
1178 /* faila and failb are disk numbers relative to d0_idx */
1179 /* pd_idx become disks-2 and qd_idx become disks-1 */
1180 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1181 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1182
1183 BUG_ON(faila == failb);
1184 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1185
1186 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1187 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1188
1189 if ( failb == disks-1 ) {
1190 /* Q disk is one of the missing disks */
1191 if ( faila == disks-2 ) {
1192 /* Missing P+Q, just recompute */
1193 compute_parity6(sh, UPDATE_PARITY);
1194 return;
1195 } else {
1196 /* We're missing D+Q; recompute D from P */
1197 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1198 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1199 return;
1200 }
1201 }
1202
1203 /* We're missing D+P or D+D; build pointer table */
1204 {
1205 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1206 void *ptrs[disks];
1207
1208 count = 0;
1209 i = d0_idx;
1210 do {
1211 ptrs[count++] = page_address(sh->dev[i].page);
1212 i = raid6_next_disk(i, disks);
1213 if (i != dd_idx1 && i != dd_idx2 &&
1214 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1215 printk("compute_2 with missing block %d/%d\n", count, i);
1216 } while ( i != d0_idx );
1217
1218 if ( failb == disks-2 ) {
1219 /* We're missing D+P. */
1220 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1221 } else {
1222 /* We're missing D+D. */
1223 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1224 }
1225
1226 /* Both the above update both missing blocks */
1227 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1228 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1229 }
1230}
1231
1232
1233
1da177e4
LT
1234/*
1235 * Each stripe/dev can have one or more bion attached.
16a53ecc 1236 * toread/towrite point to the first in a chain.
1da177e4
LT
1237 * The bi_next chain must be in order.
1238 */
1239static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1240{
1241 struct bio **bip;
1242 raid5_conf_t *conf = sh->raid_conf;
72626685 1243 int firstwrite=0;
1da177e4
LT
1244
1245 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1246 (unsigned long long)bi->bi_sector,
1247 (unsigned long long)sh->sector);
1248
1249
1250 spin_lock(&sh->lock);
1251 spin_lock_irq(&conf->device_lock);
72626685 1252 if (forwrite) {
1da177e4 1253 bip = &sh->dev[dd_idx].towrite;
72626685
N
1254 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1255 firstwrite = 1;
1256 } else
1da177e4
LT
1257 bip = &sh->dev[dd_idx].toread;
1258 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1259 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1260 goto overlap;
1261 bip = & (*bip)->bi_next;
1262 }
1263 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1264 goto overlap;
1265
78bafebd 1266 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1da177e4
LT
1267 if (*bip)
1268 bi->bi_next = *bip;
1269 *bip = bi;
1270 bi->bi_phys_segments ++;
1271 spin_unlock_irq(&conf->device_lock);
1272 spin_unlock(&sh->lock);
1273
1274 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1275 (unsigned long long)bi->bi_sector,
1276 (unsigned long long)sh->sector, dd_idx);
1277
72626685 1278 if (conf->mddev->bitmap && firstwrite) {
72626685
N
1279 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1280 STRIPE_SECTORS, 0);
ae3c20cc 1281 sh->bm_seq = conf->seq_flush+1;
72626685
N
1282 set_bit(STRIPE_BIT_DELAY, &sh->state);
1283 }
1284
1da177e4
LT
1285 if (forwrite) {
1286 /* check if page is covered */
1287 sector_t sector = sh->dev[dd_idx].sector;
1288 for (bi=sh->dev[dd_idx].towrite;
1289 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1290 bi && bi->bi_sector <= sector;
1291 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1292 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1293 sector = bi->bi_sector + (bi->bi_size>>9);
1294 }
1295 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1296 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1297 }
1298 return 1;
1299
1300 overlap:
1301 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1302 spin_unlock_irq(&conf->device_lock);
1303 spin_unlock(&sh->lock);
1304 return 0;
1305}
1306
29269553
N
1307static void end_reshape(raid5_conf_t *conf);
1308
16a53ecc
N
1309static int page_is_zero(struct page *p)
1310{
1311 char *a = page_address(p);
1312 return ((*(u32*)a) == 0 &&
1313 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1314}
1315
ccfcc3c1
N
1316static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1317{
1318 int sectors_per_chunk = conf->chunk_size >> 9;
ccfcc3c1 1319 int pd_idx, dd_idx;
2d2063ce
CQH
1320 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1321
b875e531
N
1322 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1323 *sectors_per_chunk + chunk_offset,
1324 disks, disks - conf->max_degraded,
1325 &dd_idx, &pd_idx, conf);
ccfcc3c1
N
1326 return pd_idx;
1327}
1328
1da177e4
LT
1329
1330/*
1331 * handle_stripe - do things to a stripe.
1332 *
1333 * We lock the stripe and then examine the state of various bits
1334 * to see what needs to be done.
1335 * Possible results:
1336 * return some read request which now have data
1337 * return some write requests which are safely on disc
1338 * schedule a read on some buffers
1339 * schedule a write of some buffers
1340 * return confirmation of parity correctness
1341 *
1342 * Parity calculations are done inside the stripe lock
1343 * buffers are taken off read_list or write_list, and bh_cache buffers
1344 * get BH_Lock set before the stripe lock is released.
1345 *
1346 */
1347
16a53ecc 1348static void handle_stripe5(struct stripe_head *sh)
1da177e4
LT
1349{
1350 raid5_conf_t *conf = sh->raid_conf;
7ecaa1e6 1351 int disks = sh->disks;
1da177e4
LT
1352 struct bio *return_bi= NULL;
1353 struct bio *bi;
1354 int i;
ccfcc3c1 1355 int syncing, expanding, expanded;
1da177e4
LT
1356 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1357 int non_overwrite = 0;
1358 int failed_num=0;
1359 struct r5dev *dev;
1360
1361 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1362 (unsigned long long)sh->sector, atomic_read(&sh->count),
1363 sh->pd_idx);
1364
1365 spin_lock(&sh->lock);
1366 clear_bit(STRIPE_HANDLE, &sh->state);
1367 clear_bit(STRIPE_DELAYED, &sh->state);
1368
1369 syncing = test_bit(STRIPE_SYNCING, &sh->state);
ccfcc3c1
N
1370 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1371 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
1da177e4
LT
1372 /* Now to look around and see what can be done */
1373
9910f16a 1374 rcu_read_lock();
1da177e4
LT
1375 for (i=disks; i--; ) {
1376 mdk_rdev_t *rdev;
1377 dev = &sh->dev[i];
1378 clear_bit(R5_Insync, &dev->flags);
1da177e4
LT
1379
1380 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1381 i, dev->flags, dev->toread, dev->towrite, dev->written);
1382 /* maybe we can reply to a read */
1383 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1384 struct bio *rbi, *rbi2;
1385 PRINTK("Return read for disc %d\n", i);
1386 spin_lock_irq(&conf->device_lock);
1387 rbi = dev->toread;
1388 dev->toread = NULL;
1389 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1390 wake_up(&conf->wait_for_overlap);
1391 spin_unlock_irq(&conf->device_lock);
1392 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1393 copy_data(0, rbi, dev->page, dev->sector);
1394 rbi2 = r5_next_bio(rbi, dev->sector);
1395 spin_lock_irq(&conf->device_lock);
1396 if (--rbi->bi_phys_segments == 0) {
1397 rbi->bi_next = return_bi;
1398 return_bi = rbi;
1399 }
1400 spin_unlock_irq(&conf->device_lock);
1401 rbi = rbi2;
1402 }
1403 }
1404
1405 /* now count some things */
1406 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1407 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1408
1409
1410 if (dev->toread) to_read++;
1411 if (dev->towrite) {
1412 to_write++;
1413 if (!test_bit(R5_OVERWRITE, &dev->flags))
1414 non_overwrite++;
1415 }
1416 if (dev->written) written++;
9910f16a 1417 rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1418 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
14f8d26b 1419 /* The ReadError flag will just be confusing now */
4e5314b5
N
1420 clear_bit(R5_ReadError, &dev->flags);
1421 clear_bit(R5_ReWrite, &dev->flags);
1422 }
b2d444d7 1423 if (!rdev || !test_bit(In_sync, &rdev->flags)
4e5314b5 1424 || test_bit(R5_ReadError, &dev->flags)) {
1da177e4
LT
1425 failed++;
1426 failed_num = i;
1427 } else
1428 set_bit(R5_Insync, &dev->flags);
1429 }
9910f16a 1430 rcu_read_unlock();
1da177e4
LT
1431 PRINTK("locked=%d uptodate=%d to_read=%d"
1432 " to_write=%d failed=%d failed_num=%d\n",
1433 locked, uptodate, to_read, to_write, failed, failed_num);
1434 /* check if the array has lost two devices and, if so, some requests might
1435 * need to be failed
1436 */
1437 if (failed > 1 && to_read+to_write+written) {
1da177e4 1438 for (i=disks; i--; ) {
72626685 1439 int bitmap_end = 0;
4e5314b5
N
1440
1441 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
9910f16a
N
1442 mdk_rdev_t *rdev;
1443 rcu_read_lock();
1444 rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1445 if (rdev && test_bit(In_sync, &rdev->flags))
4e5314b5
N
1446 /* multiple read failures in one stripe */
1447 md_error(conf->mddev, rdev);
9910f16a 1448 rcu_read_unlock();
4e5314b5
N
1449 }
1450
72626685 1451 spin_lock_irq(&conf->device_lock);
1da177e4
LT
1452 /* fail all writes first */
1453 bi = sh->dev[i].towrite;
1454 sh->dev[i].towrite = NULL;
72626685 1455 if (bi) { to_write--; bitmap_end = 1; }
1da177e4
LT
1456
1457 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1458 wake_up(&conf->wait_for_overlap);
1459
1460 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1461 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1462 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1463 if (--bi->bi_phys_segments == 0) {
1464 md_write_end(conf->mddev);
1465 bi->bi_next = return_bi;
1466 return_bi = bi;
1467 }
1468 bi = nextbi;
1469 }
1470 /* and fail all 'written' */
1471 bi = sh->dev[i].written;
1472 sh->dev[i].written = NULL;
72626685 1473 if (bi) bitmap_end = 1;
1da177e4
LT
1474 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1475 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1476 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1477 if (--bi->bi_phys_segments == 0) {
1478 md_write_end(conf->mddev);
1479 bi->bi_next = return_bi;
1480 return_bi = bi;
1481 }
1482 bi = bi2;
1483 }
1484
1485 /* fail any reads if this device is non-operational */
4e5314b5
N
1486 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1487 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1da177e4
LT
1488 bi = sh->dev[i].toread;
1489 sh->dev[i].toread = NULL;
1490 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1491 wake_up(&conf->wait_for_overlap);
1492 if (bi) to_read--;
1493 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1494 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1495 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1496 if (--bi->bi_phys_segments == 0) {
1497 bi->bi_next = return_bi;
1498 return_bi = bi;
1499 }
1500 bi = nextbi;
1501 }
1502 }
72626685
N
1503 spin_unlock_irq(&conf->device_lock);
1504 if (bitmap_end)
1505 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1506 STRIPE_SECTORS, 0, 0);
1da177e4 1507 }
1da177e4
LT
1508 }
1509 if (failed > 1 && syncing) {
1510 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1511 clear_bit(STRIPE_SYNCING, &sh->state);
1512 syncing = 0;
1513 }
1514
1515 /* might be able to return some write requests if the parity block
1516 * is safe, or on a failed drive
1517 */
1518 dev = &sh->dev[sh->pd_idx];
1519 if ( written &&
1520 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1521 test_bit(R5_UPTODATE, &dev->flags))
1522 || (failed == 1 && failed_num == sh->pd_idx))
1523 ) {
1524 /* any written block on an uptodate or failed drive can be returned.
1525 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1526 * never LOCKED, so we don't need to test 'failed' directly.
1527 */
1528 for (i=disks; i--; )
1529 if (sh->dev[i].written) {
1530 dev = &sh->dev[i];
1531 if (!test_bit(R5_LOCKED, &dev->flags) &&
1532 test_bit(R5_UPTODATE, &dev->flags) ) {
1533 /* We can return any write requests */
1534 struct bio *wbi, *wbi2;
72626685 1535 int bitmap_end = 0;
1da177e4
LT
1536 PRINTK("Return write for disc %d\n", i);
1537 spin_lock_irq(&conf->device_lock);
1538 wbi = dev->written;
1539 dev->written = NULL;
1540 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1541 wbi2 = r5_next_bio(wbi, dev->sector);
1542 if (--wbi->bi_phys_segments == 0) {
1543 md_write_end(conf->mddev);
1544 wbi->bi_next = return_bi;
1545 return_bi = wbi;
1546 }
1547 wbi = wbi2;
1548 }
72626685
N
1549 if (dev->towrite == NULL)
1550 bitmap_end = 1;
1da177e4 1551 spin_unlock_irq(&conf->device_lock);
72626685
N
1552 if (bitmap_end)
1553 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1554 STRIPE_SECTORS,
1555 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1da177e4
LT
1556 }
1557 }
1558 }
1559
1560 /* Now we might consider reading some blocks, either to check/generate
1561 * parity, or to satisfy requests
1562 * or to load a block that is being partially written.
1563 */
ccfcc3c1 1564 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
1da177e4
LT
1565 for (i=disks; i--;) {
1566 dev = &sh->dev[i];
1567 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1568 (dev->toread ||
1569 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1570 syncing ||
ccfcc3c1 1571 expanding ||
1da177e4
LT
1572 (failed && (sh->dev[failed_num].toread ||
1573 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1574 )
1575 ) {
1576 /* we would like to get this block, possibly
1577 * by computing it, but we might not be able to
1578 */
1579 if (uptodate == disks-1) {
1580 PRINTK("Computing block %d\n", i);
1581 compute_block(sh, i);
1582 uptodate++;
1583 } else if (test_bit(R5_Insync, &dev->flags)) {
1584 set_bit(R5_LOCKED, &dev->flags);
1585 set_bit(R5_Wantread, &dev->flags);
1da177e4
LT
1586 locked++;
1587 PRINTK("Reading block %d (sync=%d)\n",
1588 i, syncing);
1da177e4
LT
1589 }
1590 }
1591 }
1592 set_bit(STRIPE_HANDLE, &sh->state);
1593 }
1594
1595 /* now to consider writing and what else, if anything should be read */
1596 if (to_write) {
1597 int rmw=0, rcw=0;
1598 for (i=disks ; i--;) {
1599 /* would I have to read this buffer for read_modify_write */
1600 dev = &sh->dev[i];
1601 if ((dev->towrite || i == sh->pd_idx) &&
1602 (!test_bit(R5_LOCKED, &dev->flags)
1da177e4
LT
1603 ) &&
1604 !test_bit(R5_UPTODATE, &dev->flags)) {
1605 if (test_bit(R5_Insync, &dev->flags)
1606/* && !(!mddev->insync && i == sh->pd_idx) */
1607 )
1608 rmw++;
1609 else rmw += 2*disks; /* cannot read it */
1610 }
1611 /* Would I have to read this buffer for reconstruct_write */
1612 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1613 (!test_bit(R5_LOCKED, &dev->flags)
1da177e4
LT
1614 ) &&
1615 !test_bit(R5_UPTODATE, &dev->flags)) {
1616 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1617 else rcw += 2*disks;
1618 }
1619 }
1620 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1621 (unsigned long long)sh->sector, rmw, rcw);
1622 set_bit(STRIPE_HANDLE, &sh->state);
1623 if (rmw < rcw && rmw > 0)
1624 /* prefer read-modify-write, but need to get some data */
1625 for (i=disks; i--;) {
1626 dev = &sh->dev[i];
1627 if ((dev->towrite || i == sh->pd_idx) &&
1628 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1629 test_bit(R5_Insync, &dev->flags)) {
1630 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1631 {
1632 PRINTK("Read_old block %d for r-m-w\n", i);
1633 set_bit(R5_LOCKED, &dev->flags);
1634 set_bit(R5_Wantread, &dev->flags);
1635 locked++;
1636 } else {
1637 set_bit(STRIPE_DELAYED, &sh->state);
1638 set_bit(STRIPE_HANDLE, &sh->state);
1639 }
1640 }
1641 }
1642 if (rcw <= rmw && rcw > 0)
1643 /* want reconstruct write, but need to get some data */
1644 for (i=disks; i--;) {
1645 dev = &sh->dev[i];
1646 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1647 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1648 test_bit(R5_Insync, &dev->flags)) {
1649 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1650 {
1651 PRINTK("Read_old block %d for Reconstruct\n", i);
1652 set_bit(R5_LOCKED, &dev->flags);
1653 set_bit(R5_Wantread, &dev->flags);
1654 locked++;
1655 } else {
1656 set_bit(STRIPE_DELAYED, &sh->state);
1657 set_bit(STRIPE_HANDLE, &sh->state);
1658 }
1659 }
1660 }
1661 /* now if nothing is locked, and if we have enough data, we can start a write request */
72626685
N
1662 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1663 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1da177e4 1664 PRINTK("Computing parity...\n");
16a53ecc 1665 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1da177e4
LT
1666 /* now every locked buffer is ready to be written */
1667 for (i=disks; i--;)
1668 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1669 PRINTK("Writing block %d\n", i);
1670 locked++;
1671 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1672 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1673 || (i==sh->pd_idx && failed == 0))
1674 set_bit(STRIPE_INSYNC, &sh->state);
1675 }
1676 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1677 atomic_dec(&conf->preread_active_stripes);
1678 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1679 md_wakeup_thread(conf->mddev->thread);
1680 }
1681 }
1682 }
1683
1684 /* maybe we need to check and possibly fix the parity for this stripe
1685 * Any reads will already have been scheduled, so we just see if enough data
1686 * is available
1687 */
1688 if (syncing && locked == 0 &&
14f8d26b 1689 !test_bit(STRIPE_INSYNC, &sh->state)) {
1da177e4
LT
1690 set_bit(STRIPE_HANDLE, &sh->state);
1691 if (failed == 0) {
78bafebd 1692 BUG_ON(uptodate != disks);
16a53ecc 1693 compute_parity5(sh, CHECK_PARITY);
1da177e4 1694 uptodate--;
16a53ecc 1695 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
1da177e4
LT
1696 /* parity is correct (on disc, not in buffer any more) */
1697 set_bit(STRIPE_INSYNC, &sh->state);
9d88883e
N
1698 } else {
1699 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1700 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1701 /* don't try to repair!! */
1702 set_bit(STRIPE_INSYNC, &sh->state);
14f8d26b
N
1703 else {
1704 compute_block(sh, sh->pd_idx);
1705 uptodate++;
1706 }
1da177e4
LT
1707 }
1708 }
1709 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
14f8d26b 1710 /* either failed parity check, or recovery is happening */
1da177e4
LT
1711 if (failed==0)
1712 failed_num = sh->pd_idx;
1da177e4 1713 dev = &sh->dev[failed_num];
14f8d26b
N
1714 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1715 BUG_ON(uptodate != disks);
1716
1da177e4
LT
1717 set_bit(R5_LOCKED, &dev->flags);
1718 set_bit(R5_Wantwrite, &dev->flags);
72626685 1719 clear_bit(STRIPE_DEGRADED, &sh->state);
1da177e4
LT
1720 locked++;
1721 set_bit(STRIPE_INSYNC, &sh->state);
1da177e4
LT
1722 }
1723 }
1724 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1725 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1726 clear_bit(STRIPE_SYNCING, &sh->state);
1727 }
4e5314b5
N
1728
1729 /* If the failed drive is just a ReadError, then we might need to progress
1730 * the repair/check process
1731 */
ba22dcbf
N
1732 if (failed == 1 && ! conf->mddev->ro &&
1733 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
4e5314b5
N
1734 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1735 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1736 ) {
1737 dev = &sh->dev[failed_num];
1738 if (!test_bit(R5_ReWrite, &dev->flags)) {
1739 set_bit(R5_Wantwrite, &dev->flags);
1740 set_bit(R5_ReWrite, &dev->flags);
1741 set_bit(R5_LOCKED, &dev->flags);
ccfcc3c1 1742 locked++;
4e5314b5
N
1743 } else {
1744 /* let's read it back */
1745 set_bit(R5_Wantread, &dev->flags);
1746 set_bit(R5_LOCKED, &dev->flags);
ccfcc3c1 1747 locked++;
4e5314b5
N
1748 }
1749 }
1750
ccfcc3c1
N
1751 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1752 /* Need to write out all blocks after computing parity */
1753 sh->disks = conf->raid_disks;
1754 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
16a53ecc 1755 compute_parity5(sh, RECONSTRUCT_WRITE);
ccfcc3c1
N
1756 for (i= conf->raid_disks; i--;) {
1757 set_bit(R5_LOCKED, &sh->dev[i].flags);
1758 locked++;
1759 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1760 }
1761 clear_bit(STRIPE_EXPANDING, &sh->state);
1762 } else if (expanded) {
1763 clear_bit(STRIPE_EXPAND_READY, &sh->state);
f6705578 1764 atomic_dec(&conf->reshape_stripes);
ccfcc3c1
N
1765 wake_up(&conf->wait_for_overlap);
1766 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1767 }
1768
1769 if (expanding && locked == 0) {
1770 /* We have read all the blocks in this stripe and now we need to
1771 * copy some of them into a target stripe for expand.
1772 */
1773 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1774 for (i=0; i< sh->disks; i++)
1775 if (i != sh->pd_idx) {
1776 int dd_idx, pd_idx, j;
1777 struct stripe_head *sh2;
1778
1779 sector_t bn = compute_blocknr(sh, i);
1780 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1781 conf->raid_disks-1,
1782 &dd_idx, &pd_idx, conf);
1783 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1784 if (sh2 == NULL)
1785 /* so far only the early blocks of this stripe
1786 * have been requested. When later blocks
1787 * get requested, we will try again
1788 */
1789 continue;
1790 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1791 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1792 /* must have already done this block */
1793 release_stripe(sh2);
1794 continue;
1795 }
1796 memcpy(page_address(sh2->dev[dd_idx].page),
1797 page_address(sh->dev[i].page),
1798 STRIPE_SIZE);
1799 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1800 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1801 for (j=0; j<conf->raid_disks; j++)
1802 if (j != sh2->pd_idx &&
1803 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1804 break;
1805 if (j == conf->raid_disks) {
1806 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1807 set_bit(STRIPE_HANDLE, &sh2->state);
1808 }
1809 release_stripe(sh2);
1810 }
1811 }
1812
1da177e4
LT
1813 spin_unlock(&sh->lock);
1814
1815 while ((bi=return_bi)) {
1816 int bytes = bi->bi_size;
1817
1818 return_bi = bi->bi_next;
1819 bi->bi_next = NULL;
1820 bi->bi_size = 0;
1821 bi->bi_end_io(bi, bytes, 0);
1822 }
1823 for (i=disks; i-- ;) {
1824 int rw;
1825 struct bio *bi;
1826 mdk_rdev_t *rdev;
1827 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1828 rw = 1;
1829 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1830 rw = 0;
1831 else
1832 continue;
1833
1834 bi = &sh->dev[i].req;
1835
1836 bi->bi_rw = rw;
1837 if (rw)
1838 bi->bi_end_io = raid5_end_write_request;
1839 else
1840 bi->bi_end_io = raid5_end_read_request;
1841
1842 rcu_read_lock();
d6065f7b 1843 rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1844 if (rdev && test_bit(Faulty, &rdev->flags))
1da177e4
LT
1845 rdev = NULL;
1846 if (rdev)
1847 atomic_inc(&rdev->nr_pending);
1848 rcu_read_unlock();
1849
1850 if (rdev) {
ccfcc3c1 1851 if (syncing || expanding || expanded)
1da177e4
LT
1852 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1853
1854 bi->bi_bdev = rdev->bdev;
1855 PRINTK("for %llu schedule op %ld on disc %d\n",
1856 (unsigned long long)sh->sector, bi->bi_rw, i);
1857 atomic_inc(&sh->count);
1858 bi->bi_sector = sh->sector + rdev->data_offset;
1859 bi->bi_flags = 1 << BIO_UPTODATE;
1860 bi->bi_vcnt = 1;
1861 bi->bi_max_vecs = 1;
1862 bi->bi_idx = 0;
1863 bi->bi_io_vec = &sh->dev[i].vec;
1864 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1865 bi->bi_io_vec[0].bv_offset = 0;
1866 bi->bi_size = STRIPE_SIZE;
1867 bi->bi_next = NULL;
4dbcdc75
N
1868 if (rw == WRITE &&
1869 test_bit(R5_ReWrite, &sh->dev[i].flags))
1870 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1da177e4
LT
1871 generic_make_request(bi);
1872 } else {
72626685
N
1873 if (rw == 1)
1874 set_bit(STRIPE_DEGRADED, &sh->state);
1da177e4
LT
1875 PRINTK("skip op %ld on disc %d for sector %llu\n",
1876 bi->bi_rw, i, (unsigned long long)sh->sector);
1877 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1878 set_bit(STRIPE_HANDLE, &sh->state);
1879 }
1880 }
1881}
1882
16a53ecc 1883static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1da177e4 1884{
16a53ecc
N
1885 raid6_conf_t *conf = sh->raid_conf;
1886 int disks = conf->raid_disks;
1887 struct bio *return_bi= NULL;
1888 struct bio *bi;
1889 int i;
1890 int syncing;
1891 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1892 int non_overwrite = 0;
1893 int failed_num[2] = {0, 0};
1894 struct r5dev *dev, *pdev, *qdev;
1895 int pd_idx = sh->pd_idx;
1896 int qd_idx = raid6_next_disk(pd_idx, disks);
1897 int p_failed, q_failed;
1da177e4 1898
16a53ecc
N
1899 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1900 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1901 pd_idx, qd_idx);
72626685 1902
16a53ecc
N
1903 spin_lock(&sh->lock);
1904 clear_bit(STRIPE_HANDLE, &sh->state);
1905 clear_bit(STRIPE_DELAYED, &sh->state);
1906
1907 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1908 /* Now to look around and see what can be done */
1da177e4
LT
1909
1910 rcu_read_lock();
16a53ecc
N
1911 for (i=disks; i--; ) {
1912 mdk_rdev_t *rdev;
1913 dev = &sh->dev[i];
1914 clear_bit(R5_Insync, &dev->flags);
1da177e4 1915
16a53ecc
N
1916 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1917 i, dev->flags, dev->toread, dev->towrite, dev->written);
1918 /* maybe we can reply to a read */
1919 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1920 struct bio *rbi, *rbi2;
1921 PRINTK("Return read for disc %d\n", i);
1922 spin_lock_irq(&conf->device_lock);
1923 rbi = dev->toread;
1924 dev->toread = NULL;
1925 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1926 wake_up(&conf->wait_for_overlap);
1927 spin_unlock_irq(&conf->device_lock);
1928 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1929 copy_data(0, rbi, dev->page, dev->sector);
1930 rbi2 = r5_next_bio(rbi, dev->sector);
1931 spin_lock_irq(&conf->device_lock);
1932 if (--rbi->bi_phys_segments == 0) {
1933 rbi->bi_next = return_bi;
1934 return_bi = rbi;
1935 }
1936 spin_unlock_irq(&conf->device_lock);
1937 rbi = rbi2;
1938 }
1939 }
1da177e4 1940
16a53ecc
N
1941 /* now count some things */
1942 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1943 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1da177e4 1944
16a53ecc
N
1945
1946 if (dev->toread) to_read++;
1947 if (dev->towrite) {
1948 to_write++;
1949 if (!test_bit(R5_OVERWRITE, &dev->flags))
1950 non_overwrite++;
1951 }
1952 if (dev->written) written++;
1953 rdev = rcu_dereference(conf->disks[i].rdev);
1954 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1955 /* The ReadError flag will just be confusing now */
1956 clear_bit(R5_ReadError, &dev->flags);
1957 clear_bit(R5_ReWrite, &dev->flags);
1da177e4 1958 }
16a53ecc
N
1959 if (!rdev || !test_bit(In_sync, &rdev->flags)
1960 || test_bit(R5_ReadError, &dev->flags)) {
1961 if ( failed < 2 )
1962 failed_num[failed] = i;
1963 failed++;
1964 } else
1965 set_bit(R5_Insync, &dev->flags);
1da177e4
LT
1966 }
1967 rcu_read_unlock();
16a53ecc
N
1968 PRINTK("locked=%d uptodate=%d to_read=%d"
1969 " to_write=%d failed=%d failed_num=%d,%d\n",
1970 locked, uptodate, to_read, to_write, failed,
1971 failed_num[0], failed_num[1]);
1972 /* check if the array has lost >2 devices and, if so, some requests might
1973 * need to be failed
1974 */
1975 if (failed > 2 && to_read+to_write+written) {
1976 for (i=disks; i--; ) {
1977 int bitmap_end = 0;
1da177e4 1978
16a53ecc
N
1979 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1980 mdk_rdev_t *rdev;
1981 rcu_read_lock();
1982 rdev = rcu_dereference(conf->disks[i].rdev);
1983 if (rdev && test_bit(In_sync, &rdev->flags))
1984 /* multiple read failures in one stripe */
1985 md_error(conf->mddev, rdev);
1986 rcu_read_unlock();
1987 }
1da177e4 1988
16a53ecc
N
1989 spin_lock_irq(&conf->device_lock);
1990 /* fail all writes first */
1991 bi = sh->dev[i].towrite;
1992 sh->dev[i].towrite = NULL;
1993 if (bi) { to_write--; bitmap_end = 1; }
1da177e4 1994
16a53ecc
N
1995 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1996 wake_up(&conf->wait_for_overlap);
1997
1998 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1999 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2000 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2001 if (--bi->bi_phys_segments == 0) {
2002 md_write_end(conf->mddev);
2003 bi->bi_next = return_bi;
2004 return_bi = bi;
2005 }
2006 bi = nextbi;
2007 }
2008 /* and fail all 'written' */
2009 bi = sh->dev[i].written;
2010 sh->dev[i].written = NULL;
2011 if (bi) bitmap_end = 1;
2012 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2013 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2014 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2015 if (--bi->bi_phys_segments == 0) {
2016 md_write_end(conf->mddev);
2017 bi->bi_next = return_bi;
2018 return_bi = bi;
2019 }
2020 bi = bi2;
2021 }
2022
2023 /* fail any reads if this device is non-operational */
2024 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2025 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2026 bi = sh->dev[i].toread;
2027 sh->dev[i].toread = NULL;
2028 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2029 wake_up(&conf->wait_for_overlap);
2030 if (bi) to_read--;
2031 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2032 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2033 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2034 if (--bi->bi_phys_segments == 0) {
2035 bi->bi_next = return_bi;
2036 return_bi = bi;
2037 }
2038 bi = nextbi;
2039 }
2040 }
2041 spin_unlock_irq(&conf->device_lock);
2042 if (bitmap_end)
2043 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2044 STRIPE_SECTORS, 0, 0);
2045 }
2046 }
2047 if (failed > 2 && syncing) {
2048 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2049 clear_bit(STRIPE_SYNCING, &sh->state);
2050 syncing = 0;
2051 }
2052
2053 /*
2054 * might be able to return some write requests if the parity blocks
2055 * are safe, or on a failed drive
2056 */
2057 pdev = &sh->dev[pd_idx];
2058 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2059 || (failed >= 2 && failed_num[1] == pd_idx);
2060 qdev = &sh->dev[qd_idx];
2061 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2062 || (failed >= 2 && failed_num[1] == qd_idx);
2063
2064 if ( written &&
2065 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2066 && !test_bit(R5_LOCKED, &pdev->flags)
2067 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2068 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2069 && !test_bit(R5_LOCKED, &qdev->flags)
2070 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2071 /* any written block on an uptodate or failed drive can be
2072 * returned. Note that if we 'wrote' to a failed drive,
2073 * it will be UPTODATE, but never LOCKED, so we don't need
2074 * to test 'failed' directly.
2075 */
2076 for (i=disks; i--; )
2077 if (sh->dev[i].written) {
2078 dev = &sh->dev[i];
2079 if (!test_bit(R5_LOCKED, &dev->flags) &&
2080 test_bit(R5_UPTODATE, &dev->flags) ) {
2081 /* We can return any write requests */
2082 int bitmap_end = 0;
2083 struct bio *wbi, *wbi2;
2084 PRINTK("Return write for stripe %llu disc %d\n",
2085 (unsigned long long)sh->sector, i);
2086 spin_lock_irq(&conf->device_lock);
2087 wbi = dev->written;
2088 dev->written = NULL;
2089 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2090 wbi2 = r5_next_bio(wbi, dev->sector);
2091 if (--wbi->bi_phys_segments == 0) {
2092 md_write_end(conf->mddev);
2093 wbi->bi_next = return_bi;
2094 return_bi = wbi;
2095 }
2096 wbi = wbi2;
2097 }
2098 if (dev->towrite == NULL)
2099 bitmap_end = 1;
2100 spin_unlock_irq(&conf->device_lock);
2101 if (bitmap_end)
2102 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2103 STRIPE_SECTORS,
2104 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2105 }
2106 }
2107 }
2108
2109 /* Now we might consider reading some blocks, either to check/generate
2110 * parity, or to satisfy requests
2111 * or to load a block that is being partially written.
2112 */
2113 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2114 for (i=disks; i--;) {
2115 dev = &sh->dev[i];
2116 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2117 (dev->toread ||
2118 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2119 syncing ||
2120 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2121 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2122 )
2123 ) {
2124 /* we would like to get this block, possibly
2125 * by computing it, but we might not be able to
2126 */
2127 if (uptodate == disks-1) {
2128 PRINTK("Computing stripe %llu block %d\n",
2129 (unsigned long long)sh->sector, i);
2130 compute_block_1(sh, i, 0);
2131 uptodate++;
2132 } else if ( uptodate == disks-2 && failed >= 2 ) {
2133 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2134 int other;
2135 for (other=disks; other--;) {
2136 if ( other == i )
2137 continue;
2138 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2139 break;
2140 }
2141 BUG_ON(other < 0);
2142 PRINTK("Computing stripe %llu blocks %d,%d\n",
2143 (unsigned long long)sh->sector, i, other);
2144 compute_block_2(sh, i, other);
2145 uptodate += 2;
2146 } else if (test_bit(R5_Insync, &dev->flags)) {
2147 set_bit(R5_LOCKED, &dev->flags);
2148 set_bit(R5_Wantread, &dev->flags);
16a53ecc
N
2149 locked++;
2150 PRINTK("Reading block %d (sync=%d)\n",
2151 i, syncing);
2152 }
2153 }
2154 }
2155 set_bit(STRIPE_HANDLE, &sh->state);
2156 }
2157
2158 /* now to consider writing and what else, if anything should be read */
2159 if (to_write) {
2160 int rcw=0, must_compute=0;
2161 for (i=disks ; i--;) {
2162 dev = &sh->dev[i];
2163 /* Would I have to read this buffer for reconstruct_write */
2164 if (!test_bit(R5_OVERWRITE, &dev->flags)
2165 && i != pd_idx && i != qd_idx
2166 && (!test_bit(R5_LOCKED, &dev->flags)
16a53ecc
N
2167 ) &&
2168 !test_bit(R5_UPTODATE, &dev->flags)) {
2169 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2170 else {
2171 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2172 must_compute++;
2173 }
2174 }
2175 }
2176 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2177 (unsigned long long)sh->sector, rcw, must_compute);
2178 set_bit(STRIPE_HANDLE, &sh->state);
2179
2180 if (rcw > 0)
2181 /* want reconstruct write, but need to get some data */
2182 for (i=disks; i--;) {
2183 dev = &sh->dev[i];
2184 if (!test_bit(R5_OVERWRITE, &dev->flags)
2185 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2186 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2187 test_bit(R5_Insync, &dev->flags)) {
2188 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2189 {
2190 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2191 (unsigned long long)sh->sector, i);
2192 set_bit(R5_LOCKED, &dev->flags);
2193 set_bit(R5_Wantread, &dev->flags);
2194 locked++;
2195 } else {
2196 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2197 (unsigned long long)sh->sector, i);
2198 set_bit(STRIPE_DELAYED, &sh->state);
2199 set_bit(STRIPE_HANDLE, &sh->state);
2200 }
2201 }
2202 }
2203 /* now if nothing is locked, and if we have enough data, we can start a write request */
2204 if (locked == 0 && rcw == 0 &&
2205 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2206 if ( must_compute > 0 ) {
2207 /* We have failed blocks and need to compute them */
2208 switch ( failed ) {
2209 case 0: BUG();
2210 case 1: compute_block_1(sh, failed_num[0], 0); break;
2211 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2212 default: BUG(); /* This request should have been failed? */
2213 }
2214 }
2215
2216 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2217 compute_parity6(sh, RECONSTRUCT_WRITE);
2218 /* now every locked buffer is ready to be written */
2219 for (i=disks; i--;)
2220 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2221 PRINTK("Writing stripe %llu block %d\n",
2222 (unsigned long long)sh->sector, i);
2223 locked++;
2224 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2225 }
2226 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2227 set_bit(STRIPE_INSYNC, &sh->state);
2228
2229 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2230 atomic_dec(&conf->preread_active_stripes);
2231 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2232 md_wakeup_thread(conf->mddev->thread);
2233 }
2234 }
2235 }
2236
2237 /* maybe we need to check and possibly fix the parity for this stripe
2238 * Any reads will already have been scheduled, so we just see if enough data
2239 * is available
2240 */
2241 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2242 int update_p = 0, update_q = 0;
2243 struct r5dev *dev;
2244
2245 set_bit(STRIPE_HANDLE, &sh->state);
2246
2247 BUG_ON(failed>2);
2248 BUG_ON(uptodate < disks);
2249 /* Want to check and possibly repair P and Q.
2250 * However there could be one 'failed' device, in which
2251 * case we can only check one of them, possibly using the
2252 * other to generate missing data
2253 */
2254
2255 /* If !tmp_page, we cannot do the calculations,
2256 * but as we have set STRIPE_HANDLE, we will soon be called
2257 * by stripe_handle with a tmp_page - just wait until then.
2258 */
2259 if (tmp_page) {
2260 if (failed == q_failed) {
2261 /* The only possible failed device holds 'Q', so it makes
2262 * sense to check P (If anything else were failed, we would
2263 * have used P to recreate it).
2264 */
2265 compute_block_1(sh, pd_idx, 1);
2266 if (!page_is_zero(sh->dev[pd_idx].page)) {
2267 compute_block_1(sh,pd_idx,0);
2268 update_p = 1;
2269 }
2270 }
2271 if (!q_failed && failed < 2) {
2272 /* q is not failed, and we didn't use it to generate
2273 * anything, so it makes sense to check it
2274 */
2275 memcpy(page_address(tmp_page),
2276 page_address(sh->dev[qd_idx].page),
2277 STRIPE_SIZE);
2278 compute_parity6(sh, UPDATE_PARITY);
2279 if (memcmp(page_address(tmp_page),
2280 page_address(sh->dev[qd_idx].page),
2281 STRIPE_SIZE)!= 0) {
2282 clear_bit(STRIPE_INSYNC, &sh->state);
2283 update_q = 1;
2284 }
2285 }
2286 if (update_p || update_q) {
2287 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2288 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2289 /* don't try to repair!! */
2290 update_p = update_q = 0;
2291 }
2292
2293 /* now write out any block on a failed drive,
2294 * or P or Q if they need it
2295 */
2296
2297 if (failed == 2) {
2298 dev = &sh->dev[failed_num[1]];
2299 locked++;
2300 set_bit(R5_LOCKED, &dev->flags);
2301 set_bit(R5_Wantwrite, &dev->flags);
2302 }
2303 if (failed >= 1) {
2304 dev = &sh->dev[failed_num[0]];
2305 locked++;
2306 set_bit(R5_LOCKED, &dev->flags);
2307 set_bit(R5_Wantwrite, &dev->flags);
2308 }
2309
2310 if (update_p) {
2311 dev = &sh->dev[pd_idx];
2312 locked ++;
2313 set_bit(R5_LOCKED, &dev->flags);
2314 set_bit(R5_Wantwrite, &dev->flags);
2315 }
2316 if (update_q) {
2317 dev = &sh->dev[qd_idx];
2318 locked++;
2319 set_bit(R5_LOCKED, &dev->flags);
2320 set_bit(R5_Wantwrite, &dev->flags);
2321 }
2322 clear_bit(STRIPE_DEGRADED, &sh->state);
2323
2324 set_bit(STRIPE_INSYNC, &sh->state);
2325 }
2326 }
2327
2328 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2329 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2330 clear_bit(STRIPE_SYNCING, &sh->state);
2331 }
2332
2333 /* If the failed drives are just a ReadError, then we might need
2334 * to progress the repair/check process
2335 */
2336 if (failed <= 2 && ! conf->mddev->ro)
2337 for (i=0; i<failed;i++) {
2338 dev = &sh->dev[failed_num[i]];
2339 if (test_bit(R5_ReadError, &dev->flags)
2340 && !test_bit(R5_LOCKED, &dev->flags)
2341 && test_bit(R5_UPTODATE, &dev->flags)
2342 ) {
2343 if (!test_bit(R5_ReWrite, &dev->flags)) {
2344 set_bit(R5_Wantwrite, &dev->flags);
2345 set_bit(R5_ReWrite, &dev->flags);
2346 set_bit(R5_LOCKED, &dev->flags);
2347 } else {
2348 /* let's read it back */
2349 set_bit(R5_Wantread, &dev->flags);
2350 set_bit(R5_LOCKED, &dev->flags);
2351 }
2352 }
2353 }
2354 spin_unlock(&sh->lock);
2355
2356 while ((bi=return_bi)) {
2357 int bytes = bi->bi_size;
2358
2359 return_bi = bi->bi_next;
2360 bi->bi_next = NULL;
2361 bi->bi_size = 0;
2362 bi->bi_end_io(bi, bytes, 0);
2363 }
2364 for (i=disks; i-- ;) {
2365 int rw;
2366 struct bio *bi;
2367 mdk_rdev_t *rdev;
2368 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2369 rw = 1;
2370 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2371 rw = 0;
2372 else
2373 continue;
2374
2375 bi = &sh->dev[i].req;
2376
2377 bi->bi_rw = rw;
2378 if (rw)
2379 bi->bi_end_io = raid5_end_write_request;
2380 else
2381 bi->bi_end_io = raid5_end_read_request;
2382
2383 rcu_read_lock();
2384 rdev = rcu_dereference(conf->disks[i].rdev);
2385 if (rdev && test_bit(Faulty, &rdev->flags))
2386 rdev = NULL;
2387 if (rdev)
2388 atomic_inc(&rdev->nr_pending);
2389 rcu_read_unlock();
2390
2391 if (rdev) {
2392 if (syncing)
2393 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2394
2395 bi->bi_bdev = rdev->bdev;
2396 PRINTK("for %llu schedule op %ld on disc %d\n",
2397 (unsigned long long)sh->sector, bi->bi_rw, i);
2398 atomic_inc(&sh->count);
2399 bi->bi_sector = sh->sector + rdev->data_offset;
2400 bi->bi_flags = 1 << BIO_UPTODATE;
2401 bi->bi_vcnt = 1;
2402 bi->bi_max_vecs = 1;
2403 bi->bi_idx = 0;
2404 bi->bi_io_vec = &sh->dev[i].vec;
2405 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2406 bi->bi_io_vec[0].bv_offset = 0;
2407 bi->bi_size = STRIPE_SIZE;
2408 bi->bi_next = NULL;
2409 if (rw == WRITE &&
2410 test_bit(R5_ReWrite, &sh->dev[i].flags))
2411 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2412 generic_make_request(bi);
2413 } else {
2414 if (rw == 1)
2415 set_bit(STRIPE_DEGRADED, &sh->state);
2416 PRINTK("skip op %ld on disc %d for sector %llu\n",
2417 bi->bi_rw, i, (unsigned long long)sh->sector);
2418 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2419 set_bit(STRIPE_HANDLE, &sh->state);
2420 }
2421 }
2422}
2423
2424static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2425{
2426 if (sh->raid_conf->level == 6)
2427 handle_stripe6(sh, tmp_page);
2428 else
2429 handle_stripe5(sh);
2430}
2431
2432
2433
2434static void raid5_activate_delayed(raid5_conf_t *conf)
2435{
2436 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2437 while (!list_empty(&conf->delayed_list)) {
2438 struct list_head *l = conf->delayed_list.next;
2439 struct stripe_head *sh;
2440 sh = list_entry(l, struct stripe_head, lru);
2441 list_del_init(l);
2442 clear_bit(STRIPE_DELAYED, &sh->state);
2443 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2444 atomic_inc(&conf->preread_active_stripes);
2445 list_add_tail(&sh->lru, &conf->handle_list);
2446 }
2447 }
2448}
2449
2450static void activate_bit_delay(raid5_conf_t *conf)
2451{
2452 /* device_lock is held */
2453 struct list_head head;
2454 list_add(&head, &conf->bitmap_list);
2455 list_del_init(&conf->bitmap_list);
2456 while (!list_empty(&head)) {
2457 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2458 list_del_init(&sh->lru);
2459 atomic_inc(&sh->count);
2460 __release_stripe(conf, sh);
2461 }
2462}
2463
2464static void unplug_slaves(mddev_t *mddev)
2465{
2466 raid5_conf_t *conf = mddev_to_conf(mddev);
2467 int i;
2468
2469 rcu_read_lock();
2470 for (i=0; i<mddev->raid_disks; i++) {
2471 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
2472 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
2473 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2474
2475 atomic_inc(&rdev->nr_pending);
2476 rcu_read_unlock();
2477
2478 if (r_queue->unplug_fn)
2479 r_queue->unplug_fn(r_queue);
2480
2481 rdev_dec_pending(rdev, mddev);
2482 rcu_read_lock();
2483 }
2484 }
2485 rcu_read_unlock();
2486}
2487
2488static void raid5_unplug_device(request_queue_t *q)
2489{
2490 mddev_t *mddev = q->queuedata;
2491 raid5_conf_t *conf = mddev_to_conf(mddev);
2492 unsigned long flags;
2493
2494 spin_lock_irqsave(&conf->device_lock, flags);
2495
2496 if (blk_remove_plug(q)) {
2497 conf->seq_flush++;
2498 raid5_activate_delayed(conf);
72626685 2499 }
1da177e4
LT
2500 md_wakeup_thread(mddev->thread);
2501
2502 spin_unlock_irqrestore(&conf->device_lock, flags);
2503
2504 unplug_slaves(mddev);
2505}
2506
2507static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2508 sector_t *error_sector)
2509{
2510 mddev_t *mddev = q->queuedata;
2511 raid5_conf_t *conf = mddev_to_conf(mddev);
2512 int i, ret = 0;
2513
2514 rcu_read_lock();
2515 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
d6065f7b 2516 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 2517 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
2518 struct block_device *bdev = rdev->bdev;
2519 request_queue_t *r_queue = bdev_get_queue(bdev);
2520
2521 if (!r_queue->issue_flush_fn)
2522 ret = -EOPNOTSUPP;
2523 else {
2524 atomic_inc(&rdev->nr_pending);
2525 rcu_read_unlock();
2526 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2527 error_sector);
2528 rdev_dec_pending(rdev, mddev);
2529 rcu_read_lock();
2530 }
2531 }
2532 }
2533 rcu_read_unlock();
2534 return ret;
2535}
2536
f022b2fd
N
2537static int raid5_congested(void *data, int bits)
2538{
2539 mddev_t *mddev = data;
2540 raid5_conf_t *conf = mddev_to_conf(mddev);
2541
2542 /* No difference between reads and writes. Just check
2543 * how busy the stripe_cache is
2544 */
2545 if (conf->inactive_blocked)
2546 return 1;
2547 if (conf->quiesce)
2548 return 1;
2549 if (list_empty_careful(&conf->inactive_list))
2550 return 1;
2551
2552 return 0;
2553}
2554
23032a0e
RBJ
2555/* We want read requests to align with chunks where possible,
2556 * but write requests don't need to.
2557 */
2558static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
2559{
2560 mddev_t *mddev = q->queuedata;
2561 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2562 int max;
2563 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2564 unsigned int bio_sectors = bio->bi_size >> 9;
2565
2566 if (bio_data_dir(bio))
2567 return biovec->bv_len; /* always allow writes to be mergeable */
2568
2569 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
2570 if (max < 0) max = 0;
2571 if (max <= biovec->bv_len && bio_sectors == 0)
2572 return biovec->bv_len;
2573 else
2574 return max;
2575}
2576
f679623f
RBJ
2577
2578static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
2579{
2580 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2581 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2582 unsigned int bio_sectors = bio->bi_size >> 9;
2583
2584 return chunk_sectors >=
2585 ((sector & (chunk_sectors - 1)) + bio_sectors);
2586}
2587
46031f9a
RBJ
2588/*
2589 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
2590 * later sampled by raid5d.
2591 */
2592static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
2593{
2594 unsigned long flags;
2595
2596 spin_lock_irqsave(&conf->device_lock, flags);
2597
2598 bi->bi_next = conf->retry_read_aligned_list;
2599 conf->retry_read_aligned_list = bi;
2600
2601 spin_unlock_irqrestore(&conf->device_lock, flags);
2602 md_wakeup_thread(conf->mddev->thread);
2603}
2604
2605
2606static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
2607{
2608 struct bio *bi;
2609
2610 bi = conf->retry_read_aligned;
2611 if (bi) {
2612 conf->retry_read_aligned = NULL;
2613 return bi;
2614 }
2615 bi = conf->retry_read_aligned_list;
2616 if(bi) {
2617 conf->retry_read_aligned = bi->bi_next;
2618 bi->bi_next = NULL;
2619 bi->bi_phys_segments = 1; /* biased count of active stripes */
2620 bi->bi_hw_segments = 0; /* count of processed stripes */
2621 }
2622
2623 return bi;
2624}
2625
2626
f679623f
RBJ
2627/*
2628 * The "raid5_align_endio" should check if the read succeeded and if it
2629 * did, call bio_endio on the original bio (having bio_put the new bio
2630 * first).
2631 * If the read failed..
2632 */
46031f9a 2633static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
f679623f
RBJ
2634{
2635 struct bio* raid_bi = bi->bi_private;
46031f9a
RBJ
2636 mddev_t *mddev;
2637 raid5_conf_t *conf;
2638 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
2639 mdk_rdev_t *rdev;
2640
f679623f
RBJ
2641 if (bi->bi_size)
2642 return 1;
2643 bio_put(bi);
46031f9a
RBJ
2644
2645 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
2646 conf = mddev_to_conf(mddev);
2647 rdev = (void*)raid_bi->bi_next;
2648 raid_bi->bi_next = NULL;
2649
2650 rdev_dec_pending(rdev, conf->mddev);
2651
2652 if (!error && uptodate) {
2653 bio_endio(raid_bi, bytes, 0);
2654 if (atomic_dec_and_test(&conf->active_aligned_reads))
2655 wake_up(&conf->wait_for_stripe);
2656 return 0;
2657 }
2658
2659
2660 PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
2661
2662 add_bio_to_retry(raid_bi, conf);
f679623f
RBJ
2663 return 0;
2664}
2665
2666static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
2667{
2668 mddev_t *mddev = q->queuedata;
2669 raid5_conf_t *conf = mddev_to_conf(mddev);
2670 const unsigned int raid_disks = conf->raid_disks;
46031f9a 2671 const unsigned int data_disks = raid_disks - conf->max_degraded;
f679623f
RBJ
2672 unsigned int dd_idx, pd_idx;
2673 struct bio* align_bi;
2674 mdk_rdev_t *rdev;
2675
2676 if (!in_chunk_boundary(mddev, raid_bio)) {
2677 printk("chunk_aligned_read : non aligned\n");
2678 return 0;
2679 }
2680 /*
2681 * use bio_clone to make a copy of the bio
2682 */
2683 align_bi = bio_clone(raid_bio, GFP_NOIO);
2684 if (!align_bi)
2685 return 0;
2686 /*
2687 * set bi_end_io to a new function, and set bi_private to the
2688 * original bio.
2689 */
2690 align_bi->bi_end_io = raid5_align_endio;
2691 align_bi->bi_private = raid_bio;
2692 /*
2693 * compute position
2694 */
2695 align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
2696 raid_disks,
2697 data_disks,
2698 &dd_idx,
2699 &pd_idx,
2700 conf);
2701
2702 rcu_read_lock();
2703 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
2704 if (rdev && test_bit(In_sync, &rdev->flags)) {
f679623f
RBJ
2705 atomic_inc(&rdev->nr_pending);
2706 rcu_read_unlock();
46031f9a
RBJ
2707 raid_bio->bi_next = (void*)rdev;
2708 align_bi->bi_bdev = rdev->bdev;
2709 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
2710 align_bi->bi_sector += rdev->data_offset;
2711
2712 spin_lock_irq(&conf->device_lock);
2713 wait_event_lock_irq(conf->wait_for_stripe,
2714 conf->quiesce == 0,
2715 conf->device_lock, /* nothing */);
2716 atomic_inc(&conf->active_aligned_reads);
2717 spin_unlock_irq(&conf->device_lock);
2718
f679623f
RBJ
2719 generic_make_request(align_bi);
2720 return 1;
2721 } else {
2722 rcu_read_unlock();
46031f9a 2723 bio_put(align_bi);
f679623f
RBJ
2724 return 0;
2725 }
2726}
2727
2728
7ecaa1e6 2729static int make_request(request_queue_t *q, struct bio * bi)
1da177e4
LT
2730{
2731 mddev_t *mddev = q->queuedata;
2732 raid5_conf_t *conf = mddev_to_conf(mddev);
1da177e4
LT
2733 unsigned int dd_idx, pd_idx;
2734 sector_t new_sector;
2735 sector_t logical_sector, last_sector;
2736 struct stripe_head *sh;
a362357b 2737 const int rw = bio_data_dir(bi);
f6344757 2738 int remaining;
1da177e4 2739
e5dcdd80
N
2740 if (unlikely(bio_barrier(bi))) {
2741 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2742 return 0;
2743 }
2744
3d310eb7 2745 md_write_start(mddev, bi);
06d91a5f 2746
a362357b
JA
2747 disk_stat_inc(mddev->gendisk, ios[rw]);
2748 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1da177e4 2749
52488615
RBJ
2750 if (bio_data_dir(bi) == READ &&
2751 mddev->reshape_position == MaxSector &&
2752 chunk_aligned_read(q,bi))
2753 return 0;
2754
1da177e4
LT
2755 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2756 last_sector = bi->bi_sector + (bi->bi_size>>9);
2757 bi->bi_next = NULL;
2758 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
06d91a5f 2759
1da177e4
LT
2760 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2761 DEFINE_WAIT(w);
16a53ecc 2762 int disks, data_disks;
b578d55f 2763
7ecaa1e6 2764 retry:
b578d55f 2765 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
7ecaa1e6
N
2766 if (likely(conf->expand_progress == MaxSector))
2767 disks = conf->raid_disks;
2768 else {
df8e7f76
N
2769 /* spinlock is needed as expand_progress may be
2770 * 64bit on a 32bit platform, and so it might be
2771 * possible to see a half-updated value
2772 * Ofcourse expand_progress could change after
2773 * the lock is dropped, so once we get a reference
2774 * to the stripe that we think it is, we will have
2775 * to check again.
2776 */
7ecaa1e6
N
2777 spin_lock_irq(&conf->device_lock);
2778 disks = conf->raid_disks;
2779 if (logical_sector >= conf->expand_progress)
2780 disks = conf->previous_raid_disks;
b578d55f
N
2781 else {
2782 if (logical_sector >= conf->expand_lo) {
2783 spin_unlock_irq(&conf->device_lock);
2784 schedule();
2785 goto retry;
2786 }
2787 }
7ecaa1e6
N
2788 spin_unlock_irq(&conf->device_lock);
2789 }
16a53ecc
N
2790 data_disks = disks - conf->max_degraded;
2791
2792 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
7ecaa1e6 2793 &dd_idx, &pd_idx, conf);
1da177e4
LT
2794 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2795 (unsigned long long)new_sector,
2796 (unsigned long long)logical_sector);
2797
7ecaa1e6 2798 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
1da177e4 2799 if (sh) {
7ecaa1e6
N
2800 if (unlikely(conf->expand_progress != MaxSector)) {
2801 /* expansion might have moved on while waiting for a
df8e7f76
N
2802 * stripe, so we must do the range check again.
2803 * Expansion could still move past after this
2804 * test, but as we are holding a reference to
2805 * 'sh', we know that if that happens,
2806 * STRIPE_EXPANDING will get set and the expansion
2807 * won't proceed until we finish with the stripe.
7ecaa1e6
N
2808 */
2809 int must_retry = 0;
2810 spin_lock_irq(&conf->device_lock);
2811 if (logical_sector < conf->expand_progress &&
2812 disks == conf->previous_raid_disks)
2813 /* mismatch, need to try again */
2814 must_retry = 1;
2815 spin_unlock_irq(&conf->device_lock);
2816 if (must_retry) {
2817 release_stripe(sh);
2818 goto retry;
2819 }
2820 }
e464eafd
N
2821 /* FIXME what if we get a false positive because these
2822 * are being updated.
2823 */
2824 if (logical_sector >= mddev->suspend_lo &&
2825 logical_sector < mddev->suspend_hi) {
2826 release_stripe(sh);
2827 schedule();
2828 goto retry;
2829 }
7ecaa1e6
N
2830
2831 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2832 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2833 /* Stripe is busy expanding or
2834 * add failed due to overlap. Flush everything
1da177e4
LT
2835 * and wait a while
2836 */
2837 raid5_unplug_device(mddev->queue);
2838 release_stripe(sh);
2839 schedule();
2840 goto retry;
2841 }
2842 finish_wait(&conf->wait_for_overlap, &w);
16a53ecc 2843 handle_stripe(sh, NULL);
1da177e4 2844 release_stripe(sh);
1da177e4
LT
2845 } else {
2846 /* cannot get stripe for read-ahead, just give-up */
2847 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2848 finish_wait(&conf->wait_for_overlap, &w);
2849 break;
2850 }
2851
2852 }
2853 spin_lock_irq(&conf->device_lock);
f6344757
N
2854 remaining = --bi->bi_phys_segments;
2855 spin_unlock_irq(&conf->device_lock);
2856 if (remaining == 0) {
1da177e4
LT
2857 int bytes = bi->bi_size;
2858
16a53ecc 2859 if ( rw == WRITE )
1da177e4
LT
2860 md_write_end(mddev);
2861 bi->bi_size = 0;
2862 bi->bi_end_io(bi, bytes, 0);
2863 }
1da177e4
LT
2864 return 0;
2865}
2866
52c03291 2867static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
1da177e4 2868{
52c03291
N
2869 /* reshaping is quite different to recovery/resync so it is
2870 * handled quite separately ... here.
2871 *
2872 * On each call to sync_request, we gather one chunk worth of
2873 * destination stripes and flag them as expanding.
2874 * Then we find all the source stripes and request reads.
2875 * As the reads complete, handle_stripe will copy the data
2876 * into the destination stripe and release that stripe.
2877 */
1da177e4
LT
2878 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2879 struct stripe_head *sh;
ccfcc3c1
N
2880 int pd_idx;
2881 sector_t first_sector, last_sector;
52c03291
N
2882 int raid_disks;
2883 int data_disks;
2884 int i;
2885 int dd_idx;
2886 sector_t writepos, safepos, gap;
2887
2888 if (sector_nr == 0 &&
2889 conf->expand_progress != 0) {
2890 /* restarting in the middle, skip the initial sectors */
2891 sector_nr = conf->expand_progress;
2892 sector_div(sector_nr, conf->raid_disks-1);
2893 *skipped = 1;
2894 return sector_nr;
2895 }
2896
2897 /* we update the metadata when there is more than 3Meg
2898 * in the block range (that is rather arbitrary, should
2899 * probably be time based) or when the data about to be
2900 * copied would over-write the source of the data at
2901 * the front of the range.
2902 * i.e. one new_stripe forward from expand_progress new_maps
2903 * to after where expand_lo old_maps to
2904 */
2905 writepos = conf->expand_progress +
2906 conf->chunk_size/512*(conf->raid_disks-1);
2907 sector_div(writepos, conf->raid_disks-1);
2908 safepos = conf->expand_lo;
2909 sector_div(safepos, conf->previous_raid_disks-1);
2910 gap = conf->expand_progress - conf->expand_lo;
2911
2912 if (writepos >= safepos ||
2913 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2914 /* Cannot proceed until we've updated the superblock... */
2915 wait_event(conf->wait_for_overlap,
2916 atomic_read(&conf->reshape_stripes)==0);
2917 mddev->reshape_position = conf->expand_progress;
850b2b42 2918 set_bit(MD_CHANGE_DEVS, &mddev->flags);
52c03291 2919 md_wakeup_thread(mddev->thread);
850b2b42 2920 wait_event(mddev->sb_wait, mddev->flags == 0 ||
52c03291
N
2921 kthread_should_stop());
2922 spin_lock_irq(&conf->device_lock);
2923 conf->expand_lo = mddev->reshape_position;
2924 spin_unlock_irq(&conf->device_lock);
2925 wake_up(&conf->wait_for_overlap);
2926 }
2927
2928 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2929 int j;
2930 int skipped = 0;
2931 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2932 sh = get_active_stripe(conf, sector_nr+i,
2933 conf->raid_disks, pd_idx, 0);
2934 set_bit(STRIPE_EXPANDING, &sh->state);
2935 atomic_inc(&conf->reshape_stripes);
2936 /* If any of this stripe is beyond the end of the old
2937 * array, then we need to zero those blocks
2938 */
2939 for (j=sh->disks; j--;) {
2940 sector_t s;
2941 if (j == sh->pd_idx)
2942 continue;
2943 s = compute_blocknr(sh, j);
2944 if (s < (mddev->array_size<<1)) {
2945 skipped = 1;
2946 continue;
2947 }
2948 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2949 set_bit(R5_Expanded, &sh->dev[j].flags);
2950 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2951 }
2952 if (!skipped) {
2953 set_bit(STRIPE_EXPAND_READY, &sh->state);
2954 set_bit(STRIPE_HANDLE, &sh->state);
2955 }
2956 release_stripe(sh);
2957 }
2958 spin_lock_irq(&conf->device_lock);
2959 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2960 spin_unlock_irq(&conf->device_lock);
2961 /* Ok, those stripe are ready. We can start scheduling
2962 * reads on the source stripes.
2963 * The source stripes are determined by mapping the first and last
2964 * block on the destination stripes.
2965 */
2966 raid_disks = conf->previous_raid_disks;
2967 data_disks = raid_disks - 1;
2968 first_sector =
2969 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2970 raid_disks, data_disks,
2971 &dd_idx, &pd_idx, conf);
2972 last_sector =
2973 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2974 *(conf->raid_disks-1) -1,
2975 raid_disks, data_disks,
2976 &dd_idx, &pd_idx, conf);
2977 if (last_sector >= (mddev->size<<1))
2978 last_sector = (mddev->size<<1)-1;
2979 while (first_sector <= last_sector) {
2980 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2981 sh = get_active_stripe(conf, first_sector,
2982 conf->previous_raid_disks, pd_idx, 0);
2983 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2984 set_bit(STRIPE_HANDLE, &sh->state);
2985 release_stripe(sh);
2986 first_sector += STRIPE_SECTORS;
2987 }
2988 return conf->chunk_size>>9;
2989}
2990
2991/* FIXME go_faster isn't used */
2992static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2993{
2994 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2995 struct stripe_head *sh;
2996 int pd_idx;
1da177e4 2997 int raid_disks = conf->raid_disks;
72626685
N
2998 sector_t max_sector = mddev->size << 1;
2999 int sync_blocks;
16a53ecc
N
3000 int still_degraded = 0;
3001 int i;
1da177e4 3002
72626685 3003 if (sector_nr >= max_sector) {
1da177e4
LT
3004 /* just being told to finish up .. nothing much to do */
3005 unplug_slaves(mddev);
29269553
N
3006 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3007 end_reshape(conf);
3008 return 0;
3009 }
72626685
N
3010
3011 if (mddev->curr_resync < max_sector) /* aborted */
3012 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3013 &sync_blocks, 1);
16a53ecc 3014 else /* completed sync */
72626685
N
3015 conf->fullsync = 0;
3016 bitmap_close_sync(mddev->bitmap);
3017
1da177e4
LT
3018 return 0;
3019 }
ccfcc3c1 3020
52c03291
N
3021 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3022 return reshape_request(mddev, sector_nr, skipped);
f6705578 3023
16a53ecc 3024 /* if there is too many failed drives and we are trying
1da177e4
LT
3025 * to resync, then assert that we are finished, because there is
3026 * nothing we can do.
3027 */
3285edf1 3028 if (mddev->degraded >= conf->max_degraded &&
16a53ecc 3029 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
57afd89f
N
3030 sector_t rv = (mddev->size << 1) - sector_nr;
3031 *skipped = 1;
1da177e4
LT
3032 return rv;
3033 }
72626685 3034 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
3855ad9f 3035 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
72626685
N
3036 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3037 /* we can skip this block, and probably more */
3038 sync_blocks /= STRIPE_SECTORS;
3039 *skipped = 1;
3040 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3041 }
1da177e4 3042
ccfcc3c1 3043 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
7ecaa1e6 3044 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
1da177e4 3045 if (sh == NULL) {
7ecaa1e6 3046 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
1da177e4 3047 /* make sure we don't swamp the stripe cache if someone else
16a53ecc 3048 * is trying to get access
1da177e4 3049 */
66c006a5 3050 schedule_timeout_uninterruptible(1);
1da177e4 3051 }
16a53ecc
N
3052 /* Need to check if array will still be degraded after recovery/resync
3053 * We don't need to check the 'failed' flag as when that gets set,
3054 * recovery aborts.
3055 */
3056 for (i=0; i<mddev->raid_disks; i++)
3057 if (conf->disks[i].rdev == NULL)
3058 still_degraded = 1;
3059
3060 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3061
3062 spin_lock(&sh->lock);
1da177e4
LT
3063 set_bit(STRIPE_SYNCING, &sh->state);
3064 clear_bit(STRIPE_INSYNC, &sh->state);
3065 spin_unlock(&sh->lock);
3066
16a53ecc 3067 handle_stripe(sh, NULL);
1da177e4
LT
3068 release_stripe(sh);
3069
3070 return STRIPE_SECTORS;
3071}
3072
46031f9a
RBJ
3073static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3074{
3075 /* We may not be able to submit a whole bio at once as there
3076 * may not be enough stripe_heads available.
3077 * We cannot pre-allocate enough stripe_heads as we may need
3078 * more than exist in the cache (if we allow ever large chunks).
3079 * So we do one stripe head at a time and record in
3080 * ->bi_hw_segments how many have been done.
3081 *
3082 * We *know* that this entire raid_bio is in one chunk, so
3083 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3084 */
3085 struct stripe_head *sh;
3086 int dd_idx, pd_idx;
3087 sector_t sector, logical_sector, last_sector;
3088 int scnt = 0;
3089 int remaining;
3090 int handled = 0;
3091
3092 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3093 sector = raid5_compute_sector( logical_sector,
3094 conf->raid_disks,
3095 conf->raid_disks - conf->max_degraded,
3096 &dd_idx,
3097 &pd_idx,
3098 conf);
3099 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3100
3101 for (; logical_sector < last_sector;
3102 logical_sector += STRIPE_SECTORS, scnt++) {
3103
3104 if (scnt < raid_bio->bi_hw_segments)
3105 /* already done this stripe */
3106 continue;
3107
3108 sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
3109
3110 if (!sh) {
3111 /* failed to get a stripe - must wait */
3112 raid_bio->bi_hw_segments = scnt;
3113 conf->retry_read_aligned = raid_bio;
3114 return handled;
3115 }
3116
3117 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
3118 add_stripe_bio(sh, raid_bio, dd_idx, 0);
3119 handle_stripe(sh, NULL);
3120 release_stripe(sh);
3121 handled++;
3122 }
3123 spin_lock_irq(&conf->device_lock);
3124 remaining = --raid_bio->bi_phys_segments;
3125 spin_unlock_irq(&conf->device_lock);
3126 if (remaining == 0) {
3127 int bytes = raid_bio->bi_size;
3128
3129 raid_bio->bi_size = 0;
3130 raid_bio->bi_end_io(raid_bio, bytes, 0);
3131 }
3132 if (atomic_dec_and_test(&conf->active_aligned_reads))
3133 wake_up(&conf->wait_for_stripe);
3134 return handled;
3135}
3136
3137
3138
1da177e4
LT
3139/*
3140 * This is our raid5 kernel thread.
3141 *
3142 * We scan the hash table for stripes which can be handled now.
3143 * During the scan, completed stripes are saved for us by the interrupt
3144 * handler, so that they will not have to wait for our next wakeup.
3145 */
3146static void raid5d (mddev_t *mddev)
3147{
3148 struct stripe_head *sh;
3149 raid5_conf_t *conf = mddev_to_conf(mddev);
3150 int handled;
3151
3152 PRINTK("+++ raid5d active\n");
3153
3154 md_check_recovery(mddev);
1da177e4
LT
3155
3156 handled = 0;
3157 spin_lock_irq(&conf->device_lock);
3158 while (1) {
3159 struct list_head *first;
46031f9a 3160 struct bio *bio;
1da177e4 3161
ae3c20cc 3162 if (conf->seq_flush != conf->seq_write) {
72626685 3163 int seq = conf->seq_flush;
700e432d 3164 spin_unlock_irq(&conf->device_lock);
72626685 3165 bitmap_unplug(mddev->bitmap);
700e432d 3166 spin_lock_irq(&conf->device_lock);
72626685
N
3167 conf->seq_write = seq;
3168 activate_bit_delay(conf);
3169 }
3170
1da177e4
LT
3171 if (list_empty(&conf->handle_list) &&
3172 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
3173 !blk_queue_plugged(mddev->queue) &&
3174 !list_empty(&conf->delayed_list))
3175 raid5_activate_delayed(conf);
3176
46031f9a
RBJ
3177 while ((bio = remove_bio_from_retry(conf))) {
3178 int ok;
3179 spin_unlock_irq(&conf->device_lock);
3180 ok = retry_aligned_read(conf, bio);
3181 spin_lock_irq(&conf->device_lock);
3182 if (!ok)
3183 break;
3184 handled++;
3185 }
3186
1da177e4
LT
3187 if (list_empty(&conf->handle_list))
3188 break;
3189
3190 first = conf->handle_list.next;
3191 sh = list_entry(first, struct stripe_head, lru);
3192
3193 list_del_init(first);
3194 atomic_inc(&sh->count);
78bafebd 3195 BUG_ON(atomic_read(&sh->count)!= 1);
1da177e4
LT
3196 spin_unlock_irq(&conf->device_lock);
3197
3198 handled++;
16a53ecc 3199 handle_stripe(sh, conf->spare_page);
1da177e4
LT
3200 release_stripe(sh);
3201
3202 spin_lock_irq(&conf->device_lock);
3203 }
3204 PRINTK("%d stripes handled\n", handled);
3205
3206 spin_unlock_irq(&conf->device_lock);
3207
3208 unplug_slaves(mddev);
3209
3210 PRINTK("--- raid5d inactive\n");
3211}
3212
3f294f4f 3213static ssize_t
007583c9 3214raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
3f294f4f 3215{
007583c9 3216 raid5_conf_t *conf = mddev_to_conf(mddev);
96de1e66
N
3217 if (conf)
3218 return sprintf(page, "%d\n", conf->max_nr_stripes);
3219 else
3220 return 0;
3f294f4f
N
3221}
3222
3223static ssize_t
007583c9 3224raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
3f294f4f 3225{
007583c9 3226 raid5_conf_t *conf = mddev_to_conf(mddev);
3f294f4f
N
3227 char *end;
3228 int new;
3229 if (len >= PAGE_SIZE)
3230 return -EINVAL;
96de1e66
N
3231 if (!conf)
3232 return -ENODEV;
3f294f4f
N
3233
3234 new = simple_strtoul(page, &end, 10);
3235 if (!*page || (*end && *end != '\n') )
3236 return -EINVAL;
3237 if (new <= 16 || new > 32768)
3238 return -EINVAL;
3239 while (new < conf->max_nr_stripes) {
3240 if (drop_one_stripe(conf))
3241 conf->max_nr_stripes--;
3242 else
3243 break;
3244 }
3245 while (new > conf->max_nr_stripes) {
3246 if (grow_one_stripe(conf))
3247 conf->max_nr_stripes++;
3248 else break;
3249 }
3250 return len;
3251}
007583c9 3252
96de1e66
N
3253static struct md_sysfs_entry
3254raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3255 raid5_show_stripe_cache_size,
3256 raid5_store_stripe_cache_size);
3f294f4f
N
3257
3258static ssize_t
96de1e66 3259stripe_cache_active_show(mddev_t *mddev, char *page)
3f294f4f 3260{
007583c9 3261 raid5_conf_t *conf = mddev_to_conf(mddev);
96de1e66
N
3262 if (conf)
3263 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3264 else
3265 return 0;
3f294f4f
N
3266}
3267
96de1e66
N
3268static struct md_sysfs_entry
3269raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3f294f4f 3270
007583c9 3271static struct attribute *raid5_attrs[] = {
3f294f4f
N
3272 &raid5_stripecache_size.attr,
3273 &raid5_stripecache_active.attr,
3274 NULL,
3275};
007583c9
N
3276static struct attribute_group raid5_attrs_group = {
3277 .name = NULL,
3278 .attrs = raid5_attrs,
3f294f4f
N
3279};
3280
72626685 3281static int run(mddev_t *mddev)
1da177e4
LT
3282{
3283 raid5_conf_t *conf;
3284 int raid_disk, memory;
3285 mdk_rdev_t *rdev;
3286 struct disk_info *disk;
3287 struct list_head *tmp;
02c2de8c 3288 int working_disks = 0;
1da177e4 3289
16a53ecc
N
3290 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3291 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
14f8d26b 3292 mdname(mddev), mddev->level);
1da177e4
LT
3293 return -EIO;
3294 }
3295
f6705578
N
3296 if (mddev->reshape_position != MaxSector) {
3297 /* Check that we can continue the reshape.
3298 * Currently only disks can change, it must
3299 * increase, and we must be past the point where
3300 * a stripe over-writes itself
3301 */
3302 sector_t here_new, here_old;
3303 int old_disks;
3304
3305 if (mddev->new_level != mddev->level ||
3306 mddev->new_layout != mddev->layout ||
3307 mddev->new_chunk != mddev->chunk_size) {
3308 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3309 mdname(mddev));
3310 return -EINVAL;
3311 }
3312 if (mddev->delta_disks <= 0) {
3313 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3314 mdname(mddev));
3315 return -EINVAL;
3316 }
3317 old_disks = mddev->raid_disks - mddev->delta_disks;
3318 /* reshape_position must be on a new-stripe boundary, and one
3319 * further up in new geometry must map after here in old geometry.
3320 */
3321 here_new = mddev->reshape_position;
3322 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3323 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3324 return -EINVAL;
3325 }
3326 /* here_new is the stripe we will write to */
3327 here_old = mddev->reshape_position;
3328 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3329 /* here_old is the first stripe that we might need to read from */
3330 if (here_new >= here_old) {
3331 /* Reading from the same stripe as writing to - bad */
3332 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3333 return -EINVAL;
3334 }
3335 printk(KERN_INFO "raid5: reshape will continue\n");
3336 /* OK, we should be able to continue; */
3337 }
3338
3339
b55e6bfc 3340 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
1da177e4
LT
3341 if ((conf = mddev->private) == NULL)
3342 goto abort;
f6705578
N
3343 if (mddev->reshape_position == MaxSector) {
3344 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3345 } else {
3346 conf->raid_disks = mddev->raid_disks;
3347 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3348 }
3349
3350 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
b55e6bfc
N
3351 GFP_KERNEL);
3352 if (!conf->disks)
3353 goto abort;
9ffae0cf 3354
1da177e4
LT
3355 conf->mddev = mddev;
3356
fccddba0 3357 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1da177e4 3358 goto abort;
1da177e4 3359
16a53ecc
N
3360 if (mddev->level == 6) {
3361 conf->spare_page = alloc_page(GFP_KERNEL);
3362 if (!conf->spare_page)
3363 goto abort;
3364 }
1da177e4
LT
3365 spin_lock_init(&conf->device_lock);
3366 init_waitqueue_head(&conf->wait_for_stripe);
3367 init_waitqueue_head(&conf->wait_for_overlap);
3368 INIT_LIST_HEAD(&conf->handle_list);
3369 INIT_LIST_HEAD(&conf->delayed_list);
72626685 3370 INIT_LIST_HEAD(&conf->bitmap_list);
1da177e4
LT
3371 INIT_LIST_HEAD(&conf->inactive_list);
3372 atomic_set(&conf->active_stripes, 0);
3373 atomic_set(&conf->preread_active_stripes, 0);
46031f9a 3374 atomic_set(&conf->active_aligned_reads, 0);
1da177e4 3375
1da177e4
LT
3376 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3377
3378 ITERATE_RDEV(mddev,rdev,tmp) {
3379 raid_disk = rdev->raid_disk;
f6705578 3380 if (raid_disk >= conf->raid_disks
1da177e4
LT
3381 || raid_disk < 0)
3382 continue;
3383 disk = conf->disks + raid_disk;
3384
3385 disk->rdev = rdev;
3386
b2d444d7 3387 if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
3388 char b[BDEVNAME_SIZE];
3389 printk(KERN_INFO "raid5: device %s operational as raid"
3390 " disk %d\n", bdevname(rdev->bdev,b),
3391 raid_disk);
02c2de8c 3392 working_disks++;
1da177e4
LT
3393 }
3394 }
3395
1da177e4 3396 /*
16a53ecc 3397 * 0 for a fully functional array, 1 or 2 for a degraded array.
1da177e4 3398 */
02c2de8c 3399 mddev->degraded = conf->raid_disks - working_disks;
1da177e4
LT
3400 conf->mddev = mddev;
3401 conf->chunk_size = mddev->chunk_size;
3402 conf->level = mddev->level;
16a53ecc
N
3403 if (conf->level == 6)
3404 conf->max_degraded = 2;
3405 else
3406 conf->max_degraded = 1;
1da177e4
LT
3407 conf->algorithm = mddev->layout;
3408 conf->max_nr_stripes = NR_STRIPES;
f6705578 3409 conf->expand_progress = mddev->reshape_position;
1da177e4
LT
3410
3411 /* device size must be a multiple of chunk size */
3412 mddev->size &= ~(mddev->chunk_size/1024 -1);
b1581566 3413 mddev->resync_max_sectors = mddev->size << 1;
1da177e4 3414
16a53ecc
N
3415 if (conf->level == 6 && conf->raid_disks < 4) {
3416 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3417 mdname(mddev), conf->raid_disks);
3418 goto abort;
3419 }
1da177e4
LT
3420 if (!conf->chunk_size || conf->chunk_size % 4) {
3421 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3422 conf->chunk_size, mdname(mddev));
3423 goto abort;
3424 }
3425 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3426 printk(KERN_ERR
3427 "raid5: unsupported parity algorithm %d for %s\n",
3428 conf->algorithm, mdname(mddev));
3429 goto abort;
3430 }
16a53ecc 3431 if (mddev->degraded > conf->max_degraded) {
1da177e4
LT
3432 printk(KERN_ERR "raid5: not enough operational devices for %s"
3433 " (%d/%d failed)\n",
02c2de8c 3434 mdname(mddev), mddev->degraded, conf->raid_disks);
1da177e4
LT
3435 goto abort;
3436 }
3437
16a53ecc 3438 if (mddev->degraded > 0 &&
1da177e4 3439 mddev->recovery_cp != MaxSector) {
6ff8d8ec
N
3440 if (mddev->ok_start_degraded)
3441 printk(KERN_WARNING
3442 "raid5: starting dirty degraded array: %s"
3443 "- data corruption possible.\n",
3444 mdname(mddev));
3445 else {
3446 printk(KERN_ERR
3447 "raid5: cannot start dirty degraded array for %s\n",
3448 mdname(mddev));
3449 goto abort;
3450 }
1da177e4
LT
3451 }
3452
3453 {
3454 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3455 if (!mddev->thread) {
3456 printk(KERN_ERR
3457 "raid5: couldn't allocate thread for %s\n",
3458 mdname(mddev));
3459 goto abort;
3460 }
3461 }
5036805b 3462 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1da177e4
LT
3463 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3464 if (grow_stripes(conf, conf->max_nr_stripes)) {
3465 printk(KERN_ERR
3466 "raid5: couldn't allocate %dkB for buffers\n", memory);
3467 shrink_stripes(conf);
3468 md_unregister_thread(mddev->thread);
3469 goto abort;
3470 } else
3471 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3472 memory, mdname(mddev));
3473
3474 if (mddev->degraded == 0)
3475 printk("raid5: raid level %d set %s active with %d out of %d"
3476 " devices, algorithm %d\n", conf->level, mdname(mddev),
3477 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3478 conf->algorithm);
3479 else
3480 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3481 " out of %d devices, algorithm %d\n", conf->level,
3482 mdname(mddev), mddev->raid_disks - mddev->degraded,
3483 mddev->raid_disks, conf->algorithm);
3484
3485 print_raid5_conf(conf);
3486
f6705578
N
3487 if (conf->expand_progress != MaxSector) {
3488 printk("...ok start reshape thread\n");
b578d55f 3489 conf->expand_lo = conf->expand_progress;
f6705578
N
3490 atomic_set(&conf->reshape_stripes, 0);
3491 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3492 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3493 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3494 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3495 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3496 "%s_reshape");
f6705578
N
3497 }
3498
1da177e4 3499 /* read-ahead size must cover two whole stripes, which is
16a53ecc 3500 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
1da177e4
LT
3501 */
3502 {
16a53ecc
N
3503 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3504 int stripe = data_disks *
8932c2e0 3505 (mddev->chunk_size / PAGE_SIZE);
1da177e4
LT
3506 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3507 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3508 }
3509
3510 /* Ok, everything is just fine now */
007583c9 3511 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
7a5febe9
N
3512
3513 mddev->queue->unplug_fn = raid5_unplug_device;
3514 mddev->queue->issue_flush_fn = raid5_issue_flush;
f022b2fd
N
3515 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
3516 mddev->queue->backing_dev_info.congested_data = mddev;
3517
16a53ecc
N
3518 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3519 conf->max_degraded);
7a5febe9 3520
23032a0e
RBJ
3521 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
3522
1da177e4
LT
3523 return 0;
3524abort:
3525 if (conf) {
3526 print_raid5_conf(conf);
16a53ecc 3527 safe_put_page(conf->spare_page);
b55e6bfc 3528 kfree(conf->disks);
fccddba0 3529 kfree(conf->stripe_hashtbl);
1da177e4
LT
3530 kfree(conf);
3531 }
3532 mddev->private = NULL;
3533 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3534 return -EIO;
3535}
3536
3537
3538
3f294f4f 3539static int stop(mddev_t *mddev)
1da177e4
LT
3540{
3541 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3542
3543 md_unregister_thread(mddev->thread);
3544 mddev->thread = NULL;
3545 shrink_stripes(conf);
fccddba0 3546 kfree(conf->stripe_hashtbl);
1da177e4 3547 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
007583c9 3548 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
b55e6bfc 3549 kfree(conf->disks);
96de1e66 3550 kfree(conf);
1da177e4
LT
3551 mddev->private = NULL;
3552 return 0;
3553}
3554
3555#if RAID5_DEBUG
16a53ecc 3556static void print_sh (struct seq_file *seq, struct stripe_head *sh)
1da177e4
LT
3557{
3558 int i;
3559
16a53ecc
N
3560 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3561 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3562 seq_printf(seq, "sh %llu, count %d.\n",
3563 (unsigned long long)sh->sector, atomic_read(&sh->count));
3564 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
7ecaa1e6 3565 for (i = 0; i < sh->disks; i++) {
16a53ecc
N
3566 seq_printf(seq, "(cache%d: %p %ld) ",
3567 i, sh->dev[i].page, sh->dev[i].flags);
1da177e4 3568 }
16a53ecc 3569 seq_printf(seq, "\n");
1da177e4
LT
3570}
3571
16a53ecc 3572static void printall (struct seq_file *seq, raid5_conf_t *conf)
1da177e4
LT
3573{
3574 struct stripe_head *sh;
fccddba0 3575 struct hlist_node *hn;
1da177e4
LT
3576 int i;
3577
3578 spin_lock_irq(&conf->device_lock);
3579 for (i = 0; i < NR_HASH; i++) {
fccddba0 3580 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
1da177e4
LT
3581 if (sh->raid_conf != conf)
3582 continue;
16a53ecc 3583 print_sh(seq, sh);
1da177e4
LT
3584 }
3585 }
3586 spin_unlock_irq(&conf->device_lock);
3587}
3588#endif
3589
3590static void status (struct seq_file *seq, mddev_t *mddev)
3591{
3592 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3593 int i;
3594
3595 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
02c2de8c 3596 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
1da177e4
LT
3597 for (i = 0; i < conf->raid_disks; i++)
3598 seq_printf (seq, "%s",
3599 conf->disks[i].rdev &&
b2d444d7 3600 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
1da177e4
LT
3601 seq_printf (seq, "]");
3602#if RAID5_DEBUG
16a53ecc
N
3603 seq_printf (seq, "\n");
3604 printall(seq, conf);
1da177e4
LT
3605#endif
3606}
3607
3608static void print_raid5_conf (raid5_conf_t *conf)
3609{
3610 int i;
3611 struct disk_info *tmp;
3612
3613 printk("RAID5 conf printout:\n");
3614 if (!conf) {
3615 printk("(conf==NULL)\n");
3616 return;
3617 }
02c2de8c
N
3618 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3619 conf->raid_disks - conf->mddev->degraded);
1da177e4
LT
3620
3621 for (i = 0; i < conf->raid_disks; i++) {
3622 char b[BDEVNAME_SIZE];
3623 tmp = conf->disks + i;
3624 if (tmp->rdev)
3625 printk(" disk %d, o:%d, dev:%s\n",
b2d444d7 3626 i, !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
3627 bdevname(tmp->rdev->bdev,b));
3628 }
3629}
3630
3631static int raid5_spare_active(mddev_t *mddev)
3632{
3633 int i;
3634 raid5_conf_t *conf = mddev->private;
3635 struct disk_info *tmp;
3636
3637 for (i = 0; i < conf->raid_disks; i++) {
3638 tmp = conf->disks + i;
3639 if (tmp->rdev
b2d444d7 3640 && !test_bit(Faulty, &tmp->rdev->flags)
c04be0aa
N
3641 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3642 unsigned long flags;
3643 spin_lock_irqsave(&conf->device_lock, flags);
1da177e4 3644 mddev->degraded--;
c04be0aa 3645 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
3646 }
3647 }
3648 print_raid5_conf(conf);
3649 return 0;
3650}
3651
3652static int raid5_remove_disk(mddev_t *mddev, int number)
3653{
3654 raid5_conf_t *conf = mddev->private;
3655 int err = 0;
3656 mdk_rdev_t *rdev;
3657 struct disk_info *p = conf->disks + number;
3658
3659 print_raid5_conf(conf);
3660 rdev = p->rdev;
3661 if (rdev) {
b2d444d7 3662 if (test_bit(In_sync, &rdev->flags) ||
1da177e4
LT
3663 atomic_read(&rdev->nr_pending)) {
3664 err = -EBUSY;
3665 goto abort;
3666 }
3667 p->rdev = NULL;
fbd568a3 3668 synchronize_rcu();
1da177e4
LT
3669 if (atomic_read(&rdev->nr_pending)) {
3670 /* lost the race, try later */
3671 err = -EBUSY;
3672 p->rdev = rdev;
3673 }
3674 }
3675abort:
3676
3677 print_raid5_conf(conf);
3678 return err;
3679}
3680
3681static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3682{
3683 raid5_conf_t *conf = mddev->private;
3684 int found = 0;
3685 int disk;
3686 struct disk_info *p;
3687
16a53ecc 3688 if (mddev->degraded > conf->max_degraded)
1da177e4
LT
3689 /* no point adding a device */
3690 return 0;
3691
3692 /*
16a53ecc
N
3693 * find the disk ... but prefer rdev->saved_raid_disk
3694 * if possible.
1da177e4 3695 */
16a53ecc
N
3696 if (rdev->saved_raid_disk >= 0 &&
3697 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3698 disk = rdev->saved_raid_disk;
3699 else
3700 disk = 0;
3701 for ( ; disk < conf->raid_disks; disk++)
1da177e4 3702 if ((p=conf->disks + disk)->rdev == NULL) {
b2d444d7 3703 clear_bit(In_sync, &rdev->flags);
1da177e4
LT
3704 rdev->raid_disk = disk;
3705 found = 1;
72626685
N
3706 if (rdev->saved_raid_disk != disk)
3707 conf->fullsync = 1;
d6065f7b 3708 rcu_assign_pointer(p->rdev, rdev);
1da177e4
LT
3709 break;
3710 }
3711 print_raid5_conf(conf);
3712 return found;
3713}
3714
3715static int raid5_resize(mddev_t *mddev, sector_t sectors)
3716{
3717 /* no resync is happening, and there is enough space
3718 * on all devices, so we can resize.
3719 * We need to make sure resync covers any new space.
3720 * If the array is shrinking we should possibly wait until
3721 * any io in the removed space completes, but it hardly seems
3722 * worth it.
3723 */
16a53ecc
N
3724 raid5_conf_t *conf = mddev_to_conf(mddev);
3725
1da177e4 3726 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
16a53ecc 3727 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
1da177e4
LT
3728 set_capacity(mddev->gendisk, mddev->array_size << 1);
3729 mddev->changed = 1;
3730 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3731 mddev->recovery_cp = mddev->size << 1;
3732 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3733 }
3734 mddev->size = sectors /2;
4b5c7ae8 3735 mddev->resync_max_sectors = sectors;
1da177e4
LT
3736 return 0;
3737}
3738
29269553 3739#ifdef CONFIG_MD_RAID5_RESHAPE
63c70c4f 3740static int raid5_check_reshape(mddev_t *mddev)
29269553
N
3741{
3742 raid5_conf_t *conf = mddev_to_conf(mddev);
3743 int err;
29269553 3744
63c70c4f
N
3745 if (mddev->delta_disks < 0 ||
3746 mddev->new_level != mddev->level)
3747 return -EINVAL; /* Cannot shrink array or change level yet */
3748 if (mddev->delta_disks == 0)
29269553
N
3749 return 0; /* nothing to do */
3750
3751 /* Can only proceed if there are plenty of stripe_heads.
3752 * We need a minimum of one full stripe,, and for sensible progress
3753 * it is best to have about 4 times that.
3754 * If we require 4 times, then the default 256 4K stripe_heads will
3755 * allow for chunk sizes up to 256K, which is probably OK.
3756 * If the chunk size is greater, user-space should request more
3757 * stripe_heads first.
3758 */
63c70c4f
N
3759 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3760 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
29269553
N
3761 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3762 (mddev->chunk_size / STRIPE_SIZE)*4);
3763 return -ENOSPC;
3764 }
3765
63c70c4f
N
3766 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3767 if (err)
3768 return err;
3769
3770 /* looks like we might be able to manage this */
3771 return 0;
3772}
3773
3774static int raid5_start_reshape(mddev_t *mddev)
3775{
3776 raid5_conf_t *conf = mddev_to_conf(mddev);
3777 mdk_rdev_t *rdev;
3778 struct list_head *rtmp;
3779 int spares = 0;
3780 int added_devices = 0;
c04be0aa 3781 unsigned long flags;
63c70c4f
N
3782
3783 if (mddev->degraded ||
3784 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3785 return -EBUSY;
3786
29269553
N
3787 ITERATE_RDEV(mddev, rdev, rtmp)
3788 if (rdev->raid_disk < 0 &&
3789 !test_bit(Faulty, &rdev->flags))
3790 spares++;
63c70c4f
N
3791
3792 if (spares < mddev->delta_disks-1)
29269553
N
3793 /* Not enough devices even to make a degraded array
3794 * of that size
3795 */
3796 return -EINVAL;
3797
f6705578 3798 atomic_set(&conf->reshape_stripes, 0);
29269553
N
3799 spin_lock_irq(&conf->device_lock);
3800 conf->previous_raid_disks = conf->raid_disks;
63c70c4f 3801 conf->raid_disks += mddev->delta_disks;
29269553 3802 conf->expand_progress = 0;
b578d55f 3803 conf->expand_lo = 0;
29269553
N
3804 spin_unlock_irq(&conf->device_lock);
3805
3806 /* Add some new drives, as many as will fit.
3807 * We know there are enough to make the newly sized array work.
3808 */
3809 ITERATE_RDEV(mddev, rdev, rtmp)
3810 if (rdev->raid_disk < 0 &&
3811 !test_bit(Faulty, &rdev->flags)) {
3812 if (raid5_add_disk(mddev, rdev)) {
3813 char nm[20];
3814 set_bit(In_sync, &rdev->flags);
29269553 3815 added_devices++;
5fd6c1dc 3816 rdev->recovery_offset = 0;
29269553
N
3817 sprintf(nm, "rd%d", rdev->raid_disk);
3818 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3819 } else
3820 break;
3821 }
3822
c04be0aa 3823 spin_lock_irqsave(&conf->device_lock, flags);
63c70c4f 3824 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
c04be0aa 3825 spin_unlock_irqrestore(&conf->device_lock, flags);
63c70c4f 3826 mddev->raid_disks = conf->raid_disks;
f6705578 3827 mddev->reshape_position = 0;
850b2b42 3828 set_bit(MD_CHANGE_DEVS, &mddev->flags);
f6705578 3829
29269553
N
3830 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3831 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3832 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3833 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3834 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3835 "%s_reshape");
3836 if (!mddev->sync_thread) {
3837 mddev->recovery = 0;
3838 spin_lock_irq(&conf->device_lock);
3839 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3840 conf->expand_progress = MaxSector;
3841 spin_unlock_irq(&conf->device_lock);
3842 return -EAGAIN;
3843 }
3844 md_wakeup_thread(mddev->sync_thread);
3845 md_new_event(mddev);
3846 return 0;
3847}
3848#endif
3849
3850static void end_reshape(raid5_conf_t *conf)
3851{
3852 struct block_device *bdev;
3853
f6705578
N
3854 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3855 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3856 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3857 conf->mddev->changed = 1;
3858
3859 bdev = bdget_disk(conf->mddev->gendisk, 0);
3860 if (bdev) {
3861 mutex_lock(&bdev->bd_inode->i_mutex);
0692c6b1 3862 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
f6705578
N
3863 mutex_unlock(&bdev->bd_inode->i_mutex);
3864 bdput(bdev);
3865 }
3866 spin_lock_irq(&conf->device_lock);
3867 conf->expand_progress = MaxSector;
3868 spin_unlock_irq(&conf->device_lock);
3869 conf->mddev->reshape_position = MaxSector;
16a53ecc
N
3870
3871 /* read-ahead size must cover two whole stripes, which is
3872 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3873 */
3874 {
3875 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3876 int stripe = data_disks *
3877 (conf->mddev->chunk_size / PAGE_SIZE);
3878 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3879 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3880 }
29269553 3881 }
29269553
N
3882}
3883
72626685
N
3884static void raid5_quiesce(mddev_t *mddev, int state)
3885{
3886 raid5_conf_t *conf = mddev_to_conf(mddev);
3887
3888 switch(state) {
e464eafd
N
3889 case 2: /* resume for a suspend */
3890 wake_up(&conf->wait_for_overlap);
3891 break;
3892
72626685
N
3893 case 1: /* stop all writes */
3894 spin_lock_irq(&conf->device_lock);
3895 conf->quiesce = 1;
3896 wait_event_lock_irq(conf->wait_for_stripe,
46031f9a
RBJ
3897 atomic_read(&conf->active_stripes) == 0 &&
3898 atomic_read(&conf->active_aligned_reads) == 0,
72626685
N
3899 conf->device_lock, /* nothing */);
3900 spin_unlock_irq(&conf->device_lock);
3901 break;
3902
3903 case 0: /* re-enable writes */
3904 spin_lock_irq(&conf->device_lock);
3905 conf->quiesce = 0;
3906 wake_up(&conf->wait_for_stripe);
e464eafd 3907 wake_up(&conf->wait_for_overlap);
72626685
N
3908 spin_unlock_irq(&conf->device_lock);
3909 break;
3910 }
72626685 3911}
b15c2e57 3912
16a53ecc
N
3913static struct mdk_personality raid6_personality =
3914{
3915 .name = "raid6",
3916 .level = 6,
3917 .owner = THIS_MODULE,
3918 .make_request = make_request,
3919 .run = run,
3920 .stop = stop,
3921 .status = status,
3922 .error_handler = error,
3923 .hot_add_disk = raid5_add_disk,
3924 .hot_remove_disk= raid5_remove_disk,
3925 .spare_active = raid5_spare_active,
3926 .sync_request = sync_request,
3927 .resize = raid5_resize,
3928 .quiesce = raid5_quiesce,
3929};
2604b703 3930static struct mdk_personality raid5_personality =
1da177e4
LT
3931{
3932 .name = "raid5",
2604b703 3933 .level = 5,
1da177e4
LT
3934 .owner = THIS_MODULE,
3935 .make_request = make_request,
3936 .run = run,
3937 .stop = stop,
3938 .status = status,
3939 .error_handler = error,
3940 .hot_add_disk = raid5_add_disk,
3941 .hot_remove_disk= raid5_remove_disk,
3942 .spare_active = raid5_spare_active,
3943 .sync_request = sync_request,
3944 .resize = raid5_resize,
29269553 3945#ifdef CONFIG_MD_RAID5_RESHAPE
63c70c4f
N
3946 .check_reshape = raid5_check_reshape,
3947 .start_reshape = raid5_start_reshape,
29269553 3948#endif
72626685 3949 .quiesce = raid5_quiesce,
1da177e4
LT
3950};
3951
2604b703 3952static struct mdk_personality raid4_personality =
1da177e4 3953{
2604b703
N
3954 .name = "raid4",
3955 .level = 4,
3956 .owner = THIS_MODULE,
3957 .make_request = make_request,
3958 .run = run,
3959 .stop = stop,
3960 .status = status,
3961 .error_handler = error,
3962 .hot_add_disk = raid5_add_disk,
3963 .hot_remove_disk= raid5_remove_disk,
3964 .spare_active = raid5_spare_active,
3965 .sync_request = sync_request,
3966 .resize = raid5_resize,
3967 .quiesce = raid5_quiesce,
3968};
3969
3970static int __init raid5_init(void)
3971{
16a53ecc
N
3972 int e;
3973
3974 e = raid6_select_algo();
3975 if ( e )
3976 return e;
3977 register_md_personality(&raid6_personality);
2604b703
N
3978 register_md_personality(&raid5_personality);
3979 register_md_personality(&raid4_personality);
3980 return 0;
1da177e4
LT
3981}
3982
2604b703 3983static void raid5_exit(void)
1da177e4 3984{
16a53ecc 3985 unregister_md_personality(&raid6_personality);
2604b703
N
3986 unregister_md_personality(&raid5_personality);
3987 unregister_md_personality(&raid4_personality);
1da177e4
LT
3988}
3989
3990module_init(raid5_init);
3991module_exit(raid5_exit);
3992MODULE_LICENSE("GPL");
3993MODULE_ALIAS("md-personality-4"); /* RAID5 */
d9d166c2
N
3994MODULE_ALIAS("md-raid5");
3995MODULE_ALIAS("md-raid4");
2604b703
N
3996MODULE_ALIAS("md-level-5");
3997MODULE_ALIAS("md-level-4");
16a53ecc
N
3998MODULE_ALIAS("md-personality-8"); /* RAID6 */
3999MODULE_ALIAS("md-raid6");
4000MODULE_ALIAS("md-level-6");
4001
4002/* This used to be two separate modules, they were: */
4003MODULE_ALIAS("raid5");
4004MODULE_ALIAS("raid6");