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