1fa70c34b7d2bc6b4d3d11d19bf67b12925d6194
[firefly-linux-kernel-4.4.55.git] / drivers / md / raid10.c
1 /*
2  * raid10.c : Multiple Devices driver for Linux
3  *
4  * Copyright (C) 2000-2004 Neil Brown
5  *
6  * RAID-10 support for md.
7  *
8  * Base on code in raid1.c.  See raid1.c for futher copyright information.
9  *
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * You should have received a copy of the GNU General Public License
17  * (for example /usr/src/linux/COPYING); if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "dm-bio-list.h"
22 #include <linux/raid/raid10.h>
23 #include <linux/raid/bitmap.h>
24
25 /*
26  * RAID10 provides a combination of RAID0 and RAID1 functionality.
27  * The layout of data is defined by
28  *    chunk_size
29  *    raid_disks
30  *    near_copies (stored in low byte of layout)
31  *    far_copies (stored in second byte of layout)
32  *
33  * The data to be stored is divided into chunks using chunksize.
34  * Each device is divided into far_copies sections.
35  * In each section, chunks are laid out in a style similar to raid0, but
36  * near_copies copies of each chunk is stored (each on a different drive).
37  * The starting device for each section is offset near_copies from the starting
38  * device of the previous section.
39  * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
40  * drive.
41  * near_copies and far_copies must be at least one, and their product is at most
42  * raid_disks.
43  */
44
45 /*
46  * Number of guaranteed r10bios in case of extreme VM load:
47  */
48 #define NR_RAID10_BIOS 256
49
50 static void unplug_slaves(mddev_t *mddev);
51
52 static void allow_barrier(conf_t *conf);
53 static void lower_barrier(conf_t *conf);
54
55 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
56 {
57         conf_t *conf = data;
58         r10bio_t *r10_bio;
59         int size = offsetof(struct r10bio_s, devs[conf->copies]);
60
61         /* allocate a r10bio with room for raid_disks entries in the bios array */
62         r10_bio = kmalloc(size, gfp_flags);
63         if (r10_bio)
64                 memset(r10_bio, 0, size);
65         else
66                 unplug_slaves(conf->mddev);
67
68         return r10_bio;
69 }
70
71 static void r10bio_pool_free(void *r10_bio, void *data)
72 {
73         kfree(r10_bio);
74 }
75
76 #define RESYNC_BLOCK_SIZE (64*1024)
77 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
78 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
79 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
80 #define RESYNC_WINDOW (2048*1024)
81
82 /*
83  * When performing a resync, we need to read and compare, so
84  * we need as many pages are there are copies.
85  * When performing a recovery, we need 2 bios, one for read,
86  * one for write (we recover only one drive per r10buf)
87  *
88  */
89 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
90 {
91         conf_t *conf = data;
92         struct page *page;
93         r10bio_t *r10_bio;
94         struct bio *bio;
95         int i, j;
96         int nalloc;
97
98         r10_bio = r10bio_pool_alloc(gfp_flags, conf);
99         if (!r10_bio) {
100                 unplug_slaves(conf->mddev);
101                 return NULL;
102         }
103
104         if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
105                 nalloc = conf->copies; /* resync */
106         else
107                 nalloc = 2; /* recovery */
108
109         /*
110          * Allocate bios.
111          */
112         for (j = nalloc ; j-- ; ) {
113                 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
114                 if (!bio)
115                         goto out_free_bio;
116                 r10_bio->devs[j].bio = bio;
117         }
118         /*
119          * Allocate RESYNC_PAGES data pages and attach them
120          * where needed.
121          */
122         for (j = 0 ; j < nalloc; j++) {
123                 bio = r10_bio->devs[j].bio;
124                 for (i = 0; i < RESYNC_PAGES; i++) {
125                         page = alloc_page(gfp_flags);
126                         if (unlikely(!page))
127                                 goto out_free_pages;
128
129                         bio->bi_io_vec[i].bv_page = page;
130                 }
131         }
132
133         return r10_bio;
134
135 out_free_pages:
136         for ( ; i > 0 ; i--)
137                 __free_page(bio->bi_io_vec[i-1].bv_page);
138         while (j--)
139                 for (i = 0; i < RESYNC_PAGES ; i++)
140                         __free_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
141         j = -1;
142 out_free_bio:
143         while ( ++j < nalloc )
144                 bio_put(r10_bio->devs[j].bio);
145         r10bio_pool_free(r10_bio, conf);
146         return NULL;
147 }
148
149 static void r10buf_pool_free(void *__r10_bio, void *data)
150 {
151         int i;
152         conf_t *conf = data;
153         r10bio_t *r10bio = __r10_bio;
154         int j;
155
156         for (j=0; j < conf->copies; j++) {
157                 struct bio *bio = r10bio->devs[j].bio;
158                 if (bio) {
159                         for (i = 0; i < RESYNC_PAGES; i++) {
160                                 __free_page(bio->bi_io_vec[i].bv_page);
161                                 bio->bi_io_vec[i].bv_page = NULL;
162                         }
163                         bio_put(bio);
164                 }
165         }
166         r10bio_pool_free(r10bio, conf);
167 }
168
169 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
170 {
171         int i;
172
173         for (i = 0; i < conf->copies; i++) {
174                 struct bio **bio = & r10_bio->devs[i].bio;
175                 if (*bio)
176                         bio_put(*bio);
177                 *bio = NULL;
178         }
179 }
180
181 static inline void free_r10bio(r10bio_t *r10_bio)
182 {
183         conf_t *conf = mddev_to_conf(r10_bio->mddev);
184
185         /*
186          * Wake up any possible resync thread that waits for the device
187          * to go idle.
188          */
189         allow_barrier(conf);
190
191         put_all_bios(conf, r10_bio);
192         mempool_free(r10_bio, conf->r10bio_pool);
193 }
194
195 static inline void put_buf(r10bio_t *r10_bio)
196 {
197         conf_t *conf = mddev_to_conf(r10_bio->mddev);
198
199         mempool_free(r10_bio, conf->r10buf_pool);
200
201         lower_barrier(conf);
202 }
203
204 static void reschedule_retry(r10bio_t *r10_bio)
205 {
206         unsigned long flags;
207         mddev_t *mddev = r10_bio->mddev;
208         conf_t *conf = mddev_to_conf(mddev);
209
210         spin_lock_irqsave(&conf->device_lock, flags);
211         list_add(&r10_bio->retry_list, &conf->retry_list);
212         spin_unlock_irqrestore(&conf->device_lock, flags);
213
214         md_wakeup_thread(mddev->thread);
215 }
216
217 /*
218  * raid_end_bio_io() is called when we have finished servicing a mirrored
219  * operation and are ready to return a success/failure code to the buffer
220  * cache layer.
221  */
222 static void raid_end_bio_io(r10bio_t *r10_bio)
223 {
224         struct bio *bio = r10_bio->master_bio;
225
226         bio_endio(bio, bio->bi_size,
227                 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
228         free_r10bio(r10_bio);
229 }
230
231 /*
232  * Update disk head position estimator based on IRQ completion info.
233  */
234 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
235 {
236         conf_t *conf = mddev_to_conf(r10_bio->mddev);
237
238         conf->mirrors[r10_bio->devs[slot].devnum].head_position =
239                 r10_bio->devs[slot].addr + (r10_bio->sectors);
240 }
241
242 static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
243 {
244         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
245         r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
246         int slot, dev;
247         conf_t *conf = mddev_to_conf(r10_bio->mddev);
248
249         if (bio->bi_size)
250                 return 1;
251
252         slot = r10_bio->read_slot;
253         dev = r10_bio->devs[slot].devnum;
254         /*
255          * this branch is our 'one mirror IO has finished' event handler:
256          */
257         if (!uptodate)
258                 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
259         else
260                 /*
261                  * Set R10BIO_Uptodate in our master bio, so that
262                  * we will return a good error code to the higher
263                  * levels even if IO on some other mirrored buffer fails.
264                  *
265                  * The 'master' represents the composite IO operation to
266                  * user-side. So if something waits for IO, then it will
267                  * wait for the 'master' bio.
268                  */
269                 set_bit(R10BIO_Uptodate, &r10_bio->state);
270
271         update_head_pos(slot, r10_bio);
272
273         /*
274          * we have only one bio on the read side
275          */
276         if (uptodate)
277                 raid_end_bio_io(r10_bio);
278         else {
279                 /*
280                  * oops, read error:
281                  */
282                 char b[BDEVNAME_SIZE];
283                 if (printk_ratelimit())
284                         printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
285                                bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
286                 reschedule_retry(r10_bio);
287         }
288
289         rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
290         return 0;
291 }
292
293 static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
294 {
295         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
296         r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
297         int slot, dev;
298         conf_t *conf = mddev_to_conf(r10_bio->mddev);
299
300         if (bio->bi_size)
301                 return 1;
302
303         for (slot = 0; slot < conf->copies; slot++)
304                 if (r10_bio->devs[slot].bio == bio)
305                         break;
306         dev = r10_bio->devs[slot].devnum;
307
308         /*
309          * this branch is our 'one mirror IO has finished' event handler:
310          */
311         if (!uptodate) {
312                 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
313                 /* an I/O failed, we can't clear the bitmap */
314                 set_bit(R10BIO_Degraded, &r10_bio->state);
315         } else
316                 /*
317                  * Set R10BIO_Uptodate in our master bio, so that
318                  * we will return a good error code for to the higher
319                  * levels even if IO on some other mirrored buffer fails.
320                  *
321                  * The 'master' represents the composite IO operation to
322                  * user-side. So if something waits for IO, then it will
323                  * wait for the 'master' bio.
324                  */
325                 set_bit(R10BIO_Uptodate, &r10_bio->state);
326
327         update_head_pos(slot, r10_bio);
328
329         /*
330          *
331          * Let's see if all mirrored write operations have finished
332          * already.
333          */
334         if (atomic_dec_and_test(&r10_bio->remaining)) {
335                 /* clear the bitmap if all writes complete successfully */
336                 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
337                                 r10_bio->sectors,
338                                 !test_bit(R10BIO_Degraded, &r10_bio->state),
339                                 0);
340                 md_write_end(r10_bio->mddev);
341                 raid_end_bio_io(r10_bio);
342         }
343
344         rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
345         return 0;
346 }
347
348
349 /*
350  * RAID10 layout manager
351  * Aswell as the chunksize and raid_disks count, there are two
352  * parameters: near_copies and far_copies.
353  * near_copies * far_copies must be <= raid_disks.
354  * Normally one of these will be 1.
355  * If both are 1, we get raid0.
356  * If near_copies == raid_disks, we get raid1.
357  *
358  * Chunks are layed out in raid0 style with near_copies copies of the
359  * first chunk, followed by near_copies copies of the next chunk and
360  * so on.
361  * If far_copies > 1, then after 1/far_copies of the array has been assigned
362  * as described above, we start again with a device offset of near_copies.
363  * So we effectively have another copy of the whole array further down all
364  * the drives, but with blocks on different drives.
365  * With this layout, and block is never stored twice on the one device.
366  *
367  * raid10_find_phys finds the sector offset of a given virtual sector
368  * on each device that it is on. If a block isn't on a device,
369  * that entry in the array is set to MaxSector.
370  *
371  * raid10_find_virt does the reverse mapping, from a device and a
372  * sector offset to a virtual address
373  */
374
375 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
376 {
377         int n,f;
378         sector_t sector;
379         sector_t chunk;
380         sector_t stripe;
381         int dev;
382
383         int slot = 0;
384
385         /* now calculate first sector/dev */
386         chunk = r10bio->sector >> conf->chunk_shift;
387         sector = r10bio->sector & conf->chunk_mask;
388
389         chunk *= conf->near_copies;
390         stripe = chunk;
391         dev = sector_div(stripe, conf->raid_disks);
392
393         sector += stripe << conf->chunk_shift;
394
395         /* and calculate all the others */
396         for (n=0; n < conf->near_copies; n++) {
397                 int d = dev;
398                 sector_t s = sector;
399                 r10bio->devs[slot].addr = sector;
400                 r10bio->devs[slot].devnum = d;
401                 slot++;
402
403                 for (f = 1; f < conf->far_copies; f++) {
404                         d += conf->near_copies;
405                         if (d >= conf->raid_disks)
406                                 d -= conf->raid_disks;
407                         s += conf->stride;
408                         r10bio->devs[slot].devnum = d;
409                         r10bio->devs[slot].addr = s;
410                         slot++;
411                 }
412                 dev++;
413                 if (dev >= conf->raid_disks) {
414                         dev = 0;
415                         sector += (conf->chunk_mask + 1);
416                 }
417         }
418         BUG_ON(slot != conf->copies);
419 }
420
421 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
422 {
423         sector_t offset, chunk, vchunk;
424
425         while (sector > conf->stride) {
426                 sector -= conf->stride;
427                 if (dev < conf->near_copies)
428                         dev += conf->raid_disks - conf->near_copies;
429                 else
430                         dev -= conf->near_copies;
431         }
432
433         offset = sector & conf->chunk_mask;
434         chunk = sector >> conf->chunk_shift;
435         vchunk = chunk * conf->raid_disks + dev;
436         sector_div(vchunk, conf->near_copies);
437         return (vchunk << conf->chunk_shift) + offset;
438 }
439
440 /**
441  *      raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
442  *      @q: request queue
443  *      @bio: the buffer head that's been built up so far
444  *      @biovec: the request that could be merged to it.
445  *
446  *      Return amount of bytes we can accept at this offset
447  *      If near_copies == raid_disk, there are no striping issues,
448  *      but in that case, the function isn't called at all.
449  */
450 static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
451                                 struct bio_vec *bio_vec)
452 {
453         mddev_t *mddev = q->queuedata;
454         sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
455         int max;
456         unsigned int chunk_sectors = mddev->chunk_size >> 9;
457         unsigned int bio_sectors = bio->bi_size >> 9;
458
459         max =  (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
460         if (max < 0) max = 0; /* bio_add cannot handle a negative return */
461         if (max <= bio_vec->bv_len && bio_sectors == 0)
462                 return bio_vec->bv_len;
463         else
464                 return max;
465 }
466
467 /*
468  * This routine returns the disk from which the requested read should
469  * be done. There is a per-array 'next expected sequential IO' sector
470  * number - if this matches on the next IO then we use the last disk.
471  * There is also a per-disk 'last know head position' sector that is
472  * maintained from IRQ contexts, both the normal and the resync IO
473  * completion handlers update this position correctly. If there is no
474  * perfect sequential match then we pick the disk whose head is closest.
475  *
476  * If there are 2 mirrors in the same 2 devices, performance degrades
477  * because position is mirror, not device based.
478  *
479  * The rdev for the device selected will have nr_pending incremented.
480  */
481
482 /*
483  * FIXME: possibly should rethink readbalancing and do it differently
484  * depending on near_copies / far_copies geometry.
485  */
486 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
487 {
488         const unsigned long this_sector = r10_bio->sector;
489         int disk, slot, nslot;
490         const int sectors = r10_bio->sectors;
491         sector_t new_distance, current_distance;
492         mdk_rdev_t *rdev;
493
494         raid10_find_phys(conf, r10_bio);
495         rcu_read_lock();
496         /*
497          * Check if we can balance. We can balance on the whole
498          * device if no resync is going on (recovery is ok), or below
499          * the resync window. We take the first readable disk when
500          * above the resync window.
501          */
502         if (conf->mddev->recovery_cp < MaxSector
503             && (this_sector + sectors >= conf->next_resync)) {
504                 /* make sure that disk is operational */
505                 slot = 0;
506                 disk = r10_bio->devs[slot].devnum;
507
508                 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
509                        !test_bit(In_sync, &rdev->flags)) {
510                         slot++;
511                         if (slot == conf->copies) {
512                                 slot = 0;
513                                 disk = -1;
514                                 break;
515                         }
516                         disk = r10_bio->devs[slot].devnum;
517                 }
518                 goto rb_out;
519         }
520
521
522         /* make sure the disk is operational */
523         slot = 0;
524         disk = r10_bio->devs[slot].devnum;
525         while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
526                !test_bit(In_sync, &rdev->flags)) {
527                 slot ++;
528                 if (slot == conf->copies) {
529                         disk = -1;
530                         goto rb_out;
531                 }
532                 disk = r10_bio->devs[slot].devnum;
533         }
534
535
536         current_distance = abs(r10_bio->devs[slot].addr -
537                                conf->mirrors[disk].head_position);
538
539         /* Find the disk whose head is closest */
540
541         for (nslot = slot; nslot < conf->copies; nslot++) {
542                 int ndisk = r10_bio->devs[nslot].devnum;
543
544
545                 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
546                     !test_bit(In_sync, &rdev->flags))
547                         continue;
548
549                 /* This optimisation is debatable, and completely destroys
550                  * sequential read speed for 'far copies' arrays.  So only
551                  * keep it for 'near' arrays, and review those later.
552                  */
553                 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
554                         disk = ndisk;
555                         slot = nslot;
556                         break;
557                 }
558                 new_distance = abs(r10_bio->devs[nslot].addr -
559                                    conf->mirrors[ndisk].head_position);
560                 if (new_distance < current_distance) {
561                         current_distance = new_distance;
562                         disk = ndisk;
563                         slot = nslot;
564                 }
565         }
566
567 rb_out:
568         r10_bio->read_slot = slot;
569 /*      conf->next_seq_sect = this_sector + sectors;*/
570
571         if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
572                 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
573         rcu_read_unlock();
574
575         return disk;
576 }
577
578 static void unplug_slaves(mddev_t *mddev)
579 {
580         conf_t *conf = mddev_to_conf(mddev);
581         int i;
582
583         rcu_read_lock();
584         for (i=0; i<mddev->raid_disks; i++) {
585                 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
586                 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
587                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
588
589                         atomic_inc(&rdev->nr_pending);
590                         rcu_read_unlock();
591
592                         if (r_queue->unplug_fn)
593                                 r_queue->unplug_fn(r_queue);
594
595                         rdev_dec_pending(rdev, mddev);
596                         rcu_read_lock();
597                 }
598         }
599         rcu_read_unlock();
600 }
601
602 static void raid10_unplug(request_queue_t *q)
603 {
604         mddev_t *mddev = q->queuedata;
605
606         unplug_slaves(q->queuedata);
607         md_wakeup_thread(mddev->thread);
608 }
609
610 static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
611                              sector_t *error_sector)
612 {
613         mddev_t *mddev = q->queuedata;
614         conf_t *conf = mddev_to_conf(mddev);
615         int i, ret = 0;
616
617         rcu_read_lock();
618         for (i=0; i<mddev->raid_disks && ret == 0; i++) {
619                 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
620                 if (rdev && !test_bit(Faulty, &rdev->flags)) {
621                         struct block_device *bdev = rdev->bdev;
622                         request_queue_t *r_queue = bdev_get_queue(bdev);
623
624                         if (!r_queue->issue_flush_fn)
625                                 ret = -EOPNOTSUPP;
626                         else {
627                                 atomic_inc(&rdev->nr_pending);
628                                 rcu_read_unlock();
629                                 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
630                                                               error_sector);
631                                 rdev_dec_pending(rdev, mddev);
632                                 rcu_read_lock();
633                         }
634                 }
635         }
636         rcu_read_unlock();
637         return ret;
638 }
639
640 /* Barriers....
641  * Sometimes we need to suspend IO while we do something else,
642  * either some resync/recovery, or reconfigure the array.
643  * To do this we raise a 'barrier'.
644  * The 'barrier' is a counter that can be raised multiple times
645  * to count how many activities are happening which preclude
646  * normal IO.
647  * We can only raise the barrier if there is no pending IO.
648  * i.e. if nr_pending == 0.
649  * We choose only to raise the barrier if no-one is waiting for the
650  * barrier to go down.  This means that as soon as an IO request
651  * is ready, no other operations which require a barrier will start
652  * until the IO request has had a chance.
653  *
654  * So: regular IO calls 'wait_barrier'.  When that returns there
655  *    is no backgroup IO happening,  It must arrange to call
656  *    allow_barrier when it has finished its IO.
657  * backgroup IO calls must call raise_barrier.  Once that returns
658  *    there is no normal IO happeing.  It must arrange to call
659  *    lower_barrier when the particular background IO completes.
660  */
661 #define RESYNC_DEPTH 32
662
663 static void raise_barrier(conf_t *conf, int force)
664 {
665         BUG_ON(force && !conf->barrier);
666         spin_lock_irq(&conf->resync_lock);
667
668         /* Wait until no block IO is waiting (unless 'force') */
669         wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
670                             conf->resync_lock,
671                             raid10_unplug(conf->mddev->queue));
672
673         /* block any new IO from starting */
674         conf->barrier++;
675
676         /* No wait for all pending IO to complete */
677         wait_event_lock_irq(conf->wait_barrier,
678                             !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
679                             conf->resync_lock,
680                             raid10_unplug(conf->mddev->queue));
681
682         spin_unlock_irq(&conf->resync_lock);
683 }
684
685 static void lower_barrier(conf_t *conf)
686 {
687         unsigned long flags;
688         spin_lock_irqsave(&conf->resync_lock, flags);
689         conf->barrier--;
690         spin_unlock_irqrestore(&conf->resync_lock, flags);
691         wake_up(&conf->wait_barrier);
692 }
693
694 static void wait_barrier(conf_t *conf)
695 {
696         spin_lock_irq(&conf->resync_lock);
697         if (conf->barrier) {
698                 conf->nr_waiting++;
699                 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
700                                     conf->resync_lock,
701                                     raid10_unplug(conf->mddev->queue));
702                 conf->nr_waiting--;
703         }
704         conf->nr_pending++;
705         spin_unlock_irq(&conf->resync_lock);
706 }
707
708 static void allow_barrier(conf_t *conf)
709 {
710         unsigned long flags;
711         spin_lock_irqsave(&conf->resync_lock, flags);
712         conf->nr_pending--;
713         spin_unlock_irqrestore(&conf->resync_lock, flags);
714         wake_up(&conf->wait_barrier);
715 }
716
717 static int make_request(request_queue_t *q, struct bio * bio)
718 {
719         mddev_t *mddev = q->queuedata;
720         conf_t *conf = mddev_to_conf(mddev);
721         mirror_info_t *mirror;
722         r10bio_t *r10_bio;
723         struct bio *read_bio;
724         int i;
725         int chunk_sects = conf->chunk_mask + 1;
726         const int rw = bio_data_dir(bio);
727         struct bio_list bl;
728         unsigned long flags;
729
730         if (unlikely(bio_barrier(bio))) {
731                 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
732                 return 0;
733         }
734
735         /* If this request crosses a chunk boundary, we need to
736          * split it.  This will only happen for 1 PAGE (or less) requests.
737          */
738         if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
739                       > chunk_sects &&
740                     conf->near_copies < conf->raid_disks)) {
741                 struct bio_pair *bp;
742                 /* Sanity check -- queue functions should prevent this happening */
743                 if (bio->bi_vcnt != 1 ||
744                     bio->bi_idx != 0)
745                         goto bad_map;
746                 /* This is a one page bio that upper layers
747                  * refuse to split for us, so we need to split it.
748                  */
749                 bp = bio_split(bio, bio_split_pool,
750                                chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
751                 if (make_request(q, &bp->bio1))
752                         generic_make_request(&bp->bio1);
753                 if (make_request(q, &bp->bio2))
754                         generic_make_request(&bp->bio2);
755
756                 bio_pair_release(bp);
757                 return 0;
758         bad_map:
759                 printk("raid10_make_request bug: can't convert block across chunks"
760                        " or bigger than %dk %llu %d\n", chunk_sects/2,
761                        (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
762
763                 bio_io_error(bio, bio->bi_size);
764                 return 0;
765         }
766
767         md_write_start(mddev, bio);
768
769         /*
770          * Register the new request and wait if the reconstruction
771          * thread has put up a bar for new requests.
772          * Continue immediately if no resync is active currently.
773          */
774         wait_barrier(conf);
775
776         disk_stat_inc(mddev->gendisk, ios[rw]);
777         disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
778
779         r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
780
781         r10_bio->master_bio = bio;
782         r10_bio->sectors = bio->bi_size >> 9;
783
784         r10_bio->mddev = mddev;
785         r10_bio->sector = bio->bi_sector;
786         r10_bio->state = 0;
787
788         if (rw == READ) {
789                 /*
790                  * read balancing logic:
791                  */
792                 int disk = read_balance(conf, r10_bio);
793                 int slot = r10_bio->read_slot;
794                 if (disk < 0) {
795                         raid_end_bio_io(r10_bio);
796                         return 0;
797                 }
798                 mirror = conf->mirrors + disk;
799
800                 read_bio = bio_clone(bio, GFP_NOIO);
801
802                 r10_bio->devs[slot].bio = read_bio;
803
804                 read_bio->bi_sector = r10_bio->devs[slot].addr +
805                         mirror->rdev->data_offset;
806                 read_bio->bi_bdev = mirror->rdev->bdev;
807                 read_bio->bi_end_io = raid10_end_read_request;
808                 read_bio->bi_rw = READ;
809                 read_bio->bi_private = r10_bio;
810
811                 generic_make_request(read_bio);
812                 return 0;
813         }
814
815         /*
816          * WRITE:
817          */
818         /* first select target devices under spinlock and
819          * inc refcount on their rdev.  Record them by setting
820          * bios[x] to bio
821          */
822         raid10_find_phys(conf, r10_bio);
823         rcu_read_lock();
824         for (i = 0;  i < conf->copies; i++) {
825                 int d = r10_bio->devs[i].devnum;
826                 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
827                 if (rdev &&
828                     !test_bit(Faulty, &rdev->flags)) {
829                         atomic_inc(&rdev->nr_pending);
830                         r10_bio->devs[i].bio = bio;
831                 } else {
832                         r10_bio->devs[i].bio = NULL;
833                         set_bit(R10BIO_Degraded, &r10_bio->state);
834                 }
835         }
836         rcu_read_unlock();
837
838         atomic_set(&r10_bio->remaining, 0);
839
840         bio_list_init(&bl);
841         for (i = 0; i < conf->copies; i++) {
842                 struct bio *mbio;
843                 int d = r10_bio->devs[i].devnum;
844                 if (!r10_bio->devs[i].bio)
845                         continue;
846
847                 mbio = bio_clone(bio, GFP_NOIO);
848                 r10_bio->devs[i].bio = mbio;
849
850                 mbio->bi_sector = r10_bio->devs[i].addr+
851                         conf->mirrors[d].rdev->data_offset;
852                 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
853                 mbio->bi_end_io = raid10_end_write_request;
854                 mbio->bi_rw = WRITE;
855                 mbio->bi_private = r10_bio;
856
857                 atomic_inc(&r10_bio->remaining);
858                 bio_list_add(&bl, mbio);
859         }
860
861         bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
862         spin_lock_irqsave(&conf->device_lock, flags);
863         bio_list_merge(&conf->pending_bio_list, &bl);
864         blk_plug_device(mddev->queue);
865         spin_unlock_irqrestore(&conf->device_lock, flags);
866
867         return 0;
868 }
869
870 static void status(struct seq_file *seq, mddev_t *mddev)
871 {
872         conf_t *conf = mddev_to_conf(mddev);
873         int i;
874
875         if (conf->near_copies < conf->raid_disks)
876                 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
877         if (conf->near_copies > 1)
878                 seq_printf(seq, " %d near-copies", conf->near_copies);
879         if (conf->far_copies > 1)
880                 seq_printf(seq, " %d far-copies", conf->far_copies);
881
882         seq_printf(seq, " [%d/%d] [", conf->raid_disks,
883                                                 conf->working_disks);
884         for (i = 0; i < conf->raid_disks; i++)
885                 seq_printf(seq, "%s",
886                               conf->mirrors[i].rdev &&
887                               test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
888         seq_printf(seq, "]");
889 }
890
891 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
892 {
893         char b[BDEVNAME_SIZE];
894         conf_t *conf = mddev_to_conf(mddev);
895
896         /*
897          * If it is not operational, then we have already marked it as dead
898          * else if it is the last working disks, ignore the error, let the
899          * next level up know.
900          * else mark the drive as failed
901          */
902         if (test_bit(In_sync, &rdev->flags)
903             && conf->working_disks == 1)
904                 /*
905                  * Don't fail the drive, just return an IO error.
906                  * The test should really be more sophisticated than
907                  * "working_disks == 1", but it isn't critical, and
908                  * can wait until we do more sophisticated "is the drive
909                  * really dead" tests...
910                  */
911                 return;
912         if (test_bit(In_sync, &rdev->flags)) {
913                 mddev->degraded++;
914                 conf->working_disks--;
915                 /*
916                  * if recovery is running, make sure it aborts.
917                  */
918                 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
919         }
920         clear_bit(In_sync, &rdev->flags);
921         set_bit(Faulty, &rdev->flags);
922         mddev->sb_dirty = 1;
923         printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
924                 "       Operation continuing on %d devices\n",
925                 bdevname(rdev->bdev,b), conf->working_disks);
926 }
927
928 static void print_conf(conf_t *conf)
929 {
930         int i;
931         mirror_info_t *tmp;
932
933         printk("RAID10 conf printout:\n");
934         if (!conf) {
935                 printk("(!conf)\n");
936                 return;
937         }
938         printk(" --- wd:%d rd:%d\n", conf->working_disks,
939                 conf->raid_disks);
940
941         for (i = 0; i < conf->raid_disks; i++) {
942                 char b[BDEVNAME_SIZE];
943                 tmp = conf->mirrors + i;
944                 if (tmp->rdev)
945                         printk(" disk %d, wo:%d, o:%d, dev:%s\n",
946                                 i, !test_bit(In_sync, &tmp->rdev->flags),
947                                 !test_bit(Faulty, &tmp->rdev->flags),
948                                 bdevname(tmp->rdev->bdev,b));
949         }
950 }
951
952 static void close_sync(conf_t *conf)
953 {
954         wait_barrier(conf);
955         allow_barrier(conf);
956
957         mempool_destroy(conf->r10buf_pool);
958         conf->r10buf_pool = NULL;
959 }
960
961 /* check if there are enough drives for
962  * every block to appear on atleast one
963  */
964 static int enough(conf_t *conf)
965 {
966         int first = 0;
967
968         do {
969                 int n = conf->copies;
970                 int cnt = 0;
971                 while (n--) {
972                         if (conf->mirrors[first].rdev)
973                                 cnt++;
974                         first = (first+1) % conf->raid_disks;
975                 }
976                 if (cnt == 0)
977                         return 0;
978         } while (first != 0);
979         return 1;
980 }
981
982 static int raid10_spare_active(mddev_t *mddev)
983 {
984         int i;
985         conf_t *conf = mddev->private;
986         mirror_info_t *tmp;
987
988         /*
989          * Find all non-in_sync disks within the RAID10 configuration
990          * and mark them in_sync
991          */
992         for (i = 0; i < conf->raid_disks; i++) {
993                 tmp = conf->mirrors + i;
994                 if (tmp->rdev
995                     && !test_bit(Faulty, &tmp->rdev->flags)
996                     && !test_bit(In_sync, &tmp->rdev->flags)) {
997                         conf->working_disks++;
998                         mddev->degraded--;
999                         set_bit(In_sync, &tmp->rdev->flags);
1000                 }
1001         }
1002
1003         print_conf(conf);
1004         return 0;
1005 }
1006
1007
1008 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1009 {
1010         conf_t *conf = mddev->private;
1011         int found = 0;
1012         int mirror;
1013         mirror_info_t *p;
1014
1015         if (mddev->recovery_cp < MaxSector)
1016                 /* only hot-add to in-sync arrays, as recovery is
1017                  * very different from resync
1018                  */
1019                 return 0;
1020         if (!enough(conf))
1021                 return 0;
1022
1023         if (rdev->saved_raid_disk >= 0 &&
1024             conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1025                 mirror = rdev->saved_raid_disk;
1026         else
1027                 mirror = 0;
1028         for ( ; mirror < mddev->raid_disks; mirror++)
1029                 if ( !(p=conf->mirrors+mirror)->rdev) {
1030
1031                         blk_queue_stack_limits(mddev->queue,
1032                                                rdev->bdev->bd_disk->queue);
1033                         /* as we don't honour merge_bvec_fn, we must never risk
1034                          * violating it, so limit ->max_sector to one PAGE, as
1035                          * a one page request is never in violation.
1036                          */
1037                         if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1038                             mddev->queue->max_sectors > (PAGE_SIZE>>9))
1039                                 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1040
1041                         p->head_position = 0;
1042                         rdev->raid_disk = mirror;
1043                         found = 1;
1044                         if (rdev->saved_raid_disk != mirror)
1045                                 conf->fullsync = 1;
1046                         rcu_assign_pointer(p->rdev, rdev);
1047                         break;
1048                 }
1049
1050         print_conf(conf);
1051         return found;
1052 }
1053
1054 static int raid10_remove_disk(mddev_t *mddev, int number)
1055 {
1056         conf_t *conf = mddev->private;
1057         int err = 0;
1058         mdk_rdev_t *rdev;
1059         mirror_info_t *p = conf->mirrors+ number;
1060
1061         print_conf(conf);
1062         rdev = p->rdev;
1063         if (rdev) {
1064                 if (test_bit(In_sync, &rdev->flags) ||
1065                     atomic_read(&rdev->nr_pending)) {
1066                         err = -EBUSY;
1067                         goto abort;
1068                 }
1069                 p->rdev = NULL;
1070                 synchronize_rcu();
1071                 if (atomic_read(&rdev->nr_pending)) {
1072                         /* lost the race, try later */
1073                         err = -EBUSY;
1074                         p->rdev = rdev;
1075                 }
1076         }
1077 abort:
1078
1079         print_conf(conf);
1080         return err;
1081 }
1082
1083
1084 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1085 {
1086         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1087         r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1088         conf_t *conf = mddev_to_conf(r10_bio->mddev);
1089         int i,d;
1090
1091         if (bio->bi_size)
1092                 return 1;
1093
1094         for (i=0; i<conf->copies; i++)
1095                 if (r10_bio->devs[i].bio == bio)
1096                         break;
1097         if (i == conf->copies)
1098                 BUG();
1099         update_head_pos(i, r10_bio);
1100         d = r10_bio->devs[i].devnum;
1101         if (!uptodate)
1102                 md_error(r10_bio->mddev,
1103                          conf->mirrors[d].rdev);
1104
1105         /* for reconstruct, we always reschedule after a read.
1106          * for resync, only after all reads
1107          */
1108         if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1109             atomic_dec_and_test(&r10_bio->remaining)) {
1110                 /* we have read all the blocks,
1111                  * do the comparison in process context in raid10d
1112                  */
1113                 reschedule_retry(r10_bio);
1114         }
1115         rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1116         return 0;
1117 }
1118
1119 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1120 {
1121         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1122         r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1123         mddev_t *mddev = r10_bio->mddev;
1124         conf_t *conf = mddev_to_conf(mddev);
1125         int i,d;
1126
1127         if (bio->bi_size)
1128                 return 1;
1129
1130         for (i = 0; i < conf->copies; i++)
1131                 if (r10_bio->devs[i].bio == bio)
1132                         break;
1133         d = r10_bio->devs[i].devnum;
1134
1135         if (!uptodate)
1136                 md_error(mddev, conf->mirrors[d].rdev);
1137         update_head_pos(i, r10_bio);
1138
1139         while (atomic_dec_and_test(&r10_bio->remaining)) {
1140                 if (r10_bio->master_bio == NULL) {
1141                         /* the primary of several recovery bios */
1142                         md_done_sync(mddev, r10_bio->sectors, 1);
1143                         put_buf(r10_bio);
1144                         break;
1145                 } else {
1146                         r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1147                         put_buf(r10_bio);
1148                         r10_bio = r10_bio2;
1149                 }
1150         }
1151         rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1152         return 0;
1153 }
1154
1155 /*
1156  * Note: sync and recover and handled very differently for raid10
1157  * This code is for resync.
1158  * For resync, we read through virtual addresses and read all blocks.
1159  * If there is any error, we schedule a write.  The lowest numbered
1160  * drive is authoritative.
1161  * However requests come for physical address, so we need to map.
1162  * For every physical address there are raid_disks/copies virtual addresses,
1163  * which is always are least one, but is not necessarly an integer.
1164  * This means that a physical address can span multiple chunks, so we may
1165  * have to submit multiple io requests for a single sync request.
1166  */
1167 /*
1168  * We check if all blocks are in-sync and only write to blocks that
1169  * aren't in sync
1170  */
1171 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1172 {
1173         conf_t *conf = mddev_to_conf(mddev);
1174         int i, first;
1175         struct bio *tbio, *fbio;
1176
1177         atomic_set(&r10_bio->remaining, 1);
1178
1179         /* find the first device with a block */
1180         for (i=0; i<conf->copies; i++)
1181                 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1182                         break;
1183
1184         if (i == conf->copies)
1185                 goto done;
1186
1187         first = i;
1188         fbio = r10_bio->devs[i].bio;
1189
1190         /* now find blocks with errors */
1191         for (i=first+1 ; i < conf->copies ; i++) {
1192                 int vcnt, j, d;
1193
1194                 if (!test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1195                         continue;
1196                 /* We know that the bi_io_vec layout is the same for
1197                  * both 'first' and 'i', so we just compare them.
1198                  * All vec entries are PAGE_SIZE;
1199                  */
1200                 tbio = r10_bio->devs[i].bio;
1201                 vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1202                 for (j = 0; j < vcnt; j++)
1203                         if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1204                                    page_address(tbio->bi_io_vec[j].bv_page),
1205                                    PAGE_SIZE))
1206                                 break;
1207                 if (j == vcnt)
1208                         continue;
1209                 mddev->resync_mismatches += r10_bio->sectors;
1210                 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1211                         /* Don't fix anything. */
1212                         continue;
1213                 /* Ok, we need to write this bio
1214                  * First we need to fixup bv_offset, bv_len and
1215                  * bi_vecs, as the read request might have corrupted these
1216                  */
1217                 tbio->bi_vcnt = vcnt;
1218                 tbio->bi_size = r10_bio->sectors << 9;
1219                 tbio->bi_idx = 0;
1220                 tbio->bi_phys_segments = 0;
1221                 tbio->bi_hw_segments = 0;
1222                 tbio->bi_hw_front_size = 0;
1223                 tbio->bi_hw_back_size = 0;
1224                 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1225                 tbio->bi_flags |= 1 << BIO_UPTODATE;
1226                 tbio->bi_next = NULL;
1227                 tbio->bi_rw = WRITE;
1228                 tbio->bi_private = r10_bio;
1229                 tbio->bi_sector = r10_bio->devs[i].addr;
1230
1231                 for (j=0; j < vcnt ; j++) {
1232                         tbio->bi_io_vec[j].bv_offset = 0;
1233                         tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1234
1235                         memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1236                                page_address(fbio->bi_io_vec[j].bv_page),
1237                                PAGE_SIZE);
1238                 }
1239                 tbio->bi_end_io = end_sync_write;
1240
1241                 d = r10_bio->devs[i].devnum;
1242                 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1243                 atomic_inc(&r10_bio->remaining);
1244                 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1245
1246                 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1247                 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1248                 generic_make_request(tbio);
1249         }
1250
1251 done:
1252         if (atomic_dec_and_test(&r10_bio->remaining)) {
1253                 md_done_sync(mddev, r10_bio->sectors, 1);
1254                 put_buf(r10_bio);
1255         }
1256 }
1257
1258 /*
1259  * Now for the recovery code.
1260  * Recovery happens across physical sectors.
1261  * We recover all non-is_sync drives by finding the virtual address of
1262  * each, and then choose a working drive that also has that virt address.
1263  * There is a separate r10_bio for each non-in_sync drive.
1264  * Only the first two slots are in use. The first for reading,
1265  * The second for writing.
1266  *
1267  */
1268
1269 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1270 {
1271         conf_t *conf = mddev_to_conf(mddev);
1272         int i, d;
1273         struct bio *bio, *wbio;
1274
1275
1276         /* move the pages across to the second bio
1277          * and submit the write request
1278          */
1279         bio = r10_bio->devs[0].bio;
1280         wbio = r10_bio->devs[1].bio;
1281         for (i=0; i < wbio->bi_vcnt; i++) {
1282                 struct page *p = bio->bi_io_vec[i].bv_page;
1283                 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1284                 wbio->bi_io_vec[i].bv_page = p;
1285         }
1286         d = r10_bio->devs[1].devnum;
1287
1288         atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1289         md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1290         generic_make_request(wbio);
1291 }
1292
1293
1294 /*
1295  * This is a kernel thread which:
1296  *
1297  *      1.      Retries failed read operations on working mirrors.
1298  *      2.      Updates the raid superblock when problems encounter.
1299  *      3.      Performs writes following reads for array syncronising.
1300  */
1301
1302 static void raid10d(mddev_t *mddev)
1303 {
1304         r10bio_t *r10_bio;
1305         struct bio *bio;
1306         unsigned long flags;
1307         conf_t *conf = mddev_to_conf(mddev);
1308         struct list_head *head = &conf->retry_list;
1309         int unplug=0;
1310         mdk_rdev_t *rdev;
1311
1312         md_check_recovery(mddev);
1313
1314         for (;;) {
1315                 char b[BDEVNAME_SIZE];
1316                 spin_lock_irqsave(&conf->device_lock, flags);
1317
1318                 if (conf->pending_bio_list.head) {
1319                         bio = bio_list_get(&conf->pending_bio_list);
1320                         blk_remove_plug(mddev->queue);
1321                         spin_unlock_irqrestore(&conf->device_lock, flags);
1322                         /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1323                         if (bitmap_unplug(mddev->bitmap) != 0)
1324                                 printk("%s: bitmap file write failed!\n", mdname(mddev));
1325
1326                         while (bio) { /* submit pending writes */
1327                                 struct bio *next = bio->bi_next;
1328                                 bio->bi_next = NULL;
1329                                 generic_make_request(bio);
1330                                 bio = next;
1331                         }
1332                         unplug = 1;
1333
1334                         continue;
1335                 }
1336
1337                 if (list_empty(head))
1338                         break;
1339                 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1340                 list_del(head->prev);
1341                 spin_unlock_irqrestore(&conf->device_lock, flags);
1342
1343                 mddev = r10_bio->mddev;
1344                 conf = mddev_to_conf(mddev);
1345                 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1346                         sync_request_write(mddev, r10_bio);
1347                         unplug = 1;
1348                 } else  if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1349                         recovery_request_write(mddev, r10_bio);
1350                         unplug = 1;
1351                 } else {
1352                         int mirror;
1353                         bio = r10_bio->devs[r10_bio->read_slot].bio;
1354                         r10_bio->devs[r10_bio->read_slot].bio = NULL;
1355                         bio_put(bio);
1356                         mirror = read_balance(conf, r10_bio);
1357                         if (mirror == -1) {
1358                                 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1359                                        " read error for block %llu\n",
1360                                        bdevname(bio->bi_bdev,b),
1361                                        (unsigned long long)r10_bio->sector);
1362                                 raid_end_bio_io(r10_bio);
1363                         } else {
1364                                 rdev = conf->mirrors[mirror].rdev;
1365                                 if (printk_ratelimit())
1366                                         printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1367                                                " another mirror\n",
1368                                                bdevname(rdev->bdev,b),
1369                                                (unsigned long long)r10_bio->sector);
1370                                 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1371                                 r10_bio->devs[r10_bio->read_slot].bio = bio;
1372                                 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1373                                         + rdev->data_offset;
1374                                 bio->bi_bdev = rdev->bdev;
1375                                 bio->bi_rw = READ;
1376                                 bio->bi_private = r10_bio;
1377                                 bio->bi_end_io = raid10_end_read_request;
1378                                 unplug = 1;
1379                                 generic_make_request(bio);
1380                         }
1381                 }
1382         }
1383         spin_unlock_irqrestore(&conf->device_lock, flags);
1384         if (unplug)
1385                 unplug_slaves(mddev);
1386 }
1387
1388
1389 static int init_resync(conf_t *conf)
1390 {
1391         int buffs;
1392
1393         buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1394         if (conf->r10buf_pool)
1395                 BUG();
1396         conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1397         if (!conf->r10buf_pool)
1398                 return -ENOMEM;
1399         conf->next_resync = 0;
1400         return 0;
1401 }
1402
1403 /*
1404  * perform a "sync" on one "block"
1405  *
1406  * We need to make sure that no normal I/O request - particularly write
1407  * requests - conflict with active sync requests.
1408  *
1409  * This is achieved by tracking pending requests and a 'barrier' concept
1410  * that can be installed to exclude normal IO requests.
1411  *
1412  * Resync and recovery are handled very differently.
1413  * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1414  *
1415  * For resync, we iterate over virtual addresses, read all copies,
1416  * and update if there are differences.  If only one copy is live,
1417  * skip it.
1418  * For recovery, we iterate over physical addresses, read a good
1419  * value for each non-in_sync drive, and over-write.
1420  *
1421  * So, for recovery we may have several outstanding complex requests for a
1422  * given address, one for each out-of-sync device.  We model this by allocating
1423  * a number of r10_bio structures, one for each out-of-sync device.
1424  * As we setup these structures, we collect all bio's together into a list
1425  * which we then process collectively to add pages, and then process again
1426  * to pass to generic_make_request.
1427  *
1428  * The r10_bio structures are linked using a borrowed master_bio pointer.
1429  * This link is counted in ->remaining.  When the r10_bio that points to NULL
1430  * has its remaining count decremented to 0, the whole complex operation
1431  * is complete.
1432  *
1433  */
1434
1435 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1436 {
1437         conf_t *conf = mddev_to_conf(mddev);
1438         r10bio_t *r10_bio;
1439         struct bio *biolist = NULL, *bio;
1440         sector_t max_sector, nr_sectors;
1441         int disk;
1442         int i;
1443         int max_sync;
1444         int sync_blocks;
1445
1446         sector_t sectors_skipped = 0;
1447         int chunks_skipped = 0;
1448
1449         if (!conf->r10buf_pool)
1450                 if (init_resync(conf))
1451                         return 0;
1452
1453  skipped:
1454         max_sector = mddev->size << 1;
1455         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1456                 max_sector = mddev->resync_max_sectors;
1457         if (sector_nr >= max_sector) {
1458                 /* If we aborted, we need to abort the
1459                  * sync on the 'current' bitmap chucks (there can
1460                  * be several when recovering multiple devices).
1461                  * as we may have started syncing it but not finished.
1462                  * We can find the current address in
1463                  * mddev->curr_resync, but for recovery,
1464                  * we need to convert that to several
1465                  * virtual addresses.
1466                  */
1467                 if (mddev->curr_resync < max_sector) { /* aborted */
1468                         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1469                                 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1470                                                 &sync_blocks, 1);
1471                         else for (i=0; i<conf->raid_disks; i++) {
1472                                 sector_t sect =
1473                                         raid10_find_virt(conf, mddev->curr_resync, i);
1474                                 bitmap_end_sync(mddev->bitmap, sect,
1475                                                 &sync_blocks, 1);
1476                         }
1477                 } else /* completed sync */
1478                         conf->fullsync = 0;
1479
1480                 bitmap_close_sync(mddev->bitmap);
1481                 close_sync(conf);
1482                 *skipped = 1;
1483                 return sectors_skipped;
1484         }
1485         if (chunks_skipped >= conf->raid_disks) {
1486                 /* if there has been nothing to do on any drive,
1487                  * then there is nothing to do at all..
1488                  */
1489                 *skipped = 1;
1490                 return (max_sector - sector_nr) + sectors_skipped;
1491         }
1492
1493         /* make sure whole request will fit in a chunk - if chunks
1494          * are meaningful
1495          */
1496         if (conf->near_copies < conf->raid_disks &&
1497             max_sector > (sector_nr | conf->chunk_mask))
1498                 max_sector = (sector_nr | conf->chunk_mask) + 1;
1499         /*
1500          * If there is non-resync activity waiting for us then
1501          * put in a delay to throttle resync.
1502          */
1503         if (!go_faster && conf->nr_waiting)
1504                 msleep_interruptible(1000);
1505
1506         /* Again, very different code for resync and recovery.
1507          * Both must result in an r10bio with a list of bios that
1508          * have bi_end_io, bi_sector, bi_bdev set,
1509          * and bi_private set to the r10bio.
1510          * For recovery, we may actually create several r10bios
1511          * with 2 bios in each, that correspond to the bios in the main one.
1512          * In this case, the subordinate r10bios link back through a
1513          * borrowed master_bio pointer, and the counter in the master
1514          * includes a ref from each subordinate.
1515          */
1516         /* First, we decide what to do and set ->bi_end_io
1517          * To end_sync_read if we want to read, and
1518          * end_sync_write if we will want to write.
1519          */
1520
1521         max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1522         if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1523                 /* recovery... the complicated one */
1524                 int i, j, k;
1525                 r10_bio = NULL;
1526
1527                 for (i=0 ; i<conf->raid_disks; i++)
1528                         if (conf->mirrors[i].rdev &&
1529                             !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1530                                 int still_degraded = 0;
1531                                 /* want to reconstruct this device */
1532                                 r10bio_t *rb2 = r10_bio;
1533                                 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1534                                 int must_sync;
1535                                 /* Unless we are doing a full sync, we only need
1536                                  * to recover the block if it is set in the bitmap
1537                                  */
1538                                 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1539                                                               &sync_blocks, 1);
1540                                 if (sync_blocks < max_sync)
1541                                         max_sync = sync_blocks;
1542                                 if (!must_sync &&
1543                                     !conf->fullsync) {
1544                                         /* yep, skip the sync_blocks here, but don't assume
1545                                          * that there will never be anything to do here
1546                                          */
1547                                         chunks_skipped = -1;
1548                                         continue;
1549                                 }
1550
1551                                 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1552                                 raise_barrier(conf, rb2 != NULL);
1553                                 atomic_set(&r10_bio->remaining, 0);
1554
1555                                 r10_bio->master_bio = (struct bio*)rb2;
1556                                 if (rb2)
1557                                         atomic_inc(&rb2->remaining);
1558                                 r10_bio->mddev = mddev;
1559                                 set_bit(R10BIO_IsRecover, &r10_bio->state);
1560                                 r10_bio->sector = sect;
1561
1562                                 raid10_find_phys(conf, r10_bio);
1563                                 /* Need to check if this section will still be
1564                                  * degraded
1565                                  */
1566                                 for (j=0; j<conf->copies;j++) {
1567                                         int d = r10_bio->devs[j].devnum;
1568                                         if (conf->mirrors[d].rdev == NULL ||
1569                                             test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1570                                                 still_degraded = 1;
1571                                 }
1572                                 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1573                                                               &sync_blocks, still_degraded);
1574
1575                                 for (j=0; j<conf->copies;j++) {
1576                                         int d = r10_bio->devs[j].devnum;
1577                                         if (conf->mirrors[d].rdev &&
1578                                             test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1579                                                 /* This is where we read from */
1580                                                 bio = r10_bio->devs[0].bio;
1581                                                 bio->bi_next = biolist;
1582                                                 biolist = bio;
1583                                                 bio->bi_private = r10_bio;
1584                                                 bio->bi_end_io = end_sync_read;
1585                                                 bio->bi_rw = 0;
1586                                                 bio->bi_sector = r10_bio->devs[j].addr +
1587                                                         conf->mirrors[d].rdev->data_offset;
1588                                                 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1589                                                 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1590                                                 atomic_inc(&r10_bio->remaining);
1591                                                 /* and we write to 'i' */
1592
1593                                                 for (k=0; k<conf->copies; k++)
1594                                                         if (r10_bio->devs[k].devnum == i)
1595                                                                 break;
1596                                                 bio = r10_bio->devs[1].bio;
1597                                                 bio->bi_next = biolist;
1598                                                 biolist = bio;
1599                                                 bio->bi_private = r10_bio;
1600                                                 bio->bi_end_io = end_sync_write;
1601                                                 bio->bi_rw = 1;
1602                                                 bio->bi_sector = r10_bio->devs[k].addr +
1603                                                         conf->mirrors[i].rdev->data_offset;
1604                                                 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1605
1606                                                 r10_bio->devs[0].devnum = d;
1607                                                 r10_bio->devs[1].devnum = i;
1608
1609                                                 break;
1610                                         }
1611                                 }
1612                                 if (j == conf->copies) {
1613                                         /* Cannot recover, so abort the recovery */
1614                                         put_buf(r10_bio);
1615                                         r10_bio = rb2;
1616                                         if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1617                                                 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1618                                                        mdname(mddev));
1619                                         break;
1620                                 }
1621                         }
1622                 if (biolist == NULL) {
1623                         while (r10_bio) {
1624                                 r10bio_t *rb2 = r10_bio;
1625                                 r10_bio = (r10bio_t*) rb2->master_bio;
1626                                 rb2->master_bio = NULL;
1627                                 put_buf(rb2);
1628                         }
1629                         goto giveup;
1630                 }
1631         } else {
1632                 /* resync. Schedule a read for every block at this virt offset */
1633                 int count = 0;
1634
1635                 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1636                                        &sync_blocks, mddev->degraded) &&
1637                     !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1638                         /* We can skip this block */
1639                         *skipped = 1;
1640                         return sync_blocks + sectors_skipped;
1641                 }
1642                 if (sync_blocks < max_sync)
1643                         max_sync = sync_blocks;
1644                 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1645
1646                 r10_bio->mddev = mddev;
1647                 atomic_set(&r10_bio->remaining, 0);
1648                 raise_barrier(conf, 0);
1649                 conf->next_resync = sector_nr;
1650
1651                 r10_bio->master_bio = NULL;
1652                 r10_bio->sector = sector_nr;
1653                 set_bit(R10BIO_IsSync, &r10_bio->state);
1654                 raid10_find_phys(conf, r10_bio);
1655                 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1656
1657                 for (i=0; i<conf->copies; i++) {
1658                         int d = r10_bio->devs[i].devnum;
1659                         bio = r10_bio->devs[i].bio;
1660                         bio->bi_end_io = NULL;
1661                         if (conf->mirrors[d].rdev == NULL ||
1662                             test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1663                                 continue;
1664                         atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1665                         atomic_inc(&r10_bio->remaining);
1666                         bio->bi_next = biolist;
1667                         biolist = bio;
1668                         bio->bi_private = r10_bio;
1669                         bio->bi_end_io = end_sync_read;
1670                         bio->bi_rw = 0;
1671                         bio->bi_sector = r10_bio->devs[i].addr +
1672                                 conf->mirrors[d].rdev->data_offset;
1673                         bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1674                         count++;
1675                 }
1676
1677                 if (count < 2) {
1678                         for (i=0; i<conf->copies; i++) {
1679                                 int d = r10_bio->devs[i].devnum;
1680                                 if (r10_bio->devs[i].bio->bi_end_io)
1681                                         rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1682                         }
1683                         put_buf(r10_bio);
1684                         biolist = NULL;
1685                         goto giveup;
1686                 }
1687         }
1688
1689         for (bio = biolist; bio ; bio=bio->bi_next) {
1690
1691                 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1692                 if (bio->bi_end_io)
1693                         bio->bi_flags |= 1 << BIO_UPTODATE;
1694                 bio->bi_vcnt = 0;
1695                 bio->bi_idx = 0;
1696                 bio->bi_phys_segments = 0;
1697                 bio->bi_hw_segments = 0;
1698                 bio->bi_size = 0;
1699         }
1700
1701         nr_sectors = 0;
1702         if (sector_nr + max_sync < max_sector)
1703                 max_sector = sector_nr + max_sync;
1704         do {
1705                 struct page *page;
1706                 int len = PAGE_SIZE;
1707                 disk = 0;
1708                 if (sector_nr + (len>>9) > max_sector)
1709                         len = (max_sector - sector_nr) << 9;
1710                 if (len == 0)
1711                         break;
1712                 for (bio= biolist ; bio ; bio=bio->bi_next) {
1713                         page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1714                         if (bio_add_page(bio, page, len, 0) == 0) {
1715                                 /* stop here */
1716                                 struct bio *bio2;
1717                                 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1718                                 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1719                                         /* remove last page from this bio */
1720                                         bio2->bi_vcnt--;
1721                                         bio2->bi_size -= len;
1722                                         bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1723                                 }
1724                                 goto bio_full;
1725                         }
1726                         disk = i;
1727                 }
1728                 nr_sectors += len>>9;
1729                 sector_nr += len>>9;
1730         } while (biolist->bi_vcnt < RESYNC_PAGES);
1731  bio_full:
1732         r10_bio->sectors = nr_sectors;
1733
1734         while (biolist) {
1735                 bio = biolist;
1736                 biolist = biolist->bi_next;
1737
1738                 bio->bi_next = NULL;
1739                 r10_bio = bio->bi_private;
1740                 r10_bio->sectors = nr_sectors;
1741
1742                 if (bio->bi_end_io == end_sync_read) {
1743                         md_sync_acct(bio->bi_bdev, nr_sectors);
1744                         generic_make_request(bio);
1745                 }
1746         }
1747
1748         if (sectors_skipped)
1749                 /* pretend they weren't skipped, it makes
1750                  * no important difference in this case
1751                  */
1752                 md_done_sync(mddev, sectors_skipped, 1);
1753
1754         return sectors_skipped + nr_sectors;
1755  giveup:
1756         /* There is nowhere to write, so all non-sync
1757          * drives must be failed, so try the next chunk...
1758          */
1759         {
1760         sector_t sec = max_sector - sector_nr;
1761         sectors_skipped += sec;
1762         chunks_skipped ++;
1763         sector_nr = max_sector;
1764         goto skipped;
1765         }
1766 }
1767
1768 static int run(mddev_t *mddev)
1769 {
1770         conf_t *conf;
1771         int i, disk_idx;
1772         mirror_info_t *disk;
1773         mdk_rdev_t *rdev;
1774         struct list_head *tmp;
1775         int nc, fc;
1776         sector_t stride, size;
1777
1778         if (mddev->level != 10) {
1779                 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1780                        mdname(mddev), mddev->level);
1781                 goto out;
1782         }
1783         nc = mddev->layout & 255;
1784         fc = (mddev->layout >> 8) & 255;
1785         if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1786             (mddev->layout >> 16)) {
1787                 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1788                        mdname(mddev), mddev->layout);
1789                 goto out;
1790         }
1791         /*
1792          * copy the already verified devices into our private RAID10
1793          * bookkeeping area. [whatever we allocate in run(),
1794          * should be freed in stop()]
1795          */
1796         conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1797         mddev->private = conf;
1798         if (!conf) {
1799                 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1800                         mdname(mddev));
1801                 goto out;
1802         }
1803         memset(conf, 0, sizeof(*conf));
1804         conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1805                                  GFP_KERNEL);
1806         if (!conf->mirrors) {
1807                 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1808                        mdname(mddev));
1809                 goto out_free_conf;
1810         }
1811         memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1812
1813         conf->near_copies = nc;
1814         conf->far_copies = fc;
1815         conf->copies = nc*fc;
1816         conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1817         conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1818         stride = mddev->size >> (conf->chunk_shift-1);
1819         sector_div(stride, fc);
1820         conf->stride = stride << conf->chunk_shift;
1821
1822         conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1823                                                 r10bio_pool_free, conf);
1824         if (!conf->r10bio_pool) {
1825                 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1826                         mdname(mddev));
1827                 goto out_free_conf;
1828         }
1829
1830         ITERATE_RDEV(mddev, rdev, tmp) {
1831                 disk_idx = rdev->raid_disk;
1832                 if (disk_idx >= mddev->raid_disks
1833                     || disk_idx < 0)
1834                         continue;
1835                 disk = conf->mirrors + disk_idx;
1836
1837                 disk->rdev = rdev;
1838
1839                 blk_queue_stack_limits(mddev->queue,
1840                                        rdev->bdev->bd_disk->queue);
1841                 /* as we don't honour merge_bvec_fn, we must never risk
1842                  * violating it, so limit ->max_sector to one PAGE, as
1843                  * a one page request is never in violation.
1844                  */
1845                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1846                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
1847                         mddev->queue->max_sectors = (PAGE_SIZE>>9);
1848
1849                 disk->head_position = 0;
1850                 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1851                         conf->working_disks++;
1852         }
1853         conf->raid_disks = mddev->raid_disks;
1854         conf->mddev = mddev;
1855         spin_lock_init(&conf->device_lock);
1856         INIT_LIST_HEAD(&conf->retry_list);
1857
1858         spin_lock_init(&conf->resync_lock);
1859         init_waitqueue_head(&conf->wait_barrier);
1860
1861         /* need to check that every block has at least one working mirror */
1862         if (!enough(conf)) {
1863                 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1864                        mdname(mddev));
1865                 goto out_free_conf;
1866         }
1867
1868         mddev->degraded = 0;
1869         for (i = 0; i < conf->raid_disks; i++) {
1870
1871                 disk = conf->mirrors + i;
1872
1873                 if (!disk->rdev) {
1874                         disk->head_position = 0;
1875                         mddev->degraded++;
1876                 }
1877         }
1878
1879
1880         mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1881         if (!mddev->thread) {
1882                 printk(KERN_ERR
1883                        "raid10: couldn't allocate thread for %s\n",
1884                        mdname(mddev));
1885                 goto out_free_conf;
1886         }
1887
1888         printk(KERN_INFO
1889                 "raid10: raid set %s active with %d out of %d devices\n",
1890                 mdname(mddev), mddev->raid_disks - mddev->degraded,
1891                 mddev->raid_disks);
1892         /*
1893          * Ok, everything is just fine now
1894          */
1895         size = conf->stride * conf->raid_disks;
1896         sector_div(size, conf->near_copies);
1897         mddev->array_size = size/2;
1898         mddev->resync_max_sectors = size;
1899
1900         mddev->queue->unplug_fn = raid10_unplug;
1901         mddev->queue->issue_flush_fn = raid10_issue_flush;
1902
1903         /* Calculate max read-ahead size.
1904          * We need to readahead at least twice a whole stripe....
1905          * maybe...
1906          */
1907         {
1908                 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1909                 stripe /= conf->near_copies;
1910                 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1911                         mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1912         }
1913
1914         if (conf->near_copies < mddev->raid_disks)
1915                 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1916         return 0;
1917
1918 out_free_conf:
1919         if (conf->r10bio_pool)
1920                 mempool_destroy(conf->r10bio_pool);
1921         kfree(conf->mirrors);
1922         kfree(conf);
1923         mddev->private = NULL;
1924 out:
1925         return -EIO;
1926 }
1927
1928 static int stop(mddev_t *mddev)
1929 {
1930         conf_t *conf = mddev_to_conf(mddev);
1931
1932         md_unregister_thread(mddev->thread);
1933         mddev->thread = NULL;
1934         blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1935         if (conf->r10bio_pool)
1936                 mempool_destroy(conf->r10bio_pool);
1937         kfree(conf->mirrors);
1938         kfree(conf);
1939         mddev->private = NULL;
1940         return 0;
1941 }
1942
1943 static void raid10_quiesce(mddev_t *mddev, int state)
1944 {
1945         conf_t *conf = mddev_to_conf(mddev);
1946
1947         switch(state) {
1948         case 1:
1949                 raise_barrier(conf, 0);
1950                 break;
1951         case 0:
1952                 lower_barrier(conf);
1953                 break;
1954         }
1955         if (mddev->thread) {
1956                 if (mddev->bitmap)
1957                         mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1958                 else
1959                         mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1960                 md_wakeup_thread(mddev->thread);
1961         }
1962 }
1963
1964 static mdk_personality_t raid10_personality =
1965 {
1966         .name           = "raid10",
1967         .owner          = THIS_MODULE,
1968         .make_request   = make_request,
1969         .run            = run,
1970         .stop           = stop,
1971         .status         = status,
1972         .error_handler  = error,
1973         .hot_add_disk   = raid10_add_disk,
1974         .hot_remove_disk= raid10_remove_disk,
1975         .spare_active   = raid10_spare_active,
1976         .sync_request   = sync_request,
1977         .quiesce        = raid10_quiesce,
1978 };
1979
1980 static int __init raid_init(void)
1981 {
1982         return register_md_personality(RAID10, &raid10_personality);
1983 }
1984
1985 static void raid_exit(void)
1986 {
1987         unregister_md_personality(RAID10);
1988 }
1989
1990 module_init(raid_init);
1991 module_exit(raid_exit);
1992 MODULE_LICENSE("GPL");
1993 MODULE_ALIAS("md-personality-9"); /* RAID10 */