patch-2_6_7-vs1_9_1_12
[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 && 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                 conf->working_disks--;
481                 if (rdev->in_sync) {
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, STRIPE_SECTORS);
1075                                 }
1076                         }
1077                 }
1078                 set_bit(STRIPE_HANDLE, &sh->state);
1079         }
1080
1081         /* now to consider writing and what else, if anything should be read */
1082         if (to_write) {
1083                 int rmw=0, rcw=0;
1084                 for (i=disks ; i--;) {
1085                         /* would I have to read this buffer for read_modify_write */
1086                         dev = &sh->dev[i];
1087                         if ((dev->towrite || i == sh->pd_idx) &&
1088                             (!test_bit(R5_LOCKED, &dev->flags) 
1089 #if 0
1090 || sh->bh_page[i]!=bh->b_page
1091 #endif
1092                                     ) &&
1093                             !test_bit(R5_UPTODATE, &dev->flags)) {
1094                                 if (test_bit(R5_Insync, &dev->flags)
1095 /*                                  && !(!mddev->insync && i == sh->pd_idx) */
1096                                         )
1097                                         rmw++;
1098                                 else rmw += 2*disks;  /* cannot read it */
1099                         }
1100                         /* Would I have to read this buffer for reconstruct_write */
1101                         if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1102                             (!test_bit(R5_LOCKED, &dev->flags) 
1103 #if 0
1104 || sh->bh_page[i] != bh->b_page
1105 #endif
1106                                     ) &&
1107                             !test_bit(R5_UPTODATE, &dev->flags)) {
1108                                 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1109                                 else rcw += 2*disks;
1110                         }
1111                 }
1112                 PRINTK("for sector %llu, rmw=%d rcw=%d\n", 
1113                         (unsigned long long)sh->sector, rmw, rcw);
1114                 set_bit(STRIPE_HANDLE, &sh->state);
1115                 if (rmw < rcw && rmw > 0)
1116                         /* prefer read-modify-write, but need to get some data */
1117                         for (i=disks; i--;) {
1118                                 dev = &sh->dev[i];
1119                                 if ((dev->towrite || i == sh->pd_idx) &&
1120                                     !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1121                                     test_bit(R5_Insync, &dev->flags)) {
1122                                         if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1123                                         {
1124                                                 PRINTK("Read_old block %d for r-m-w\n", i);
1125                                                 set_bit(R5_LOCKED, &dev->flags);
1126                                                 set_bit(R5_Wantread, &dev->flags);
1127                                                 locked++;
1128                                         } else {
1129                                                 set_bit(STRIPE_DELAYED, &sh->state);
1130                                                 set_bit(STRIPE_HANDLE, &sh->state);
1131                                         }
1132                                 }
1133                         }
1134                 if (rcw <= rmw && rcw > 0)
1135                         /* want reconstruct write, but need to get some data */
1136                         for (i=disks; i--;) {
1137                                 dev = &sh->dev[i];
1138                                 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1139                                     !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1140                                     test_bit(R5_Insync, &dev->flags)) {
1141                                         if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1142                                         {
1143                                                 PRINTK("Read_old block %d for Reconstruct\n", i);
1144                                                 set_bit(R5_LOCKED, &dev->flags);
1145                                                 set_bit(R5_Wantread, &dev->flags);
1146                                                 locked++;
1147                                         } else {
1148                                                 set_bit(STRIPE_DELAYED, &sh->state);
1149                                                 set_bit(STRIPE_HANDLE, &sh->state);
1150                                         }
1151                                 }
1152                         }
1153                 /* now if nothing is locked, and if we have enough data, we can start a write request */
1154                 if (locked == 0 && (rcw == 0 ||rmw == 0)) {
1155                         PRINTK("Computing parity...\n");
1156                         compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1157                         /* now every locked buffer is ready to be written */
1158                         for (i=disks; i--;)
1159                                 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1160                                         PRINTK("Writing block %d\n", i);
1161                                         locked++;
1162                                         set_bit(R5_Wantwrite, &sh->dev[i].flags);
1163                                         if (!test_bit(R5_Insync, &sh->dev[i].flags)
1164                                             || (i==sh->pd_idx && failed == 0))
1165                                                 set_bit(STRIPE_INSYNC, &sh->state);
1166                                 }
1167                         if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1168                                 atomic_dec(&conf->preread_active_stripes);
1169                                 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1170                                         md_wakeup_thread(conf->mddev->thread);
1171                         }
1172                 }
1173         }
1174
1175         /* maybe we need to check and possibly fix the parity for this stripe
1176          * Any reads will already have been scheduled, so we just see if enough data
1177          * is available
1178          */
1179         if (syncing && locked == 0 &&
1180             !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
1181                 set_bit(STRIPE_HANDLE, &sh->state);
1182                 if (failed == 0) {
1183                         char *pagea;
1184                         if (uptodate != disks)
1185                                 BUG();
1186                         compute_parity(sh, CHECK_PARITY);
1187                         uptodate--;
1188                         pagea = page_address(sh->dev[sh->pd_idx].page);
1189                         if ((*(u32*)pagea) == 0 &&
1190                             !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1191                                 /* parity is correct (on disc, not in buffer any more) */
1192                                 set_bit(STRIPE_INSYNC, &sh->state);
1193                         }
1194                 }
1195                 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1196                         if (failed==0)
1197                                 failed_num = sh->pd_idx;
1198                         /* should be able to compute the missing block and write it to spare */
1199                         if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
1200                                 if (uptodate+1 != disks)
1201                                         BUG();
1202                                 compute_block(sh, failed_num);
1203                                 uptodate++;
1204                         }
1205                         if (uptodate != disks)
1206                                 BUG();
1207                         dev = &sh->dev[failed_num];
1208                         set_bit(R5_LOCKED, &dev->flags);
1209                         set_bit(R5_Wantwrite, &dev->flags);
1210                         locked++;
1211                         set_bit(STRIPE_INSYNC, &sh->state);
1212                         set_bit(R5_Syncio, &dev->flags);
1213                 }
1214         }
1215         if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1216                 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1217                 clear_bit(STRIPE_SYNCING, &sh->state);
1218         }
1219         
1220         spin_unlock(&sh->lock);
1221
1222         while ((bi=return_bi)) {
1223                 int bytes = bi->bi_size;
1224
1225                 return_bi = bi->bi_next;
1226                 bi->bi_next = NULL;
1227                 bi->bi_size = 0;
1228                 bi->bi_end_io(bi, bytes, 0);
1229         }
1230         for (i=disks; i-- ;) {
1231                 int rw;
1232                 struct bio *bi;
1233                 mdk_rdev_t *rdev;
1234                 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1235                         rw = 1;
1236                 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1237                         rw = 0;
1238                 else
1239                         continue;
1240  
1241                 bi = &sh->dev[i].req;
1242  
1243                 bi->bi_rw = rw;
1244                 if (rw)
1245                         bi->bi_end_io = raid5_end_write_request;
1246                 else
1247                         bi->bi_end_io = raid5_end_read_request;
1248  
1249                 spin_lock_irq(&conf->device_lock);
1250                 rdev = conf->disks[i].rdev;
1251                 if (rdev && rdev->faulty)
1252                         rdev = NULL;
1253                 if (rdev)
1254                         atomic_inc(&rdev->nr_pending);
1255                 spin_unlock_irq(&conf->device_lock);
1256  
1257                 if (rdev) {
1258                         if (test_bit(R5_Syncio, &sh->dev[i].flags))
1259                                 md_sync_acct(rdev, STRIPE_SECTORS);
1260
1261                         bi->bi_bdev = rdev->bdev;
1262                         PRINTK("for %llu schedule op %ld on disc %d\n",
1263                                 (unsigned long long)sh->sector, bi->bi_rw, i);
1264                         atomic_inc(&sh->count);
1265                         bi->bi_sector = sh->sector + rdev->data_offset;
1266                         bi->bi_flags = 1 << BIO_UPTODATE;
1267                         bi->bi_vcnt = 1;        
1268                         bi->bi_idx = 0;
1269                         bi->bi_io_vec = &sh->dev[i].vec;
1270                         bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1271                         bi->bi_io_vec[0].bv_offset = 0;
1272                         bi->bi_size = STRIPE_SIZE;
1273                         bi->bi_next = NULL;
1274                         generic_make_request(bi);
1275                 } else {
1276                         PRINTK("skip op %ld on disc %d for sector %llu\n",
1277                                 bi->bi_rw, i, (unsigned long long)sh->sector);
1278                         clear_bit(R5_LOCKED, &sh->dev[i].flags);
1279                         set_bit(STRIPE_HANDLE, &sh->state);
1280                 }
1281         }
1282 }
1283
1284 static inline void raid5_activate_delayed(raid5_conf_t *conf)
1285 {
1286         if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1287                 while (!list_empty(&conf->delayed_list)) {
1288                         struct list_head *l = conf->delayed_list.next;
1289                         struct stripe_head *sh;
1290                         sh = list_entry(l, struct stripe_head, lru);
1291                         list_del_init(l);
1292                         clear_bit(STRIPE_DELAYED, &sh->state);
1293                         if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1294                                 atomic_inc(&conf->preread_active_stripes);
1295                         list_add_tail(&sh->lru, &conf->handle_list);
1296                 }
1297         }
1298 }
1299
1300 static void unplug_slaves(mddev_t *mddev)
1301 {
1302         raid5_conf_t *conf = mddev_to_conf(mddev);
1303         int i;
1304         unsigned long flags;
1305
1306         spin_lock_irqsave(&conf->device_lock, flags);
1307         for (i=0; i<mddev->raid_disks; i++) {
1308                 mdk_rdev_t *rdev = conf->disks[i].rdev;
1309                 if (rdev && atomic_read(&rdev->nr_pending)) {
1310                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1311
1312                         atomic_inc(&rdev->nr_pending);
1313                         spin_unlock_irqrestore(&conf->device_lock, flags);
1314
1315                         if (r_queue && r_queue->unplug_fn)
1316                                 r_queue->unplug_fn(r_queue);
1317
1318                         spin_lock_irqsave(&conf->device_lock, flags);
1319                         atomic_dec(&rdev->nr_pending);
1320                 }
1321         }
1322         spin_unlock_irqrestore(&conf->device_lock, flags);
1323 }
1324
1325 static void raid5_unplug_device(request_queue_t *q)
1326 {
1327         mddev_t *mddev = q->queuedata;
1328         raid5_conf_t *conf = mddev_to_conf(mddev);
1329         unsigned long flags;
1330
1331         spin_lock_irqsave(&conf->device_lock, flags);
1332
1333         if (blk_remove_plug(q))
1334                 raid5_activate_delayed(conf);
1335         md_wakeup_thread(mddev->thread);
1336
1337         spin_unlock_irqrestore(&conf->device_lock, flags);
1338
1339         unplug_slaves(mddev);
1340 }
1341
1342 static inline void raid5_plug_device(raid5_conf_t *conf)
1343 {
1344         spin_lock_irq(&conf->device_lock);
1345         blk_plug_device(conf->mddev->queue);
1346         spin_unlock_irq(&conf->device_lock);
1347 }
1348
1349 static int make_request (request_queue_t *q, struct bio * bi)
1350 {
1351         mddev_t *mddev = q->queuedata;
1352         raid5_conf_t *conf = mddev_to_conf(mddev);
1353         const unsigned int raid_disks = conf->raid_disks;
1354         const unsigned int data_disks = raid_disks - 1;
1355         unsigned int dd_idx, pd_idx;
1356         sector_t new_sector;
1357         sector_t logical_sector, last_sector;
1358         struct stripe_head *sh;
1359
1360         if (bio_data_dir(bi)==WRITE) {
1361                 disk_stat_inc(mddev->gendisk, writes);
1362                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
1363         } else {
1364                 disk_stat_inc(mddev->gendisk, reads);
1365                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
1366         }
1367
1368         logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1369         last_sector = bi->bi_sector + (bi->bi_size>>9);
1370         bi->bi_next = NULL;
1371         bi->bi_phys_segments = 1;       /* over-loaded to count active stripes */
1372         if ( bio_data_dir(bi) == WRITE )
1373                 md_write_start(mddev);
1374         for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1375                 
1376                 new_sector = raid5_compute_sector(logical_sector,
1377                                                   raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1378
1379                 PRINTK("raid5: make_request, sector %Lu logical %Lu\n",
1380                         (unsigned long long)new_sector, 
1381                         (unsigned long long)logical_sector);
1382
1383                 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1384                 if (sh) {
1385
1386                         add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK));
1387
1388                         raid5_plug_device(conf);
1389                         handle_stripe(sh);
1390                         release_stripe(sh);
1391                 } else {
1392                         /* cannot get stripe for read-ahead, just give-up */
1393                         clear_bit(BIO_UPTODATE, &bi->bi_flags);
1394                         break;
1395                 }
1396                         
1397         }
1398         spin_lock_irq(&conf->device_lock);
1399         if (--bi->bi_phys_segments == 0) {
1400                 int bytes = bi->bi_size;
1401
1402                 if ( bio_data_dir(bi) == WRITE )
1403                         md_write_end(mddev);
1404                 bi->bi_size = 0;
1405                 bi->bi_end_io(bi, bytes, 0);
1406         }
1407         spin_unlock_irq(&conf->device_lock);
1408         return 0;
1409 }
1410
1411 /* FIXME go_faster isn't used */
1412 static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
1413 {
1414         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1415         struct stripe_head *sh;
1416         int sectors_per_chunk = conf->chunk_size >> 9;
1417         sector_t x;
1418         unsigned long stripe;
1419         int chunk_offset;
1420         int dd_idx, pd_idx;
1421         sector_t first_sector;
1422         int raid_disks = conf->raid_disks;
1423         int data_disks = raid_disks-1;
1424
1425         if (sector_nr >= mddev->size <<1) {
1426                 /* just being told to finish up .. nothing much to do */
1427                 unplug_slaves(mddev);
1428                 return 0;
1429         }
1430
1431         x = sector_nr;
1432         chunk_offset = sector_div(x, sectors_per_chunk);
1433         stripe = x;
1434         BUG_ON(x != stripe);
1435
1436         first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1437                 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1438         sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1439         if (sh == NULL) {
1440                 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1441                 /* make sure we don't swamp the stripe cache if someone else
1442                  * is trying to get access 
1443                  */
1444                 set_current_state(TASK_UNINTERRUPTIBLE);
1445                 schedule_timeout(1);
1446         }
1447         spin_lock(&sh->lock);   
1448         set_bit(STRIPE_SYNCING, &sh->state);
1449         clear_bit(STRIPE_INSYNC, &sh->state);
1450         spin_unlock(&sh->lock);
1451
1452         handle_stripe(sh);
1453         release_stripe(sh);
1454
1455         return STRIPE_SECTORS;
1456 }
1457
1458 /*
1459  * This is our raid5 kernel thread.
1460  *
1461  * We scan the hash table for stripes which can be handled now.
1462  * During the scan, completed stripes are saved for us by the interrupt
1463  * handler, so that they will not have to wait for our next wakeup.
1464  */
1465 static void raid5d (mddev_t *mddev)
1466 {
1467         struct stripe_head *sh;
1468         raid5_conf_t *conf = mddev_to_conf(mddev);
1469         int handled;
1470
1471         PRINTK("+++ raid5d active\n");
1472
1473         md_check_recovery(mddev);
1474         md_handle_safemode(mddev);
1475
1476         handled = 0;
1477         spin_lock_irq(&conf->device_lock);
1478         while (1) {
1479                 struct list_head *first;
1480
1481                 if (list_empty(&conf->handle_list) &&
1482                     atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1483                     !blk_queue_plugged(mddev->queue) &&
1484                     !list_empty(&conf->delayed_list))
1485                         raid5_activate_delayed(conf);
1486
1487                 if (list_empty(&conf->handle_list))
1488                         break;
1489
1490                 first = conf->handle_list.next;
1491                 sh = list_entry(first, struct stripe_head, lru);
1492
1493                 list_del_init(first);
1494                 atomic_inc(&sh->count);
1495                 if (atomic_read(&sh->count)!= 1)
1496                         BUG();
1497                 spin_unlock_irq(&conf->device_lock);
1498                 
1499                 handled++;
1500                 handle_stripe(sh);
1501                 release_stripe(sh);
1502
1503                 spin_lock_irq(&conf->device_lock);
1504         }
1505         PRINTK("%d stripes handled\n", handled);
1506
1507         spin_unlock_irq(&conf->device_lock);
1508
1509         unplug_slaves(mddev);
1510
1511         PRINTK("--- raid5d inactive\n");
1512 }
1513
1514 static int run (mddev_t *mddev)
1515 {
1516         raid5_conf_t *conf;
1517         int raid_disk, memory;
1518         mdk_rdev_t *rdev;
1519         struct disk_info *disk;
1520         struct list_head *tmp;
1521
1522         if (mddev->level != 5 && mddev->level != 4) {
1523                 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
1524                 return -EIO;
1525         }
1526
1527         mddev->private = kmalloc (sizeof (raid5_conf_t)
1528                                   + mddev->raid_disks * sizeof(struct disk_info),
1529                                   GFP_KERNEL);
1530         if ((conf = mddev->private) == NULL)
1531                 goto abort;
1532         memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1533         conf->mddev = mddev;
1534
1535         if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1536                 goto abort;
1537         memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1538
1539         conf->device_lock = SPIN_LOCK_UNLOCKED;
1540         init_waitqueue_head(&conf->wait_for_stripe);
1541         INIT_LIST_HEAD(&conf->handle_list);
1542         INIT_LIST_HEAD(&conf->delayed_list);
1543         INIT_LIST_HEAD(&conf->inactive_list);
1544         atomic_set(&conf->active_stripes, 0);
1545         atomic_set(&conf->preread_active_stripes, 0);
1546
1547         mddev->queue->unplug_fn = raid5_unplug_device;
1548
1549         PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1550
1551         ITERATE_RDEV(mddev,rdev,tmp) {
1552                 raid_disk = rdev->raid_disk;
1553                 if (raid_disk >= mddev->raid_disks
1554                     || raid_disk < 0)
1555                         continue;
1556                 disk = conf->disks + raid_disk;
1557
1558                 disk->rdev = rdev;
1559
1560                 if (rdev->in_sync) {
1561                         char b[BDEVNAME_SIZE];
1562                         printk(KERN_INFO "raid5: device %s operational as raid"
1563                                 " disk %d\n", bdevname(rdev->bdev,b),
1564                                 raid_disk);
1565                         conf->working_disks++;
1566                 }
1567         }
1568
1569         conf->raid_disks = mddev->raid_disks;
1570         /*
1571          * 0 for a fully functional array, 1 for a degraded array.
1572          */
1573         mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1574         conf->mddev = mddev;
1575         conf->chunk_size = mddev->chunk_size;
1576         conf->level = mddev->level;
1577         conf->algorithm = mddev->layout;
1578         conf->max_nr_stripes = NR_STRIPES;
1579
1580         /* device size must be a multiple of chunk size */
1581         mddev->size &= ~(mddev->chunk_size/1024 -1);
1582
1583         if (!conf->chunk_size || conf->chunk_size % 4) {
1584                 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1585                         conf->chunk_size, mdname(mddev));
1586                 goto abort;
1587         }
1588         if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1589                 printk(KERN_ERR 
1590                         "raid5: unsupported parity algorithm %d for %s\n",
1591                         conf->algorithm, mdname(mddev));
1592                 goto abort;
1593         }
1594         if (mddev->degraded > 1) {
1595                 printk(KERN_ERR "raid5: not enough operational devices for %s"
1596                         " (%d/%d failed)\n",
1597                         mdname(mddev), conf->failed_disks, conf->raid_disks);
1598                 goto abort;
1599         }
1600
1601         if (mddev->degraded == 1 &&
1602             mddev->recovery_cp != MaxSector) {
1603                 printk(KERN_ERR 
1604                         "raid5: cannot start dirty degraded array for %s\n",
1605                         mdname(mddev));
1606                 goto abort;
1607         }
1608
1609         {
1610                 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1611                 if (!mddev->thread) {
1612                         printk(KERN_ERR 
1613                                 "raid5: couldn't allocate thread for %s\n",
1614                                 mdname(mddev));
1615                         goto abort;
1616                 }
1617         }
1618 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1619                  conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1620         if (grow_stripes(conf, conf->max_nr_stripes)) {
1621                 printk(KERN_ERR 
1622                         "raid5: couldn't allocate %dkB for buffers\n", memory);
1623                 shrink_stripes(conf);
1624                 md_unregister_thread(mddev->thread);
1625                 goto abort;
1626         } else
1627                 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1628                         memory, mdname(mddev));
1629
1630         if (mddev->degraded == 0)
1631                 printk("raid5: raid level %d set %s active with %d out of %d"
1632                         " devices, algorithm %d\n", conf->level, mdname(mddev), 
1633                         mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1634                         conf->algorithm);
1635         else
1636                 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1637                         " out of %d devices, algorithm %d\n", conf->level,
1638                         mdname(mddev), mddev->raid_disks - mddev->degraded,
1639                         mddev->raid_disks, conf->algorithm);
1640
1641         print_raid5_conf(conf);
1642
1643         /* read-ahead size must cover two whole stripes, which is
1644          * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1645          */
1646         {
1647                 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
1648                         / PAGE_CACHE_SIZE;
1649                 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1650                         mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1651         }
1652
1653         /* Ok, everything is just fine now */
1654         mddev->array_size =  mddev->size * (mddev->raid_disks - 1);
1655         return 0;
1656 abort:
1657         if (conf) {
1658                 print_raid5_conf(conf);
1659                 if (conf->stripe_hashtbl)
1660                         free_pages((unsigned long) conf->stripe_hashtbl,
1661                                                         HASH_PAGES_ORDER);
1662                 kfree(conf);
1663         }
1664         mddev->private = NULL;
1665         printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
1666         return -EIO;
1667 }
1668
1669
1670
1671 static int stop (mddev_t *mddev)
1672 {
1673         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1674
1675         md_unregister_thread(mddev->thread);
1676         mddev->thread = NULL;
1677         shrink_stripes(conf);
1678         free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1679         kfree(conf);
1680         mddev->private = NULL;
1681         return 0;
1682 }
1683
1684 #if RAID5_DEBUG
1685 static void print_sh (struct stripe_head *sh)
1686 {
1687         int i;
1688
1689         printk("sh %llu, pd_idx %d, state %ld.\n",
1690                 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1691         printk("sh %llu,  count %d.\n",
1692                 (unsigned long long)sh->sector, atomic_read(&sh->count));
1693         printk("sh %llu, ", (unsigned long long)sh->sector);
1694         for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1695                 printk("(cache%d: %p %ld) ", 
1696                         i, sh->dev[i].page, sh->dev[i].flags);
1697         }
1698         printk("\n");
1699 }
1700
1701 static void printall (raid5_conf_t *conf)
1702 {
1703         struct stripe_head *sh;
1704         int i;
1705
1706         spin_lock_irq(&conf->device_lock);
1707         for (i = 0; i < NR_HASH; i++) {
1708                 sh = conf->stripe_hashtbl[i];
1709                 for (; sh; sh = sh->hash_next) {
1710                         if (sh->raid_conf != conf)
1711                                 continue;
1712                         print_sh(sh);
1713                 }
1714         }
1715         spin_unlock_irq(&conf->device_lock);
1716 }
1717 #endif
1718
1719 static void status (struct seq_file *seq, mddev_t *mddev)
1720 {
1721         raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1722         int i;
1723
1724         seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1725         seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1726         for (i = 0; i < conf->raid_disks; i++)
1727                 seq_printf (seq, "%s",
1728                                conf->disks[i].rdev &&
1729                                conf->disks[i].rdev->in_sync ? "U" : "_");
1730         seq_printf (seq, "]");
1731 #if RAID5_DEBUG
1732 #define D(x) \
1733         seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
1734         printall(conf);
1735 #endif
1736 }
1737
1738 static void print_raid5_conf (raid5_conf_t *conf)
1739 {
1740         int i;
1741         struct disk_info *tmp;
1742
1743         printk("RAID5 conf printout:\n");
1744         if (!conf) {
1745                 printk("(conf==NULL)\n");
1746                 return;
1747         }
1748         printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1749                  conf->working_disks, conf->failed_disks);
1750
1751         for (i = 0; i < conf->raid_disks; i++) {
1752                 char b[BDEVNAME_SIZE];
1753                 tmp = conf->disks + i;
1754                 if (tmp->rdev)
1755                 printk(" disk %d, o:%d, dev:%s\n",
1756                         i, !tmp->rdev->faulty,
1757                         bdevname(tmp->rdev->bdev,b));
1758         }
1759 }
1760
1761 static int raid5_spare_active(mddev_t *mddev)
1762 {
1763         int i;
1764         raid5_conf_t *conf = mddev->private;
1765         struct disk_info *tmp;
1766
1767         spin_lock_irq(&conf->device_lock);
1768         for (i = 0; i < conf->raid_disks; i++) {
1769                 tmp = conf->disks + i;
1770                 if (tmp->rdev
1771                     && !tmp->rdev->faulty
1772                     && !tmp->rdev->in_sync) {
1773                         mddev->degraded--;
1774                         conf->failed_disks--;
1775                         conf->working_disks++;
1776                         tmp->rdev->in_sync = 1;
1777                 }
1778         }
1779         spin_unlock_irq(&conf->device_lock);
1780         print_raid5_conf(conf);
1781         return 0;
1782 }
1783
1784 static int raid5_remove_disk(mddev_t *mddev, int number)
1785 {
1786         raid5_conf_t *conf = mddev->private;
1787         int err = 1;
1788         struct disk_info *p = conf->disks + number;
1789
1790         print_raid5_conf(conf);
1791         spin_lock_irq(&conf->device_lock);
1792
1793         if (p->rdev) {
1794                 if (p->rdev->in_sync || 
1795                     atomic_read(&p->rdev->nr_pending)) {
1796                         err = -EBUSY;
1797                         goto abort;
1798                 }
1799                 p->rdev = NULL;
1800                 err = 0;
1801         }
1802         if (err)
1803                 MD_BUG();
1804 abort:
1805         spin_unlock_irq(&conf->device_lock);
1806         print_raid5_conf(conf);
1807         return err;
1808 }
1809
1810 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1811 {
1812         raid5_conf_t *conf = mddev->private;
1813         int found = 0;
1814         int disk;
1815         struct disk_info *p;
1816
1817         spin_lock_irq(&conf->device_lock);
1818         /*
1819          * find the disk ...
1820          */
1821         for (disk=0; disk < mddev->raid_disks; disk++)
1822                 if ((p=conf->disks + disk)->rdev == NULL) {
1823                         p->rdev = rdev;
1824                         rdev->in_sync = 0;
1825                         rdev->raid_disk = disk;
1826                         found = 1;
1827                         break;
1828                 }
1829         spin_unlock_irq(&conf->device_lock);
1830         print_raid5_conf(conf);
1831         return found;
1832 }
1833
1834 static int raid5_resize(mddev_t *mddev, sector_t sectors)
1835 {
1836         /* no resync is happening, and there is enough space
1837          * on all devices, so we can resize.
1838          * We need to make sure resync covers any new space.
1839          * If the array is shrinking we should possibly wait until
1840          * any io in the removed space completes, but it hardly seems
1841          * worth it.
1842          */
1843         sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
1844         mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
1845         set_capacity(mddev->gendisk, mddev->array_size << 1);
1846         mddev->changed = 1;
1847         if (sectors/2  > mddev->size && mddev->recovery_cp == MaxSector) {
1848                 mddev->recovery_cp = mddev->size << 1;
1849                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1850         }
1851         mddev->size = sectors /2;
1852         return 0;
1853 }
1854
1855 static mdk_personality_t raid5_personality=
1856 {
1857         .name           = "raid5",
1858         .owner          = THIS_MODULE,
1859         .make_request   = make_request,
1860         .run            = run,
1861         .stop           = stop,
1862         .status         = status,
1863         .error_handler  = error,
1864         .hot_add_disk   = raid5_add_disk,
1865         .hot_remove_disk= raid5_remove_disk,
1866         .spare_active   = raid5_spare_active,
1867         .sync_request   = sync_request,
1868         .resize         = raid5_resize,
1869 };
1870
1871 static int __init raid5_init (void)
1872 {
1873         return register_md_personality (RAID5, &raid5_personality);
1874 }
1875
1876 static void raid5_exit (void)
1877 {
1878         unregister_md_personality (RAID5);
1879 }
1880
1881 module_init(raid5_init);
1882 module_exit(raid5_exit);
1883 MODULE_LICENSE("GPL");
1884 MODULE_ALIAS("md-personality-4"); /* RAID5 */