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