vserver 1.9.3
[linux-2.6.git] / drivers / md / raid5.c
1 /*
2  * raid5.c : Multiple Devices driver for Linux
3  *         Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4  *         Copyright (C) 1999, 2000 Ingo Molnar
5  *
6  * RAID-5 management functions.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * You should have received a copy of the GNU General Public License
14  * (for example /usr/src/linux/COPYING); if not, write to the Free
15  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/raid/raid5.h>
23 #include <linux/highmem.h>
24 #include <asm/bitops.h>
25 #include <asm/atomic.h>
26
27 /*
28  * Stripe cache
29  */
30
31 #define NR_STRIPES              256
32 #define STRIPE_SIZE             PAGE_SIZE
33 #define STRIPE_SHIFT            (PAGE_SHIFT - 9)
34 #define STRIPE_SECTORS          (STRIPE_SIZE>>9)
35 #define IO_THRESHOLD            1
36 #define HASH_PAGES              1
37 #define HASH_PAGES_ORDER        0
38 #define NR_HASH                 (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
39 #define HASH_MASK               (NR_HASH - 1)
40
41 #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
42
43 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
44  * order without overlap.  There may be several bio's per stripe+device, and
45  * a bio could span several devices.
46  * When walking this list for a particular stripe+device, we must never proceed
47  * beyond a bio that extends past this device, as the next bio might no longer
48  * be valid.
49  * This macro is used to determine the 'next' bio in the list, given the sector
50  * of the current stripe+device
51  */
52 #define r5_next_bio(bio, sect) ( ( bio->bi_sector + (bio->bi_size>>9) < sect + STRIPE_SECTORS) ? bio->bi_next : NULL)
53 /*
54  * The following can be used to debug the driver
55  */
56 #define RAID5_DEBUG     0
57 #define RAID5_PARANOIA  1
58 #if RAID5_PARANOIA && defined(CONFIG_SMP)
59 # define CHECK_DEVLOCK() if (!spin_is_locked(&conf->device_lock)) BUG()
60 #else
61 # define CHECK_DEVLOCK()
62 #endif
63
64 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
65 #if RAID5_DEBUG
66 #define inline
67 #define __inline__
68 #endif
69
70 static void print_raid5_conf (raid5_conf_t *conf);
71
72 static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
73 {
74         if (atomic_dec_and_test(&sh->count)) {
75                 if (!list_empty(&sh->lru))
76                         BUG();
77                 if (atomic_read(&conf->active_stripes)==0)
78                         BUG();
79                 if (test_bit(STRIPE_HANDLE, &sh->state)) {
80                         if (test_bit(STRIPE_DELAYED, &sh->state))
81                                 list_add_tail(&sh->lru, &conf->delayed_list);
82                         else
83                                 list_add_tail(&sh->lru, &conf->handle_list);
84                         md_wakeup_thread(conf->mddev->thread);
85                 } else {
86                         if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
87                                 atomic_dec(&conf->preread_active_stripes);
88                                 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
89                                         md_wakeup_thread(conf->mddev->thread);
90                         }
91                         list_add_tail(&sh->lru, &conf->inactive_list);
92                         atomic_dec(&conf->active_stripes);
93                         if (!conf->inactive_blocked ||
94                             atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
95                                 wake_up(&conf->wait_for_stripe);
96                 }
97         }
98 }
99 static void release_stripe(struct stripe_head *sh)
100 {
101         raid5_conf_t *conf = sh->raid_conf;
102         unsigned long flags;
103         
104         spin_lock_irqsave(&conf->device_lock, flags);
105         __release_stripe(conf, sh);
106         spin_unlock_irqrestore(&conf->device_lock, flags);
107 }
108
109 static void remove_hash(struct stripe_head *sh)
110 {
111         PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
112
113         if (sh->hash_pprev) {
114                 if (sh->hash_next)
115                         sh->hash_next->hash_pprev = sh->hash_pprev;
116                 *sh->hash_pprev = sh->hash_next;
117                 sh->hash_pprev = NULL;
118         }
119 }
120
121 static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
122 {
123         struct stripe_head **shp = &stripe_hash(conf, sh->sector);
124
125         PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
126
127         CHECK_DEVLOCK();
128         if ((sh->hash_next = *shp) != NULL)
129                 (*shp)->hash_pprev = &sh->hash_next;
130         *shp = sh;
131         sh->hash_pprev = shp;
132 }
133
134
135 /* find an idle stripe, make sure it is unhashed, and return it. */
136 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
137 {
138         struct stripe_head *sh = NULL;
139         struct list_head *first;
140
141         CHECK_DEVLOCK();
142         if (list_empty(&conf->inactive_list))
143                 goto out;
144         first = conf->inactive_list.next;
145         sh = list_entry(first, struct stripe_head, lru);
146         list_del_init(first);
147         remove_hash(sh);
148         atomic_inc(&conf->active_stripes);
149 out:
150         return sh;
151 }
152
153 static void shrink_buffers(struct stripe_head *sh, int num)
154 {
155         struct page *p;
156         int i;
157
158         for (i=0; i<num ; i++) {
159                 p = sh->dev[i].page;
160                 if (!p)
161                         continue;
162                 sh->dev[i].page = NULL;
163                 page_cache_release(p);
164         }
165 }
166
167 static int grow_buffers(struct stripe_head *sh, int num)
168 {
169         int i;
170
171         for (i=0; i<num; i++) {
172                 struct page *page;
173
174                 if (!(page = alloc_page(GFP_KERNEL))) {
175                         return 1;
176                 }
177                 sh->dev[i].page = page;
178         }
179         return 0;
180 }
181
182 static void raid5_build_block (struct stripe_head *sh, int i);
183
184 static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
185 {
186         raid5_conf_t *conf = sh->raid_conf;
187         int disks = conf->raid_disks, i;
188
189         if (atomic_read(&sh->count) != 0)
190                 BUG();
191         if (test_bit(STRIPE_HANDLE, &sh->state))
192                 BUG();
193         
194         CHECK_DEVLOCK();
195         PRINTK("init_stripe called, stripe %llu\n", 
196                 (unsigned long long)sh->sector);
197
198         remove_hash(sh);
199         
200         sh->sector = sector;
201         sh->pd_idx = pd_idx;
202         sh->state = 0;
203
204         for (i=disks; i--; ) {
205                 struct r5dev *dev = &sh->dev[i];
206
207                 if (dev->toread || dev->towrite || dev->written ||
208                     test_bit(R5_LOCKED, &dev->flags)) {
209                         printk("sector=%llx i=%d %p %p %p %d\n",
210                                (unsigned long long)sh->sector, i, dev->toread,
211                                dev->towrite, dev->written,
212                                test_bit(R5_LOCKED, &dev->flags));
213                         BUG();
214                 }
215                 dev->flags = 0;
216                 raid5_build_block(sh, i);
217         }
218         insert_hash(conf, sh);
219 }
220
221 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector)
222 {
223         struct stripe_head *sh;
224
225         CHECK_DEVLOCK();
226         PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
227         for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
228                 if (sh->sector == sector)
229                         return sh;
230         PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
231         return NULL;
232 }
233
234 static void unplug_slaves(mddev_t *mddev);
235
236 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector,
237                                              int pd_idx, int noblock) 
238 {
239         struct stripe_head *sh;
240
241         PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
242
243         spin_lock_irq(&conf->device_lock);
244
245         do {
246                 sh = __find_stripe(conf, sector);
247                 if (!sh) {
248                         if (!conf->inactive_blocked)
249                                 sh = get_free_stripe(conf);
250                         if (noblock && sh == NULL)
251                                 break;
252                         if (!sh) {
253                                 conf->inactive_blocked = 1;
254                                 wait_event_lock_irq(conf->wait_for_stripe,
255                                                     !list_empty(&conf->inactive_list) &&
256                                                     (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
257                                                      || !conf->inactive_blocked),
258                                                     conf->device_lock,
259                                                     unplug_slaves(conf->mddev);
260                                         );
261                                 conf->inactive_blocked = 0;
262                         } else
263                                 init_stripe(sh, sector, pd_idx);
264                 } else {
265                         if (atomic_read(&sh->count)) {
266                                 if (!list_empty(&sh->lru))
267                                         BUG();
268                         } else {
269                                 if (!test_bit(STRIPE_HANDLE, &sh->state))
270                                         atomic_inc(&conf->active_stripes);
271                                 if (list_empty(&sh->lru))
272                                         BUG();
273                                 list_del_init(&sh->lru);
274                         }
275                 }
276         } while (sh == NULL);
277
278         if (sh)
279                 atomic_inc(&sh->count);
280
281         spin_unlock_irq(&conf->device_lock);
282         return sh;
283 }
284
285 static int grow_stripes(raid5_conf_t *conf, int num)
286 {
287         struct stripe_head *sh;
288         kmem_cache_t *sc;
289         int devs = conf->raid_disks;
290
291         sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
292
293         sc = kmem_cache_create(conf->cache_name, 
294                                sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
295                                0, 0, NULL, NULL);
296         if (!sc)
297                 return 1;
298         conf->slab_cache = sc;
299         while (num--) {
300                 sh = kmem_cache_alloc(sc, GFP_KERNEL);
301                 if (!sh)
302                         return 1;
303                 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
304                 sh->raid_conf = conf;
305                 sh->lock = SPIN_LOCK_UNLOCKED;
306
307                 if (grow_buffers(sh, conf->raid_disks)) {
308                         shrink_buffers(sh, conf->raid_disks);
309                         kmem_cache_free(sc, sh);
310                         return 1;
311                 }
312                 /* we just created an active stripe so... */
313                 atomic_set(&sh->count, 1);
314                 atomic_inc(&conf->active_stripes);
315                 INIT_LIST_HEAD(&sh->lru);
316                 release_stripe(sh);
317         }
318         return 0;
319 }
320
321 static void shrink_stripes(raid5_conf_t *conf)
322 {
323         struct stripe_head *sh;
324
325         while (1) {
326                 spin_lock_irq(&conf->device_lock);
327                 sh = get_free_stripe(conf);
328                 spin_unlock_irq(&conf->device_lock);
329                 if (!sh)
330                         break;
331                 if (atomic_read(&sh->count))
332                         BUG();
333                 shrink_buffers(sh, conf->raid_disks);
334                 kmem_cache_free(conf->slab_cache, sh);
335                 atomic_dec(&conf->active_stripes);
336         }
337         kmem_cache_destroy(conf->slab_cache);
338         conf->slab_cache = NULL;
339 }
340
341 static int raid5_end_read_request (struct bio * bi, unsigned int bytes_done,
342                                    int error)
343 {
344         struct stripe_head *sh = bi->bi_private;
345         raid5_conf_t *conf = sh->raid_conf;
346         int disks = conf->raid_disks, i;
347         int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
348
349         if (bi->bi_size)
350                 return 1;
351
352         for (i=0 ; i<disks; i++)
353                 if (bi == &sh->dev[i].req)
354                         break;
355
356         PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", 
357                 (unsigned long long)sh->sector, i, atomic_read(&sh->count), 
358                 uptodate);
359         if (i == disks) {
360                 BUG();
361                 return 0;
362         }
363
364         if (uptodate) {
365 #if 0
366                 struct bio *bio;
367                 unsigned long flags;
368                 spin_lock_irqsave(&conf->device_lock, flags);
369                 /* we can return a buffer if we bypassed the cache or
370                  * if the top buffer is not in highmem.  If there are
371                  * multiple buffers, leave the extra work to
372                  * handle_stripe
373                  */
374                 buffer = sh->bh_read[i];
375                 if (buffer &&
376                     (!PageHighMem(buffer->b_page)
377                      || buffer->b_page == bh->b_page )
378                         ) {
379                         sh->bh_read[i] = buffer->b_reqnext;
380                         buffer->b_reqnext = NULL;
381                 } else
382                         buffer = NULL;
383                 spin_unlock_irqrestore(&conf->device_lock, flags);
384                 if (sh->bh_page[i]==bh->b_page)
385                         set_buffer_uptodate(bh);
386                 if (buffer) {
387                         if (buffer->b_page != bh->b_page)
388                                 memcpy(buffer->b_data, bh->b_data, bh->b_size);
389                         buffer->b_end_io(buffer, 1);
390                 }
391 #else
392                 set_bit(R5_UPTODATE, &sh->dev[i].flags);
393 #endif          
394         } else {
395                 md_error(conf->mddev, conf->disks[i].rdev);
396                 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
397         }
398         rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
399 #if 0
400         /* must restore b_page before unlocking buffer... */
401         if (sh->bh_page[i] != bh->b_page) {
402                 bh->b_page = sh->bh_page[i];
403                 bh->b_data = page_address(bh->b_page);
404                 clear_buffer_uptodate(bh);
405         }
406 #endif
407         clear_bit(R5_LOCKED, &sh->dev[i].flags);
408         set_bit(STRIPE_HANDLE, &sh->state);
409         release_stripe(sh);
410         return 0;
411 }
412
413 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
414                                     int error)
415 {
416         struct stripe_head *sh = bi->bi_private;
417         raid5_conf_t *conf = sh->raid_conf;
418         int disks = conf->raid_disks, i;
419         unsigned long flags;
420         int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
421
422         if (bi->bi_size)
423                 return 1;
424
425         for (i=0 ; i<disks; i++)
426                 if (bi == &sh->dev[i].req)
427                         break;
428
429         PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", 
430                 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
431                 uptodate);
432         if (i == disks) {
433                 BUG();
434                 return 0;
435         }
436
437         spin_lock_irqsave(&conf->device_lock, flags);
438         if (!uptodate)
439                 md_error(conf->mddev, conf->disks[i].rdev);
440
441         rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
442         
443         clear_bit(R5_LOCKED, &sh->dev[i].flags);
444         set_bit(STRIPE_HANDLE, &sh->state);
445         __release_stripe(conf, sh);
446         spin_unlock_irqrestore(&conf->device_lock, flags);
447         return 0;
448 }
449
450
451 static sector_t compute_blocknr(struct stripe_head *sh, int i);
452         
453 static void raid5_build_block (struct stripe_head *sh, int i)
454 {
455         struct r5dev *dev = &sh->dev[i];
456
457         bio_init(&dev->req);
458         dev->req.bi_io_vec = &dev->vec;
459         dev->req.bi_vcnt++;
460         dev->vec.bv_page = dev->page;
461         dev->vec.bv_len = STRIPE_SIZE;
462         dev->vec.bv_offset = 0;
463
464         dev->req.bi_sector = sh->sector;
465         dev->req.bi_private = sh;
466
467         dev->flags = 0;
468         if (i != sh->pd_idx)
469                 dev->sector = compute_blocknr(sh, i);
470 }
471
472 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
473 {
474         char b[BDEVNAME_SIZE];
475         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
476         PRINTK("raid5: error called\n");
477
478         if (!rdev->faulty) {
479                 mddev->sb_dirty = 1;
480                 if (rdev->in_sync) {
481                         conf->working_disks--;
482                         mddev->degraded++;
483                         conf->failed_disks++;
484                         rdev->in_sync = 0;
485                         /*
486                          * if recovery was running, make sure it aborts.
487                          */
488                         set_bit(MD_RECOVERY_ERR, &mddev->recovery);
489                 }
490                 rdev->faulty = 1;
491                 printk (KERN_ALERT
492                         "raid5: Disk failure on %s, disabling device."
493                         " Operation continuing on %d devices\n",
494                         bdevname(rdev->bdev,b), conf->working_disks);
495         }
496 }       
497
498 /*
499  * Input: a 'big' sector number,
500  * Output: index of the data and parity disk, and the sector # in them.
501  */
502 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
503                         unsigned int data_disks, unsigned int * dd_idx,
504                         unsigned int * pd_idx, raid5_conf_t *conf)
505 {
506         long stripe;
507         unsigned long chunk_number;
508         unsigned int chunk_offset;
509         sector_t new_sector;
510         int sectors_per_chunk = conf->chunk_size >> 9;
511
512         /* First compute the information on this sector */
513
514         /*
515          * Compute the chunk number and the sector offset inside the chunk
516          */
517         chunk_offset = sector_div(r_sector, sectors_per_chunk);
518         chunk_number = r_sector;
519         BUG_ON(r_sector != chunk_number);
520
521         /*
522          * Compute the stripe number
523          */
524         stripe = chunk_number / data_disks;
525
526         /*
527          * Compute the data disk and parity disk indexes inside the stripe
528          */
529         *dd_idx = chunk_number % data_disks;
530
531         /*
532          * Select the parity disk based on the user selected algorithm.
533          */
534         if (conf->level == 4)
535                 *pd_idx = data_disks;
536         else switch (conf->algorithm) {
537                 case ALGORITHM_LEFT_ASYMMETRIC:
538                         *pd_idx = data_disks - stripe % raid_disks;
539                         if (*dd_idx >= *pd_idx)
540                                 (*dd_idx)++;
541                         break;
542                 case ALGORITHM_RIGHT_ASYMMETRIC:
543                         *pd_idx = stripe % raid_disks;
544                         if (*dd_idx >= *pd_idx)
545                                 (*dd_idx)++;
546                         break;
547                 case ALGORITHM_LEFT_SYMMETRIC:
548                         *pd_idx = data_disks - stripe % raid_disks;
549                         *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
550                         break;
551                 case ALGORITHM_RIGHT_SYMMETRIC:
552                         *pd_idx = stripe % raid_disks;
553                         *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
554                         break;
555                 default:
556                         printk("raid5: unsupported algorithm %d\n",
557                                 conf->algorithm);
558         }
559
560         /*
561          * Finally, compute the new sector number
562          */
563         new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
564         return new_sector;
565 }
566
567
568 static sector_t compute_blocknr(struct stripe_head *sh, int i)
569 {
570         raid5_conf_t *conf = sh->raid_conf;
571         int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
572         sector_t new_sector = sh->sector, check;
573         int sectors_per_chunk = conf->chunk_size >> 9;
574         sector_t stripe;
575         int chunk_offset;
576         int chunk_number, dummy1, dummy2, dd_idx = i;
577         sector_t r_sector;
578
579         chunk_offset = sector_div(new_sector, sectors_per_chunk);
580         stripe = new_sector;
581         BUG_ON(new_sector != stripe);
582
583         
584         switch (conf->algorithm) {
585                 case ALGORITHM_LEFT_ASYMMETRIC:
586                 case ALGORITHM_RIGHT_ASYMMETRIC:
587                         if (i > sh->pd_idx)
588                                 i--;
589                         break;
590                 case ALGORITHM_LEFT_SYMMETRIC:
591                 case ALGORITHM_RIGHT_SYMMETRIC:
592                         if (i < sh->pd_idx)
593                                 i += raid_disks;
594                         i -= (sh->pd_idx + 1);
595                         break;
596                 default:
597                         printk("raid5: unsupported algorithm %d\n",
598                                 conf->algorithm);
599         }
600
601         chunk_number = stripe * data_disks + i;
602         r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
603
604         check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
605         if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
606                 printk("compute_blocknr: map not correct\n");
607                 return 0;
608         }
609         return r_sector;
610 }
611
612
613
614 /*
615  * Copy data between a page in the stripe cache, and one or more bion
616  * The page could align with the middle of the bio, or there could be 
617  * several bion, each with several bio_vecs, which cover part of the page
618  * Multiple bion are linked together on bi_next.  There may be extras
619  * at the end of this list.  We ignore them.
620  */
621 static void copy_data(int frombio, struct bio *bio,
622                      struct page *page,
623                      sector_t sector)
624 {
625         char *pa = page_address(page);
626         struct bio_vec *bvl;
627         int i;
628
629         for (;bio && bio->bi_sector < sector+STRIPE_SECTORS;
630               bio = r5_next_bio(bio, sector) ) {
631                 int page_offset;
632                 if (bio->bi_sector >= sector)
633                         page_offset = (signed)(bio->bi_sector - sector) * 512;
634                 else 
635                         page_offset = (signed)(sector - bio->bi_sector) * -512;
636                 bio_for_each_segment(bvl, bio, i) {
637                         int len = bio_iovec_idx(bio,i)->bv_len;
638                         int clen;
639                         int b_offset = 0;                       
640
641                         if (page_offset < 0) {
642                                 b_offset = -page_offset;
643                                 page_offset += b_offset;
644                                 len -= b_offset;
645                         }
646
647                         if (len > 0 && page_offset + len > STRIPE_SIZE)
648                                 clen = STRIPE_SIZE - page_offset;       
649                         else clen = len;
650                         
651                         if (clen > 0) {
652                                 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
653                                 if (frombio)
654                                         memcpy(pa+page_offset, ba+b_offset, clen);
655                                 else
656                                         memcpy(ba+b_offset, pa+page_offset, clen);
657                                 __bio_kunmap_atomic(ba, KM_USER0);
658                         }       
659                         if (clen < len) /* hit end of page */
660                                 break;
661                         page_offset +=  len;
662                 }
663         }
664 }
665
666 #define check_xor()     do {                                            \
667                            if (count == MAX_XOR_BLOCKS) {               \
668                                 xor_block(count, STRIPE_SIZE, ptr);     \
669                                 count = 1;                              \
670                            }                                            \
671                         } while(0)
672
673
674 static void compute_block(struct stripe_head *sh, int dd_idx)
675 {
676         raid5_conf_t *conf = sh->raid_conf;
677         int i, count, disks = conf->raid_disks;
678         void *ptr[MAX_XOR_BLOCKS], *p;
679
680         PRINTK("compute_block, stripe %llu, idx %d\n", 
681                 (unsigned long long)sh->sector, dd_idx);
682
683         ptr[0] = page_address(sh->dev[dd_idx].page);
684         memset(ptr[0], 0, STRIPE_SIZE);
685         count = 1;
686         for (i = disks ; i--; ) {
687                 if (i == dd_idx)
688                         continue;
689                 p = page_address(sh->dev[i].page);
690                 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
691                         ptr[count++] = p;
692                 else
693                         printk("compute_block() %d, stripe %llu, %d"
694                                 " not present\n", dd_idx,
695                                 (unsigned long long)sh->sector, i);
696
697                 check_xor();
698         }
699         if (count != 1)
700                 xor_block(count, STRIPE_SIZE, ptr);
701         set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
702 }
703
704 static void compute_parity(struct stripe_head *sh, int method)
705 {
706         raid5_conf_t *conf = sh->raid_conf;
707         int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
708         void *ptr[MAX_XOR_BLOCKS];
709         struct bio *chosen;
710
711         PRINTK("compute_parity, stripe %llu, method %d\n",
712                 (unsigned long long)sh->sector, method);
713
714         count = 1;
715         ptr[0] = page_address(sh->dev[pd_idx].page);
716         switch(method) {
717         case READ_MODIFY_WRITE:
718                 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
719                         BUG();
720                 for (i=disks ; i-- ;) {
721                         if (i==pd_idx)
722                                 continue;
723                         if (sh->dev[i].towrite &&
724                             test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
725                                 ptr[count++] = page_address(sh->dev[i].page);
726                                 chosen = sh->dev[i].towrite;
727                                 sh->dev[i].towrite = NULL;
728                                 if (sh->dev[i].written) BUG();
729                                 sh->dev[i].written = chosen;
730                                 check_xor();
731                         }
732                 }
733                 break;
734         case RECONSTRUCT_WRITE:
735                 memset(ptr[0], 0, STRIPE_SIZE);
736                 for (i= disks; i-- ;)
737                         if (i!=pd_idx && sh->dev[i].towrite) {
738                                 chosen = sh->dev[i].towrite;
739                                 sh->dev[i].towrite = NULL;
740                                 if (sh->dev[i].written) BUG();
741                                 sh->dev[i].written = chosen;
742                         }
743                 break;
744         case CHECK_PARITY:
745                 break;
746         }
747         if (count>1) {
748                 xor_block(count, STRIPE_SIZE, ptr);
749                 count = 1;
750         }
751         
752         for (i = disks; i--;)
753                 if (sh->dev[i].written) {
754                         sector_t sector = sh->dev[i].sector;
755                         struct bio *wbi = sh->dev[i].written;
756                         while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
757                                 copy_data(1, wbi, sh->dev[i].page, sector);
758                                 wbi = r5_next_bio(wbi, sector);
759                         }
760
761                         set_bit(R5_LOCKED, &sh->dev[i].flags);
762                         set_bit(R5_UPTODATE, &sh->dev[i].flags);
763                 }
764
765         switch(method) {
766         case RECONSTRUCT_WRITE:
767         case CHECK_PARITY:
768                 for (i=disks; i--;)
769                         if (i != pd_idx) {
770                                 ptr[count++] = page_address(sh->dev[i].page);
771                                 check_xor();
772                         }
773                 break;
774         case READ_MODIFY_WRITE:
775                 for (i = disks; i--;)
776                         if (sh->dev[i].written) {
777                                 ptr[count++] = page_address(sh->dev[i].page);
778                                 check_xor();
779                         }
780         }
781         if (count != 1)
782                 xor_block(count, STRIPE_SIZE, ptr);
783         
784         if (method != CHECK_PARITY) {
785                 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
786                 set_bit(R5_LOCKED,   &sh->dev[pd_idx].flags);
787         } else
788                 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
789 }
790
791 /*
792  * Each stripe/dev can have one or more bion attached.
793  * toread/towrite point to the first in a chain. 
794  * The bi_next chain must be in order.
795  */
796 static void add_stripe_bio (struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
797 {
798         struct bio **bip;
799         raid5_conf_t *conf = sh->raid_conf;
800
801         PRINTK("adding bh b#%llu to stripe s#%llu\n",
802                 (unsigned long long)bi->bi_sector,
803                 (unsigned long long)sh->sector);
804
805
806         spin_lock(&sh->lock);
807         spin_lock_irq(&conf->device_lock);
808         if (forwrite)
809                 bip = &sh->dev[dd_idx].towrite;
810         else
811                 bip = &sh->dev[dd_idx].toread;
812         while (*bip && (*bip)->bi_sector < bi->bi_sector) {
813                 BUG_ON((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector);
814                 bip = & (*bip)->bi_next;
815         }
816 /* FIXME do I need to worry about overlapping bion */
817         if (*bip && bi->bi_next && (*bip) != bi->bi_next)
818                 BUG();
819         if (*bip)
820                 bi->bi_next = *bip;
821         *bip = bi;
822         bi->bi_phys_segments ++;
823         spin_unlock_irq(&conf->device_lock);
824         spin_unlock(&sh->lock);
825
826         PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
827                 (unsigned long long)bi->bi_sector,
828                 (unsigned long long)sh->sector, dd_idx);
829
830         if (forwrite) {
831                 /* check if page is coverred */
832                 sector_t sector = sh->dev[dd_idx].sector;
833                 for (bi=sh->dev[dd_idx].towrite;
834                      sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
835                              bi && bi->bi_sector <= sector;
836                      bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
837                         if (bi->bi_sector + (bi->bi_size>>9) >= sector)
838                                 sector = bi->bi_sector + (bi->bi_size>>9);
839                 }
840                 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
841                         set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
842         }
843 }
844
845
846 /*
847  * handle_stripe - do things to a stripe.
848  *
849  * We lock the stripe and then examine the state of various bits
850  * to see what needs to be done.
851  * Possible results:
852  *    return some read request which now have data
853  *    return some write requests which are safely on disc
854  *    schedule a read on some buffers
855  *    schedule a write of some buffers
856  *    return confirmation of parity correctness
857  *
858  * Parity calculations are done inside the stripe lock
859  * buffers are taken off read_list or write_list, and bh_cache buffers
860  * get BH_Lock set before the stripe lock is released.
861  *
862  */
863  
864 static void handle_stripe(struct stripe_head *sh)
865 {
866         raid5_conf_t *conf = sh->raid_conf;
867         int disks = conf->raid_disks;
868         struct bio *return_bi= NULL;
869         struct bio *bi;
870         int i;
871         int syncing;
872         int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
873         int non_overwrite = 0;
874         int failed_num=0;
875         struct r5dev *dev;
876
877         PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
878                 (unsigned long long)sh->sector, atomic_read(&sh->count),
879                 sh->pd_idx);
880
881         spin_lock(&sh->lock);
882         clear_bit(STRIPE_HANDLE, &sh->state);
883         clear_bit(STRIPE_DELAYED, &sh->state);
884
885         syncing = test_bit(STRIPE_SYNCING, &sh->state);
886         /* Now to look around and see what can be done */
887
888         for (i=disks; i--; ) {
889                 mdk_rdev_t *rdev;
890                 dev = &sh->dev[i];
891                 clear_bit(R5_Insync, &dev->flags);
892                 clear_bit(R5_Syncio, &dev->flags);
893
894                 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
895                         i, dev->flags, dev->toread, dev->towrite, dev->written);
896                 /* maybe we can reply to a read */
897                 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
898                         struct bio *rbi, *rbi2;
899                         PRINTK("Return read for disc %d\n", i);
900                         spin_lock_irq(&conf->device_lock);
901                         rbi = dev->toread;
902                         dev->toread = NULL;
903                         spin_unlock_irq(&conf->device_lock);
904                         while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
905                                 copy_data(0, rbi, dev->page, dev->sector);
906                                 rbi2 = r5_next_bio(rbi, dev->sector);
907                                 spin_lock_irq(&conf->device_lock);
908                                 if (--rbi->bi_phys_segments == 0) {
909                                         rbi->bi_next = return_bi;
910                                         return_bi = rbi;
911                                 }
912                                 spin_unlock_irq(&conf->device_lock);
913                                 rbi = rbi2;
914                         }
915                 }
916
917                 /* now count some things */
918                 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
919                 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
920
921                 
922                 if (dev->toread) to_read++;
923                 if (dev->towrite) {
924                         to_write++;
925                         if (!test_bit(R5_OVERWRITE, &dev->flags))
926                                 non_overwrite++;
927                 }
928                 if (dev->written) written++;
929                 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
930                 if (!rdev || !rdev->in_sync) {
931                         failed++;
932                         failed_num = i;
933                 } else
934                         set_bit(R5_Insync, &dev->flags);
935         }
936         PRINTK("locked=%d uptodate=%d to_read=%d"
937                 " to_write=%d failed=%d failed_num=%d\n",
938                 locked, uptodate, to_read, to_write, failed, failed_num);
939         /* check if the array has lost two devices and, if so, some requests might
940          * need to be failed
941          */
942         if (failed > 1 && to_read+to_write+written) {
943                 spin_lock_irq(&conf->device_lock);
944                 for (i=disks; i--; ) {
945                         /* fail all writes first */
946                         bi = sh->dev[i].towrite;
947                         sh->dev[i].towrite = NULL;
948                         if (bi) to_write--;
949
950                         while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
951                                 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
952                                 clear_bit(BIO_UPTODATE, &bi->bi_flags);
953                                 if (--bi->bi_phys_segments == 0) {
954                                         md_write_end(conf->mddev);
955                                         bi->bi_next = return_bi;
956                                         return_bi = bi;
957                                 }
958                                 bi = nextbi;
959                         }
960                         /* and fail all 'written' */
961                         bi = sh->dev[i].written;
962                         sh->dev[i].written = NULL;
963                         while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
964                                 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
965                                 clear_bit(BIO_UPTODATE, &bi->bi_flags);
966                                 if (--bi->bi_phys_segments == 0) {
967                                         md_write_end(conf->mddev);
968                                         bi->bi_next = return_bi;
969                                         return_bi = bi;
970                                 }
971                                 bi = bi2;
972                         }
973
974                         /* fail any reads if this device is non-operational */
975                         if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
976                                 bi = sh->dev[i].toread;
977                                 sh->dev[i].toread = NULL;
978                                 if (bi) to_read--;
979                                 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
980                                         struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
981                                         clear_bit(BIO_UPTODATE, &bi->bi_flags);
982                                         if (--bi->bi_phys_segments == 0) {
983                                                 bi->bi_next = return_bi;
984                                                 return_bi = bi;
985                                         }
986                                         bi = nextbi;
987                                 }
988                         }
989                 }
990                 spin_unlock_irq(&conf->device_lock);
991         }
992         if (failed > 1 && syncing) {
993                 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
994                 clear_bit(STRIPE_SYNCING, &sh->state);
995                 syncing = 0;
996         }
997
998         /* might be able to return some write requests if the parity block
999          * is safe, or on a failed drive
1000          */
1001         dev = &sh->dev[sh->pd_idx];
1002         if ( written &&
1003              ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1004                 test_bit(R5_UPTODATE, &dev->flags))
1005                || (failed == 1 && failed_num == sh->pd_idx))
1006             ) {
1007             /* any written block on an uptodate or failed drive can be returned.
1008              * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but 
1009              * never LOCKED, so we don't need to test 'failed' directly.
1010              */
1011             for (i=disks; i--; )
1012                 if (sh->dev[i].written) {
1013                     dev = &sh->dev[i];
1014                     if (!test_bit(R5_LOCKED, &dev->flags) &&
1015                          test_bit(R5_UPTODATE, &dev->flags) ) {
1016                         /* We can return any write requests */
1017                             struct bio *wbi, *wbi2;
1018                             PRINTK("Return write for disc %d\n", i);
1019                             spin_lock_irq(&conf->device_lock);
1020                             wbi = dev->written;
1021                             dev->written = NULL;
1022                             while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1023                                     wbi2 = r5_next_bio(wbi, dev->sector);
1024                                     if (--wbi->bi_phys_segments == 0) {
1025                                             md_write_end(conf->mddev);
1026                                             wbi->bi_next = return_bi;
1027                                             return_bi = wbi;
1028                                     }
1029                                     wbi = wbi2;
1030                             }
1031                             spin_unlock_irq(&conf->device_lock);
1032                     }
1033                 }
1034         }
1035
1036         /* Now we might consider reading some blocks, either to check/generate
1037          * parity, or to satisfy requests
1038          * or to load a block that is being partially written.
1039          */
1040         if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1041                 for (i=disks; i--;) {
1042                         dev = &sh->dev[i];
1043                         if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1044                             (dev->toread ||
1045                              (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1046                              syncing ||
1047                              (failed && (sh->dev[failed_num].toread ||
1048                                          (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1049                                     )
1050                                 ) {
1051                                 /* we would like to get this block, possibly
1052                                  * by computing it, but we might not be able to
1053                                  */
1054                                 if (uptodate == disks-1) {
1055                                         PRINTK("Computing block %d\n", i);
1056                                         compute_block(sh, i);
1057                                         uptodate++;
1058                                 } else if (test_bit(R5_Insync, &dev->flags)) {
1059                                         set_bit(R5_LOCKED, &dev->flags);
1060                                         set_bit(R5_Wantread, &dev->flags);
1061 #if 0
1062                                         /* if I am just reading this block and we don't have
1063                                            a failed drive, or any pending writes then sidestep the cache */
1064                                         if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1065                                             ! syncing && !failed && !to_write) {
1066                                                 sh->bh_cache[i]->b_page =  sh->bh_read[i]->b_page;
1067                                                 sh->bh_cache[i]->b_data =  sh->bh_read[i]->b_data;
1068                                         }
1069 #endif
1070                                         locked++;
1071                                         PRINTK("Reading block %d (sync=%d)\n", 
1072                                                 i, syncing);
1073                                         if (syncing)
1074                                                 md_sync_acct(conf->disks[i].rdev->bdev,
1075                                                              STRIPE_SECTORS);
1076                                 }
1077                         }
1078                 }
1079                 set_bit(STRIPE_HANDLE, &sh->state);
1080         }
1081
1082         /* now to consider writing and what else, if anything should be read */
1083         if (to_write) {
1084                 int rmw=0, rcw=0;
1085                 for (i=disks ; i--;) {
1086                         /* would I have to read this buffer for read_modify_write */
1087                         dev = &sh->dev[i];
1088                         if ((dev->towrite || i == sh->pd_idx) &&
1089                             (!test_bit(R5_LOCKED, &dev->flags) 
1090 #if 0
1091 || sh->bh_page[i]!=bh->b_page
1092 #endif
1093                                     ) &&
1094                             !test_bit(R5_UPTODATE, &dev->flags)) {
1095                                 if (test_bit(R5_Insync, &dev->flags)
1096 /*                                  && !(!mddev->insync && i == sh->pd_idx) */
1097                                         )
1098                                         rmw++;
1099                                 else rmw += 2*disks;  /* cannot read it */
1100                         }
1101                         /* Would I have to read this buffer for reconstruct_write */
1102                         if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1103                             (!test_bit(R5_LOCKED, &dev->flags) 
1104 #if 0
1105 || sh->bh_page[i] != bh->b_page
1106 #endif
1107                                     ) &&
1108                             !test_bit(R5_UPTODATE, &dev->flags)) {
1109                                 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1110                                 else rcw += 2*disks;
1111                         }
1112                 }
1113                 PRINTK("for sector %llu, rmw=%d rcw=%d\n", 
1114                         (unsigned long long)sh->sector, rmw, rcw);
1115                 set_bit(STRIPE_HANDLE, &sh->state);
1116                 if (rmw < rcw && rmw > 0)
1117                         /* prefer read-modify-write, but need to get some data */
1118                         for (i=disks; i--;) {
1119                                 dev = &sh->dev[i];
1120                                 if ((dev->towrite || i == sh->pd_idx) &&
1121                                     !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1122                                     test_bit(R5_Insync, &dev->flags)) {
1123                                         if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1124                                         {
1125                                                 PRINTK("Read_old block %d for r-m-w\n", i);
1126                                                 set_bit(R5_LOCKED, &dev->flags);
1127                                                 set_bit(R5_Wantread, &dev->flags);
1128                                                 locked++;
1129                                         } else {
1130                                                 set_bit(STRIPE_DELAYED, &sh->state);
1131                                                 set_bit(STRIPE_HANDLE, &sh->state);
1132                                         }
1133                                 }
1134                         }
1135                 if (rcw <= rmw && rcw > 0)
1136                         /* want reconstruct write, but need to get some data */
1137                         for (i=disks; i--;) {
1138                                 dev = &sh->dev[i];
1139                                 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1140                                     !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1141                                     test_bit(R5_Insync, &dev->flags)) {
1142                                         if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1143                                         {
1144                                                 PRINTK("Read_old block %d for Reconstruct\n", i);
1145                                                 set_bit(R5_LOCKED, &dev->flags);
1146                                                 set_bit(R5_Wantread, &dev->flags);
1147                                                 locked++;
1148                                         } else {
1149                                                 set_bit(STRIPE_DELAYED, &sh->state);
1150                                                 set_bit(STRIPE_HANDLE, &sh->state);
1151                                         }
1152                                 }
1153                         }
1154                 /* now if nothing is locked, and if we have enough data, we can start a write request */
1155                 if (locked == 0 && (rcw == 0 ||rmw == 0)) {
1156                         PRINTK("Computing parity...\n");
1157                         compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1158                         /* now every locked buffer is ready to be written */
1159                         for (i=disks; i--;)
1160                                 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1161                                         PRINTK("Writing block %d\n", i);
1162                                         locked++;
1163                                         set_bit(R5_Wantwrite, &sh->dev[i].flags);
1164                                         if (!test_bit(R5_Insync, &sh->dev[i].flags)
1165                                             || (i==sh->pd_idx && failed == 0))
1166                                                 set_bit(STRIPE_INSYNC, &sh->state);
1167                                 }
1168                         if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1169                                 atomic_dec(&conf->preread_active_stripes);
1170                                 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1171                                         md_wakeup_thread(conf->mddev->thread);
1172                         }
1173                 }
1174         }
1175
1176         /* maybe we need to check and possibly fix the parity for this stripe
1177          * Any reads will already have been scheduled, so we just see if enough data
1178          * is available
1179          */
1180         if (syncing && locked == 0 &&
1181             !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
1182                 set_bit(STRIPE_HANDLE, &sh->state);
1183                 if (failed == 0) {
1184                         char *pagea;
1185                         if (uptodate != disks)
1186                                 BUG();
1187                         compute_parity(sh, CHECK_PARITY);
1188                         uptodate--;
1189                         pagea = page_address(sh->dev[sh->pd_idx].page);
1190                         if ((*(u32*)pagea) == 0 &&
1191                             !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1192                                 /* parity is correct (on disc, not in buffer any more) */
1193                                 set_bit(STRIPE_INSYNC, &sh->state);
1194                         }
1195                 }
1196                 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1197                         if (failed==0)
1198                                 failed_num = sh->pd_idx;
1199                         /* should be able to compute the missing block and write it to spare */
1200                         if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
1201                                 if (uptodate+1 != disks)
1202                                         BUG();
1203                                 compute_block(sh, failed_num);
1204                                 uptodate++;
1205                         }
1206                         if (uptodate != disks)
1207                                 BUG();
1208                         dev = &sh->dev[failed_num];
1209                         set_bit(R5_LOCKED, &dev->flags);
1210                         set_bit(R5_Wantwrite, &dev->flags);
1211                         locked++;
1212                         set_bit(STRIPE_INSYNC, &sh->state);
1213                         set_bit(R5_Syncio, &dev->flags);
1214                 }
1215         }
1216         if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1217                 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1218                 clear_bit(STRIPE_SYNCING, &sh->state);
1219         }
1220         
1221         spin_unlock(&sh->lock);
1222
1223         while ((bi=return_bi)) {
1224                 int bytes = bi->bi_size;
1225
1226                 return_bi = bi->bi_next;
1227                 bi->bi_next = NULL;
1228                 bi->bi_size = 0;
1229                 bi->bi_end_io(bi, bytes, 0);
1230         }
1231         for (i=disks; i-- ;) {
1232                 int rw;
1233                 struct bio *bi;
1234                 mdk_rdev_t *rdev;
1235                 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1236                         rw = 1;
1237                 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1238                         rw = 0;
1239                 else
1240                         continue;
1241  
1242                 bi = &sh->dev[i].req;
1243  
1244                 bi->bi_rw = rw;
1245                 if (rw)
1246                         bi->bi_end_io = raid5_end_write_request;
1247                 else
1248                         bi->bi_end_io = raid5_end_read_request;
1249  
1250                 spin_lock_irq(&conf->device_lock);
1251                 rdev = conf->disks[i].rdev;
1252                 if (rdev && rdev->faulty)
1253                         rdev = NULL;
1254                 if (rdev)
1255                         atomic_inc(&rdev->nr_pending);
1256                 spin_unlock_irq(&conf->device_lock);
1257  
1258                 if (rdev) {
1259                         if (test_bit(R5_Syncio, &sh->dev[i].flags))
1260                                 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1261
1262                         bi->bi_bdev = rdev->bdev;
1263                         PRINTK("for %llu schedule op %ld on disc %d\n",
1264                                 (unsigned long long)sh->sector, bi->bi_rw, i);
1265                         atomic_inc(&sh->count);
1266                         bi->bi_sector = sh->sector + rdev->data_offset;
1267                         bi->bi_flags = 1 << BIO_UPTODATE;
1268                         bi->bi_vcnt = 1;        
1269                         bi->bi_idx = 0;
1270                         bi->bi_io_vec = &sh->dev[i].vec;
1271                         bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1272                         bi->bi_io_vec[0].bv_offset = 0;
1273                         bi->bi_size = STRIPE_SIZE;
1274                         bi->bi_next = NULL;
1275                         generic_make_request(bi);
1276                 } else {
1277                         PRINTK("skip op %ld on disc %d for sector %llu\n",
1278                                 bi->bi_rw, i, (unsigned long long)sh->sector);
1279                         clear_bit(R5_LOCKED, &sh->dev[i].flags);
1280                         set_bit(STRIPE_HANDLE, &sh->state);
1281                 }
1282         }
1283 }
1284
1285 static inline void raid5_activate_delayed(raid5_conf_t *conf)
1286 {
1287         if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1288                 while (!list_empty(&conf->delayed_list)) {
1289                         struct list_head *l = conf->delayed_list.next;
1290                         struct stripe_head *sh;
1291                         sh = list_entry(l, struct stripe_head, lru);
1292                         list_del_init(l);
1293                         clear_bit(STRIPE_DELAYED, &sh->state);
1294                         if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1295                                 atomic_inc(&conf->preread_active_stripes);
1296                         list_add_tail(&sh->lru, &conf->handle_list);
1297                 }
1298         }
1299 }
1300
1301 static void unplug_slaves(mddev_t *mddev)
1302 {
1303         raid5_conf_t *conf = mddev_to_conf(mddev);
1304         int i;
1305         unsigned long flags;
1306
1307         spin_lock_irqsave(&conf->device_lock, flags);
1308         for (i=0; i<mddev->raid_disks; i++) {
1309                 mdk_rdev_t *rdev = conf->disks[i].rdev;
1310                 if (rdev && atomic_read(&rdev->nr_pending)) {
1311                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1312
1313                         atomic_inc(&rdev->nr_pending);
1314                         spin_unlock_irqrestore(&conf->device_lock, flags);
1315
1316                         if (r_queue && r_queue->unplug_fn)
1317                                 r_queue->unplug_fn(r_queue);
1318
1319                         spin_lock_irqsave(&conf->device_lock, flags);
1320                         atomic_dec(&rdev->nr_pending);
1321                 }
1322         }
1323         spin_unlock_irqrestore(&conf->device_lock, flags);
1324 }
1325
1326 static void raid5_unplug_device(request_queue_t *q)
1327 {
1328         mddev_t *mddev = q->queuedata;
1329         raid5_conf_t *conf = mddev_to_conf(mddev);
1330         unsigned long flags;
1331
1332         spin_lock_irqsave(&conf->device_lock, flags);
1333
1334         if (blk_remove_plug(q))
1335                 raid5_activate_delayed(conf);
1336         md_wakeup_thread(mddev->thread);
1337
1338         spin_unlock_irqrestore(&conf->device_lock, flags);
1339
1340         unplug_slaves(mddev);
1341 }
1342
1343 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1344                              sector_t *error_sector)
1345 {
1346         mddev_t *mddev = q->queuedata;
1347         raid5_conf_t *conf = mddev_to_conf(mddev);
1348         int i, ret = 0;
1349
1350         for (i=0; i<mddev->raid_disks; i++) {
1351                 mdk_rdev_t *rdev = conf->disks[i].rdev;
1352                 if (rdev && !rdev->faulty) {
1353                         struct block_device *bdev = rdev->bdev;
1354                         request_queue_t *r_queue;
1355
1356                         if (!bdev)
1357                                 continue;
1358
1359                         r_queue = bdev_get_queue(bdev);
1360                         if (!r_queue)
1361                                 continue;
1362
1363                         if (!r_queue->issue_flush_fn) {
1364                                 ret = -EOPNOTSUPP;
1365                                 break;
1366                         }
1367
1368                         ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, error_sector);
1369                         if (ret)
1370                                 break;
1371                 }
1372         }
1373         return ret;
1374 }
1375
1376 static inline void raid5_plug_device(raid5_conf_t *conf)
1377 {
1378         spin_lock_irq(&conf->device_lock);
1379         blk_plug_device(conf->mddev->queue);
1380         spin_unlock_irq(&conf->device_lock);
1381 }
1382
1383 static int make_request (request_queue_t *q, struct bio * bi)
1384 {
1385         mddev_t *mddev = q->queuedata;
1386         raid5_conf_t *conf = mddev_to_conf(mddev);
1387         const unsigned int raid_disks = conf->raid_disks;
1388         const unsigned int data_disks = raid_disks - 1;
1389         unsigned int dd_idx, pd_idx;
1390         sector_t new_sector;
1391         sector_t logical_sector, last_sector;
1392         struct stripe_head *sh;
1393
1394         if (bio_data_dir(bi)==WRITE) {
1395                 disk_stat_inc(mddev->gendisk, writes);
1396                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
1397         } else {
1398                 disk_stat_inc(mddev->gendisk, reads);
1399                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
1400         }
1401
1402         logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1403         last_sector = bi->bi_sector + (bi->bi_size>>9);
1404         bi->bi_next = NULL;
1405         bi->bi_phys_segments = 1;       /* over-loaded to count active stripes */
1406         if ( bio_data_dir(bi) == WRITE )
1407                 md_write_start(mddev);
1408         for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1409                 
1410                 new_sector = raid5_compute_sector(logical_sector,
1411                                                   raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1412
1413                 PRINTK("raid5: make_request, sector %Lu logical %Lu\n",
1414                         (unsigned long long)new_sector, 
1415                         (unsigned long long)logical_sector);
1416
1417                 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1418                 if (sh) {
1419
1420                         add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK));
1421
1422                         raid5_plug_device(conf);
1423                         handle_stripe(sh);
1424                         release_stripe(sh);
1425                 } else {
1426                         /* cannot get stripe for read-ahead, just give-up */
1427                         clear_bit(BIO_UPTODATE, &bi->bi_flags);
1428                         break;
1429                 }
1430                         
1431         }
1432         spin_lock_irq(&conf->device_lock);
1433         if (--bi->bi_phys_segments == 0) {
1434                 int bytes = bi->bi_size;
1435
1436                 if ( bio_data_dir(bi) == WRITE )
1437                         md_write_end(mddev);
1438                 bi->bi_size = 0;
1439                 bi->bi_end_io(bi, bytes, 0);
1440         }
1441         spin_unlock_irq(&conf->device_lock);
1442         return 0;
1443 }
1444
1445 /* FIXME go_faster isn't used */
1446 static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
1447 {
1448         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1449         struct stripe_head *sh;
1450         int sectors_per_chunk = conf->chunk_size >> 9;
1451         sector_t x;
1452         unsigned long stripe;
1453         int chunk_offset;
1454         int dd_idx, pd_idx;
1455         sector_t first_sector;
1456         int raid_disks = conf->raid_disks;
1457         int data_disks = raid_disks-1;
1458
1459         if (sector_nr >= mddev->size <<1) {
1460                 /* just being told to finish up .. nothing much to do */
1461                 unplug_slaves(mddev);
1462                 return 0;
1463         }
1464
1465         x = sector_nr;
1466         chunk_offset = sector_div(x, sectors_per_chunk);
1467         stripe = x;
1468         BUG_ON(x != stripe);
1469
1470         first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1471                 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1472         sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1473         if (sh == NULL) {
1474                 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1475                 /* make sure we don't swamp the stripe cache if someone else
1476                  * is trying to get access 
1477                  */
1478                 set_current_state(TASK_UNINTERRUPTIBLE);
1479                 schedule_timeout(1);
1480         }
1481         spin_lock(&sh->lock);   
1482         set_bit(STRIPE_SYNCING, &sh->state);
1483         clear_bit(STRIPE_INSYNC, &sh->state);
1484         spin_unlock(&sh->lock);
1485
1486         handle_stripe(sh);
1487         release_stripe(sh);
1488
1489         return STRIPE_SECTORS;
1490 }
1491
1492 /*
1493  * This is our raid5 kernel thread.
1494  *
1495  * We scan the hash table for stripes which can be handled now.
1496  * During the scan, completed stripes are saved for us by the interrupt
1497  * handler, so that they will not have to wait for our next wakeup.
1498  */
1499 static void raid5d (mddev_t *mddev)
1500 {
1501         struct stripe_head *sh;
1502         raid5_conf_t *conf = mddev_to_conf(mddev);
1503         int handled;
1504
1505         PRINTK("+++ raid5d active\n");
1506
1507         md_check_recovery(mddev);
1508         md_handle_safemode(mddev);
1509
1510         handled = 0;
1511         spin_lock_irq(&conf->device_lock);
1512         while (1) {
1513                 struct list_head *first;
1514
1515                 if (list_empty(&conf->handle_list) &&
1516                     atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1517                     !blk_queue_plugged(mddev->queue) &&
1518                     !list_empty(&conf->delayed_list))
1519                         raid5_activate_delayed(conf);
1520
1521                 if (list_empty(&conf->handle_list))
1522                         break;
1523
1524                 first = conf->handle_list.next;
1525                 sh = list_entry(first, struct stripe_head, lru);
1526
1527                 list_del_init(first);
1528                 atomic_inc(&sh->count);
1529                 if (atomic_read(&sh->count)!= 1)
1530                         BUG();
1531                 spin_unlock_irq(&conf->device_lock);
1532                 
1533                 handled++;
1534                 handle_stripe(sh);
1535                 release_stripe(sh);
1536
1537                 spin_lock_irq(&conf->device_lock);
1538         }
1539         PRINTK("%d stripes handled\n", handled);
1540
1541         spin_unlock_irq(&conf->device_lock);
1542
1543         unplug_slaves(mddev);
1544
1545         PRINTK("--- raid5d inactive\n");
1546 }
1547
1548 static int run (mddev_t *mddev)
1549 {
1550         raid5_conf_t *conf;
1551         int raid_disk, memory;
1552         mdk_rdev_t *rdev;
1553         struct disk_info *disk;
1554         struct list_head *tmp;
1555
1556         if (mddev->level != 5 && mddev->level != 4) {
1557                 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
1558                 return -EIO;
1559         }
1560
1561         mddev->private = kmalloc (sizeof (raid5_conf_t)
1562                                   + mddev->raid_disks * sizeof(struct disk_info),
1563                                   GFP_KERNEL);
1564         if ((conf = mddev->private) == NULL)
1565                 goto abort;
1566         memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1567         conf->mddev = mddev;
1568
1569         if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1570                 goto abort;
1571         memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1572
1573         conf->device_lock = SPIN_LOCK_UNLOCKED;
1574         init_waitqueue_head(&conf->wait_for_stripe);
1575         INIT_LIST_HEAD(&conf->handle_list);
1576         INIT_LIST_HEAD(&conf->delayed_list);
1577         INIT_LIST_HEAD(&conf->inactive_list);
1578         atomic_set(&conf->active_stripes, 0);
1579         atomic_set(&conf->preread_active_stripes, 0);
1580
1581         mddev->queue->unplug_fn = raid5_unplug_device;
1582         mddev->queue->issue_flush_fn = raid5_issue_flush;
1583
1584         PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1585
1586         ITERATE_RDEV(mddev,rdev,tmp) {
1587                 raid_disk = rdev->raid_disk;
1588                 if (raid_disk >= mddev->raid_disks
1589                     || raid_disk < 0)
1590                         continue;
1591                 disk = conf->disks + raid_disk;
1592
1593                 disk->rdev = rdev;
1594
1595                 if (rdev->in_sync) {
1596                         char b[BDEVNAME_SIZE];
1597                         printk(KERN_INFO "raid5: device %s operational as raid"
1598                                 " disk %d\n", bdevname(rdev->bdev,b),
1599                                 raid_disk);
1600                         conf->working_disks++;
1601                 }
1602         }
1603
1604         conf->raid_disks = mddev->raid_disks;
1605         /*
1606          * 0 for a fully functional array, 1 for a degraded array.
1607          */
1608         mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1609         conf->mddev = mddev;
1610         conf->chunk_size = mddev->chunk_size;
1611         conf->level = mddev->level;
1612         conf->algorithm = mddev->layout;
1613         conf->max_nr_stripes = NR_STRIPES;
1614
1615         /* device size must be a multiple of chunk size */
1616         mddev->size &= ~(mddev->chunk_size/1024 -1);
1617
1618         if (!conf->chunk_size || conf->chunk_size % 4) {
1619                 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1620                         conf->chunk_size, mdname(mddev));
1621                 goto abort;
1622         }
1623         if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1624                 printk(KERN_ERR 
1625                         "raid5: unsupported parity algorithm %d for %s\n",
1626                         conf->algorithm, mdname(mddev));
1627                 goto abort;
1628         }
1629         if (mddev->degraded > 1) {
1630                 printk(KERN_ERR "raid5: not enough operational devices for %s"
1631                         " (%d/%d failed)\n",
1632                         mdname(mddev), conf->failed_disks, conf->raid_disks);
1633                 goto abort;
1634         }
1635
1636         if (mddev->degraded == 1 &&
1637             mddev->recovery_cp != MaxSector) {
1638                 printk(KERN_ERR 
1639                         "raid5: cannot start dirty degraded array for %s\n",
1640                         mdname(mddev));
1641                 goto abort;
1642         }
1643
1644         {
1645                 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1646                 if (!mddev->thread) {
1647                         printk(KERN_ERR 
1648                                 "raid5: couldn't allocate thread for %s\n",
1649                                 mdname(mddev));
1650                         goto abort;
1651                 }
1652         }
1653 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1654                  conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1655         if (grow_stripes(conf, conf->max_nr_stripes)) {
1656                 printk(KERN_ERR 
1657                         "raid5: couldn't allocate %dkB for buffers\n", memory);
1658                 shrink_stripes(conf);
1659                 md_unregister_thread(mddev->thread);
1660                 goto abort;
1661         } else
1662                 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1663                         memory, mdname(mddev));
1664
1665         if (mddev->degraded == 0)
1666                 printk("raid5: raid level %d set %s active with %d out of %d"
1667                         " devices, algorithm %d\n", conf->level, mdname(mddev), 
1668                         mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1669                         conf->algorithm);
1670         else
1671                 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1672                         " out of %d devices, algorithm %d\n", conf->level,
1673                         mdname(mddev), mddev->raid_disks - mddev->degraded,
1674                         mddev->raid_disks, conf->algorithm);
1675
1676         print_raid5_conf(conf);
1677
1678         /* read-ahead size must cover two whole stripes, which is
1679          * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1680          */
1681         {
1682                 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
1683                         / PAGE_CACHE_SIZE;
1684                 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1685                         mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1686         }
1687
1688         /* Ok, everything is just fine now */
1689         mddev->array_size =  mddev->size * (mddev->raid_disks - 1);
1690         return 0;
1691 abort:
1692         if (conf) {
1693                 print_raid5_conf(conf);
1694                 if (conf->stripe_hashtbl)
1695                         free_pages((unsigned long) conf->stripe_hashtbl,
1696                                                         HASH_PAGES_ORDER);
1697                 kfree(conf);
1698         }
1699         mddev->private = NULL;
1700         printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
1701         return -EIO;
1702 }
1703
1704
1705
1706 static int stop (mddev_t *mddev)
1707 {
1708         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1709
1710         md_unregister_thread(mddev->thread);
1711         mddev->thread = NULL;
1712         shrink_stripes(conf);
1713         free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1714         kfree(conf);
1715         mddev->private = NULL;
1716         return 0;
1717 }
1718
1719 #if RAID5_DEBUG
1720 static void print_sh (struct stripe_head *sh)
1721 {
1722         int i;
1723
1724         printk("sh %llu, pd_idx %d, state %ld.\n",
1725                 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1726         printk("sh %llu,  count %d.\n",
1727                 (unsigned long long)sh->sector, atomic_read(&sh->count));
1728         printk("sh %llu, ", (unsigned long long)sh->sector);
1729         for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1730                 printk("(cache%d: %p %ld) ", 
1731                         i, sh->dev[i].page, sh->dev[i].flags);
1732         }
1733         printk("\n");
1734 }
1735
1736 static void printall (raid5_conf_t *conf)
1737 {
1738         struct stripe_head *sh;
1739         int i;
1740
1741         spin_lock_irq(&conf->device_lock);
1742         for (i = 0; i < NR_HASH; i++) {
1743                 sh = conf->stripe_hashtbl[i];
1744                 for (; sh; sh = sh->hash_next) {
1745                         if (sh->raid_conf != conf)
1746                                 continue;
1747                         print_sh(sh);
1748                 }
1749         }
1750         spin_unlock_irq(&conf->device_lock);
1751 }
1752 #endif
1753
1754 static void status (struct seq_file *seq, mddev_t *mddev)
1755 {
1756         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1757         int i;
1758
1759         seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1760         seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1761         for (i = 0; i < conf->raid_disks; i++)
1762                 seq_printf (seq, "%s",
1763                                conf->disks[i].rdev &&
1764                                conf->disks[i].rdev->in_sync ? "U" : "_");
1765         seq_printf (seq, "]");
1766 #if RAID5_DEBUG
1767 #define D(x) \
1768         seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
1769         printall(conf);
1770 #endif
1771 }
1772
1773 static void print_raid5_conf (raid5_conf_t *conf)
1774 {
1775         int i;
1776         struct disk_info *tmp;
1777
1778         printk("RAID5 conf printout:\n");
1779         if (!conf) {
1780                 printk("(conf==NULL)\n");
1781                 return;
1782         }
1783         printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1784                  conf->working_disks, conf->failed_disks);
1785
1786         for (i = 0; i < conf->raid_disks; i++) {
1787                 char b[BDEVNAME_SIZE];
1788                 tmp = conf->disks + i;
1789                 if (tmp->rdev)
1790                 printk(" disk %d, o:%d, dev:%s\n",
1791                         i, !tmp->rdev->faulty,
1792                         bdevname(tmp->rdev->bdev,b));
1793         }
1794 }
1795
1796 static int raid5_spare_active(mddev_t *mddev)
1797 {
1798         int i;
1799         raid5_conf_t *conf = mddev->private;
1800         struct disk_info *tmp;
1801
1802         spin_lock_irq(&conf->device_lock);
1803         for (i = 0; i < conf->raid_disks; i++) {
1804                 tmp = conf->disks + i;
1805                 if (tmp->rdev
1806                     && !tmp->rdev->faulty
1807                     && !tmp->rdev->in_sync) {
1808                         mddev->degraded--;
1809                         conf->failed_disks--;
1810                         conf->working_disks++;
1811                         tmp->rdev->in_sync = 1;
1812                 }
1813         }
1814         spin_unlock_irq(&conf->device_lock);
1815         print_raid5_conf(conf);
1816         return 0;
1817 }
1818
1819 static int raid5_remove_disk(mddev_t *mddev, int number)
1820 {
1821         raid5_conf_t *conf = mddev->private;
1822         int err = 1;
1823         struct disk_info *p = conf->disks + number;
1824
1825         print_raid5_conf(conf);
1826         spin_lock_irq(&conf->device_lock);
1827
1828         if (p->rdev) {
1829                 if (p->rdev->in_sync || 
1830                     atomic_read(&p->rdev->nr_pending)) {
1831                         err = -EBUSY;
1832                         goto abort;
1833                 }
1834                 p->rdev = NULL;
1835                 err = 0;
1836         }
1837         if (err)
1838                 MD_BUG();
1839 abort:
1840         spin_unlock_irq(&conf->device_lock);
1841         print_raid5_conf(conf);
1842         return err;
1843 }
1844
1845 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1846 {
1847         raid5_conf_t *conf = mddev->private;
1848         int found = 0;
1849         int disk;
1850         struct disk_info *p;
1851
1852         spin_lock_irq(&conf->device_lock);
1853         /*
1854          * find the disk ...
1855          */
1856         for (disk=0; disk < mddev->raid_disks; disk++)
1857                 if ((p=conf->disks + disk)->rdev == NULL) {
1858                         p->rdev = rdev;
1859                         rdev->in_sync = 0;
1860                         rdev->raid_disk = disk;
1861                         found = 1;
1862                         break;
1863                 }
1864         spin_unlock_irq(&conf->device_lock);
1865         print_raid5_conf(conf);
1866         return found;
1867 }
1868
1869 static int raid5_resize(mddev_t *mddev, sector_t sectors)
1870 {
1871         /* no resync is happening, and there is enough space
1872          * on all devices, so we can resize.
1873          * We need to make sure resync covers any new space.
1874          * If the array is shrinking we should possibly wait until
1875          * any io in the removed space completes, but it hardly seems
1876          * worth it.
1877          */
1878         sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
1879         mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
1880         set_capacity(mddev->gendisk, mddev->array_size << 1);
1881         mddev->changed = 1;
1882         if (sectors/2  > mddev->size && mddev->recovery_cp == MaxSector) {
1883                 mddev->recovery_cp = mddev->size << 1;
1884                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1885         }
1886         mddev->size = sectors /2;
1887         return 0;
1888 }
1889
1890 static mdk_personality_t raid5_personality=
1891 {
1892         .name           = "raid5",
1893         .owner          = THIS_MODULE,
1894         .make_request   = make_request,
1895         .run            = run,
1896         .stop           = stop,
1897         .status         = status,
1898         .error_handler  = error,
1899         .hot_add_disk   = raid5_add_disk,
1900         .hot_remove_disk= raid5_remove_disk,
1901         .spare_active   = raid5_spare_active,
1902         .sync_request   = sync_request,
1903         .resize         = raid5_resize,
1904 };
1905
1906 static int __init raid5_init (void)
1907 {
1908         return register_md_personality (RAID5, &raid5_personality);
1909 }
1910
1911 static void raid5_exit (void)
1912 {
1913         unregister_md_personality (RAID5);
1914 }
1915
1916 module_init(raid5_init);
1917 module_exit(raid5_exit);
1918 MODULE_LICENSE("GPL");
1919 MODULE_ALIAS("md-personality-4"); /* RAID5 */