]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/md/raid6main.c
[PATCH] md: fix up some rdev rcu locking in raid5/6
[net-next-2.6.git] / drivers / md / raid6main.c
CommitLineData
1da177e4
LT
1/*
2 * raid6main.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
5 * Copyright (C) 2002, 2003 H. Peter Anvin
6 *
7 * RAID-6 management functions. This code is derived from raid5.c.
8 * Last merge from raid5.c bkcvs version 1.79 (kernel 2.6.1).
9 *
10 * Thanks to Penguin Computing for making the RAID-6 development possible
11 * by donating a test server!
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * You should have received a copy of the GNU General Public License
19 * (for example /usr/src/linux/COPYING); if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24#include <linux/config.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/highmem.h>
28#include <linux/bitops.h>
29#include <asm/atomic.h>
30#include "raid6.h"
31
934ce7c8
N
32#include <linux/raid/bitmap.h>
33
1da177e4
LT
34/*
35 * Stripe cache
36 */
37
38#define NR_STRIPES 256
39#define STRIPE_SIZE PAGE_SIZE
40#define STRIPE_SHIFT (PAGE_SHIFT - 9)
41#define STRIPE_SECTORS (STRIPE_SIZE>>9)
42#define IO_THRESHOLD 1
43#define HASH_PAGES 1
44#define HASH_PAGES_ORDER 0
45#define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
46#define HASH_MASK (NR_HASH - 1)
47
48#define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
49
50/* bio's attached to a stripe+device for I/O are linked together in bi_sector
51 * order without overlap. There may be several bio's per stripe+device, and
52 * a bio could span several devices.
53 * When walking this list for a particular stripe+device, we must never proceed
54 * beyond a bio that extends past this device, as the next bio might no longer
55 * be valid.
56 * This macro is used to determine the 'next' bio in the list, given the sector
57 * of the current stripe+device
58 */
59#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
60/*
61 * The following can be used to debug the driver
62 */
63#define RAID6_DEBUG 0 /* Extremely verbose printk */
64#define RAID6_PARANOIA 1 /* Check spinlocks */
65#define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
66#if RAID6_PARANOIA && defined(CONFIG_SMP)
67# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
68#else
69# define CHECK_DEVLOCK()
70#endif
71
72#define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
73#if RAID6_DEBUG
74#undef inline
75#undef __inline__
76#define inline
77#define __inline__
78#endif
79
80#if !RAID6_USE_EMPTY_ZERO_PAGE
81/* In .bss so it's zeroed */
82const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
83#endif
84
85static inline int raid6_next_disk(int disk, int raid_disks)
86{
87 disk++;
88 return (disk < raid_disks) ? disk : 0;
89}
90
91static void print_raid6_conf (raid6_conf_t *conf);
92
93static inline void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
94{
95 if (atomic_dec_and_test(&sh->count)) {
96 if (!list_empty(&sh->lru))
97 BUG();
98 if (atomic_read(&conf->active_stripes)==0)
99 BUG();
100 if (test_bit(STRIPE_HANDLE, &sh->state)) {
101 if (test_bit(STRIPE_DELAYED, &sh->state))
102 list_add_tail(&sh->lru, &conf->delayed_list);
934ce7c8
N
103 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
104 conf->seq_write == sh->bm_seq)
105 list_add_tail(&sh->lru, &conf->bitmap_list);
106 else {
107 clear_bit(STRIPE_BIT_DELAY, &sh->state);
1da177e4 108 list_add_tail(&sh->lru, &conf->handle_list);
934ce7c8 109 }
1da177e4
LT
110 md_wakeup_thread(conf->mddev->thread);
111 } else {
112 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
113 atomic_dec(&conf->preread_active_stripes);
114 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
115 md_wakeup_thread(conf->mddev->thread);
116 }
117 list_add_tail(&sh->lru, &conf->inactive_list);
118 atomic_dec(&conf->active_stripes);
119 if (!conf->inactive_blocked ||
120 atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
121 wake_up(&conf->wait_for_stripe);
122 }
123 }
124}
125static void release_stripe(struct stripe_head *sh)
126{
127 raid6_conf_t *conf = sh->raid_conf;
128 unsigned long flags;
129
130 spin_lock_irqsave(&conf->device_lock, flags);
131 __release_stripe(conf, sh);
132 spin_unlock_irqrestore(&conf->device_lock, flags);
133}
134
135static void remove_hash(struct stripe_head *sh)
136{
137 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
138
139 if (sh->hash_pprev) {
140 if (sh->hash_next)
141 sh->hash_next->hash_pprev = sh->hash_pprev;
142 *sh->hash_pprev = sh->hash_next;
143 sh->hash_pprev = NULL;
144 }
145}
146
147static __inline__ void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
148{
149 struct stripe_head **shp = &stripe_hash(conf, sh->sector);
150
151 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
152
153 CHECK_DEVLOCK();
154 if ((sh->hash_next = *shp) != NULL)
155 (*shp)->hash_pprev = &sh->hash_next;
156 *shp = sh;
157 sh->hash_pprev = shp;
158}
159
160
161/* find an idle stripe, make sure it is unhashed, and return it. */
162static struct stripe_head *get_free_stripe(raid6_conf_t *conf)
163{
164 struct stripe_head *sh = NULL;
165 struct list_head *first;
166
167 CHECK_DEVLOCK();
168 if (list_empty(&conf->inactive_list))
169 goto out;
170 first = conf->inactive_list.next;
171 sh = list_entry(first, struct stripe_head, lru);
172 list_del_init(first);
173 remove_hash(sh);
174 atomic_inc(&conf->active_stripes);
175out:
176 return sh;
177}
178
179static void shrink_buffers(struct stripe_head *sh, int num)
180{
181 struct page *p;
182 int i;
183
184 for (i=0; i<num ; i++) {
185 p = sh->dev[i].page;
186 if (!p)
187 continue;
188 sh->dev[i].page = NULL;
189 page_cache_release(p);
190 }
191}
192
193static int grow_buffers(struct stripe_head *sh, int num)
194{
195 int i;
196
197 for (i=0; i<num; i++) {
198 struct page *page;
199
200 if (!(page = alloc_page(GFP_KERNEL))) {
201 return 1;
202 }
203 sh->dev[i].page = page;
204 }
205 return 0;
206}
207
208static void raid6_build_block (struct stripe_head *sh, int i);
209
210static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
211{
212 raid6_conf_t *conf = sh->raid_conf;
213 int disks = conf->raid_disks, i;
214
215 if (atomic_read(&sh->count) != 0)
216 BUG();
217 if (test_bit(STRIPE_HANDLE, &sh->state))
218 BUG();
219
220 CHECK_DEVLOCK();
221 PRINTK("init_stripe called, stripe %llu\n",
222 (unsigned long long)sh->sector);
223
224 remove_hash(sh);
225
226 sh->sector = sector;
227 sh->pd_idx = pd_idx;
228 sh->state = 0;
229
230 for (i=disks; i--; ) {
231 struct r5dev *dev = &sh->dev[i];
232
233 if (dev->toread || dev->towrite || dev->written ||
234 test_bit(R5_LOCKED, &dev->flags)) {
235 PRINTK("sector=%llx i=%d %p %p %p %d\n",
236 (unsigned long long)sh->sector, i, dev->toread,
237 dev->towrite, dev->written,
238 test_bit(R5_LOCKED, &dev->flags));
239 BUG();
240 }
241 dev->flags = 0;
242 raid6_build_block(sh, i);
243 }
244 insert_hash(conf, sh);
245}
246
247static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
248{
249 struct stripe_head *sh;
250
251 CHECK_DEVLOCK();
252 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
253 for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
254 if (sh->sector == sector)
255 return sh;
256 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
257 return NULL;
258}
259
260static void unplug_slaves(mddev_t *mddev);
261
262static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector,
263 int pd_idx, int noblock)
264{
265 struct stripe_head *sh;
266
267 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
268
269 spin_lock_irq(&conf->device_lock);
270
271 do {
934ce7c8
N
272 wait_event_lock_irq(conf->wait_for_stripe,
273 conf->quiesce == 0,
274 conf->device_lock, /* nothing */);
1da177e4
LT
275 sh = __find_stripe(conf, sector);
276 if (!sh) {
277 if (!conf->inactive_blocked)
278 sh = get_free_stripe(conf);
279 if (noblock && sh == NULL)
280 break;
281 if (!sh) {
282 conf->inactive_blocked = 1;
283 wait_event_lock_irq(conf->wait_for_stripe,
284 !list_empty(&conf->inactive_list) &&
285 (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
286 || !conf->inactive_blocked),
287 conf->device_lock,
288 unplug_slaves(conf->mddev);
289 );
290 conf->inactive_blocked = 0;
291 } else
292 init_stripe(sh, sector, pd_idx);
293 } else {
294 if (atomic_read(&sh->count)) {
295 if (!list_empty(&sh->lru))
296 BUG();
297 } else {
298 if (!test_bit(STRIPE_HANDLE, &sh->state))
299 atomic_inc(&conf->active_stripes);
300 if (list_empty(&sh->lru))
301 BUG();
302 list_del_init(&sh->lru);
303 }
304 }
305 } while (sh == NULL);
306
307 if (sh)
308 atomic_inc(&sh->count);
309
310 spin_unlock_irq(&conf->device_lock);
311 return sh;
312}
313
314static int grow_stripes(raid6_conf_t *conf, int num)
315{
316 struct stripe_head *sh;
317 kmem_cache_t *sc;
318 int devs = conf->raid_disks;
319
320 sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
321
322 sc = kmem_cache_create(conf->cache_name,
323 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
324 0, 0, NULL, NULL);
325 if (!sc)
326 return 1;
327 conf->slab_cache = sc;
328 while (num--) {
329 sh = kmem_cache_alloc(sc, GFP_KERNEL);
330 if (!sh)
331 return 1;
332 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
333 sh->raid_conf = conf;
334 spin_lock_init(&sh->lock);
335
336 if (grow_buffers(sh, conf->raid_disks)) {
337 shrink_buffers(sh, conf->raid_disks);
338 kmem_cache_free(sc, sh);
339 return 1;
340 }
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 }
347 return 0;
348}
349
350static void shrink_stripes(raid6_conf_t *conf)
351{
352 struct stripe_head *sh;
353
354 while (1) {
355 spin_lock_irq(&conf->device_lock);
356 sh = get_free_stripe(conf);
357 spin_unlock_irq(&conf->device_lock);
358 if (!sh)
359 break;
360 if (atomic_read(&sh->count))
361 BUG();
362 shrink_buffers(sh, conf->raid_disks);
363 kmem_cache_free(conf->slab_cache, sh);
364 atomic_dec(&conf->active_stripes);
365 }
366 kmem_cache_destroy(conf->slab_cache);
367 conf->slab_cache = NULL;
368}
369
d69762e9
N
370static int raid6_end_read_request(struct bio * bi, unsigned int bytes_done,
371 int error)
1da177e4
LT
372{
373 struct stripe_head *sh = bi->bi_private;
374 raid6_conf_t *conf = sh->raid_conf;
375 int disks = conf->raid_disks, i;
376 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
377
378 if (bi->bi_size)
379 return 1;
380
381 for (i=0 ; i<disks; i++)
382 if (bi == &sh->dev[i].req)
383 break;
384
385 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
386 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
387 uptodate);
388 if (i == disks) {
389 BUG();
390 return 0;
391 }
392
393 if (uptodate) {
394#if 0
395 struct bio *bio;
396 unsigned long flags;
397 spin_lock_irqsave(&conf->device_lock, flags);
398 /* we can return a buffer if we bypassed the cache or
399 * if the top buffer is not in highmem. If there are
400 * multiple buffers, leave the extra work to
401 * handle_stripe
402 */
403 buffer = sh->bh_read[i];
404 if (buffer &&
405 (!PageHighMem(buffer->b_page)
406 || buffer->b_page == bh->b_page )
407 ) {
408 sh->bh_read[i] = buffer->b_reqnext;
409 buffer->b_reqnext = NULL;
410 } else
411 buffer = NULL;
412 spin_unlock_irqrestore(&conf->device_lock, flags);
413 if (sh->bh_page[i]==bh->b_page)
414 set_buffer_uptodate(bh);
415 if (buffer) {
416 if (buffer->b_page != bh->b_page)
417 memcpy(buffer->b_data, bh->b_data, bh->b_size);
418 buffer->b_end_io(buffer, 1);
419 }
420#else
421 set_bit(R5_UPTODATE, &sh->dev[i].flags);
422#endif
d69762e9
N
423 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
424 printk(KERN_INFO "raid6: read error corrected!!\n");
425 clear_bit(R5_ReadError, &sh->dev[i].flags);
426 clear_bit(R5_ReWrite, &sh->dev[i].flags);
427 }
428 if (atomic_read(&conf->disks[i].rdev->read_errors))
429 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1da177e4 430 } else {
d69762e9 431 int retry = 0;
1da177e4 432 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
d69762e9
N
433 atomic_inc(&conf->disks[i].rdev->read_errors);
434 if (conf->mddev->degraded)
435 printk(KERN_WARNING "raid6: read error not correctable.\n");
436 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
437 /* Oh, no!!! */
438 printk(KERN_WARNING "raid6: read error NOT corrected!!\n");
439 else if (atomic_read(&conf->disks[i].rdev->read_errors)
440 > conf->max_nr_stripes)
441 printk(KERN_WARNING
442 "raid6: Too many read errors, failing device.\n");
443 else
444 retry = 1;
445 if (retry)
446 set_bit(R5_ReadError, &sh->dev[i].flags);
447 else {
448 clear_bit(R5_ReadError, &sh->dev[i].flags);
449 clear_bit(R5_ReWrite, &sh->dev[i].flags);
450 md_error(conf->mddev, conf->disks[i].rdev);
451 }
1da177e4
LT
452 }
453 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
454#if 0
455 /* must restore b_page before unlocking buffer... */
456 if (sh->bh_page[i] != bh->b_page) {
457 bh->b_page = sh->bh_page[i];
458 bh->b_data = page_address(bh->b_page);
459 clear_buffer_uptodate(bh);
460 }
461#endif
462 clear_bit(R5_LOCKED, &sh->dev[i].flags);
463 set_bit(STRIPE_HANDLE, &sh->state);
464 release_stripe(sh);
465 return 0;
466}
467
468static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
469 int error)
470{
471 struct stripe_head *sh = bi->bi_private;
472 raid6_conf_t *conf = sh->raid_conf;
473 int disks = conf->raid_disks, i;
474 unsigned long flags;
475 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
476
477 if (bi->bi_size)
478 return 1;
479
480 for (i=0 ; i<disks; i++)
481 if (bi == &sh->dev[i].req)
482 break;
483
484 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
485 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
486 uptodate);
487 if (i == disks) {
488 BUG();
489 return 0;
490 }
491
492 spin_lock_irqsave(&conf->device_lock, flags);
493 if (!uptodate)
494 md_error(conf->mddev, conf->disks[i].rdev);
495
496 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
497
498 clear_bit(R5_LOCKED, &sh->dev[i].flags);
499 set_bit(STRIPE_HANDLE, &sh->state);
500 __release_stripe(conf, sh);
501 spin_unlock_irqrestore(&conf->device_lock, flags);
502 return 0;
503}
504
505
506static sector_t compute_blocknr(struct stripe_head *sh, int i);
507
508static void raid6_build_block (struct stripe_head *sh, int i)
509{
510 struct r5dev *dev = &sh->dev[i];
511 int pd_idx = sh->pd_idx;
512 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
513
514 bio_init(&dev->req);
515 dev->req.bi_io_vec = &dev->vec;
516 dev->req.bi_vcnt++;
517 dev->req.bi_max_vecs++;
518 dev->vec.bv_page = dev->page;
519 dev->vec.bv_len = STRIPE_SIZE;
520 dev->vec.bv_offset = 0;
521
522 dev->req.bi_sector = sh->sector;
523 dev->req.bi_private = sh;
524
525 dev->flags = 0;
526 if (i != pd_idx && i != qd_idx)
527 dev->sector = compute_blocknr(sh, i);
528}
529
530static void error(mddev_t *mddev, mdk_rdev_t *rdev)
531{
532 char b[BDEVNAME_SIZE];
533 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
534 PRINTK("raid6: error called\n");
535
b2d444d7 536 if (!test_bit(Faulty, &rdev->flags)) {
1da177e4 537 mddev->sb_dirty = 1;
b2d444d7 538 if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
539 conf->working_disks--;
540 mddev->degraded++;
541 conf->failed_disks++;
b2d444d7 542 clear_bit(In_sync, &rdev->flags);
1da177e4
LT
543 /*
544 * if recovery was running, make sure it aborts.
545 */
546 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
547 }
b2d444d7 548 set_bit(Faulty, &rdev->flags);
1da177e4
LT
549 printk (KERN_ALERT
550 "raid6: Disk failure on %s, disabling device."
551 " Operation continuing on %d devices\n",
552 bdevname(rdev->bdev,b), conf->working_disks);
553 }
554}
555
556/*
557 * Input: a 'big' sector number,
558 * Output: index of the data and parity disk, and the sector # in them.
559 */
560static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
561 unsigned int data_disks, unsigned int * dd_idx,
562 unsigned int * pd_idx, raid6_conf_t *conf)
563{
564 long stripe;
565 unsigned long chunk_number;
566 unsigned int chunk_offset;
567 sector_t new_sector;
568 int sectors_per_chunk = conf->chunk_size >> 9;
569
570 /* First compute the information on this sector */
571
572 /*
573 * Compute the chunk number and the sector offset inside the chunk
574 */
575 chunk_offset = sector_div(r_sector, sectors_per_chunk);
576 chunk_number = r_sector;
577 if ( r_sector != chunk_number ) {
578 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
579 (unsigned long long)r_sector, (unsigned long)chunk_number);
580 BUG();
581 }
582
583 /*
584 * Compute the stripe number
585 */
586 stripe = chunk_number / data_disks;
587
588 /*
589 * Compute the data disk and parity disk indexes inside the stripe
590 */
591 *dd_idx = chunk_number % data_disks;
592
593 /*
594 * Select the parity disk based on the user selected algorithm.
595 */
596
597 /**** FIX THIS ****/
598 switch (conf->algorithm) {
599 case ALGORITHM_LEFT_ASYMMETRIC:
600 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
601 if (*pd_idx == raid_disks-1)
602 (*dd_idx)++; /* Q D D D P */
603 else if (*dd_idx >= *pd_idx)
604 (*dd_idx) += 2; /* D D P Q D */
605 break;
606 case ALGORITHM_RIGHT_ASYMMETRIC:
607 *pd_idx = stripe % raid_disks;
608 if (*pd_idx == raid_disks-1)
609 (*dd_idx)++; /* Q D D D P */
610 else if (*dd_idx >= *pd_idx)
611 (*dd_idx) += 2; /* D D P Q D */
612 break;
613 case ALGORITHM_LEFT_SYMMETRIC:
614 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
615 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
616 break;
617 case ALGORITHM_RIGHT_SYMMETRIC:
618 *pd_idx = stripe % raid_disks;
619 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
620 break;
621 default:
622 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
623 conf->algorithm);
624 }
625
626 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
627 chunk_number, *pd_idx, *dd_idx);
628
629 /*
630 * Finally, compute the new sector number
631 */
632 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
633 return new_sector;
634}
635
636
637static sector_t compute_blocknr(struct stripe_head *sh, int i)
638{
639 raid6_conf_t *conf = sh->raid_conf;
640 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
641 sector_t new_sector = sh->sector, check;
642 int sectors_per_chunk = conf->chunk_size >> 9;
643 sector_t stripe;
644 int chunk_offset;
645 int chunk_number, dummy1, dummy2, dd_idx = i;
646 sector_t r_sector;
647 int i0 = i;
648
649 chunk_offset = sector_div(new_sector, sectors_per_chunk);
650 stripe = new_sector;
651 if ( new_sector != stripe ) {
652 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
653 (unsigned long long)new_sector, (unsigned long)stripe);
654 BUG();
655 }
656
657 switch (conf->algorithm) {
658 case ALGORITHM_LEFT_ASYMMETRIC:
659 case ALGORITHM_RIGHT_ASYMMETRIC:
660 if (sh->pd_idx == raid_disks-1)
661 i--; /* Q D D D P */
662 else if (i > sh->pd_idx)
663 i -= 2; /* D D P Q D */
664 break;
665 case ALGORITHM_LEFT_SYMMETRIC:
666 case ALGORITHM_RIGHT_SYMMETRIC:
667 if (sh->pd_idx == raid_disks-1)
668 i--; /* Q D D D P */
669 else {
670 /* D D P Q D */
671 if (i < sh->pd_idx)
672 i += raid_disks;
673 i -= (sh->pd_idx + 2);
674 }
675 break;
676 default:
677 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
678 conf->algorithm);
679 }
680
681 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
682
683 chunk_number = stripe * data_disks + i;
684 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
685
686 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
687 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
688 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
689 return 0;
690 }
691 return r_sector;
692}
693
694
695
696/*
697 * Copy data between a page in the stripe cache, and one or more bion
698 * The page could align with the middle of the bio, or there could be
699 * several bion, each with several bio_vecs, which cover part of the page
700 * Multiple bion are linked together on bi_next. There may be extras
701 * at the end of this list. We ignore them.
702 */
703static void copy_data(int frombio, struct bio *bio,
704 struct page *page,
705 sector_t sector)
706{
707 char *pa = page_address(page);
708 struct bio_vec *bvl;
709 int i;
710 int page_offset;
711
712 if (bio->bi_sector >= sector)
713 page_offset = (signed)(bio->bi_sector - sector) * 512;
714 else
715 page_offset = (signed)(sector - bio->bi_sector) * -512;
716 bio_for_each_segment(bvl, bio, i) {
717 int len = bio_iovec_idx(bio,i)->bv_len;
718 int clen;
719 int b_offset = 0;
720
721 if (page_offset < 0) {
722 b_offset = -page_offset;
723 page_offset += b_offset;
724 len -= b_offset;
725 }
726
727 if (len > 0 && page_offset + len > STRIPE_SIZE)
728 clen = STRIPE_SIZE - page_offset;
729 else clen = len;
730
731 if (clen > 0) {
732 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
733 if (frombio)
734 memcpy(pa+page_offset, ba+b_offset, clen);
735 else
736 memcpy(ba+b_offset, pa+page_offset, clen);
737 __bio_kunmap_atomic(ba, KM_USER0);
738 }
739 if (clen < len) /* hit end of page */
740 break;
741 page_offset += len;
742 }
743}
744
745#define check_xor() do { \
746 if (count == MAX_XOR_BLOCKS) { \
747 xor_block(count, STRIPE_SIZE, ptr); \
748 count = 1; \
749 } \
750 } while(0)
751
752/* Compute P and Q syndromes */
753static void compute_parity(struct stripe_head *sh, int method)
754{
755 raid6_conf_t *conf = sh->raid_conf;
756 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
757 struct bio *chosen;
758 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
759 void *ptrs[disks];
760
761 qd_idx = raid6_next_disk(pd_idx, disks);
762 d0_idx = raid6_next_disk(qd_idx, disks);
763
764 PRINTK("compute_parity, stripe %llu, method %d\n",
765 (unsigned long long)sh->sector, method);
766
767 switch(method) {
768 case READ_MODIFY_WRITE:
769 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
770 case RECONSTRUCT_WRITE:
771 for (i= disks; i-- ;)
772 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
773 chosen = sh->dev[i].towrite;
774 sh->dev[i].towrite = NULL;
775
776 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
777 wake_up(&conf->wait_for_overlap);
778
779 if (sh->dev[i].written) BUG();
780 sh->dev[i].written = chosen;
781 }
782 break;
783 case CHECK_PARITY:
784 BUG(); /* Not implemented yet */
785 }
786
787 for (i = disks; i--;)
788 if (sh->dev[i].written) {
789 sector_t sector = sh->dev[i].sector;
790 struct bio *wbi = sh->dev[i].written;
791 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
792 copy_data(1, wbi, sh->dev[i].page, sector);
793 wbi = r5_next_bio(wbi, sector);
794 }
795
796 set_bit(R5_LOCKED, &sh->dev[i].flags);
797 set_bit(R5_UPTODATE, &sh->dev[i].flags);
798 }
799
800// switch(method) {
801// case RECONSTRUCT_WRITE:
802// case CHECK_PARITY:
803// case UPDATE_PARITY:
804 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
805 /* FIX: Is this ordering of drives even remotely optimal? */
806 count = 0;
807 i = d0_idx;
808 do {
809 ptrs[count++] = page_address(sh->dev[i].page);
810 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
811 printk("block %d/%d not uptodate on parity calc\n", i,count);
812 i = raid6_next_disk(i, disks);
813 } while ( i != d0_idx );
814// break;
815// }
816
817 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
818
819 switch(method) {
820 case RECONSTRUCT_WRITE:
821 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
822 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
823 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
824 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
825 break;
826 case UPDATE_PARITY:
827 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
828 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
829 break;
830 }
831}
832
833/* Compute one missing block */
ca65b73b 834static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1da177e4
LT
835{
836 raid6_conf_t *conf = sh->raid_conf;
837 int i, count, disks = conf->raid_disks;
838 void *ptr[MAX_XOR_BLOCKS], *p;
839 int pd_idx = sh->pd_idx;
840 int qd_idx = raid6_next_disk(pd_idx, disks);
841
842 PRINTK("compute_block_1, stripe %llu, idx %d\n",
843 (unsigned long long)sh->sector, dd_idx);
844
845 if ( dd_idx == qd_idx ) {
846 /* We're actually computing the Q drive */
847 compute_parity(sh, UPDATE_PARITY);
848 } else {
849 ptr[0] = page_address(sh->dev[dd_idx].page);
ca65b73b 850 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1da177e4
LT
851 count = 1;
852 for (i = disks ; i--; ) {
853 if (i == dd_idx || i == qd_idx)
854 continue;
855 p = page_address(sh->dev[i].page);
856 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
857 ptr[count++] = p;
858 else
859 printk("compute_block() %d, stripe %llu, %d"
860 " not present\n", dd_idx,
861 (unsigned long long)sh->sector, i);
862
863 check_xor();
864 }
865 if (count != 1)
866 xor_block(count, STRIPE_SIZE, ptr);
ca65b73b
N
867 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
868 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1da177e4
LT
869 }
870}
871
872/* Compute two missing blocks */
873static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
874{
875 raid6_conf_t *conf = sh->raid_conf;
876 int i, count, disks = conf->raid_disks;
877 int pd_idx = sh->pd_idx;
878 int qd_idx = raid6_next_disk(pd_idx, disks);
879 int d0_idx = raid6_next_disk(qd_idx, disks);
880 int faila, failb;
881
882 /* faila and failb are disk numbers relative to d0_idx */
883 /* pd_idx become disks-2 and qd_idx become disks-1 */
884 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
885 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
886
887 BUG_ON(faila == failb);
888 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
889
890 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
891 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
892
893 if ( failb == disks-1 ) {
894 /* Q disk is one of the missing disks */
895 if ( faila == disks-2 ) {
896 /* Missing P+Q, just recompute */
897 compute_parity(sh, UPDATE_PARITY);
898 return;
899 } else {
900 /* We're missing D+Q; recompute D from P */
ca65b73b 901 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1da177e4
LT
902 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
903 return;
904 }
905 }
906
907 /* We're missing D+P or D+D; build pointer table */
908 {
909 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
910 void *ptrs[disks];
911
912 count = 0;
913 i = d0_idx;
914 do {
915 ptrs[count++] = page_address(sh->dev[i].page);
916 i = raid6_next_disk(i, disks);
917 if (i != dd_idx1 && i != dd_idx2 &&
918 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
919 printk("compute_2 with missing block %d/%d\n", count, i);
920 } while ( i != d0_idx );
921
922 if ( failb == disks-2 ) {
923 /* We're missing D+P. */
924 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
925 } else {
926 /* We're missing D+D. */
927 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
928 }
929
930 /* Both the above update both missing blocks */
931 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
932 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
933 }
934}
935
936
937/*
938 * Each stripe/dev can have one or more bion attached.
939 * toread/towrite point to the first in a chain.
940 * The bi_next chain must be in order.
941 */
942static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
943{
944 struct bio **bip;
945 raid6_conf_t *conf = sh->raid_conf;
934ce7c8 946 int firstwrite=0;
1da177e4
LT
947
948 PRINTK("adding bh b#%llu to stripe s#%llu\n",
949 (unsigned long long)bi->bi_sector,
950 (unsigned long long)sh->sector);
951
952
953 spin_lock(&sh->lock);
954 spin_lock_irq(&conf->device_lock);
934ce7c8 955 if (forwrite) {
1da177e4 956 bip = &sh->dev[dd_idx].towrite;
934ce7c8
N
957 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
958 firstwrite = 1;
959 } else
1da177e4
LT
960 bip = &sh->dev[dd_idx].toread;
961 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
962 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
963 goto overlap;
964 bip = &(*bip)->bi_next;
965 }
966 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
967 goto overlap;
968
969 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
970 BUG();
971 if (*bip)
972 bi->bi_next = *bip;
973 *bip = bi;
974 bi->bi_phys_segments ++;
975 spin_unlock_irq(&conf->device_lock);
976 spin_unlock(&sh->lock);
977
978 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
979 (unsigned long long)bi->bi_sector,
980 (unsigned long long)sh->sector, dd_idx);
981
934ce7c8
N
982 if (conf->mddev->bitmap && firstwrite) {
983 sh->bm_seq = conf->seq_write;
984 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
985 STRIPE_SECTORS, 0);
986 set_bit(STRIPE_BIT_DELAY, &sh->state);
987 }
988
1da177e4
LT
989 if (forwrite) {
990 /* check if page is covered */
991 sector_t sector = sh->dev[dd_idx].sector;
992 for (bi=sh->dev[dd_idx].towrite;
993 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
994 bi && bi->bi_sector <= sector;
995 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
996 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
997 sector = bi->bi_sector + (bi->bi_size>>9);
998 }
999 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1000 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1001 }
1002 return 1;
1003
1004 overlap:
1005 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1006 spin_unlock_irq(&conf->device_lock);
1007 spin_unlock(&sh->lock);
1008 return 0;
1009}
1010
1011
ca65b73b
N
1012static int page_is_zero(struct page *p)
1013{
1014 char *a = page_address(p);
1015 return ((*(u32*)a) == 0 &&
1016 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1017}
1da177e4
LT
1018/*
1019 * handle_stripe - do things to a stripe.
1020 *
1021 * We lock the stripe and then examine the state of various bits
1022 * to see what needs to be done.
1023 * Possible results:
1024 * return some read request which now have data
1025 * return some write requests which are safely on disc
1026 * schedule a read on some buffers
1027 * schedule a write of some buffers
1028 * return confirmation of parity correctness
1029 *
1030 * Parity calculations are done inside the stripe lock
1031 * buffers are taken off read_list or write_list, and bh_cache buffers
1032 * get BH_Lock set before the stripe lock is released.
1033 *
1034 */
1035
ca65b73b 1036static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
1da177e4
LT
1037{
1038 raid6_conf_t *conf = sh->raid_conf;
1039 int disks = conf->raid_disks;
1040 struct bio *return_bi= NULL;
1041 struct bio *bi;
1042 int i;
1043 int syncing;
1044 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1045 int non_overwrite = 0;
1046 int failed_num[2] = {0, 0};
1047 struct r5dev *dev, *pdev, *qdev;
1048 int pd_idx = sh->pd_idx;
1049 int qd_idx = raid6_next_disk(pd_idx, disks);
1050 int p_failed, q_failed;
1051
1052 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1053 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1054 pd_idx, qd_idx);
1055
1056 spin_lock(&sh->lock);
1057 clear_bit(STRIPE_HANDLE, &sh->state);
1058 clear_bit(STRIPE_DELAYED, &sh->state);
1059
1060 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1061 /* Now to look around and see what can be done */
1062
9910f16a 1063 rcu_read_lock();
1da177e4
LT
1064 for (i=disks; i--; ) {
1065 mdk_rdev_t *rdev;
1066 dev = &sh->dev[i];
1067 clear_bit(R5_Insync, &dev->flags);
1da177e4
LT
1068
1069 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1070 i, dev->flags, dev->toread, dev->towrite, dev->written);
1071 /* maybe we can reply to a read */
1072 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1073 struct bio *rbi, *rbi2;
1074 PRINTK("Return read for disc %d\n", i);
1075 spin_lock_irq(&conf->device_lock);
1076 rbi = dev->toread;
1077 dev->toread = NULL;
1078 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1079 wake_up(&conf->wait_for_overlap);
1080 spin_unlock_irq(&conf->device_lock);
1081 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1082 copy_data(0, rbi, dev->page, dev->sector);
1083 rbi2 = r5_next_bio(rbi, dev->sector);
1084 spin_lock_irq(&conf->device_lock);
1085 if (--rbi->bi_phys_segments == 0) {
1086 rbi->bi_next = return_bi;
1087 return_bi = rbi;
1088 }
1089 spin_unlock_irq(&conf->device_lock);
1090 rbi = rbi2;
1091 }
1092 }
1093
1094 /* now count some things */
1095 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1096 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1097
1098
1099 if (dev->toread) to_read++;
1100 if (dev->towrite) {
1101 to_write++;
1102 if (!test_bit(R5_OVERWRITE, &dev->flags))
1103 non_overwrite++;
1104 }
1105 if (dev->written) written++;
9910f16a 1106 rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1107 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
d69762e9
N
1108 /* The ReadError flag will just be confusing now */
1109 clear_bit(R5_ReadError, &dev->flags);
1110 clear_bit(R5_ReWrite, &dev->flags);
1111 }
1112 if (!rdev || !test_bit(In_sync, &rdev->flags)
1113 || test_bit(R5_ReadError, &dev->flags)) {
1da177e4
LT
1114 if ( failed < 2 )
1115 failed_num[failed] = i;
1116 failed++;
1117 } else
1118 set_bit(R5_Insync, &dev->flags);
1119 }
9910f16a 1120 rcu_read_unlock();
1da177e4
LT
1121 PRINTK("locked=%d uptodate=%d to_read=%d"
1122 " to_write=%d failed=%d failed_num=%d,%d\n",
1123 locked, uptodate, to_read, to_write, failed,
1124 failed_num[0], failed_num[1]);
1125 /* check if the array has lost >2 devices and, if so, some requests might
1126 * need to be failed
1127 */
1128 if (failed > 2 && to_read+to_write+written) {
1da177e4 1129 for (i=disks; i--; ) {
934ce7c8 1130 int bitmap_end = 0;
d69762e9
N
1131
1132 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
9910f16a
N
1133 mdk_rdev_t *rdev;
1134 rcu_read_lock();
1135 rdev = rcu_dereference(conf->disks[i].rdev);
d69762e9
N
1136 if (rdev && test_bit(In_sync, &rdev->flags))
1137 /* multiple read failures in one stripe */
1138 md_error(conf->mddev, rdev);
9910f16a 1139 rcu_read_unlock();
d69762e9
N
1140 }
1141
934ce7c8 1142 spin_lock_irq(&conf->device_lock);
1da177e4
LT
1143 /* fail all writes first */
1144 bi = sh->dev[i].towrite;
1145 sh->dev[i].towrite = NULL;
934ce7c8 1146 if (bi) { to_write--; bitmap_end = 1; }
1da177e4
LT
1147
1148 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1149 wake_up(&conf->wait_for_overlap);
1150
1151 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1152 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1153 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1154 if (--bi->bi_phys_segments == 0) {
1155 md_write_end(conf->mddev);
1156 bi->bi_next = return_bi;
1157 return_bi = bi;
1158 }
1159 bi = nextbi;
1160 }
1161 /* and fail all 'written' */
1162 bi = sh->dev[i].written;
1163 sh->dev[i].written = NULL;
934ce7c8 1164 if (bi) bitmap_end = 1;
1da177e4
LT
1165 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1166 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1167 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1168 if (--bi->bi_phys_segments == 0) {
1169 md_write_end(conf->mddev);
1170 bi->bi_next = return_bi;
1171 return_bi = bi;
1172 }
1173 bi = bi2;
1174 }
1175
1176 /* fail any reads if this device is non-operational */
d69762e9
N
1177 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1178 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1da177e4
LT
1179 bi = sh->dev[i].toread;
1180 sh->dev[i].toread = NULL;
1181 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1182 wake_up(&conf->wait_for_overlap);
1183 if (bi) to_read--;
1184 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1185 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1186 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1187 if (--bi->bi_phys_segments == 0) {
1188 bi->bi_next = return_bi;
1189 return_bi = bi;
1190 }
1191 bi = nextbi;
1192 }
1193 }
934ce7c8
N
1194 spin_unlock_irq(&conf->device_lock);
1195 if (bitmap_end)
1196 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1197 STRIPE_SECTORS, 0, 0);
1da177e4 1198 }
1da177e4
LT
1199 }
1200 if (failed > 2 && syncing) {
1201 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1202 clear_bit(STRIPE_SYNCING, &sh->state);
1203 syncing = 0;
1204 }
1205
1206 /*
1207 * might be able to return some write requests if the parity blocks
1208 * are safe, or on a failed drive
1209 */
1210 pdev = &sh->dev[pd_idx];
1211 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1212 || (failed >= 2 && failed_num[1] == pd_idx);
1213 qdev = &sh->dev[qd_idx];
1214 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1215 || (failed >= 2 && failed_num[1] == qd_idx);
1216
1217 if ( written &&
1218 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1219 && !test_bit(R5_LOCKED, &pdev->flags)
1220 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1221 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1222 && !test_bit(R5_LOCKED, &qdev->flags)
1223 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1224 /* any written block on an uptodate or failed drive can be
1225 * returned. Note that if we 'wrote' to a failed drive,
1226 * it will be UPTODATE, but never LOCKED, so we don't need
1227 * to test 'failed' directly.
1228 */
1229 for (i=disks; i--; )
1230 if (sh->dev[i].written) {
1231 dev = &sh->dev[i];
1232 if (!test_bit(R5_LOCKED, &dev->flags) &&
1233 test_bit(R5_UPTODATE, &dev->flags) ) {
1234 /* We can return any write requests */
934ce7c8 1235 int bitmap_end = 0;
1da177e4
LT
1236 struct bio *wbi, *wbi2;
1237 PRINTK("Return write for stripe %llu disc %d\n",
1238 (unsigned long long)sh->sector, i);
1239 spin_lock_irq(&conf->device_lock);
1240 wbi = dev->written;
1241 dev->written = NULL;
1242 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1243 wbi2 = r5_next_bio(wbi, dev->sector);
1244 if (--wbi->bi_phys_segments == 0) {
1245 md_write_end(conf->mddev);
1246 wbi->bi_next = return_bi;
1247 return_bi = wbi;
1248 }
1249 wbi = wbi2;
1250 }
934ce7c8
N
1251 if (dev->towrite == NULL)
1252 bitmap_end = 1;
1da177e4 1253 spin_unlock_irq(&conf->device_lock);
934ce7c8
N
1254 if (bitmap_end)
1255 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1256 STRIPE_SECTORS,
1257 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1da177e4
LT
1258 }
1259 }
1260 }
1261
1262 /* Now we might consider reading some blocks, either to check/generate
1263 * parity, or to satisfy requests
1264 * or to load a block that is being partially written.
1265 */
1266 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1267 for (i=disks; i--;) {
1268 dev = &sh->dev[i];
1269 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1270 (dev->toread ||
1271 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1272 syncing ||
1273 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1274 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1275 )
1276 ) {
1277 /* we would like to get this block, possibly
1278 * by computing it, but we might not be able to
1279 */
1280 if (uptodate == disks-1) {
1281 PRINTK("Computing stripe %llu block %d\n",
1282 (unsigned long long)sh->sector, i);
ca65b73b 1283 compute_block_1(sh, i, 0);
1da177e4
LT
1284 uptodate++;
1285 } else if ( uptodate == disks-2 && failed >= 2 ) {
1286 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1287 int other;
1288 for (other=disks; other--;) {
1289 if ( other == i )
1290 continue;
1291 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1292 break;
1293 }
1294 BUG_ON(other < 0);
1295 PRINTK("Computing stripe %llu blocks %d,%d\n",
1296 (unsigned long long)sh->sector, i, other);
1297 compute_block_2(sh, i, other);
1298 uptodate += 2;
1299 } else if (test_bit(R5_Insync, &dev->flags)) {
1300 set_bit(R5_LOCKED, &dev->flags);
1301 set_bit(R5_Wantread, &dev->flags);
1302#if 0
1303 /* if I am just reading this block and we don't have
1304 a failed drive, or any pending writes then sidestep the cache */
1305 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1306 ! syncing && !failed && !to_write) {
1307 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1308 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1309 }
1310#endif
1311 locked++;
1312 PRINTK("Reading block %d (sync=%d)\n",
1313 i, syncing);
1da177e4
LT
1314 }
1315 }
1316 }
1317 set_bit(STRIPE_HANDLE, &sh->state);
1318 }
1319
1320 /* now to consider writing and what else, if anything should be read */
1321 if (to_write) {
1322 int rcw=0, must_compute=0;
1323 for (i=disks ; i--;) {
1324 dev = &sh->dev[i];
1325 /* Would I have to read this buffer for reconstruct_write */
1326 if (!test_bit(R5_OVERWRITE, &dev->flags)
1327 && i != pd_idx && i != qd_idx
1328 && (!test_bit(R5_LOCKED, &dev->flags)
1329#if 0
1330 || sh->bh_page[i] != bh->b_page
1331#endif
1332 ) &&
1333 !test_bit(R5_UPTODATE, &dev->flags)) {
1334 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1335 else {
1336 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1337 must_compute++;
1338 }
1339 }
1340 }
1341 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1342 (unsigned long long)sh->sector, rcw, must_compute);
1343 set_bit(STRIPE_HANDLE, &sh->state);
1344
1345 if (rcw > 0)
1346 /* want reconstruct write, but need to get some data */
1347 for (i=disks; i--;) {
1348 dev = &sh->dev[i];
1349 if (!test_bit(R5_OVERWRITE, &dev->flags)
1350 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1351 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1352 test_bit(R5_Insync, &dev->flags)) {
1353 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1354 {
1355 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1356 (unsigned long long)sh->sector, i);
1357 set_bit(R5_LOCKED, &dev->flags);
1358 set_bit(R5_Wantread, &dev->flags);
1359 locked++;
1360 } else {
1361 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1362 (unsigned long long)sh->sector, i);
1363 set_bit(STRIPE_DELAYED, &sh->state);
1364 set_bit(STRIPE_HANDLE, &sh->state);
1365 }
1366 }
1367 }
1368 /* now if nothing is locked, and if we have enough data, we can start a write request */
934ce7c8
N
1369 if (locked == 0 && rcw == 0 &&
1370 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1da177e4
LT
1371 if ( must_compute > 0 ) {
1372 /* We have failed blocks and need to compute them */
1373 switch ( failed ) {
1374 case 0: BUG();
ca65b73b 1375 case 1: compute_block_1(sh, failed_num[0], 0); break;
1da177e4
LT
1376 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1377 default: BUG(); /* This request should have been failed? */
1378 }
1379 }
1380
1381 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1382 compute_parity(sh, RECONSTRUCT_WRITE);
1383 /* now every locked buffer is ready to be written */
1384 for (i=disks; i--;)
1385 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1386 PRINTK("Writing stripe %llu block %d\n",
1387 (unsigned long long)sh->sector, i);
1388 locked++;
1389 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1da177e4 1390 }
ca65b73b
N
1391 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1392 set_bit(STRIPE_INSYNC, &sh->state);
1393
1da177e4
LT
1394 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1395 atomic_dec(&conf->preread_active_stripes);
1396 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1397 md_wakeup_thread(conf->mddev->thread);
1398 }
1399 }
1400 }
1401
1402 /* maybe we need to check and possibly fix the parity for this stripe
1403 * Any reads will already have been scheduled, so we just see if enough data
1404 * is available
1405 */
ca65b73b
N
1406 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
1407 int update_p = 0, update_q = 0;
1408 struct r5dev *dev;
1da177e4 1409
ca65b73b 1410 set_bit(STRIPE_HANDLE, &sh->state);
1da177e4 1411
ca65b73b
N
1412 BUG_ON(failed>2);
1413 BUG_ON(uptodate < disks);
1414 /* Want to check and possibly repair P and Q.
1415 * However there could be one 'failed' device, in which
1416 * case we can only check one of them, possibly using the
1417 * other to generate missing data
1418 */
1da177e4 1419
ca65b73b
N
1420 /* If !tmp_page, we cannot do the calculations,
1421 * but as we have set STRIPE_HANDLE, we will soon be called
1422 * by stripe_handle with a tmp_page - just wait until then.
1423 */
1424 if (tmp_page) {
1425 if (failed == q_failed) {
1426 /* The only possible failed device holds 'Q', so it makes
1427 * sense to check P (If anything else were failed, we would
1428 * have used P to recreate it).
1429 */
1430 compute_block_1(sh, pd_idx, 1);
1431 if (!page_is_zero(sh->dev[pd_idx].page)) {
1432 compute_block_1(sh,pd_idx,0);
1433 update_p = 1;
1434 }
1435 }
1436 if (!q_failed && failed < 2) {
1437 /* q is not failed, and we didn't use it to generate
1438 * anything, so it makes sense to check it
1439 */
1440 memcpy(page_address(tmp_page),
1441 page_address(sh->dev[qd_idx].page),
1442 STRIPE_SIZE);
1443 compute_parity(sh, UPDATE_PARITY);
1444 if (memcmp(page_address(tmp_page),
1445 page_address(sh->dev[qd_idx].page),
1446 STRIPE_SIZE)!= 0) {
1447 clear_bit(STRIPE_INSYNC, &sh->state);
1448 update_q = 1;
1449 }
1450 }
1451 if (update_p || update_q) {
1452 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1453 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1454 /* don't try to repair!! */
1455 update_p = update_q = 0;
1da177e4 1456 }
1da177e4 1457
ca65b73b
N
1458 /* now write out any block on a failed drive,
1459 * or P or Q if they need it
1460 */
1da177e4 1461
ca65b73b
N
1462 if (failed == 2) {
1463 dev = &sh->dev[failed_num[1]];
1464 locked++;
1465 set_bit(R5_LOCKED, &dev->flags);
1466 set_bit(R5_Wantwrite, &dev->flags);
ca65b73b
N
1467 }
1468 if (failed >= 1) {
1469 dev = &sh->dev[failed_num[0]];
1470 locked++;
1471 set_bit(R5_LOCKED, &dev->flags);
1472 set_bit(R5_Wantwrite, &dev->flags);
ca65b73b 1473 }
1da177e4 1474
ca65b73b
N
1475 if (update_p) {
1476 dev = &sh->dev[pd_idx];
1477 locked ++;
1478 set_bit(R5_LOCKED, &dev->flags);
1479 set_bit(R5_Wantwrite, &dev->flags);
ca65b73b
N
1480 }
1481 if (update_q) {
1482 dev = &sh->dev[qd_idx];
1483 locked++;
1484 set_bit(R5_LOCKED, &dev->flags);
1485 set_bit(R5_Wantwrite, &dev->flags);
ca65b73b 1486 }
934ce7c8 1487 clear_bit(STRIPE_DEGRADED, &sh->state);
1da177e4
LT
1488
1489 set_bit(STRIPE_INSYNC, &sh->state);
1da177e4
LT
1490 }
1491 }
ca65b73b 1492
1da177e4
LT
1493 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1494 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1495 clear_bit(STRIPE_SYNCING, &sh->state);
1496 }
1497
d69762e9
N
1498 /* If the failed drives are just a ReadError, then we might need
1499 * to progress the repair/check process
1500 */
1501 if (failed <= 2 && ! conf->mddev->ro)
1502 for (i=0; i<failed;i++) {
1503 dev = &sh->dev[failed_num[i]];
1504 if (test_bit(R5_ReadError, &dev->flags)
1505 && !test_bit(R5_LOCKED, &dev->flags)
1506 && test_bit(R5_UPTODATE, &dev->flags)
1507 ) {
1508 if (!test_bit(R5_ReWrite, &dev->flags)) {
1509 set_bit(R5_Wantwrite, &dev->flags);
1510 set_bit(R5_ReWrite, &dev->flags);
1511 set_bit(R5_LOCKED, &dev->flags);
1512 } else {
1513 /* let's read it back */
1514 set_bit(R5_Wantread, &dev->flags);
1515 set_bit(R5_LOCKED, &dev->flags);
1516 }
1517 }
1518 }
1da177e4
LT
1519 spin_unlock(&sh->lock);
1520
1521 while ((bi=return_bi)) {
1522 int bytes = bi->bi_size;
1523
1524 return_bi = bi->bi_next;
1525 bi->bi_next = NULL;
1526 bi->bi_size = 0;
1527 bi->bi_end_io(bi, bytes, 0);
1528 }
1529 for (i=disks; i-- ;) {
1530 int rw;
1531 struct bio *bi;
1532 mdk_rdev_t *rdev;
1533 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1534 rw = 1;
1535 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1536 rw = 0;
1537 else
1538 continue;
1539
1540 bi = &sh->dev[i].req;
1541
1542 bi->bi_rw = rw;
1543 if (rw)
1544 bi->bi_end_io = raid6_end_write_request;
1545 else
1546 bi->bi_end_io = raid6_end_read_request;
1547
1548 rcu_read_lock();
d6065f7b 1549 rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1550 if (rdev && test_bit(Faulty, &rdev->flags))
1da177e4
LT
1551 rdev = NULL;
1552 if (rdev)
1553 atomic_inc(&rdev->nr_pending);
1554 rcu_read_unlock();
1555
1556 if (rdev) {
9910f16a 1557 if (syncing)
1da177e4
LT
1558 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1559
1560 bi->bi_bdev = rdev->bdev;
1561 PRINTK("for %llu schedule op %ld on disc %d\n",
1562 (unsigned long long)sh->sector, bi->bi_rw, i);
1563 atomic_inc(&sh->count);
1564 bi->bi_sector = sh->sector + rdev->data_offset;
1565 bi->bi_flags = 1 << BIO_UPTODATE;
1566 bi->bi_vcnt = 1;
1567 bi->bi_max_vecs = 1;
1568 bi->bi_idx = 0;
1569 bi->bi_io_vec = &sh->dev[i].vec;
1570 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1571 bi->bi_io_vec[0].bv_offset = 0;
1572 bi->bi_size = STRIPE_SIZE;
1573 bi->bi_next = NULL;
1574 generic_make_request(bi);
1575 } else {
934ce7c8
N
1576 if (rw == 1)
1577 set_bit(STRIPE_DEGRADED, &sh->state);
1da177e4
LT
1578 PRINTK("skip op %ld on disc %d for sector %llu\n",
1579 bi->bi_rw, i, (unsigned long long)sh->sector);
1580 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1581 set_bit(STRIPE_HANDLE, &sh->state);
1582 }
1583 }
1584}
1585
1586static inline void raid6_activate_delayed(raid6_conf_t *conf)
1587{
1588 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1589 while (!list_empty(&conf->delayed_list)) {
1590 struct list_head *l = conf->delayed_list.next;
1591 struct stripe_head *sh;
1592 sh = list_entry(l, struct stripe_head, lru);
1593 list_del_init(l);
1594 clear_bit(STRIPE_DELAYED, &sh->state);
1595 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1596 atomic_inc(&conf->preread_active_stripes);
1597 list_add_tail(&sh->lru, &conf->handle_list);
1598 }
1599 }
1600}
1601
934ce7c8
N
1602static inline void activate_bit_delay(raid6_conf_t *conf)
1603{
1604 /* device_lock is held */
1605 struct list_head head;
1606 list_add(&head, &conf->bitmap_list);
1607 list_del_init(&conf->bitmap_list);
1608 while (!list_empty(&head)) {
1609 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1610 list_del_init(&sh->lru);
1611 atomic_inc(&sh->count);
1612 __release_stripe(conf, sh);
1613 }
1614}
1615
1da177e4
LT
1616static void unplug_slaves(mddev_t *mddev)
1617{
1618 raid6_conf_t *conf = mddev_to_conf(mddev);
1619 int i;
1620
1621 rcu_read_lock();
1622 for (i=0; i<mddev->raid_disks; i++) {
d6065f7b 1623 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1624 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1da177e4
LT
1625 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1626
1627 atomic_inc(&rdev->nr_pending);
1628 rcu_read_unlock();
1629
1630 if (r_queue->unplug_fn)
1631 r_queue->unplug_fn(r_queue);
1632
1633 rdev_dec_pending(rdev, mddev);
1634 rcu_read_lock();
1635 }
1636 }
1637 rcu_read_unlock();
1638}
1639
1640static void raid6_unplug_device(request_queue_t *q)
1641{
1642 mddev_t *mddev = q->queuedata;
1643 raid6_conf_t *conf = mddev_to_conf(mddev);
1644 unsigned long flags;
1645
1646 spin_lock_irqsave(&conf->device_lock, flags);
1647
934ce7c8
N
1648 if (blk_remove_plug(q)) {
1649 conf->seq_flush++;
1da177e4 1650 raid6_activate_delayed(conf);
934ce7c8 1651 }
1da177e4
LT
1652 md_wakeup_thread(mddev->thread);
1653
1654 spin_unlock_irqrestore(&conf->device_lock, flags);
1655
1656 unplug_slaves(mddev);
1657}
1658
1659static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1660 sector_t *error_sector)
1661{
1662 mddev_t *mddev = q->queuedata;
1663 raid6_conf_t *conf = mddev_to_conf(mddev);
1664 int i, ret = 0;
1665
1666 rcu_read_lock();
1667 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
d6065f7b 1668 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
b2d444d7 1669 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
1670 struct block_device *bdev = rdev->bdev;
1671 request_queue_t *r_queue = bdev_get_queue(bdev);
1672
1673 if (!r_queue->issue_flush_fn)
1674 ret = -EOPNOTSUPP;
1675 else {
1676 atomic_inc(&rdev->nr_pending);
1677 rcu_read_unlock();
1678 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1679 error_sector);
1680 rdev_dec_pending(rdev, mddev);
1681 rcu_read_lock();
1682 }
1683 }
1684 }
1685 rcu_read_unlock();
1686 return ret;
1687}
1688
1689static inline void raid6_plug_device(raid6_conf_t *conf)
1690{
1691 spin_lock_irq(&conf->device_lock);
1692 blk_plug_device(conf->mddev->queue);
1693 spin_unlock_irq(&conf->device_lock);
1694}
1695
1696static int make_request (request_queue_t *q, struct bio * bi)
1697{
1698 mddev_t *mddev = q->queuedata;
1699 raid6_conf_t *conf = mddev_to_conf(mddev);
1700 const unsigned int raid_disks = conf->raid_disks;
1701 const unsigned int data_disks = raid_disks - 2;
1702 unsigned int dd_idx, pd_idx;
1703 sector_t new_sector;
1704 sector_t logical_sector, last_sector;
1705 struct stripe_head *sh;
a362357b 1706 const int rw = bio_data_dir(bi);
1da177e4 1707
e5dcdd80
N
1708 if (unlikely(bio_barrier(bi))) {
1709 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1710 return 0;
1711 }
1712
3d310eb7 1713 md_write_start(mddev, bi);
06d91a5f 1714
a362357b
JA
1715 disk_stat_inc(mddev->gendisk, ios[rw]);
1716 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1da177e4
LT
1717
1718 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1719 last_sector = bi->bi_sector + (bi->bi_size>>9);
1720
1721 bi->bi_next = NULL;
1722 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
06d91a5f 1723
1da177e4
LT
1724 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1725 DEFINE_WAIT(w);
1726
1727 new_sector = raid6_compute_sector(logical_sector,
1728 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1729
1730 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1731 (unsigned long long)new_sector,
1732 (unsigned long long)logical_sector);
1733
1734 retry:
1735 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1736 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1737 if (sh) {
1738 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1739 /* Add failed due to overlap. Flush everything
1740 * and wait a while
1741 */
1742 raid6_unplug_device(mddev->queue);
1743 release_stripe(sh);
1744 schedule();
1745 goto retry;
1746 }
1747 finish_wait(&conf->wait_for_overlap, &w);
1748 raid6_plug_device(conf);
ca65b73b 1749 handle_stripe(sh, NULL);
1da177e4
LT
1750 release_stripe(sh);
1751 } else {
1752 /* cannot get stripe for read-ahead, just give-up */
1753 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1754 finish_wait(&conf->wait_for_overlap, &w);
1755 break;
1756 }
1757
1758 }
1759 spin_lock_irq(&conf->device_lock);
1760 if (--bi->bi_phys_segments == 0) {
1761 int bytes = bi->bi_size;
1762
a362357b 1763 if (rw == WRITE )
1da177e4
LT
1764 md_write_end(mddev);
1765 bi->bi_size = 0;
1766 bi->bi_end_io(bi, bytes, 0);
1767 }
1768 spin_unlock_irq(&conf->device_lock);
1769 return 0;
1770}
1771
1772/* FIXME go_faster isn't used */
57afd89f 1773static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1da177e4
LT
1774{
1775 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1776 struct stripe_head *sh;
1777 int sectors_per_chunk = conf->chunk_size >> 9;
1778 sector_t x;
1779 unsigned long stripe;
1780 int chunk_offset;
1781 int dd_idx, pd_idx;
1782 sector_t first_sector;
1783 int raid_disks = conf->raid_disks;
1784 int data_disks = raid_disks - 2;
934ce7c8
N
1785 sector_t max_sector = mddev->size << 1;
1786 int sync_blocks;
b5ab28a3
N
1787 int still_degraded = 0;
1788 int i;
1da177e4 1789
934ce7c8 1790 if (sector_nr >= max_sector) {
1da177e4
LT
1791 /* just being told to finish up .. nothing much to do */
1792 unplug_slaves(mddev);
934ce7c8
N
1793
1794 if (mddev->curr_resync < max_sector) /* aborted */
1795 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1796 &sync_blocks, 1);
b5ab28a3 1797 else /* completed sync */
934ce7c8
N
1798 conf->fullsync = 0;
1799 bitmap_close_sync(mddev->bitmap);
1800
1da177e4
LT
1801 return 0;
1802 }
1803 /* if there are 2 or more failed drives and we are trying
1804 * to resync, then assert that we are finished, because there is
1805 * nothing we can do.
1806 */
1807 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
57afd89f
N
1808 sector_t rv = (mddev->size << 1) - sector_nr;
1809 *skipped = 1;
1da177e4
LT
1810 return rv;
1811 }
934ce7c8 1812 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
ca65b73b 1813 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
934ce7c8
N
1814 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1815 /* we can skip this block, and probably more */
1816 sync_blocks /= STRIPE_SECTORS;
1817 *skipped = 1;
1818 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1819 }
1da177e4
LT
1820
1821 x = sector_nr;
1822 chunk_offset = sector_div(x, sectors_per_chunk);
1823 stripe = x;
1824 BUG_ON(x != stripe);
1825
1826 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1827 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1828 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1829 if (sh == NULL) {
1830 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1831 /* make sure we don't swamp the stripe cache if someone else
1832 * is trying to get access
1833 */
66c006a5 1834 schedule_timeout_uninterruptible(1);
1da177e4 1835 }
b5ab28a3
N
1836 /* Need to check if array will still be degraded after recovery/resync
1837 * We don't need to check the 'failed' flag as when that gets set,
1838 * recovery aborts.
1839 */
1840 for (i=0; i<mddev->raid_disks; i++)
1841 if (conf->disks[i].rdev == NULL)
1842 still_degraded = 1;
1843
1844 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
1845
1da177e4
LT
1846 spin_lock(&sh->lock);
1847 set_bit(STRIPE_SYNCING, &sh->state);
1848 clear_bit(STRIPE_INSYNC, &sh->state);
1849 spin_unlock(&sh->lock);
1850
ca65b73b 1851 handle_stripe(sh, NULL);
1da177e4
LT
1852 release_stripe(sh);
1853
1854 return STRIPE_SECTORS;
1855}
1856
1857/*
1858 * This is our raid6 kernel thread.
1859 *
1860 * We scan the hash table for stripes which can be handled now.
1861 * During the scan, completed stripes are saved for us by the interrupt
1862 * handler, so that they will not have to wait for our next wakeup.
1863 */
1864static void raid6d (mddev_t *mddev)
1865{
1866 struct stripe_head *sh;
1867 raid6_conf_t *conf = mddev_to_conf(mddev);
1868 int handled;
1869
1870 PRINTK("+++ raid6d active\n");
1871
1872 md_check_recovery(mddev);
1da177e4
LT
1873
1874 handled = 0;
1875 spin_lock_irq(&conf->device_lock);
1876 while (1) {
1877 struct list_head *first;
1878
934ce7c8
N
1879 if (conf->seq_flush - conf->seq_write > 0) {
1880 int seq = conf->seq_flush;
700e432d 1881 spin_unlock_irq(&conf->device_lock);
934ce7c8 1882 bitmap_unplug(mddev->bitmap);
700e432d 1883 spin_lock_irq(&conf->device_lock);
934ce7c8
N
1884 conf->seq_write = seq;
1885 activate_bit_delay(conf);
1886 }
1887
1da177e4
LT
1888 if (list_empty(&conf->handle_list) &&
1889 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1890 !blk_queue_plugged(mddev->queue) &&
1891 !list_empty(&conf->delayed_list))
1892 raid6_activate_delayed(conf);
1893
1894 if (list_empty(&conf->handle_list))
1895 break;
1896
1897 first = conf->handle_list.next;
1898 sh = list_entry(first, struct stripe_head, lru);
1899
1900 list_del_init(first);
1901 atomic_inc(&sh->count);
1902 if (atomic_read(&sh->count)!= 1)
1903 BUG();
1904 spin_unlock_irq(&conf->device_lock);
1905
1906 handled++;
ca65b73b 1907 handle_stripe(sh, conf->spare_page);
1da177e4
LT
1908 release_stripe(sh);
1909
1910 spin_lock_irq(&conf->device_lock);
1911 }
1912 PRINTK("%d stripes handled\n", handled);
1913
1914 spin_unlock_irq(&conf->device_lock);
1915
1916 unplug_slaves(mddev);
1917
1918 PRINTK("--- raid6d inactive\n");
1919}
1920
934ce7c8 1921static int run(mddev_t *mddev)
1da177e4
LT
1922{
1923 raid6_conf_t *conf;
1924 int raid_disk, memory;
1925 mdk_rdev_t *rdev;
1926 struct disk_info *disk;
1927 struct list_head *tmp;
1928
1929 if (mddev->level != 6) {
1930 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
1931 return -EIO;
1932 }
1933
1934 mddev->private = kmalloc (sizeof (raid6_conf_t)
1935 + mddev->raid_disks * sizeof(struct disk_info),
1936 GFP_KERNEL);
1937 if ((conf = mddev->private) == NULL)
1938 goto abort;
1939 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1940 conf->mddev = mddev;
1941
1942 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1943 goto abort;
1944 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1945
ca65b73b
N
1946 conf->spare_page = alloc_page(GFP_KERNEL);
1947 if (!conf->spare_page)
1948 goto abort;
1949
1da177e4
LT
1950 spin_lock_init(&conf->device_lock);
1951 init_waitqueue_head(&conf->wait_for_stripe);
1952 init_waitqueue_head(&conf->wait_for_overlap);
1953 INIT_LIST_HEAD(&conf->handle_list);
1954 INIT_LIST_HEAD(&conf->delayed_list);
934ce7c8 1955 INIT_LIST_HEAD(&conf->bitmap_list);
1da177e4
LT
1956 INIT_LIST_HEAD(&conf->inactive_list);
1957 atomic_set(&conf->active_stripes, 0);
1958 atomic_set(&conf->preread_active_stripes, 0);
1959
1da177e4
LT
1960 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
1961
1962 ITERATE_RDEV(mddev,rdev,tmp) {
1963 raid_disk = rdev->raid_disk;
1964 if (raid_disk >= mddev->raid_disks
1965 || raid_disk < 0)
1966 continue;
1967 disk = conf->disks + raid_disk;
1968
1969 disk->rdev = rdev;
1970
b2d444d7 1971 if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
1972 char b[BDEVNAME_SIZE];
1973 printk(KERN_INFO "raid6: device %s operational as raid"
1974 " disk %d\n", bdevname(rdev->bdev,b),
1975 raid_disk);
1976 conf->working_disks++;
1977 }
1978 }
1979
1980 conf->raid_disks = mddev->raid_disks;
1981
1982 /*
1983 * 0 for a fully functional array, 1 or 2 for a degraded array.
1984 */
1985 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1986 conf->mddev = mddev;
1987 conf->chunk_size = mddev->chunk_size;
1988 conf->level = mddev->level;
1989 conf->algorithm = mddev->layout;
1990 conf->max_nr_stripes = NR_STRIPES;
1991
1992 /* device size must be a multiple of chunk size */
1993 mddev->size &= ~(mddev->chunk_size/1024 -1);
b1581566 1994 mddev->resync_max_sectors = mddev->size << 1;
1da177e4
LT
1995
1996 if (conf->raid_disks < 4) {
1997 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
1998 mdname(mddev), conf->raid_disks);
1999 goto abort;
2000 }
2001 if (!conf->chunk_size || conf->chunk_size % 4) {
2002 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
2003 conf->chunk_size, mdname(mddev));
2004 goto abort;
2005 }
2006 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2007 printk(KERN_ERR
2008 "raid6: unsupported parity algorithm %d for %s\n",
2009 conf->algorithm, mdname(mddev));
2010 goto abort;
2011 }
2012 if (mddev->degraded > 2) {
2013 printk(KERN_ERR "raid6: not enough operational devices for %s"
2014 " (%d/%d failed)\n",
2015 mdname(mddev), conf->failed_disks, conf->raid_disks);
2016 goto abort;
2017 }
2018
1da177e4
LT
2019 if (mddev->degraded > 0 &&
2020 mddev->recovery_cp != MaxSector) {
6ff8d8ec
N
2021 if (mddev->ok_start_degraded)
2022 printk(KERN_WARNING "raid6: starting dirty degraded array:%s"
2023 "- data corruption possible.\n",
2024 mdname(mddev));
2025 else {
2026 printk(KERN_ERR "raid6: cannot start dirty degraded array"
2027 " for %s\n", mdname(mddev));
2028 goto abort;
2029 }
1da177e4 2030 }
1da177e4
LT
2031
2032 {
2033 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
2034 if (!mddev->thread) {
2035 printk(KERN_ERR
2036 "raid6: couldn't allocate thread for %s\n",
2037 mdname(mddev));
2038 goto abort;
2039 }
2040 }
2041
2042 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2043 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2044 if (grow_stripes(conf, conf->max_nr_stripes)) {
2045 printk(KERN_ERR
2046 "raid6: couldn't allocate %dkB for buffers\n", memory);
2047 shrink_stripes(conf);
2048 md_unregister_thread(mddev->thread);
2049 goto abort;
2050 } else
2051 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
2052 memory, mdname(mddev));
2053
2054 if (mddev->degraded == 0)
2055 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
2056 " devices, algorithm %d\n", conf->level, mdname(mddev),
2057 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2058 conf->algorithm);
2059 else
2060 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
2061 " out of %d devices, algorithm %d\n", conf->level,
2062 mdname(mddev), mddev->raid_disks - mddev->degraded,
2063 mddev->raid_disks, conf->algorithm);
2064
2065 print_raid6_conf(conf);
2066
2067 /* read-ahead size must cover two whole stripes, which is
2068 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2069 */
2070 {
2071 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
2072 / PAGE_CACHE_SIZE;
2073 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2074 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2075 }
2076
2077 /* Ok, everything is just fine now */
2078 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
7a5febe9
N
2079
2080 mddev->queue->unplug_fn = raid6_unplug_device;
2081 mddev->queue->issue_flush_fn = raid6_issue_flush;
1da177e4
LT
2082 return 0;
2083abort:
2084 if (conf) {
2085 print_raid6_conf(conf);
ca65b73b
N
2086 if (conf->spare_page)
2087 page_cache_release(conf->spare_page);
1da177e4
LT
2088 if (conf->stripe_hashtbl)
2089 free_pages((unsigned long) conf->stripe_hashtbl,
2090 HASH_PAGES_ORDER);
2091 kfree(conf);
2092 }
2093 mddev->private = NULL;
2094 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
2095 return -EIO;
2096}
2097
2098
2099
2100static int stop (mddev_t *mddev)
2101{
2102 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2103
2104 md_unregister_thread(mddev->thread);
2105 mddev->thread = NULL;
2106 shrink_stripes(conf);
2107 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
2108 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2109 kfree(conf);
2110 mddev->private = NULL;
2111 return 0;
2112}
2113
2114#if RAID6_DUMPSTATE
2115static void print_sh (struct seq_file *seq, struct stripe_head *sh)
2116{
2117 int i;
2118
2119 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
2120 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2121 seq_printf(seq, "sh %llu, count %d.\n",
2122 (unsigned long long)sh->sector, atomic_read(&sh->count));
2123 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
2124 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2125 seq_printf(seq, "(cache%d: %p %ld) ",
2126 i, sh->dev[i].page, sh->dev[i].flags);
2127 }
2128 seq_printf(seq, "\n");
2129}
2130
2131static void printall (struct seq_file *seq, raid6_conf_t *conf)
2132{
2133 struct stripe_head *sh;
2134 int i;
2135
2136 spin_lock_irq(&conf->device_lock);
2137 for (i = 0; i < NR_HASH; i++) {
2138 sh = conf->stripe_hashtbl[i];
2139 for (; sh; sh = sh->hash_next) {
2140 if (sh->raid_conf != conf)
2141 continue;
2142 print_sh(seq, sh);
2143 }
2144 }
2145 spin_unlock_irq(&conf->device_lock);
2146}
2147#endif
2148
2149static void status (struct seq_file *seq, mddev_t *mddev)
2150{
2151 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2152 int i;
2153
2154 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2155 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2156 for (i = 0; i < conf->raid_disks; i++)
2157 seq_printf (seq, "%s",
2158 conf->disks[i].rdev &&
b2d444d7 2159 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
1da177e4
LT
2160 seq_printf (seq, "]");
2161#if RAID6_DUMPSTATE
2162 seq_printf (seq, "\n");
2163 printall(seq, conf);
2164#endif
2165}
2166
2167static void print_raid6_conf (raid6_conf_t *conf)
2168{
2169 int i;
2170 struct disk_info *tmp;
2171
2172 printk("RAID6 conf printout:\n");
2173 if (!conf) {
2174 printk("(conf==NULL)\n");
2175 return;
2176 }
2177 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2178 conf->working_disks, conf->failed_disks);
2179
2180 for (i = 0; i < conf->raid_disks; i++) {
2181 char b[BDEVNAME_SIZE];
2182 tmp = conf->disks + i;
2183 if (tmp->rdev)
2184 printk(" disk %d, o:%d, dev:%s\n",
b2d444d7 2185 i, !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
2186 bdevname(tmp->rdev->bdev,b));
2187 }
2188}
2189
2190static int raid6_spare_active(mddev_t *mddev)
2191{
2192 int i;
2193 raid6_conf_t *conf = mddev->private;
2194 struct disk_info *tmp;
2195
2196 for (i = 0; i < conf->raid_disks; i++) {
2197 tmp = conf->disks + i;
2198 if (tmp->rdev
b2d444d7
N
2199 && !test_bit(Faulty, &tmp->rdev->flags)
2200 && !test_bit(In_sync, &tmp->rdev->flags)) {
1da177e4
LT
2201 mddev->degraded--;
2202 conf->failed_disks--;
2203 conf->working_disks++;
b2d444d7 2204 set_bit(In_sync, &tmp->rdev->flags);
1da177e4
LT
2205 }
2206 }
2207 print_raid6_conf(conf);
2208 return 0;
2209}
2210
2211static int raid6_remove_disk(mddev_t *mddev, int number)
2212{
2213 raid6_conf_t *conf = mddev->private;
2214 int err = 0;
2215 mdk_rdev_t *rdev;
2216 struct disk_info *p = conf->disks + number;
2217
2218 print_raid6_conf(conf);
2219 rdev = p->rdev;
2220 if (rdev) {
b2d444d7 2221 if (test_bit(In_sync, &rdev->flags) ||
1da177e4
LT
2222 atomic_read(&rdev->nr_pending)) {
2223 err = -EBUSY;
2224 goto abort;
2225 }
2226 p->rdev = NULL;
fbd568a3 2227 synchronize_rcu();
1da177e4
LT
2228 if (atomic_read(&rdev->nr_pending)) {
2229 /* lost the race, try later */
2230 err = -EBUSY;
2231 p->rdev = rdev;
2232 }
2233 }
2234
2235abort:
2236
2237 print_raid6_conf(conf);
2238 return err;
2239}
2240
2241static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2242{
2243 raid6_conf_t *conf = mddev->private;
2244 int found = 0;
2245 int disk;
2246 struct disk_info *p;
2247
2248 if (mddev->degraded > 2)
2249 /* no point adding a device */
2250 return 0;
2251 /*
6aea114a
N
2252 * find the disk ... but prefer rdev->saved_raid_disk
2253 * if possible.
1da177e4 2254 */
6aea114a
N
2255 if (rdev->saved_raid_disk >= 0 &&
2256 conf->disks[rdev->saved_raid_disk].rdev == NULL)
2257 disk = rdev->saved_raid_disk;
2258 else
2259 disk = 0;
2260 for ( ; disk < mddev->raid_disks; disk++)
1da177e4 2261 if ((p=conf->disks + disk)->rdev == NULL) {
b2d444d7 2262 clear_bit(In_sync, &rdev->flags);
1da177e4
LT
2263 rdev->raid_disk = disk;
2264 found = 1;
934ce7c8
N
2265 if (rdev->saved_raid_disk != disk)
2266 conf->fullsync = 1;
d6065f7b 2267 rcu_assign_pointer(p->rdev, rdev);
1da177e4
LT
2268 break;
2269 }
2270 print_raid6_conf(conf);
2271 return found;
2272}
2273
2274static int raid6_resize(mddev_t *mddev, sector_t sectors)
2275{
2276 /* no resync is happening, and there is enough space
2277 * on all devices, so we can resize.
2278 * We need to make sure resync covers any new space.
2279 * If the array is shrinking we should possibly wait until
2280 * any io in the removed space completes, but it hardly seems
2281 * worth it.
2282 */
2283 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2284 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2285 set_capacity(mddev->gendisk, mddev->array_size << 1);
2286 mddev->changed = 1;
2287 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2288 mddev->recovery_cp = mddev->size << 1;
2289 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2290 }
2291 mddev->size = sectors /2;
4b5c7ae8 2292 mddev->resync_max_sectors = sectors;
1da177e4
LT
2293 return 0;
2294}
2295
934ce7c8
N
2296static void raid6_quiesce(mddev_t *mddev, int state)
2297{
2298 raid6_conf_t *conf = mddev_to_conf(mddev);
2299
2300 switch(state) {
2301 case 1: /* stop all writes */
2302 spin_lock_irq(&conf->device_lock);
2303 conf->quiesce = 1;
2304 wait_event_lock_irq(conf->wait_for_stripe,
2305 atomic_read(&conf->active_stripes) == 0,
2306 conf->device_lock, /* nothing */);
2307 spin_unlock_irq(&conf->device_lock);
2308 break;
2309
2310 case 0: /* re-enable writes */
2311 spin_lock_irq(&conf->device_lock);
2312 conf->quiesce = 0;
2313 wake_up(&conf->wait_for_stripe);
2314 spin_unlock_irq(&conf->device_lock);
2315 break;
2316 }
934ce7c8 2317}
b15c2e57 2318
1da177e4
LT
2319static mdk_personality_t raid6_personality=
2320{
2321 .name = "raid6",
2322 .owner = THIS_MODULE,
2323 .make_request = make_request,
2324 .run = run,
2325 .stop = stop,
2326 .status = status,
2327 .error_handler = error,
2328 .hot_add_disk = raid6_add_disk,
2329 .hot_remove_disk= raid6_remove_disk,
2330 .spare_active = raid6_spare_active,
2331 .sync_request = sync_request,
2332 .resize = raid6_resize,
934ce7c8 2333 .quiesce = raid6_quiesce,
1da177e4
LT
2334};
2335
2336static int __init raid6_init (void)
2337{
2338 int e;
2339
2340 e = raid6_select_algo();
2341 if ( e )
2342 return e;
2343
2344 return register_md_personality (RAID6, &raid6_personality);
2345}
2346
2347static void raid6_exit (void)
2348{
2349 unregister_md_personality (RAID6);
2350}
2351
2352module_init(raid6_init);
2353module_exit(raid6_exit);
2354MODULE_LICENSE("GPL");
2355MODULE_ALIAS("md-personality-8"); /* RAID6 */