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