blob: ab64b37e499602ac644489ed1d9fb40734d8ef9b [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
Arjan van de Ven858119e2006-01-14 13:20:43 -080091static void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
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 ||
NeilBrown35849c72006-02-02 14:28:06 -0800118 atomic_read(&conf->active_stripes) < (conf->max_nr_stripes*3/4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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
Arjan van de Ven858119e2006-01-14 13:20:43 -0800200static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
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) &&
NeilBrown35849c72006-02-02 14:28:06 -0800276 (atomic_read(&conf->active_stripes)
277 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 || !conf->inactive_blocked),
279 conf->device_lock,
280 unplug_slaves(conf->mddev);
281 );
282 conf->inactive_blocked = 0;
283 } else
284 init_stripe(sh, sector, pd_idx);
285 } else {
286 if (atomic_read(&sh->count)) {
287 if (!list_empty(&sh->lru))
288 BUG();
289 } else {
290 if (!test_bit(STRIPE_HANDLE, &sh->state))
291 atomic_inc(&conf->active_stripes);
292 if (list_empty(&sh->lru))
293 BUG();
294 list_del_init(&sh->lru);
295 }
296 }
297 } while (sh == NULL);
298
299 if (sh)
300 atomic_inc(&sh->count);
301
302 spin_unlock_irq(&conf->device_lock);
303 return sh;
304}
305
NeilBrown35849c72006-02-02 14:28:06 -0800306static int grow_one_stripe(raid6_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct stripe_head *sh;
NeilBrown35849c72006-02-02 14:28:06 -0800309 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
310 if (!sh)
311 return 0;
312 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
313 sh->raid_conf = conf;
314 spin_lock_init(&sh->lock);
315
316 if (grow_buffers(sh, conf->raid_disks)) {
317 shrink_buffers(sh, conf->raid_disks);
318 kmem_cache_free(conf->slab_cache, sh);
319 return 0;
320 }
321 /* we just created an active stripe so... */
322 atomic_set(&sh->count, 1);
323 atomic_inc(&conf->active_stripes);
324 INIT_LIST_HEAD(&sh->lru);
325 release_stripe(sh);
326 return 1;
327}
328
329static int grow_stripes(raid6_conf_t *conf, int num)
330{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 kmem_cache_t *sc;
332 int devs = conf->raid_disks;
333
NeilBrownad01c9e2006-03-27 01:18:07 -0800334 sprintf(conf->cache_name[0], "raid6/%s", mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
NeilBrownad01c9e2006-03-27 01:18:07 -0800336 sc = kmem_cache_create(conf->cache_name[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
338 0, 0, NULL, NULL);
339 if (!sc)
340 return 1;
341 conf->slab_cache = sc;
NeilBrown35849c72006-02-02 14:28:06 -0800342 while (num--)
343 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
NeilBrown35849c72006-02-02 14:28:06 -0800348static int drop_one_stripe(raid6_conf_t *conf)
349{
350 struct stripe_head *sh;
351 spin_lock_irq(&conf->device_lock);
352 sh = get_free_stripe(conf);
353 spin_unlock_irq(&conf->device_lock);
354 if (!sh)
355 return 0;
356 if (atomic_read(&sh->count))
357 BUG();
358 shrink_buffers(sh, conf->raid_disks);
359 kmem_cache_free(conf->slab_cache, sh);
360 atomic_dec(&conf->active_stripes);
361 return 1;
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static void shrink_stripes(raid6_conf_t *conf)
365{
NeilBrown35849c72006-02-02 14:28:06 -0800366 while (drop_one_stripe(conf))
367 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
NeilBrown29fc7e32006-02-03 03:03:41 -0800369 if (conf->slab_cache)
370 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 conf->slab_cache = NULL;
372}
373
NeilBrownd69762e2006-01-06 00:20:18 -0800374static int raid6_end_read_request(struct bio * bi, unsigned int bytes_done,
375 int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct stripe_head *sh = bi->bi_private;
378 raid6_conf_t *conf = sh->raid_conf;
379 int disks = conf->raid_disks, i;
380 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
381
382 if (bi->bi_size)
383 return 1;
384
385 for (i=0 ; i<disks; i++)
386 if (bi == &sh->dev[i].req)
387 break;
388
389 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
390 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
391 uptodate);
392 if (i == disks) {
393 BUG();
394 return 0;
395 }
396
397 if (uptodate) {
398#if 0
399 struct bio *bio;
400 unsigned long flags;
401 spin_lock_irqsave(&conf->device_lock, flags);
402 /* we can return a buffer if we bypassed the cache or
403 * if the top buffer is not in highmem. If there are
404 * multiple buffers, leave the extra work to
405 * handle_stripe
406 */
407 buffer = sh->bh_read[i];
408 if (buffer &&
409 (!PageHighMem(buffer->b_page)
410 || buffer->b_page == bh->b_page )
411 ) {
412 sh->bh_read[i] = buffer->b_reqnext;
413 buffer->b_reqnext = NULL;
414 } else
415 buffer = NULL;
416 spin_unlock_irqrestore(&conf->device_lock, flags);
417 if (sh->bh_page[i]==bh->b_page)
418 set_buffer_uptodate(bh);
419 if (buffer) {
420 if (buffer->b_page != bh->b_page)
421 memcpy(buffer->b_data, bh->b_data, bh->b_size);
422 buffer->b_end_io(buffer, 1);
423 }
424#else
425 set_bit(R5_UPTODATE, &sh->dev[i].flags);
426#endif
NeilBrownd69762e2006-01-06 00:20:18 -0800427 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
428 printk(KERN_INFO "raid6: read error corrected!!\n");
429 clear_bit(R5_ReadError, &sh->dev[i].flags);
430 clear_bit(R5_ReWrite, &sh->dev[i].flags);
431 }
432 if (atomic_read(&conf->disks[i].rdev->read_errors))
433 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 } else {
NeilBrownd69762e2006-01-06 00:20:18 -0800435 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd69762e2006-01-06 00:20:18 -0800437 atomic_inc(&conf->disks[i].rdev->read_errors);
438 if (conf->mddev->degraded)
439 printk(KERN_WARNING "raid6: read error not correctable.\n");
440 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
441 /* Oh, no!!! */
442 printk(KERN_WARNING "raid6: read error NOT corrected!!\n");
443 else if (atomic_read(&conf->disks[i].rdev->read_errors)
444 > conf->max_nr_stripes)
445 printk(KERN_WARNING
446 "raid6: Too many read errors, failing device.\n");
447 else
448 retry = 1;
449 if (retry)
450 set_bit(R5_ReadError, &sh->dev[i].flags);
451 else {
452 clear_bit(R5_ReadError, &sh->dev[i].flags);
453 clear_bit(R5_ReWrite, &sh->dev[i].flags);
454 md_error(conf->mddev, conf->disks[i].rdev);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
458#if 0
459 /* must restore b_page before unlocking buffer... */
460 if (sh->bh_page[i] != bh->b_page) {
461 bh->b_page = sh->bh_page[i];
462 bh->b_data = page_address(bh->b_page);
463 clear_buffer_uptodate(bh);
464 }
465#endif
466 clear_bit(R5_LOCKED, &sh->dev[i].flags);
467 set_bit(STRIPE_HANDLE, &sh->state);
468 release_stripe(sh);
469 return 0;
470}
471
472static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
473 int error)
474{
475 struct stripe_head *sh = bi->bi_private;
476 raid6_conf_t *conf = sh->raid_conf;
477 int disks = conf->raid_disks, i;
478 unsigned long flags;
479 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
480
481 if (bi->bi_size)
482 return 1;
483
484 for (i=0 ; i<disks; i++)
485 if (bi == &sh->dev[i].req)
486 break;
487
488 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
489 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
490 uptodate);
491 if (i == disks) {
492 BUG();
493 return 0;
494 }
495
496 spin_lock_irqsave(&conf->device_lock, flags);
497 if (!uptodate)
498 md_error(conf->mddev, conf->disks[i].rdev);
499
500 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
501
502 clear_bit(R5_LOCKED, &sh->dev[i].flags);
503 set_bit(STRIPE_HANDLE, &sh->state);
504 __release_stripe(conf, sh);
505 spin_unlock_irqrestore(&conf->device_lock, flags);
506 return 0;
507}
508
509
510static sector_t compute_blocknr(struct stripe_head *sh, int i);
511
512static void raid6_build_block (struct stripe_head *sh, int i)
513{
514 struct r5dev *dev = &sh->dev[i];
515 int pd_idx = sh->pd_idx;
516 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
517
518 bio_init(&dev->req);
519 dev->req.bi_io_vec = &dev->vec;
520 dev->req.bi_vcnt++;
521 dev->req.bi_max_vecs++;
522 dev->vec.bv_page = dev->page;
523 dev->vec.bv_len = STRIPE_SIZE;
524 dev->vec.bv_offset = 0;
525
526 dev->req.bi_sector = sh->sector;
527 dev->req.bi_private = sh;
528
529 dev->flags = 0;
530 if (i != pd_idx && i != qd_idx)
531 dev->sector = compute_blocknr(sh, i);
532}
533
534static void error(mddev_t *mddev, mdk_rdev_t *rdev)
535{
536 char b[BDEVNAME_SIZE];
537 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
538 PRINTK("raid6: error called\n");
539
NeilBrownb2d444d2005-11-08 21:39:31 -0800540 if (!test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 mddev->sb_dirty = 1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800542 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 conf->working_disks--;
544 mddev->degraded++;
545 conf->failed_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800546 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /*
548 * if recovery was running, make sure it aborts.
549 */
550 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
551 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800552 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 printk (KERN_ALERT
554 "raid6: Disk failure on %s, disabling device."
555 " Operation continuing on %d devices\n",
556 bdevname(rdev->bdev,b), conf->working_disks);
557 }
558}
559
560/*
561 * Input: a 'big' sector number,
562 * Output: index of the data and parity disk, and the sector # in them.
563 */
564static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
565 unsigned int data_disks, unsigned int * dd_idx,
566 unsigned int * pd_idx, raid6_conf_t *conf)
567{
568 long stripe;
569 unsigned long chunk_number;
570 unsigned int chunk_offset;
571 sector_t new_sector;
572 int sectors_per_chunk = conf->chunk_size >> 9;
573
574 /* First compute the information on this sector */
575
576 /*
577 * Compute the chunk number and the sector offset inside the chunk
578 */
579 chunk_offset = sector_div(r_sector, sectors_per_chunk);
580 chunk_number = r_sector;
581 if ( r_sector != chunk_number ) {
582 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
583 (unsigned long long)r_sector, (unsigned long)chunk_number);
584 BUG();
585 }
586
587 /*
588 * Compute the stripe number
589 */
590 stripe = chunk_number / data_disks;
591
592 /*
593 * Compute the data disk and parity disk indexes inside the stripe
594 */
595 *dd_idx = chunk_number % data_disks;
596
597 /*
598 * Select the parity disk based on the user selected algorithm.
599 */
600
601 /**** FIX THIS ****/
602 switch (conf->algorithm) {
603 case ALGORITHM_LEFT_ASYMMETRIC:
604 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
605 if (*pd_idx == raid_disks-1)
606 (*dd_idx)++; /* Q D D D P */
607 else if (*dd_idx >= *pd_idx)
608 (*dd_idx) += 2; /* D D P Q D */
609 break;
610 case ALGORITHM_RIGHT_ASYMMETRIC:
611 *pd_idx = stripe % raid_disks;
612 if (*pd_idx == raid_disks-1)
613 (*dd_idx)++; /* Q D D D P */
614 else if (*dd_idx >= *pd_idx)
615 (*dd_idx) += 2; /* D D P Q D */
616 break;
617 case ALGORITHM_LEFT_SYMMETRIC:
618 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
619 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
620 break;
621 case ALGORITHM_RIGHT_SYMMETRIC:
622 *pd_idx = stripe % raid_disks;
623 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
624 break;
625 default:
626 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
627 conf->algorithm);
628 }
629
630 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
631 chunk_number, *pd_idx, *dd_idx);
632
633 /*
634 * Finally, compute the new sector number
635 */
636 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
637 return new_sector;
638}
639
640
641static sector_t compute_blocknr(struct stripe_head *sh, int i)
642{
643 raid6_conf_t *conf = sh->raid_conf;
644 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
645 sector_t new_sector = sh->sector, check;
646 int sectors_per_chunk = conf->chunk_size >> 9;
647 sector_t stripe;
648 int chunk_offset;
649 int chunk_number, dummy1, dummy2, dd_idx = i;
650 sector_t r_sector;
651 int i0 = i;
652
653 chunk_offset = sector_div(new_sector, sectors_per_chunk);
654 stripe = new_sector;
655 if ( new_sector != stripe ) {
656 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
657 (unsigned long long)new_sector, (unsigned long)stripe);
658 BUG();
659 }
660
661 switch (conf->algorithm) {
662 case ALGORITHM_LEFT_ASYMMETRIC:
663 case ALGORITHM_RIGHT_ASYMMETRIC:
664 if (sh->pd_idx == raid_disks-1)
665 i--; /* Q D D D P */
666 else if (i > sh->pd_idx)
667 i -= 2; /* D D P Q D */
668 break;
669 case ALGORITHM_LEFT_SYMMETRIC:
670 case ALGORITHM_RIGHT_SYMMETRIC:
671 if (sh->pd_idx == raid_disks-1)
672 i--; /* Q D D D P */
673 else {
674 /* D D P Q D */
675 if (i < sh->pd_idx)
676 i += raid_disks;
677 i -= (sh->pd_idx + 2);
678 }
679 break;
680 default:
681 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
682 conf->algorithm);
683 }
684
685 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
686
687 chunk_number = stripe * data_disks + i;
688 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
689
690 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
691 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
692 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
693 return 0;
694 }
695 return r_sector;
696}
697
698
699
700/*
701 * Copy data between a page in the stripe cache, and one or more bion
702 * The page could align with the middle of the bio, or there could be
703 * several bion, each with several bio_vecs, which cover part of the page
704 * Multiple bion are linked together on bi_next. There may be extras
705 * at the end of this list. We ignore them.
706 */
707static void copy_data(int frombio, struct bio *bio,
708 struct page *page,
709 sector_t sector)
710{
711 char *pa = page_address(page);
712 struct bio_vec *bvl;
713 int i;
714 int page_offset;
715
716 if (bio->bi_sector >= sector)
717 page_offset = (signed)(bio->bi_sector - sector) * 512;
718 else
719 page_offset = (signed)(sector - bio->bi_sector) * -512;
720 bio_for_each_segment(bvl, bio, i) {
721 int len = bio_iovec_idx(bio,i)->bv_len;
722 int clen;
723 int b_offset = 0;
724
725 if (page_offset < 0) {
726 b_offset = -page_offset;
727 page_offset += b_offset;
728 len -= b_offset;
729 }
730
731 if (len > 0 && page_offset + len > STRIPE_SIZE)
732 clen = STRIPE_SIZE - page_offset;
733 else clen = len;
734
735 if (clen > 0) {
736 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
737 if (frombio)
738 memcpy(pa+page_offset, ba+b_offset, clen);
739 else
740 memcpy(ba+b_offset, pa+page_offset, clen);
741 __bio_kunmap_atomic(ba, KM_USER0);
742 }
743 if (clen < len) /* hit end of page */
744 break;
745 page_offset += len;
746 }
747}
748
749#define check_xor() do { \
750 if (count == MAX_XOR_BLOCKS) { \
751 xor_block(count, STRIPE_SIZE, ptr); \
752 count = 1; \
753 } \
754 } while(0)
755
756/* Compute P and Q syndromes */
757static void compute_parity(struct stripe_head *sh, int method)
758{
759 raid6_conf_t *conf = sh->raid_conf;
760 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
761 struct bio *chosen;
762 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
763 void *ptrs[disks];
764
765 qd_idx = raid6_next_disk(pd_idx, disks);
766 d0_idx = raid6_next_disk(qd_idx, disks);
767
768 PRINTK("compute_parity, stripe %llu, method %d\n",
769 (unsigned long long)sh->sector, method);
770
771 switch(method) {
772 case READ_MODIFY_WRITE:
773 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
774 case RECONSTRUCT_WRITE:
775 for (i= disks; i-- ;)
776 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
777 chosen = sh->dev[i].towrite;
778 sh->dev[i].towrite = NULL;
779
780 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
781 wake_up(&conf->wait_for_overlap);
782
783 if (sh->dev[i].written) BUG();
784 sh->dev[i].written = chosen;
785 }
786 break;
787 case CHECK_PARITY:
788 BUG(); /* Not implemented yet */
789 }
790
791 for (i = disks; i--;)
792 if (sh->dev[i].written) {
793 sector_t sector = sh->dev[i].sector;
794 struct bio *wbi = sh->dev[i].written;
795 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
796 copy_data(1, wbi, sh->dev[i].page, sector);
797 wbi = r5_next_bio(wbi, sector);
798 }
799
800 set_bit(R5_LOCKED, &sh->dev[i].flags);
801 set_bit(R5_UPTODATE, &sh->dev[i].flags);
802 }
803
804// switch(method) {
805// case RECONSTRUCT_WRITE:
806// case CHECK_PARITY:
807// case UPDATE_PARITY:
808 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
809 /* FIX: Is this ordering of drives even remotely optimal? */
810 count = 0;
811 i = d0_idx;
812 do {
813 ptrs[count++] = page_address(sh->dev[i].page);
814 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
815 printk("block %d/%d not uptodate on parity calc\n", i,count);
816 i = raid6_next_disk(i, disks);
817 } while ( i != d0_idx );
818// break;
819// }
820
821 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
822
823 switch(method) {
824 case RECONSTRUCT_WRITE:
825 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
826 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
827 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
828 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
829 break;
830 case UPDATE_PARITY:
831 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
832 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
833 break;
834 }
835}
836
837/* Compute one missing block */
NeilBrownca65b732006-01-06 00:20:17 -0800838static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 raid6_conf_t *conf = sh->raid_conf;
841 int i, count, disks = conf->raid_disks;
842 void *ptr[MAX_XOR_BLOCKS], *p;
843 int pd_idx = sh->pd_idx;
844 int qd_idx = raid6_next_disk(pd_idx, disks);
845
846 PRINTK("compute_block_1, stripe %llu, idx %d\n",
847 (unsigned long long)sh->sector, dd_idx);
848
849 if ( dd_idx == qd_idx ) {
850 /* We're actually computing the Q drive */
851 compute_parity(sh, UPDATE_PARITY);
852 } else {
853 ptr[0] = page_address(sh->dev[dd_idx].page);
NeilBrownca65b732006-01-06 00:20:17 -0800854 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 count = 1;
856 for (i = disks ; i--; ) {
857 if (i == dd_idx || i == qd_idx)
858 continue;
859 p = page_address(sh->dev[i].page);
860 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
861 ptr[count++] = p;
862 else
863 printk("compute_block() %d, stripe %llu, %d"
864 " not present\n", dd_idx,
865 (unsigned long long)sh->sector, i);
866
867 check_xor();
868 }
869 if (count != 1)
870 xor_block(count, STRIPE_SIZE, ptr);
NeilBrownca65b732006-01-06 00:20:17 -0800871 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
872 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874}
875
876/* Compute two missing blocks */
877static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
878{
879 raid6_conf_t *conf = sh->raid_conf;
880 int i, count, disks = conf->raid_disks;
881 int pd_idx = sh->pd_idx;
882 int qd_idx = raid6_next_disk(pd_idx, disks);
883 int d0_idx = raid6_next_disk(qd_idx, disks);
884 int faila, failb;
885
886 /* faila and failb are disk numbers relative to d0_idx */
887 /* pd_idx become disks-2 and qd_idx become disks-1 */
888 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
889 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
890
891 BUG_ON(faila == failb);
892 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
893
894 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
895 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
896
897 if ( failb == disks-1 ) {
898 /* Q disk is one of the missing disks */
899 if ( faila == disks-2 ) {
900 /* Missing P+Q, just recompute */
901 compute_parity(sh, UPDATE_PARITY);
902 return;
903 } else {
904 /* We're missing D+Q; recompute D from P */
NeilBrownca65b732006-01-06 00:20:17 -0800905 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
907 return;
908 }
909 }
910
911 /* We're missing D+P or D+D; build pointer table */
912 {
913 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
914 void *ptrs[disks];
915
916 count = 0;
917 i = d0_idx;
918 do {
919 ptrs[count++] = page_address(sh->dev[i].page);
920 i = raid6_next_disk(i, disks);
921 if (i != dd_idx1 && i != dd_idx2 &&
922 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
923 printk("compute_2 with missing block %d/%d\n", count, i);
924 } while ( i != d0_idx );
925
926 if ( failb == disks-2 ) {
927 /* We're missing D+P. */
928 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
929 } else {
930 /* We're missing D+D. */
931 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
932 }
933
934 /* Both the above update both missing blocks */
935 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
936 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
937 }
938}
939
940
941/*
942 * Each stripe/dev can have one or more bion attached.
943 * toread/towrite point to the first in a chain.
944 * The bi_next chain must be in order.
945 */
946static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
947{
948 struct bio **bip;
949 raid6_conf_t *conf = sh->raid_conf;
NeilBrown934ce7c2005-09-09 16:23:55 -0700950 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 PRINTK("adding bh b#%llu to stripe s#%llu\n",
953 (unsigned long long)bi->bi_sector,
954 (unsigned long long)sh->sector);
955
956
957 spin_lock(&sh->lock);
958 spin_lock_irq(&conf->device_lock);
NeilBrown934ce7c2005-09-09 16:23:55 -0700959 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 bip = &sh->dev[dd_idx].towrite;
NeilBrown934ce7c2005-09-09 16:23:55 -0700961 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
962 firstwrite = 1;
963 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 bip = &sh->dev[dd_idx].toread;
965 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
966 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
967 goto overlap;
968 bip = &(*bip)->bi_next;
969 }
970 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
971 goto overlap;
972
973 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
974 BUG();
975 if (*bip)
976 bi->bi_next = *bip;
977 *bip = bi;
978 bi->bi_phys_segments ++;
979 spin_unlock_irq(&conf->device_lock);
980 spin_unlock(&sh->lock);
981
982 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
983 (unsigned long long)bi->bi_sector,
984 (unsigned long long)sh->sector, dd_idx);
985
NeilBrown934ce7c2005-09-09 16:23:55 -0700986 if (conf->mddev->bitmap && firstwrite) {
987 sh->bm_seq = conf->seq_write;
988 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
989 STRIPE_SECTORS, 0);
990 set_bit(STRIPE_BIT_DELAY, &sh->state);
991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (forwrite) {
994 /* check if page is covered */
995 sector_t sector = sh->dev[dd_idx].sector;
996 for (bi=sh->dev[dd_idx].towrite;
997 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
998 bi && bi->bi_sector <= sector;
999 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1000 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1001 sector = bi->bi_sector + (bi->bi_size>>9);
1002 }
1003 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1004 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1005 }
1006 return 1;
1007
1008 overlap:
1009 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1010 spin_unlock_irq(&conf->device_lock);
1011 spin_unlock(&sh->lock);
1012 return 0;
1013}
1014
1015
NeilBrownca65b732006-01-06 00:20:17 -08001016static int page_is_zero(struct page *p)
1017{
1018 char *a = page_address(p);
1019 return ((*(u32*)a) == 0 &&
1020 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/*
1023 * handle_stripe - do things to a stripe.
1024 *
1025 * We lock the stripe and then examine the state of various bits
1026 * to see what needs to be done.
1027 * Possible results:
1028 * return some read request which now have data
1029 * return some write requests which are safely on disc
1030 * schedule a read on some buffers
1031 * schedule a write of some buffers
1032 * return confirmation of parity correctness
1033 *
1034 * Parity calculations are done inside the stripe lock
1035 * buffers are taken off read_list or write_list, and bh_cache buffers
1036 * get BH_Lock set before the stripe lock is released.
1037 *
1038 */
1039
NeilBrownca65b732006-01-06 00:20:17 -08001040static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 raid6_conf_t *conf = sh->raid_conf;
1043 int disks = conf->raid_disks;
1044 struct bio *return_bi= NULL;
1045 struct bio *bi;
1046 int i;
1047 int syncing;
1048 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1049 int non_overwrite = 0;
1050 int failed_num[2] = {0, 0};
1051 struct r5dev *dev, *pdev, *qdev;
1052 int pd_idx = sh->pd_idx;
1053 int qd_idx = raid6_next_disk(pd_idx, disks);
1054 int p_failed, q_failed;
1055
1056 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1057 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1058 pd_idx, qd_idx);
1059
1060 spin_lock(&sh->lock);
1061 clear_bit(STRIPE_HANDLE, &sh->state);
1062 clear_bit(STRIPE_DELAYED, &sh->state);
1063
1064 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1065 /* Now to look around and see what can be done */
1066
NeilBrown9910f162006-01-06 00:20:24 -08001067 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 for (i=disks; i--; ) {
1069 mdk_rdev_t *rdev;
1070 dev = &sh->dev[i];
1071 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1074 i, dev->flags, dev->toread, dev->towrite, dev->written);
1075 /* maybe we can reply to a read */
1076 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1077 struct bio *rbi, *rbi2;
1078 PRINTK("Return read for disc %d\n", i);
1079 spin_lock_irq(&conf->device_lock);
1080 rbi = dev->toread;
1081 dev->toread = NULL;
1082 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1083 wake_up(&conf->wait_for_overlap);
1084 spin_unlock_irq(&conf->device_lock);
1085 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1086 copy_data(0, rbi, dev->page, dev->sector);
1087 rbi2 = r5_next_bio(rbi, dev->sector);
1088 spin_lock_irq(&conf->device_lock);
1089 if (--rbi->bi_phys_segments == 0) {
1090 rbi->bi_next = return_bi;
1091 return_bi = rbi;
1092 }
1093 spin_unlock_irq(&conf->device_lock);
1094 rbi = rbi2;
1095 }
1096 }
1097
1098 /* now count some things */
1099 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1100 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1101
1102
1103 if (dev->toread) to_read++;
1104 if (dev->towrite) {
1105 to_write++;
1106 if (!test_bit(R5_OVERWRITE, &dev->flags))
1107 non_overwrite++;
1108 }
1109 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001110 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001111 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrownd69762e2006-01-06 00:20:18 -08001112 /* The ReadError flag will just be confusing now */
1113 clear_bit(R5_ReadError, &dev->flags);
1114 clear_bit(R5_ReWrite, &dev->flags);
1115 }
1116 if (!rdev || !test_bit(In_sync, &rdev->flags)
1117 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if ( failed < 2 )
1119 failed_num[failed] = i;
1120 failed++;
1121 } else
1122 set_bit(R5_Insync, &dev->flags);
1123 }
NeilBrown9910f162006-01-06 00:20:24 -08001124 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 PRINTK("locked=%d uptodate=%d to_read=%d"
1126 " to_write=%d failed=%d failed_num=%d,%d\n",
1127 locked, uptodate, to_read, to_write, failed,
1128 failed_num[0], failed_num[1]);
1129 /* check if the array has lost >2 devices and, if so, some requests might
1130 * need to be failed
1131 */
1132 if (failed > 2 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 for (i=disks; i--; ) {
NeilBrown934ce7c2005-09-09 16:23:55 -07001134 int bitmap_end = 0;
NeilBrownd69762e2006-01-06 00:20:18 -08001135
1136 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001137 mdk_rdev_t *rdev;
1138 rcu_read_lock();
1139 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownd69762e2006-01-06 00:20:18 -08001140 if (rdev && test_bit(In_sync, &rdev->flags))
1141 /* multiple read failures in one stripe */
1142 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001143 rcu_read_unlock();
NeilBrownd69762e2006-01-06 00:20:18 -08001144 }
1145
NeilBrown934ce7c2005-09-09 16:23:55 -07001146 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* fail all writes first */
1148 bi = sh->dev[i].towrite;
1149 sh->dev[i].towrite = NULL;
NeilBrown934ce7c2005-09-09 16:23:55 -07001150 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1153 wake_up(&conf->wait_for_overlap);
1154
1155 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1156 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1157 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1158 if (--bi->bi_phys_segments == 0) {
1159 md_write_end(conf->mddev);
1160 bi->bi_next = return_bi;
1161 return_bi = bi;
1162 }
1163 bi = nextbi;
1164 }
1165 /* and fail all 'written' */
1166 bi = sh->dev[i].written;
1167 sh->dev[i].written = NULL;
NeilBrown934ce7c2005-09-09 16:23:55 -07001168 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1170 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1171 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1172 if (--bi->bi_phys_segments == 0) {
1173 md_write_end(conf->mddev);
1174 bi->bi_next = return_bi;
1175 return_bi = bi;
1176 }
1177 bi = bi2;
1178 }
1179
1180 /* fail any reads if this device is non-operational */
NeilBrownd69762e2006-01-06 00:20:18 -08001181 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1182 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 bi = sh->dev[i].toread;
1184 sh->dev[i].toread = NULL;
1185 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1186 wake_up(&conf->wait_for_overlap);
1187 if (bi) to_read--;
1188 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1189 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1190 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1191 if (--bi->bi_phys_segments == 0) {
1192 bi->bi_next = return_bi;
1193 return_bi = bi;
1194 }
1195 bi = nextbi;
1196 }
1197 }
NeilBrown934ce7c2005-09-09 16:23:55 -07001198 spin_unlock_irq(&conf->device_lock);
1199 if (bitmap_end)
1200 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1201 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204 if (failed > 2 && syncing) {
1205 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1206 clear_bit(STRIPE_SYNCING, &sh->state);
1207 syncing = 0;
1208 }
1209
1210 /*
1211 * might be able to return some write requests if the parity blocks
1212 * are safe, or on a failed drive
1213 */
1214 pdev = &sh->dev[pd_idx];
1215 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1216 || (failed >= 2 && failed_num[1] == pd_idx);
1217 qdev = &sh->dev[qd_idx];
1218 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1219 || (failed >= 2 && failed_num[1] == qd_idx);
1220
1221 if ( written &&
1222 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1223 && !test_bit(R5_LOCKED, &pdev->flags)
1224 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1225 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1226 && !test_bit(R5_LOCKED, &qdev->flags)
1227 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1228 /* any written block on an uptodate or failed drive can be
1229 * returned. Note that if we 'wrote' to a failed drive,
1230 * it will be UPTODATE, but never LOCKED, so we don't need
1231 * to test 'failed' directly.
1232 */
1233 for (i=disks; i--; )
1234 if (sh->dev[i].written) {
1235 dev = &sh->dev[i];
1236 if (!test_bit(R5_LOCKED, &dev->flags) &&
1237 test_bit(R5_UPTODATE, &dev->flags) ) {
1238 /* We can return any write requests */
NeilBrown934ce7c2005-09-09 16:23:55 -07001239 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct bio *wbi, *wbi2;
1241 PRINTK("Return write for stripe %llu disc %d\n",
1242 (unsigned long long)sh->sector, i);
1243 spin_lock_irq(&conf->device_lock);
1244 wbi = dev->written;
1245 dev->written = NULL;
1246 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1247 wbi2 = r5_next_bio(wbi, dev->sector);
1248 if (--wbi->bi_phys_segments == 0) {
1249 md_write_end(conf->mddev);
1250 wbi->bi_next = return_bi;
1251 return_bi = wbi;
1252 }
1253 wbi = wbi2;
1254 }
NeilBrown934ce7c2005-09-09 16:23:55 -07001255 if (dev->towrite == NULL)
1256 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 spin_unlock_irq(&conf->device_lock);
NeilBrown934ce7c2005-09-09 16:23:55 -07001258 if (bitmap_end)
1259 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1260 STRIPE_SECTORS,
1261 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 }
1264 }
1265
1266 /* Now we might consider reading some blocks, either to check/generate
1267 * parity, or to satisfy requests
1268 * or to load a block that is being partially written.
1269 */
1270 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1271 for (i=disks; i--;) {
1272 dev = &sh->dev[i];
1273 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1274 (dev->toread ||
1275 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1276 syncing ||
1277 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1278 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1279 )
1280 ) {
1281 /* we would like to get this block, possibly
1282 * by computing it, but we might not be able to
1283 */
1284 if (uptodate == disks-1) {
1285 PRINTK("Computing stripe %llu block %d\n",
1286 (unsigned long long)sh->sector, i);
NeilBrownca65b732006-01-06 00:20:17 -08001287 compute_block_1(sh, i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 uptodate++;
1289 } else if ( uptodate == disks-2 && failed >= 2 ) {
1290 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1291 int other;
1292 for (other=disks; other--;) {
1293 if ( other == i )
1294 continue;
1295 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1296 break;
1297 }
1298 BUG_ON(other < 0);
1299 PRINTK("Computing stripe %llu blocks %d,%d\n",
1300 (unsigned long long)sh->sector, i, other);
1301 compute_block_2(sh, i, other);
1302 uptodate += 2;
1303 } else if (test_bit(R5_Insync, &dev->flags)) {
1304 set_bit(R5_LOCKED, &dev->flags);
1305 set_bit(R5_Wantread, &dev->flags);
1306#if 0
1307 /* if I am just reading this block and we don't have
1308 a failed drive, or any pending writes then sidestep the cache */
1309 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1310 ! syncing && !failed && !to_write) {
1311 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1312 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1313 }
1314#endif
1315 locked++;
1316 PRINTK("Reading block %d (sync=%d)\n",
1317 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 }
1320 }
1321 set_bit(STRIPE_HANDLE, &sh->state);
1322 }
1323
1324 /* now to consider writing and what else, if anything should be read */
1325 if (to_write) {
1326 int rcw=0, must_compute=0;
1327 for (i=disks ; i--;) {
1328 dev = &sh->dev[i];
1329 /* Would I have to read this buffer for reconstruct_write */
1330 if (!test_bit(R5_OVERWRITE, &dev->flags)
1331 && i != pd_idx && i != qd_idx
1332 && (!test_bit(R5_LOCKED, &dev->flags)
1333#if 0
1334 || sh->bh_page[i] != bh->b_page
1335#endif
1336 ) &&
1337 !test_bit(R5_UPTODATE, &dev->flags)) {
1338 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1339 else {
1340 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1341 must_compute++;
1342 }
1343 }
1344 }
1345 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1346 (unsigned long long)sh->sector, rcw, must_compute);
1347 set_bit(STRIPE_HANDLE, &sh->state);
1348
1349 if (rcw > 0)
1350 /* want reconstruct write, but need to get some data */
1351 for (i=disks; i--;) {
1352 dev = &sh->dev[i];
1353 if (!test_bit(R5_OVERWRITE, &dev->flags)
1354 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1355 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1356 test_bit(R5_Insync, &dev->flags)) {
1357 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1358 {
1359 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1360 (unsigned long long)sh->sector, i);
1361 set_bit(R5_LOCKED, &dev->flags);
1362 set_bit(R5_Wantread, &dev->flags);
1363 locked++;
1364 } else {
1365 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1366 (unsigned long long)sh->sector, i);
1367 set_bit(STRIPE_DELAYED, &sh->state);
1368 set_bit(STRIPE_HANDLE, &sh->state);
1369 }
1370 }
1371 }
1372 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown934ce7c2005-09-09 16:23:55 -07001373 if (locked == 0 && rcw == 0 &&
1374 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if ( must_compute > 0 ) {
1376 /* We have failed blocks and need to compute them */
1377 switch ( failed ) {
1378 case 0: BUG();
NeilBrownca65b732006-01-06 00:20:17 -08001379 case 1: compute_block_1(sh, failed_num[0], 0); break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1381 default: BUG(); /* This request should have been failed? */
1382 }
1383 }
1384
1385 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1386 compute_parity(sh, RECONSTRUCT_WRITE);
1387 /* now every locked buffer is ready to be written */
1388 for (i=disks; i--;)
1389 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1390 PRINTK("Writing stripe %llu block %d\n",
1391 (unsigned long long)sh->sector, i);
1392 locked++;
1393 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
NeilBrownca65b732006-01-06 00:20:17 -08001395 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1396 set_bit(STRIPE_INSYNC, &sh->state);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1399 atomic_dec(&conf->preread_active_stripes);
1400 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1401 md_wakeup_thread(conf->mddev->thread);
1402 }
1403 }
1404 }
1405
1406 /* maybe we need to check and possibly fix the parity for this stripe
1407 * Any reads will already have been scheduled, so we just see if enough data
1408 * is available
1409 */
NeilBrownca65b732006-01-06 00:20:17 -08001410 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
1411 int update_p = 0, update_q = 0;
1412 struct r5dev *dev;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownca65b732006-01-06 00:20:17 -08001415
1416 BUG_ON(failed>2);
1417 BUG_ON(uptodate < disks);
1418 /* Want to check and possibly repair P and Q.
1419 * However there could be one 'failed' device, in which
1420 * case we can only check one of them, possibly using the
1421 * other to generate missing data
1422 */
1423
1424 /* If !tmp_page, we cannot do the calculations,
1425 * but as we have set STRIPE_HANDLE, we will soon be called
1426 * by stripe_handle with a tmp_page - just wait until then.
1427 */
1428 if (tmp_page) {
1429 if (failed == q_failed) {
1430 /* The only possible failed device holds 'Q', so it makes
1431 * sense to check P (If anything else were failed, we would
1432 * have used P to recreate it).
1433 */
1434 compute_block_1(sh, pd_idx, 1);
1435 if (!page_is_zero(sh->dev[pd_idx].page)) {
1436 compute_block_1(sh,pd_idx,0);
1437 update_p = 1;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
NeilBrownca65b732006-01-06 00:20:17 -08001440 if (!q_failed && failed < 2) {
1441 /* q is not failed, and we didn't use it to generate
1442 * anything, so it makes sense to check it
1443 */
1444 memcpy(page_address(tmp_page),
1445 page_address(sh->dev[qd_idx].page),
1446 STRIPE_SIZE);
1447 compute_parity(sh, UPDATE_PARITY);
1448 if (memcmp(page_address(tmp_page),
1449 page_address(sh->dev[qd_idx].page),
1450 STRIPE_SIZE)!= 0) {
1451 clear_bit(STRIPE_INSYNC, &sh->state);
1452 update_q = 1;
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
NeilBrownca65b732006-01-06 00:20:17 -08001455 if (update_p || update_q) {
1456 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1457 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1458 /* don't try to repair!! */
1459 update_p = update_q = 0;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
NeilBrownca65b732006-01-06 00:20:17 -08001462 /* now write out any block on a failed drive,
1463 * or P or Q if they need it
1464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
NeilBrownca65b732006-01-06 00:20:17 -08001466 if (failed == 2) {
1467 dev = &sh->dev[failed_num[1]];
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 (failed >= 1) {
1473 dev = &sh->dev[failed_num[0]];
1474 locked++;
1475 set_bit(R5_LOCKED, &dev->flags);
1476 set_bit(R5_Wantwrite, &dev->flags);
NeilBrownca65b732006-01-06 00:20:17 -08001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
NeilBrownca65b732006-01-06 00:20:17 -08001479 if (update_p) {
1480 dev = &sh->dev[pd_idx];
1481 locked ++;
1482 set_bit(R5_LOCKED, &dev->flags);
1483 set_bit(R5_Wantwrite, &dev->flags);
NeilBrownca65b732006-01-06 00:20:17 -08001484 }
1485 if (update_q) {
1486 dev = &sh->dev[qd_idx];
1487 locked++;
1488 set_bit(R5_LOCKED, &dev->flags);
1489 set_bit(R5_Wantwrite, &dev->flags);
NeilBrownca65b732006-01-06 00:20:17 -08001490 }
NeilBrown934ce7c2005-09-09 16:23:55 -07001491 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 }
NeilBrownca65b732006-01-06 00:20:17 -08001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1498 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1499 clear_bit(STRIPE_SYNCING, &sh->state);
1500 }
1501
NeilBrownd69762e2006-01-06 00:20:18 -08001502 /* If the failed drives are just a ReadError, then we might need
1503 * to progress the repair/check process
1504 */
1505 if (failed <= 2 && ! conf->mddev->ro)
1506 for (i=0; i<failed;i++) {
1507 dev = &sh->dev[failed_num[i]];
1508 if (test_bit(R5_ReadError, &dev->flags)
1509 && !test_bit(R5_LOCKED, &dev->flags)
1510 && test_bit(R5_UPTODATE, &dev->flags)
1511 ) {
1512 if (!test_bit(R5_ReWrite, &dev->flags)) {
1513 set_bit(R5_Wantwrite, &dev->flags);
1514 set_bit(R5_ReWrite, &dev->flags);
1515 set_bit(R5_LOCKED, &dev->flags);
1516 } else {
1517 /* let's read it back */
1518 set_bit(R5_Wantread, &dev->flags);
1519 set_bit(R5_LOCKED, &dev->flags);
1520 }
1521 }
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 spin_unlock(&sh->lock);
1524
1525 while ((bi=return_bi)) {
1526 int bytes = bi->bi_size;
1527
1528 return_bi = bi->bi_next;
1529 bi->bi_next = NULL;
1530 bi->bi_size = 0;
1531 bi->bi_end_io(bi, bytes, 0);
1532 }
1533 for (i=disks; i-- ;) {
1534 int rw;
1535 struct bio *bi;
1536 mdk_rdev_t *rdev;
1537 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1538 rw = 1;
1539 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1540 rw = 0;
1541 else
1542 continue;
1543
1544 bi = &sh->dev[i].req;
1545
1546 bi->bi_rw = rw;
1547 if (rw)
1548 bi->bi_end_io = raid6_end_write_request;
1549 else
1550 bi->bi_end_io = raid6_end_read_request;
1551
1552 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001553 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001554 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 rdev = NULL;
1556 if (rdev)
1557 atomic_inc(&rdev->nr_pending);
1558 rcu_read_unlock();
1559
1560 if (rdev) {
NeilBrown9910f162006-01-06 00:20:24 -08001561 if (syncing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1563
1564 bi->bi_bdev = rdev->bdev;
1565 PRINTK("for %llu schedule op %ld on disc %d\n",
1566 (unsigned long long)sh->sector, bi->bi_rw, i);
1567 atomic_inc(&sh->count);
1568 bi->bi_sector = sh->sector + rdev->data_offset;
1569 bi->bi_flags = 1 << BIO_UPTODATE;
1570 bi->bi_vcnt = 1;
1571 bi->bi_max_vecs = 1;
1572 bi->bi_idx = 0;
1573 bi->bi_io_vec = &sh->dev[i].vec;
1574 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1575 bi->bi_io_vec[0].bv_offset = 0;
1576 bi->bi_size = STRIPE_SIZE;
1577 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001578 if (rw == WRITE &&
1579 test_bit(R5_ReWrite, &sh->dev[i].flags))
1580 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 generic_make_request(bi);
1582 } else {
NeilBrown934ce7c2005-09-09 16:23:55 -07001583 if (rw == 1)
1584 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 PRINTK("skip op %ld on disc %d for sector %llu\n",
1586 bi->bi_rw, i, (unsigned long long)sh->sector);
1587 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1588 set_bit(STRIPE_HANDLE, &sh->state);
1589 }
1590 }
1591}
1592
Arjan van de Ven858119e2006-01-14 13:20:43 -08001593static void raid6_activate_delayed(raid6_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1596 while (!list_empty(&conf->delayed_list)) {
1597 struct list_head *l = conf->delayed_list.next;
1598 struct stripe_head *sh;
1599 sh = list_entry(l, struct stripe_head, lru);
1600 list_del_init(l);
1601 clear_bit(STRIPE_DELAYED, &sh->state);
1602 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1603 atomic_inc(&conf->preread_active_stripes);
1604 list_add_tail(&sh->lru, &conf->handle_list);
1605 }
1606 }
1607}
1608
Arjan van de Ven858119e2006-01-14 13:20:43 -08001609static void activate_bit_delay(raid6_conf_t *conf)
NeilBrown934ce7c2005-09-09 16:23:55 -07001610{
1611 /* device_lock is held */
1612 struct list_head head;
1613 list_add(&head, &conf->bitmap_list);
1614 list_del_init(&conf->bitmap_list);
1615 while (!list_empty(&head)) {
1616 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1617 list_del_init(&sh->lru);
1618 atomic_inc(&sh->count);
1619 __release_stripe(conf, sh);
1620 }
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static void unplug_slaves(mddev_t *mddev)
1624{
1625 raid6_conf_t *conf = mddev_to_conf(mddev);
1626 int i;
1627
1628 rcu_read_lock();
1629 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001630 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001631 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1633
1634 atomic_inc(&rdev->nr_pending);
1635 rcu_read_unlock();
1636
1637 if (r_queue->unplug_fn)
1638 r_queue->unplug_fn(r_queue);
1639
1640 rdev_dec_pending(rdev, mddev);
1641 rcu_read_lock();
1642 }
1643 }
1644 rcu_read_unlock();
1645}
1646
1647static void raid6_unplug_device(request_queue_t *q)
1648{
1649 mddev_t *mddev = q->queuedata;
1650 raid6_conf_t *conf = mddev_to_conf(mddev);
1651 unsigned long flags;
1652
1653 spin_lock_irqsave(&conf->device_lock, flags);
1654
NeilBrown934ce7c2005-09-09 16:23:55 -07001655 if (blk_remove_plug(q)) {
1656 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 raid6_activate_delayed(conf);
NeilBrown934ce7c2005-09-09 16:23:55 -07001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 md_wakeup_thread(mddev->thread);
1660
1661 spin_unlock_irqrestore(&conf->device_lock, flags);
1662
1663 unplug_slaves(mddev);
1664}
1665
1666static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1667 sector_t *error_sector)
1668{
1669 mddev_t *mddev = q->queuedata;
1670 raid6_conf_t *conf = mddev_to_conf(mddev);
1671 int i, ret = 0;
1672
1673 rcu_read_lock();
1674 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001675 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001676 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct block_device *bdev = rdev->bdev;
1678 request_queue_t *r_queue = bdev_get_queue(bdev);
1679
1680 if (!r_queue->issue_flush_fn)
1681 ret = -EOPNOTSUPP;
1682 else {
1683 atomic_inc(&rdev->nr_pending);
1684 rcu_read_unlock();
1685 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1686 error_sector);
1687 rdev_dec_pending(rdev, mddev);
1688 rcu_read_lock();
1689 }
1690 }
1691 }
1692 rcu_read_unlock();
1693 return ret;
1694}
1695
1696static inline void raid6_plug_device(raid6_conf_t *conf)
1697{
1698 spin_lock_irq(&conf->device_lock);
1699 blk_plug_device(conf->mddev->queue);
1700 spin_unlock_irq(&conf->device_lock);
1701}
1702
1703static int make_request (request_queue_t *q, struct bio * bi)
1704{
1705 mddev_t *mddev = q->queuedata;
1706 raid6_conf_t *conf = mddev_to_conf(mddev);
1707 const unsigned int raid_disks = conf->raid_disks;
1708 const unsigned int data_disks = raid_disks - 2;
1709 unsigned int dd_idx, pd_idx;
1710 sector_t new_sector;
1711 sector_t logical_sector, last_sector;
1712 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01001713 const int rw = bio_data_dir(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
NeilBrowne5dcdd82005-09-09 16:23:41 -07001715 if (unlikely(bio_barrier(bi))) {
1716 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1717 return 0;
1718 }
1719
NeilBrown3d310eb2005-06-21 17:17:26 -07001720 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07001721
Jens Axboea3623572005-11-01 09:26:16 +01001722 disk_stat_inc(mddev->gendisk, ios[rw]);
1723 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1726 last_sector = bi->bi_sector + (bi->bi_size>>9);
1727
1728 bi->bi_next = NULL;
1729 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1732 DEFINE_WAIT(w);
1733
1734 new_sector = raid6_compute_sector(logical_sector,
1735 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1736
1737 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1738 (unsigned long long)new_sector,
1739 (unsigned long long)logical_sector);
1740
1741 retry:
1742 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1743 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1744 if (sh) {
1745 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1746 /* Add failed due to overlap. Flush everything
1747 * and wait a while
1748 */
1749 raid6_unplug_device(mddev->queue);
1750 release_stripe(sh);
1751 schedule();
1752 goto retry;
1753 }
1754 finish_wait(&conf->wait_for_overlap, &w);
1755 raid6_plug_device(conf);
NeilBrownca65b732006-01-06 00:20:17 -08001756 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 release_stripe(sh);
1758 } else {
1759 /* cannot get stripe for read-ahead, just give-up */
1760 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1761 finish_wait(&conf->wait_for_overlap, &w);
1762 break;
1763 }
1764
1765 }
1766 spin_lock_irq(&conf->device_lock);
1767 if (--bi->bi_phys_segments == 0) {
1768 int bytes = bi->bi_size;
1769
Jens Axboea3623572005-11-01 09:26:16 +01001770 if (rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 md_write_end(mddev);
1772 bi->bi_size = 0;
1773 bi->bi_end_io(bi, bytes, 0);
1774 }
1775 spin_unlock_irq(&conf->device_lock);
1776 return 0;
1777}
1778
1779/* FIXME go_faster isn't used */
NeilBrown57afd892005-06-21 17:17:13 -07001780static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
1782 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1783 struct stripe_head *sh;
1784 int sectors_per_chunk = conf->chunk_size >> 9;
1785 sector_t x;
1786 unsigned long stripe;
1787 int chunk_offset;
1788 int dd_idx, pd_idx;
1789 sector_t first_sector;
1790 int raid_disks = conf->raid_disks;
1791 int data_disks = raid_disks - 2;
NeilBrown934ce7c2005-09-09 16:23:55 -07001792 sector_t max_sector = mddev->size << 1;
1793 int sync_blocks;
NeilBrownb5ab28a2005-11-28 13:44:11 -08001794 int still_degraded = 0;
1795 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
NeilBrown934ce7c2005-09-09 16:23:55 -07001797 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /* just being told to finish up .. nothing much to do */
1799 unplug_slaves(mddev);
NeilBrown934ce7c2005-09-09 16:23:55 -07001800
1801 if (mddev->curr_resync < max_sector) /* aborted */
1802 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1803 &sync_blocks, 1);
NeilBrownb5ab28a2005-11-28 13:44:11 -08001804 else /* completed sync */
NeilBrown934ce7c2005-09-09 16:23:55 -07001805 conf->fullsync = 0;
1806 bitmap_close_sync(mddev->bitmap);
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809 }
1810 /* if there are 2 or more failed drives and we are trying
1811 * to resync, then assert that we are finished, because there is
1812 * nothing we can do.
1813 */
1814 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07001815 sector_t rv = (mddev->size << 1) - sector_nr;
1816 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return rv;
1818 }
NeilBrown934ce7c2005-09-09 16:23:55 -07001819 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrownca65b732006-01-06 00:20:17 -08001820 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown934ce7c2005-09-09 16:23:55 -07001821 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1822 /* we can skip this block, and probably more */
1823 sync_blocks /= STRIPE_SECTORS;
1824 *skipped = 1;
1825 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 x = sector_nr;
1829 chunk_offset = sector_div(x, sectors_per_chunk);
1830 stripe = x;
1831 BUG_ON(x != stripe);
1832
1833 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1834 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1835 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1836 if (sh == NULL) {
1837 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1838 /* make sure we don't swamp the stripe cache if someone else
1839 * is trying to get access
1840 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08001841 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
NeilBrownb5ab28a2005-11-28 13:44:11 -08001843 /* Need to check if array will still be degraded after recovery/resync
1844 * We don't need to check the 'failed' flag as when that gets set,
1845 * recovery aborts.
1846 */
1847 for (i=0; i<mddev->raid_disks; i++)
1848 if (conf->disks[i].rdev == NULL)
1849 still_degraded = 1;
1850
1851 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 spin_lock(&sh->lock);
1854 set_bit(STRIPE_SYNCING, &sh->state);
1855 clear_bit(STRIPE_INSYNC, &sh->state);
1856 spin_unlock(&sh->lock);
1857
NeilBrownca65b732006-01-06 00:20:17 -08001858 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 release_stripe(sh);
1860
1861 return STRIPE_SECTORS;
1862}
1863
1864/*
1865 * This is our raid6 kernel thread.
1866 *
1867 * We scan the hash table for stripes which can be handled now.
1868 * During the scan, completed stripes are saved for us by the interrupt
1869 * handler, so that they will not have to wait for our next wakeup.
1870 */
1871static void raid6d (mddev_t *mddev)
1872{
1873 struct stripe_head *sh;
1874 raid6_conf_t *conf = mddev_to_conf(mddev);
1875 int handled;
1876
1877 PRINTK("+++ raid6d active\n");
1878
1879 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 handled = 0;
1882 spin_lock_irq(&conf->device_lock);
1883 while (1) {
1884 struct list_head *first;
1885
NeilBrown934ce7c2005-09-09 16:23:55 -07001886 if (conf->seq_flush - conf->seq_write > 0) {
1887 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08001888 spin_unlock_irq(&conf->device_lock);
NeilBrown934ce7c2005-09-09 16:23:55 -07001889 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08001890 spin_lock_irq(&conf->device_lock);
NeilBrown934ce7c2005-09-09 16:23:55 -07001891 conf->seq_write = seq;
1892 activate_bit_delay(conf);
1893 }
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (list_empty(&conf->handle_list) &&
1896 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1897 !blk_queue_plugged(mddev->queue) &&
1898 !list_empty(&conf->delayed_list))
1899 raid6_activate_delayed(conf);
1900
1901 if (list_empty(&conf->handle_list))
1902 break;
1903
1904 first = conf->handle_list.next;
1905 sh = list_entry(first, struct stripe_head, lru);
1906
1907 list_del_init(first);
1908 atomic_inc(&sh->count);
1909 if (atomic_read(&sh->count)!= 1)
1910 BUG();
1911 spin_unlock_irq(&conf->device_lock);
1912
1913 handled++;
NeilBrownca65b732006-01-06 00:20:17 -08001914 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 release_stripe(sh);
1916
1917 spin_lock_irq(&conf->device_lock);
1918 }
1919 PRINTK("%d stripes handled\n", handled);
1920
1921 spin_unlock_irq(&conf->device_lock);
1922
1923 unplug_slaves(mddev);
1924
1925 PRINTK("--- raid6d inactive\n");
1926}
1927
NeilBrown35849c72006-02-02 14:28:06 -08001928static ssize_t
1929raid6_show_stripe_cache_size(mddev_t *mddev, char *page)
1930{
1931 raid6_conf_t *conf = mddev_to_conf(mddev);
1932 if (conf)
1933 return sprintf(page, "%d\n", conf->max_nr_stripes);
1934 else
1935 return 0;
1936}
1937
1938static ssize_t
1939raid6_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
1940{
1941 raid6_conf_t *conf = mddev_to_conf(mddev);
1942 char *end;
1943 int new;
1944 if (len >= PAGE_SIZE)
1945 return -EINVAL;
1946 if (!conf)
1947 return -ENODEV;
1948
1949 new = simple_strtoul(page, &end, 10);
1950 if (!*page || (*end && *end != '\n') )
1951 return -EINVAL;
1952 if (new <= 16 || new > 32768)
1953 return -EINVAL;
1954 while (new < conf->max_nr_stripes) {
1955 if (drop_one_stripe(conf))
1956 conf->max_nr_stripes--;
1957 else
1958 break;
1959 }
1960 while (new > conf->max_nr_stripes) {
1961 if (grow_one_stripe(conf))
1962 conf->max_nr_stripes++;
1963 else break;
1964 }
1965 return len;
1966}
1967
1968static struct md_sysfs_entry
1969raid6_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
1970 raid6_show_stripe_cache_size,
1971 raid6_store_stripe_cache_size);
1972
1973static ssize_t
1974stripe_cache_active_show(mddev_t *mddev, char *page)
1975{
1976 raid6_conf_t *conf = mddev_to_conf(mddev);
1977 if (conf)
1978 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
1979 else
1980 return 0;
1981}
1982
1983static struct md_sysfs_entry
1984raid6_stripecache_active = __ATTR_RO(stripe_cache_active);
1985
1986static struct attribute *raid6_attrs[] = {
1987 &raid6_stripecache_size.attr,
1988 &raid6_stripecache_active.attr,
1989 NULL,
1990};
1991static struct attribute_group raid6_attrs_group = {
1992 .name = NULL,
1993 .attrs = raid6_attrs,
1994};
1995
NeilBrown934ce7c2005-09-09 16:23:55 -07001996static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 raid6_conf_t *conf;
1999 int raid_disk, memory;
2000 mdk_rdev_t *rdev;
2001 struct disk_info *disk;
2002 struct list_head *tmp;
2003
2004 if (mddev->level != 6) {
2005 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
2006 return -EIO;
2007 }
2008
NeilBrownb55e6bf2006-03-27 01:18:06 -08002009 mddev->private = kzalloc(sizeof (raid6_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if ((conf = mddev->private) == NULL)
2011 goto abort;
NeilBrownb55e6bf2006-03-27 01:18:06 -08002012 conf->disks = kzalloc(mddev->raid_disks * sizeof(struct disk_info),
2013 GFP_KERNEL);
2014 if (!conf->disks)
2015 goto abort;
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 conf->mddev = mddev;
2018
NeilBrownfccddba2006-01-06 00:20:33 -08002019 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
NeilBrownca65b732006-01-06 00:20:17 -08002022 conf->spare_page = alloc_page(GFP_KERNEL);
2023 if (!conf->spare_page)
2024 goto abort;
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 spin_lock_init(&conf->device_lock);
2027 init_waitqueue_head(&conf->wait_for_stripe);
2028 init_waitqueue_head(&conf->wait_for_overlap);
2029 INIT_LIST_HEAD(&conf->handle_list);
2030 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown934ce7c2005-09-09 16:23:55 -07002031 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 INIT_LIST_HEAD(&conf->inactive_list);
2033 atomic_set(&conf->active_stripes, 0);
2034 atomic_set(&conf->preread_active_stripes, 0);
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
2037
2038 ITERATE_RDEV(mddev,rdev,tmp) {
2039 raid_disk = rdev->raid_disk;
2040 if (raid_disk >= mddev->raid_disks
2041 || raid_disk < 0)
2042 continue;
2043 disk = conf->disks + raid_disk;
2044
2045 disk->rdev = rdev;
2046
NeilBrownb2d444d2005-11-08 21:39:31 -08002047 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 char b[BDEVNAME_SIZE];
2049 printk(KERN_INFO "raid6: device %s operational as raid"
2050 " disk %d\n", bdevname(rdev->bdev,b),
2051 raid_disk);
2052 conf->working_disks++;
2053 }
2054 }
2055
2056 conf->raid_disks = mddev->raid_disks;
2057
2058 /*
2059 * 0 for a fully functional array, 1 or 2 for a degraded array.
2060 */
2061 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
2062 conf->mddev = mddev;
2063 conf->chunk_size = mddev->chunk_size;
2064 conf->level = mddev->level;
2065 conf->algorithm = mddev->layout;
2066 conf->max_nr_stripes = NR_STRIPES;
2067
2068 /* device size must be a multiple of chunk size */
2069 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07002070 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (conf->raid_disks < 4) {
2073 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
2074 mdname(mddev), conf->raid_disks);
2075 goto abort;
2076 }
2077 if (!conf->chunk_size || conf->chunk_size % 4) {
2078 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
2079 conf->chunk_size, mdname(mddev));
2080 goto abort;
2081 }
2082 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2083 printk(KERN_ERR
2084 "raid6: unsupported parity algorithm %d for %s\n",
2085 conf->algorithm, mdname(mddev));
2086 goto abort;
2087 }
2088 if (mddev->degraded > 2) {
2089 printk(KERN_ERR "raid6: not enough operational devices for %s"
2090 " (%d/%d failed)\n",
2091 mdname(mddev), conf->failed_disks, conf->raid_disks);
2092 goto abort;
2093 }
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (mddev->degraded > 0 &&
2096 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002097 if (mddev->ok_start_degraded)
2098 printk(KERN_WARNING "raid6: starting dirty degraded array:%s"
2099 "- data corruption possible.\n",
2100 mdname(mddev));
2101 else {
2102 printk(KERN_ERR "raid6: cannot start dirty degraded array"
2103 " for %s\n", mdname(mddev));
2104 goto abort;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 {
2109 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
2110 if (!mddev->thread) {
2111 printk(KERN_ERR
2112 "raid6: couldn't allocate thread for %s\n",
2113 mdname(mddev));
2114 goto abort;
2115 }
2116 }
2117
2118 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2119 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2120 if (grow_stripes(conf, conf->max_nr_stripes)) {
2121 printk(KERN_ERR
2122 "raid6: couldn't allocate %dkB for buffers\n", memory);
2123 shrink_stripes(conf);
2124 md_unregister_thread(mddev->thread);
2125 goto abort;
2126 } else
2127 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
2128 memory, mdname(mddev));
2129
2130 if (mddev->degraded == 0)
2131 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
2132 " devices, algorithm %d\n", conf->level, mdname(mddev),
2133 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2134 conf->algorithm);
2135 else
2136 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
2137 " out of %d devices, algorithm %d\n", conf->level,
2138 mdname(mddev), mddev->raid_disks - mddev->degraded,
2139 mddev->raid_disks, conf->algorithm);
2140
2141 print_raid6_conf(conf);
2142
2143 /* read-ahead size must cover two whole stripes, which is
2144 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2145 */
2146 {
2147 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
NeilBrown2d1f3b52006-01-06 00:20:31 -08002148 / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2150 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2151 }
2152
2153 /* Ok, everything is just fine now */
NeilBrownec350a72006-03-31 02:32:06 -08002154 sysfs_create_group(&mddev->kobj, &raid6_attrs_group);
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
NeilBrown7a5febe2005-05-16 21:53:16 -07002157
2158 mddev->queue->unplug_fn = raid6_unplug_device;
2159 mddev->queue->issue_flush_fn = raid6_issue_flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161abort:
2162 if (conf) {
2163 print_raid6_conf(conf);
NeilBrown1345b1d2006-01-06 00:20:40 -08002164 safe_put_page(conf->spare_page);
NeilBrownfccddba2006-01-06 00:20:33 -08002165 kfree(conf->stripe_hashtbl);
NeilBrownb55e6bf2006-03-27 01:18:06 -08002166 kfree(conf->disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 kfree(conf);
2168 }
2169 mddev->private = NULL;
2170 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
2171 return -EIO;
2172}
2173
2174
2175
2176static int stop (mddev_t *mddev)
2177{
2178 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2179
2180 md_unregister_thread(mddev->thread);
2181 mddev->thread = NULL;
2182 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08002183 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown35849c72006-02-02 14:28:06 -08002185 sysfs_remove_group(&mddev->kobj, &raid6_attrs_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 kfree(conf);
2187 mddev->private = NULL;
2188 return 0;
2189}
2190
2191#if RAID6_DUMPSTATE
2192static void print_sh (struct seq_file *seq, struct stripe_head *sh)
2193{
2194 int i;
2195
2196 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
2197 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2198 seq_printf(seq, "sh %llu, count %d.\n",
2199 (unsigned long long)sh->sector, atomic_read(&sh->count));
2200 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
2201 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2202 seq_printf(seq, "(cache%d: %p %ld) ",
2203 i, sh->dev[i].page, sh->dev[i].flags);
2204 }
2205 seq_printf(seq, "\n");
2206}
2207
2208static void printall (struct seq_file *seq, raid6_conf_t *conf)
2209{
2210 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08002211 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int i;
2213
2214 spin_lock_irq(&conf->device_lock);
2215 for (i = 0; i < NR_HASH; i++) {
2216 sh = conf->stripe_hashtbl[i];
NeilBrownfccddba2006-01-06 00:20:33 -08002217 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (sh->raid_conf != conf)
2219 continue;
2220 print_sh(seq, sh);
2221 }
2222 }
2223 spin_unlock_irq(&conf->device_lock);
2224}
2225#endif
2226
2227static void status (struct seq_file *seq, mddev_t *mddev)
2228{
2229 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2230 int i;
2231
2232 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2233 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2234 for (i = 0; i < conf->raid_disks; i++)
2235 seq_printf (seq, "%s",
2236 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08002237 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 seq_printf (seq, "]");
2239#if RAID6_DUMPSTATE
2240 seq_printf (seq, "\n");
2241 printall(seq, conf);
2242#endif
2243}
2244
2245static void print_raid6_conf (raid6_conf_t *conf)
2246{
2247 int i;
2248 struct disk_info *tmp;
2249
2250 printk("RAID6 conf printout:\n");
2251 if (!conf) {
2252 printk("(conf==NULL)\n");
2253 return;
2254 }
2255 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2256 conf->working_disks, conf->failed_disks);
2257
2258 for (i = 0; i < conf->raid_disks; i++) {
2259 char b[BDEVNAME_SIZE];
2260 tmp = conf->disks + i;
2261 if (tmp->rdev)
2262 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08002263 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 bdevname(tmp->rdev->bdev,b));
2265 }
2266}
2267
2268static int raid6_spare_active(mddev_t *mddev)
2269{
2270 int i;
2271 raid6_conf_t *conf = mddev->private;
2272 struct disk_info *tmp;
2273
2274 for (i = 0; i < conf->raid_disks; i++) {
2275 tmp = conf->disks + i;
2276 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08002277 && !test_bit(Faulty, &tmp->rdev->flags)
2278 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 mddev->degraded--;
2280 conf->failed_disks--;
2281 conf->working_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002282 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284 }
2285 print_raid6_conf(conf);
2286 return 0;
2287}
2288
2289static int raid6_remove_disk(mddev_t *mddev, int number)
2290{
2291 raid6_conf_t *conf = mddev->private;
2292 int err = 0;
2293 mdk_rdev_t *rdev;
2294 struct disk_info *p = conf->disks + number;
2295
2296 print_raid6_conf(conf);
2297 rdev = p->rdev;
2298 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002299 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 atomic_read(&rdev->nr_pending)) {
2301 err = -EBUSY;
2302 goto abort;
2303 }
2304 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002305 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (atomic_read(&rdev->nr_pending)) {
2307 /* lost the race, try later */
2308 err = -EBUSY;
2309 p->rdev = rdev;
2310 }
2311 }
2312
2313abort:
2314
2315 print_raid6_conf(conf);
2316 return err;
2317}
2318
2319static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2320{
2321 raid6_conf_t *conf = mddev->private;
2322 int found = 0;
2323 int disk;
2324 struct disk_info *p;
2325
2326 if (mddev->degraded > 2)
2327 /* no point adding a device */
2328 return 0;
2329 /*
NeilBrown6aea114a2005-11-28 13:44:13 -08002330 * find the disk ... but prefer rdev->saved_raid_disk
2331 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
NeilBrown6aea114a2005-11-28 13:44:13 -08002333 if (rdev->saved_raid_disk >= 0 &&
2334 conf->disks[rdev->saved_raid_disk].rdev == NULL)
2335 disk = rdev->saved_raid_disk;
2336 else
2337 disk = 0;
2338 for ( ; disk < mddev->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002340 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rdev->raid_disk = disk;
2342 found = 1;
NeilBrown934ce7c2005-09-09 16:23:55 -07002343 if (rdev->saved_raid_disk != disk)
2344 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08002345 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 break;
2347 }
2348 print_raid6_conf(conf);
2349 return found;
2350}
2351
2352static int raid6_resize(mddev_t *mddev, sector_t sectors)
2353{
2354 /* no resync is happening, and there is enough space
2355 * on all devices, so we can resize.
2356 * We need to make sure resync covers any new space.
2357 * If the array is shrinking we should possibly wait until
2358 * any io in the removed space completes, but it hardly seems
2359 * worth it.
2360 */
2361 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2362 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2363 set_capacity(mddev->gendisk, mddev->array_size << 1);
2364 mddev->changed = 1;
2365 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2366 mddev->recovery_cp = mddev->size << 1;
2367 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2368 }
2369 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002370 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372}
2373
NeilBrown934ce7c2005-09-09 16:23:55 -07002374static void raid6_quiesce(mddev_t *mddev, int state)
2375{
2376 raid6_conf_t *conf = mddev_to_conf(mddev);
2377
2378 switch(state) {
2379 case 1: /* stop all writes */
2380 spin_lock_irq(&conf->device_lock);
2381 conf->quiesce = 1;
2382 wait_event_lock_irq(conf->wait_for_stripe,
2383 atomic_read(&conf->active_stripes) == 0,
2384 conf->device_lock, /* nothing */);
2385 spin_unlock_irq(&conf->device_lock);
2386 break;
2387
2388 case 0: /* re-enable writes */
2389 spin_lock_irq(&conf->device_lock);
2390 conf->quiesce = 0;
2391 wake_up(&conf->wait_for_stripe);
2392 spin_unlock_irq(&conf->device_lock);
2393 break;
2394 }
NeilBrown934ce7c2005-09-09 16:23:55 -07002395}
NeilBrownb15c2e52006-01-06 00:20:16 -08002396
NeilBrown2604b702006-01-06 00:20:36 -08002397static struct mdk_personality raid6_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 .name = "raid6",
NeilBrown2604b702006-01-06 00:20:36 -08002400 .level = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .owner = THIS_MODULE,
2402 .make_request = make_request,
2403 .run = run,
2404 .stop = stop,
2405 .status = status,
2406 .error_handler = error,
2407 .hot_add_disk = raid6_add_disk,
2408 .hot_remove_disk= raid6_remove_disk,
2409 .spare_active = raid6_spare_active,
2410 .sync_request = sync_request,
2411 .resize = raid6_resize,
NeilBrown934ce7c2005-09-09 16:23:55 -07002412 .quiesce = raid6_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413};
2414
NeilBrown2604b702006-01-06 00:20:36 -08002415static int __init raid6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 int e;
2418
2419 e = raid6_select_algo();
2420 if ( e )
2421 return e;
2422
NeilBrown2604b702006-01-06 00:20:36 -08002423 return register_md_personality(&raid6_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426static void raid6_exit (void)
2427{
NeilBrown2604b702006-01-06 00:20:36 -08002428 unregister_md_personality(&raid6_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431module_init(raid6_init);
2432module_exit(raid6_exit);
2433MODULE_LICENSE("GPL");
2434MODULE_ALIAS("md-personality-8"); /* RAID6 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002435MODULE_ALIAS("md-raid6");
NeilBrown2604b702006-01-06 00:20:36 -08002436MODULE_ALIAS("md-level-6");