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