Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2...
[linux-2.6] / drivers / md / raid1.c
1 /*
2  * raid1.c : Multiple Devices driver for Linux
3  *
4  * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
5  *
6  * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
7  *
8  * RAID-1 management functions.
9  *
10  * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
11  *
12  * Fixes to reconstruction by Jakob Ã˜stergaard" <jakob@ostenfeld.dk>
13  * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * You should have received a copy of the GNU General Public License
21  * (for example /usr/src/linux/COPYING); if not, write to the Free
22  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include <linux/raid/raid1.h>
26
27 /*
28  * Number of guaranteed r1bios in case of extreme VM load:
29  */
30 #define NR_RAID1_BIOS 256
31
32 static mdk_personality_t raid1_personality;
33
34 static void unplug_slaves(mddev_t *mddev);
35
36
37 static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data)
38 {
39         struct pool_info *pi = data;
40         r1bio_t *r1_bio;
41         int size = offsetof(r1bio_t, bios[pi->raid_disks]);
42
43         /* allocate a r1bio with room for raid_disks entries in the bios array */
44         r1_bio = kmalloc(size, gfp_flags);
45         if (r1_bio)
46                 memset(r1_bio, 0, size);
47         else
48                 unplug_slaves(pi->mddev);
49
50         return r1_bio;
51 }
52
53 static void r1bio_pool_free(void *r1_bio, void *data)
54 {
55         kfree(r1_bio);
56 }
57
58 #define RESYNC_BLOCK_SIZE (64*1024)
59 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
60 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
61 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
62 #define RESYNC_WINDOW (2048*1024)
63
64 static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data)
65 {
66         struct pool_info *pi = data;
67         struct page *page;
68         r1bio_t *r1_bio;
69         struct bio *bio;
70         int i, j;
71
72         r1_bio = r1bio_pool_alloc(gfp_flags, pi);
73         if (!r1_bio) {
74                 unplug_slaves(pi->mddev);
75                 return NULL;
76         }
77
78         /*
79          * Allocate bios : 1 for reading, n-1 for writing
80          */
81         for (j = pi->raid_disks ; j-- ; ) {
82                 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
83                 if (!bio)
84                         goto out_free_bio;
85                 r1_bio->bios[j] = bio;
86         }
87         /*
88          * Allocate RESYNC_PAGES data pages and attach them to
89          * the first bio;
90          */
91         bio = r1_bio->bios[0];
92         for (i = 0; i < RESYNC_PAGES; i++) {
93                 page = alloc_page(gfp_flags);
94                 if (unlikely(!page))
95                         goto out_free_pages;
96
97                 bio->bi_io_vec[i].bv_page = page;
98         }
99
100         r1_bio->master_bio = NULL;
101
102         return r1_bio;
103
104 out_free_pages:
105         for ( ; i > 0 ; i--)
106                 __free_page(bio->bi_io_vec[i-1].bv_page);
107 out_free_bio:
108         while ( ++j < pi->raid_disks )
109                 bio_put(r1_bio->bios[j]);
110         r1bio_pool_free(r1_bio, data);
111         return NULL;
112 }
113
114 static void r1buf_pool_free(void *__r1_bio, void *data)
115 {
116         struct pool_info *pi = data;
117         int i;
118         r1bio_t *r1bio = __r1_bio;
119         struct bio *bio = r1bio->bios[0];
120
121         for (i = 0; i < RESYNC_PAGES; i++) {
122                 __free_page(bio->bi_io_vec[i].bv_page);
123                 bio->bi_io_vec[i].bv_page = NULL;
124         }
125         for (i=0 ; i < pi->raid_disks; i++)
126                 bio_put(r1bio->bios[i]);
127
128         r1bio_pool_free(r1bio, data);
129 }
130
131 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
132 {
133         int i;
134
135         for (i = 0; i < conf->raid_disks; i++) {
136                 struct bio **bio = r1_bio->bios + i;
137                 if (*bio)
138                         bio_put(*bio);
139                 *bio = NULL;
140         }
141 }
142
143 static inline void free_r1bio(r1bio_t *r1_bio)
144 {
145         unsigned long flags;
146
147         conf_t *conf = mddev_to_conf(r1_bio->mddev);
148
149         /*
150          * Wake up any possible resync thread that waits for the device
151          * to go idle.
152          */
153         spin_lock_irqsave(&conf->resync_lock, flags);
154         if (!--conf->nr_pending) {
155                 wake_up(&conf->wait_idle);
156                 wake_up(&conf->wait_resume);
157         }
158         spin_unlock_irqrestore(&conf->resync_lock, flags);
159
160         put_all_bios(conf, r1_bio);
161         mempool_free(r1_bio, conf->r1bio_pool);
162 }
163
164 static inline void put_buf(r1bio_t *r1_bio)
165 {
166         conf_t *conf = mddev_to_conf(r1_bio->mddev);
167         unsigned long flags;
168
169         mempool_free(r1_bio, conf->r1buf_pool);
170
171         spin_lock_irqsave(&conf->resync_lock, flags);
172         if (!conf->barrier)
173                 BUG();
174         --conf->barrier;
175         wake_up(&conf->wait_resume);
176         wake_up(&conf->wait_idle);
177
178         if (!--conf->nr_pending) {
179                 wake_up(&conf->wait_idle);
180                 wake_up(&conf->wait_resume);
181         }
182         spin_unlock_irqrestore(&conf->resync_lock, flags);
183 }
184
185 static void reschedule_retry(r1bio_t *r1_bio)
186 {
187         unsigned long flags;
188         mddev_t *mddev = r1_bio->mddev;
189         conf_t *conf = mddev_to_conf(mddev);
190
191         spin_lock_irqsave(&conf->device_lock, flags);
192         list_add(&r1_bio->retry_list, &conf->retry_list);
193         spin_unlock_irqrestore(&conf->device_lock, flags);
194
195         md_wakeup_thread(mddev->thread);
196 }
197
198 /*
199  * raid_end_bio_io() is called when we have finished servicing a mirrored
200  * operation and are ready to return a success/failure code to the buffer
201  * cache layer.
202  */
203 static void raid_end_bio_io(r1bio_t *r1_bio)
204 {
205         struct bio *bio = r1_bio->master_bio;
206
207         bio_endio(bio, bio->bi_size,
208                 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
209         free_r1bio(r1_bio);
210 }
211
212 /*
213  * Update disk head position estimator based on IRQ completion info.
214  */
215 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
216 {
217         conf_t *conf = mddev_to_conf(r1_bio->mddev);
218
219         conf->mirrors[disk].head_position =
220                 r1_bio->sector + (r1_bio->sectors);
221 }
222
223 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
224 {
225         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
226         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
227         int mirror;
228         conf_t *conf = mddev_to_conf(r1_bio->mddev);
229
230         if (bio->bi_size)
231                 return 1;
232         
233         mirror = r1_bio->read_disk;
234         /*
235          * this branch is our 'one mirror IO has finished' event handler:
236          */
237         if (!uptodate)
238                 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
239         else
240                 /*
241                  * Set R1BIO_Uptodate in our master bio, so that
242                  * we will return a good error code for to the higher
243                  * levels even if IO on some other mirrored buffer fails.
244                  *
245                  * The 'master' represents the composite IO operation to
246                  * user-side. So if something waits for IO, then it will
247                  * wait for the 'master' bio.
248                  */
249                 set_bit(R1BIO_Uptodate, &r1_bio->state);
250
251         update_head_pos(mirror, r1_bio);
252
253         /*
254          * we have only one bio on the read side
255          */
256         if (uptodate)
257                 raid_end_bio_io(r1_bio);
258         else {
259                 /*
260                  * oops, read error:
261                  */
262                 char b[BDEVNAME_SIZE];
263                 if (printk_ratelimit())
264                         printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
265                                bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
266                 reschedule_retry(r1_bio);
267         }
268
269         rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
270         return 0;
271 }
272
273 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
274 {
275         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
276         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
277         int mirror;
278         conf_t *conf = mddev_to_conf(r1_bio->mddev);
279
280         if (bio->bi_size)
281                 return 1;
282
283         for (mirror = 0; mirror < conf->raid_disks; mirror++)
284                 if (r1_bio->bios[mirror] == bio)
285                         break;
286
287         /*
288          * this branch is our 'one mirror IO has finished' event handler:
289          */
290         if (!uptodate)
291                 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
292         else
293                 /*
294                  * Set R1BIO_Uptodate in our master bio, so that
295                  * we will return a good error code for to the higher
296                  * levels even if IO on some other mirrored buffer fails.
297                  *
298                  * The 'master' represents the composite IO operation to
299                  * user-side. So if something waits for IO, then it will
300                  * wait for the 'master' bio.
301                  */
302                 set_bit(R1BIO_Uptodate, &r1_bio->state);
303
304         update_head_pos(mirror, r1_bio);
305
306         /*
307          *
308          * Let's see if all mirrored write operations have finished
309          * already.
310          */
311         if (atomic_dec_and_test(&r1_bio->remaining)) {
312                 md_write_end(r1_bio->mddev);
313                 raid_end_bio_io(r1_bio);
314         }
315
316         rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
317         return 0;
318 }
319
320
321 /*
322  * This routine returns the disk from which the requested read should
323  * be done. There is a per-array 'next expected sequential IO' sector
324  * number - if this matches on the next IO then we use the last disk.
325  * There is also a per-disk 'last know head position' sector that is
326  * maintained from IRQ contexts, both the normal and the resync IO
327  * completion handlers update this position correctly. If there is no
328  * perfect sequential match then we pick the disk whose head is closest.
329  *
330  * If there are 2 mirrors in the same 2 devices, performance degrades
331  * because position is mirror, not device based.
332  *
333  * The rdev for the device selected will have nr_pending incremented.
334  */
335 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
336 {
337         const unsigned long this_sector = r1_bio->sector;
338         int new_disk = conf->last_used, disk = new_disk;
339         const int sectors = r1_bio->sectors;
340         sector_t new_distance, current_distance;
341         mdk_rdev_t *new_rdev, *rdev;
342
343         rcu_read_lock();
344         /*
345          * Check if it if we can balance. We can balance on the whole
346          * device if no resync is going on, or below the resync window.
347          * We take the first readable disk when above the resync window.
348          */
349  retry:
350         if (conf->mddev->recovery_cp < MaxSector &&
351             (this_sector + sectors >= conf->next_resync)) {
352                 /* Choose the first operation device, for consistancy */
353                 new_disk = 0;
354
355                 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
356                        !new_rdev->in_sync) {
357                         new_disk++;
358                         if (new_disk == conf->raid_disks) {
359                                 new_disk = -1;
360                                 break;
361                         }
362                 }
363                 goto rb_out;
364         }
365
366
367         /* make sure the disk is operational */
368         while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
369                !new_rdev->in_sync) {
370                 if (new_disk <= 0)
371                         new_disk = conf->raid_disks;
372                 new_disk--;
373                 if (new_disk == disk) {
374                         new_disk = -1;
375                         goto rb_out;
376                 }
377         }
378         disk = new_disk;
379         /* now disk == new_disk == starting point for search */
380
381         /*
382          * Don't change to another disk for sequential reads:
383          */
384         if (conf->next_seq_sect == this_sector)
385                 goto rb_out;
386         if (this_sector == conf->mirrors[new_disk].head_position)
387                 goto rb_out;
388
389         current_distance = abs(this_sector - conf->mirrors[disk].head_position);
390
391         /* Find the disk whose head is closest */
392
393         do {
394                 if (disk <= 0)
395                         disk = conf->raid_disks;
396                 disk--;
397
398                 if ((rdev=conf->mirrors[disk].rdev) == NULL ||
399                     !rdev->in_sync)
400                         continue;
401
402                 if (!atomic_read(&rdev->nr_pending)) {
403                         new_disk = disk;
404                         new_rdev = rdev;
405                         break;
406                 }
407                 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
408                 if (new_distance < current_distance) {
409                         current_distance = new_distance;
410                         new_disk = disk;
411                         new_rdev = rdev;
412                 }
413         } while (disk != conf->last_used);
414
415 rb_out:
416
417
418         if (new_disk >= 0) {
419                 conf->next_seq_sect = this_sector + sectors;
420                 conf->last_used = new_disk;
421                 atomic_inc(&new_rdev->nr_pending);
422                 if (!new_rdev->in_sync) {
423                         /* cannot risk returning a device that failed
424                          * before we inc'ed nr_pending
425                          */
426                         atomic_dec(&new_rdev->nr_pending);
427                         goto retry;
428                 }
429         }
430         rcu_read_unlock();
431
432         return new_disk;
433 }
434
435 static void unplug_slaves(mddev_t *mddev)
436 {
437         conf_t *conf = mddev_to_conf(mddev);
438         int i;
439
440         rcu_read_lock();
441         for (i=0; i<mddev->raid_disks; i++) {
442                 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
443                 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
444                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
445
446                         atomic_inc(&rdev->nr_pending);
447                         rcu_read_unlock();
448
449                         if (r_queue->unplug_fn)
450                                 r_queue->unplug_fn(r_queue);
451
452                         rdev_dec_pending(rdev, mddev);
453                         rcu_read_lock();
454                 }
455         }
456         rcu_read_unlock();
457 }
458
459 static void raid1_unplug(request_queue_t *q)
460 {
461         unplug_slaves(q->queuedata);
462 }
463
464 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
465                              sector_t *error_sector)
466 {
467         mddev_t *mddev = q->queuedata;
468         conf_t *conf = mddev_to_conf(mddev);
469         int i, ret = 0;
470
471         rcu_read_lock();
472         for (i=0; i<mddev->raid_disks && ret == 0; i++) {
473                 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
474                 if (rdev && !rdev->faulty) {
475                         struct block_device *bdev = rdev->bdev;
476                         request_queue_t *r_queue = bdev_get_queue(bdev);
477
478                         if (!r_queue->issue_flush_fn)
479                                 ret = -EOPNOTSUPP;
480                         else {
481                                 atomic_inc(&rdev->nr_pending);
482                                 rcu_read_unlock();
483                                 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
484                                                               error_sector);
485                                 rdev_dec_pending(rdev, mddev);
486                                 rcu_read_lock();
487                         }
488                 }
489         }
490         rcu_read_unlock();
491         return ret;
492 }
493
494 /*
495  * Throttle resync depth, so that we can both get proper overlapping of
496  * requests, but are still able to handle normal requests quickly.
497  */
498 #define RESYNC_DEPTH 32
499
500 static void device_barrier(conf_t *conf, sector_t sect)
501 {
502         spin_lock_irq(&conf->resync_lock);
503         wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
504                             conf->resync_lock, unplug_slaves(conf->mddev));
505         
506         if (!conf->barrier++) {
507                 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
508                                     conf->resync_lock, unplug_slaves(conf->mddev));
509                 if (conf->nr_pending)
510                         BUG();
511         }
512         wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
513                             conf->resync_lock, unplug_slaves(conf->mddev));
514         conf->next_resync = sect;
515         spin_unlock_irq(&conf->resync_lock);
516 }
517
518 static int make_request(request_queue_t *q, struct bio * bio)
519 {
520         mddev_t *mddev = q->queuedata;
521         conf_t *conf = mddev_to_conf(mddev);
522         mirror_info_t *mirror;
523         r1bio_t *r1_bio;
524         struct bio *read_bio;
525         int i, disks;
526         mdk_rdev_t *rdev;
527
528         /*
529          * Register the new request and wait if the reconstruction
530          * thread has put up a bar for new requests.
531          * Continue immediately if no resync is active currently.
532          */
533         spin_lock_irq(&conf->resync_lock);
534         wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
535         conf->nr_pending++;
536         spin_unlock_irq(&conf->resync_lock);
537
538         if (bio_data_dir(bio)==WRITE) {
539                 disk_stat_inc(mddev->gendisk, writes);
540                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
541         } else {
542                 disk_stat_inc(mddev->gendisk, reads);
543                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
544         }
545
546         /*
547          * make_request() can abort the operation when READA is being
548          * used and no empty request is available.
549          *
550          */
551         r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
552
553         r1_bio->master_bio = bio;
554         r1_bio->sectors = bio->bi_size >> 9;
555
556         r1_bio->mddev = mddev;
557         r1_bio->sector = bio->bi_sector;
558
559         r1_bio->state = 0;
560
561         if (bio_data_dir(bio) == READ) {
562                 /*
563                  * read balancing logic:
564                  */
565                 int rdisk = read_balance(conf, r1_bio);
566
567                 if (rdisk < 0) {
568                         /* couldn't find anywhere to read from */
569                         raid_end_bio_io(r1_bio);
570                         return 0;
571                 }
572                 mirror = conf->mirrors + rdisk;
573
574                 r1_bio->read_disk = rdisk;
575
576                 read_bio = bio_clone(bio, GFP_NOIO);
577
578                 r1_bio->bios[rdisk] = read_bio;
579
580                 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
581                 read_bio->bi_bdev = mirror->rdev->bdev;
582                 read_bio->bi_end_io = raid1_end_read_request;
583                 read_bio->bi_rw = READ;
584                 read_bio->bi_private = r1_bio;
585
586                 generic_make_request(read_bio);
587                 return 0;
588         }
589
590         /*
591          * WRITE:
592          */
593         /* first select target devices under spinlock and
594          * inc refcount on their rdev.  Record them by setting
595          * bios[x] to bio
596          */
597         disks = conf->raid_disks;
598         rcu_read_lock();
599         for (i = 0;  i < disks; i++) {
600                 if ((rdev=conf->mirrors[i].rdev) != NULL &&
601                     !rdev->faulty) {
602                         atomic_inc(&rdev->nr_pending);
603                         if (rdev->faulty) {
604                                 atomic_dec(&rdev->nr_pending);
605                                 r1_bio->bios[i] = NULL;
606                         } else
607                                 r1_bio->bios[i] = bio;
608                 } else
609                         r1_bio->bios[i] = NULL;
610         }
611         rcu_read_unlock();
612
613         atomic_set(&r1_bio->remaining, 1);
614         md_write_start(mddev);
615         for (i = 0; i < disks; i++) {
616                 struct bio *mbio;
617                 if (!r1_bio->bios[i])
618                         continue;
619
620                 mbio = bio_clone(bio, GFP_NOIO);
621                 r1_bio->bios[i] = mbio;
622
623                 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
624                 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
625                 mbio->bi_end_io = raid1_end_write_request;
626                 mbio->bi_rw = WRITE;
627                 mbio->bi_private = r1_bio;
628
629                 atomic_inc(&r1_bio->remaining);
630                 generic_make_request(mbio);
631         }
632
633         if (atomic_dec_and_test(&r1_bio->remaining)) {
634                 md_write_end(mddev);
635                 raid_end_bio_io(r1_bio);
636         }
637
638         return 0;
639 }
640
641 static void status(struct seq_file *seq, mddev_t *mddev)
642 {
643         conf_t *conf = mddev_to_conf(mddev);
644         int i;
645
646         seq_printf(seq, " [%d/%d] [", conf->raid_disks,
647                                                 conf->working_disks);
648         for (i = 0; i < conf->raid_disks; i++)
649                 seq_printf(seq, "%s",
650                               conf->mirrors[i].rdev &&
651                               conf->mirrors[i].rdev->in_sync ? "U" : "_");
652         seq_printf(seq, "]");
653 }
654
655
656 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
657 {
658         char b[BDEVNAME_SIZE];
659         conf_t *conf = mddev_to_conf(mddev);
660
661         /*
662          * If it is not operational, then we have already marked it as dead
663          * else if it is the last working disks, ignore the error, let the
664          * next level up know.
665          * else mark the drive as failed
666          */
667         if (rdev->in_sync
668             && conf->working_disks == 1)
669                 /*
670                  * Don't fail the drive, act as though we were just a
671                  * normal single drive
672                  */
673                 return;
674         if (rdev->in_sync) {
675                 mddev->degraded++;
676                 conf->working_disks--;
677                 /*
678                  * if recovery is running, make sure it aborts.
679                  */
680                 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
681         }
682         rdev->in_sync = 0;
683         rdev->faulty = 1;
684         mddev->sb_dirty = 1;
685         printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
686                 "       Operation continuing on %d devices\n",
687                 bdevname(rdev->bdev,b), conf->working_disks);
688 }
689
690 static void print_conf(conf_t *conf)
691 {
692         int i;
693         mirror_info_t *tmp;
694
695         printk("RAID1 conf printout:\n");
696         if (!conf) {
697                 printk("(!conf)\n");
698                 return;
699         }
700         printk(" --- wd:%d rd:%d\n", conf->working_disks,
701                 conf->raid_disks);
702
703         for (i = 0; i < conf->raid_disks; i++) {
704                 char b[BDEVNAME_SIZE];
705                 tmp = conf->mirrors + i;
706                 if (tmp->rdev)
707                         printk(" disk %d, wo:%d, o:%d, dev:%s\n",
708                                 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
709                                 bdevname(tmp->rdev->bdev,b));
710         }
711 }
712
713 static void close_sync(conf_t *conf)
714 {
715         spin_lock_irq(&conf->resync_lock);
716         wait_event_lock_irq(conf->wait_resume, !conf->barrier,
717                             conf->resync_lock,  unplug_slaves(conf->mddev));
718         spin_unlock_irq(&conf->resync_lock);
719
720         if (conf->barrier) BUG();
721         if (waitqueue_active(&conf->wait_idle)) BUG();
722
723         mempool_destroy(conf->r1buf_pool);
724         conf->r1buf_pool = NULL;
725 }
726
727 static int raid1_spare_active(mddev_t *mddev)
728 {
729         int i;
730         conf_t *conf = mddev->private;
731         mirror_info_t *tmp;
732
733         /*
734          * Find all failed disks within the RAID1 configuration 
735          * and mark them readable
736          */
737         for (i = 0; i < conf->raid_disks; i++) {
738                 tmp = conf->mirrors + i;
739                 if (tmp->rdev 
740                     && !tmp->rdev->faulty
741                     && !tmp->rdev->in_sync) {
742                         conf->working_disks++;
743                         mddev->degraded--;
744                         tmp->rdev->in_sync = 1;
745                 }
746         }
747
748         print_conf(conf);
749         return 0;
750 }
751
752
753 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
754 {
755         conf_t *conf = mddev->private;
756         int found = 0;
757         int mirror;
758         mirror_info_t *p;
759
760         for (mirror=0; mirror < mddev->raid_disks; mirror++)
761                 if ( !(p=conf->mirrors+mirror)->rdev) {
762
763                         blk_queue_stack_limits(mddev->queue,
764                                                rdev->bdev->bd_disk->queue);
765                         /* as we don't honour merge_bvec_fn, we must never risk
766                          * violating it, so limit ->max_sector to one PAGE, as
767                          * a one page request is never in violation.
768                          */
769                         if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
770                             mddev->queue->max_sectors > (PAGE_SIZE>>9))
771                                 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
772
773                         p->head_position = 0;
774                         rdev->raid_disk = mirror;
775                         found = 1;
776                         p->rdev = rdev;
777                         break;
778                 }
779
780         print_conf(conf);
781         return found;
782 }
783
784 static int raid1_remove_disk(mddev_t *mddev, int number)
785 {
786         conf_t *conf = mddev->private;
787         int err = 0;
788         mdk_rdev_t *rdev;
789         mirror_info_t *p = conf->mirrors+ number;
790
791         print_conf(conf);
792         rdev = p->rdev;
793         if (rdev) {
794                 if (rdev->in_sync ||
795                     atomic_read(&rdev->nr_pending)) {
796                         err = -EBUSY;
797                         goto abort;
798                 }
799                 p->rdev = NULL;
800                 synchronize_rcu();
801                 if (atomic_read(&rdev->nr_pending)) {
802                         /* lost the race, try later */
803                         err = -EBUSY;
804                         p->rdev = rdev;
805                 }
806         }
807 abort:
808
809         print_conf(conf);
810         return err;
811 }
812
813
814 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
815 {
816         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
817         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
818         conf_t *conf = mddev_to_conf(r1_bio->mddev);
819
820         if (bio->bi_size)
821                 return 1;
822
823         if (r1_bio->bios[r1_bio->read_disk] != bio)
824                 BUG();
825         update_head_pos(r1_bio->read_disk, r1_bio);
826         /*
827          * we have read a block, now it needs to be re-written,
828          * or re-read if the read failed.
829          * We don't do much here, just schedule handling by raid1d
830          */
831         if (!uptodate)
832                 md_error(r1_bio->mddev,
833                          conf->mirrors[r1_bio->read_disk].rdev);
834         else
835                 set_bit(R1BIO_Uptodate, &r1_bio->state);
836         rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
837         reschedule_retry(r1_bio);
838         return 0;
839 }
840
841 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
842 {
843         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
844         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
845         mddev_t *mddev = r1_bio->mddev;
846         conf_t *conf = mddev_to_conf(mddev);
847         int i;
848         int mirror=0;
849
850         if (bio->bi_size)
851                 return 1;
852
853         for (i = 0; i < conf->raid_disks; i++)
854                 if (r1_bio->bios[i] == bio) {
855                         mirror = i;
856                         break;
857                 }
858         if (!uptodate)
859                 md_error(mddev, conf->mirrors[mirror].rdev);
860         update_head_pos(mirror, r1_bio);
861
862         if (atomic_dec_and_test(&r1_bio->remaining)) {
863                 md_done_sync(mddev, r1_bio->sectors, uptodate);
864                 put_buf(r1_bio);
865         }
866         rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
867         return 0;
868 }
869
870 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
871 {
872         conf_t *conf = mddev_to_conf(mddev);
873         int i;
874         int disks = conf->raid_disks;
875         struct bio *bio, *wbio;
876
877         bio = r1_bio->bios[r1_bio->read_disk];
878
879         /*
880          * schedule writes
881          */
882         if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
883                 /*
884                  * There is no point trying a read-for-reconstruct as
885                  * reconstruct is about to be aborted
886                  */
887                 char b[BDEVNAME_SIZE];
888                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
889                         " for block %llu\n",
890                         bdevname(bio->bi_bdev,b), 
891                         (unsigned long long)r1_bio->sector);
892                 md_done_sync(mddev, r1_bio->sectors, 0);
893                 put_buf(r1_bio);
894                 return;
895         }
896
897         atomic_set(&r1_bio->remaining, 1);
898         for (i = 0; i < disks ; i++) {
899                 wbio = r1_bio->bios[i];
900                 if (wbio->bi_end_io != end_sync_write)
901                         continue;
902
903                 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
904                 atomic_inc(&r1_bio->remaining);
905                 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
906                 generic_make_request(wbio);
907         }
908
909         if (atomic_dec_and_test(&r1_bio->remaining)) {
910                 md_done_sync(mddev, r1_bio->sectors, 1);
911                 put_buf(r1_bio);
912         }
913 }
914
915 /*
916  * This is a kernel thread which:
917  *
918  *      1.      Retries failed read operations on working mirrors.
919  *      2.      Updates the raid superblock when problems encounter.
920  *      3.      Performs writes following reads for array syncronising.
921  */
922
923 static void raid1d(mddev_t *mddev)
924 {
925         r1bio_t *r1_bio;
926         struct bio *bio;
927         unsigned long flags;
928         conf_t *conf = mddev_to_conf(mddev);
929         struct list_head *head = &conf->retry_list;
930         int unplug=0;
931         mdk_rdev_t *rdev;
932
933         md_check_recovery(mddev);
934         md_handle_safemode(mddev);
935         
936         for (;;) {
937                 char b[BDEVNAME_SIZE];
938                 spin_lock_irqsave(&conf->device_lock, flags);
939                 if (list_empty(head))
940                         break;
941                 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
942                 list_del(head->prev);
943                 spin_unlock_irqrestore(&conf->device_lock, flags);
944
945                 mddev = r1_bio->mddev;
946                 conf = mddev_to_conf(mddev);
947                 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
948                         sync_request_write(mddev, r1_bio);
949                         unplug = 1;
950                 } else {
951                         int disk;
952                         bio = r1_bio->bios[r1_bio->read_disk];
953                         if ((disk=read_balance(conf, r1_bio)) == -1) {
954                                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
955                                        " read error for block %llu\n",
956                                        bdevname(bio->bi_bdev,b),
957                                        (unsigned long long)r1_bio->sector);
958                                 raid_end_bio_io(r1_bio);
959                         } else {
960                                 r1_bio->bios[r1_bio->read_disk] = NULL;
961                                 r1_bio->read_disk = disk;
962                                 bio_put(bio);
963                                 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
964                                 r1_bio->bios[r1_bio->read_disk] = bio;
965                                 rdev = conf->mirrors[disk].rdev;
966                                 if (printk_ratelimit())
967                                         printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
968                                                " another mirror\n",
969                                                bdevname(rdev->bdev,b),
970                                                (unsigned long long)r1_bio->sector);
971                                 bio->bi_sector = r1_bio->sector + rdev->data_offset;
972                                 bio->bi_bdev = rdev->bdev;
973                                 bio->bi_end_io = raid1_end_read_request;
974                                 bio->bi_rw = READ;
975                                 bio->bi_private = r1_bio;
976                                 unplug = 1;
977                                 generic_make_request(bio);
978                         }
979                 }
980         }
981         spin_unlock_irqrestore(&conf->device_lock, flags);
982         if (unplug)
983                 unplug_slaves(mddev);
984 }
985
986
987 static int init_resync(conf_t *conf)
988 {
989         int buffs;
990
991         buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
992         if (conf->r1buf_pool)
993                 BUG();
994         conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
995                                           conf->poolinfo);
996         if (!conf->r1buf_pool)
997                 return -ENOMEM;
998         conf->next_resync = 0;
999         return 0;
1000 }
1001
1002 /*
1003  * perform a "sync" on one "block"
1004  *
1005  * We need to make sure that no normal I/O request - particularly write
1006  * requests - conflict with active sync requests.
1007  *
1008  * This is achieved by tracking pending requests and a 'barrier' concept
1009  * that can be installed to exclude normal IO requests.
1010  */
1011
1012 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
1013 {
1014         conf_t *conf = mddev_to_conf(mddev);
1015         mirror_info_t *mirror;
1016         r1bio_t *r1_bio;
1017         struct bio *bio;
1018         sector_t max_sector, nr_sectors;
1019         int disk;
1020         int i;
1021         int write_targets = 0;
1022
1023         if (!conf->r1buf_pool)
1024                 if (init_resync(conf))
1025                         return -ENOMEM;
1026
1027         max_sector = mddev->size << 1;
1028         if (sector_nr >= max_sector) {
1029                 close_sync(conf);
1030                 return 0;
1031         }
1032
1033         /*
1034          * If there is non-resync activity waiting for us then
1035          * put in a delay to throttle resync.
1036          */
1037         if (!go_faster && waitqueue_active(&conf->wait_resume))
1038                 msleep_interruptible(1000);
1039         device_barrier(conf, sector_nr + RESYNC_SECTORS);
1040
1041         /*
1042          * If reconstructing, and >1 working disc,
1043          * could dedicate one to rebuild and others to
1044          * service read requests ..
1045          */
1046         disk = conf->last_used;
1047         /* make sure disk is operational */
1048
1049         while (conf->mirrors[disk].rdev == NULL ||
1050                !conf->mirrors[disk].rdev->in_sync) {
1051                 if (disk <= 0)
1052                         disk = conf->raid_disks;
1053                 disk--;
1054                 if (disk == conf->last_used)
1055                         break;
1056         }
1057         conf->last_used = disk;
1058         atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1059
1060
1061         mirror = conf->mirrors + disk;
1062
1063         r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1064
1065         spin_lock_irq(&conf->resync_lock);
1066         conf->nr_pending++;
1067         spin_unlock_irq(&conf->resync_lock);
1068
1069         r1_bio->mddev = mddev;
1070         r1_bio->sector = sector_nr;
1071         set_bit(R1BIO_IsSync, &r1_bio->state);
1072         r1_bio->read_disk = disk;
1073
1074         for (i=0; i < conf->raid_disks; i++) {
1075                 bio = r1_bio->bios[i];
1076
1077                 /* take from bio_init */
1078                 bio->bi_next = NULL;
1079                 bio->bi_flags |= 1 << BIO_UPTODATE;
1080                 bio->bi_rw = 0;
1081                 bio->bi_vcnt = 0;
1082                 bio->bi_idx = 0;
1083                 bio->bi_phys_segments = 0;
1084                 bio->bi_hw_segments = 0;
1085                 bio->bi_size = 0;
1086                 bio->bi_end_io = NULL;
1087                 bio->bi_private = NULL;
1088
1089                 if (i == disk) {
1090                         bio->bi_rw = READ;
1091                         bio->bi_end_io = end_sync_read;
1092                 } else if (conf->mirrors[i].rdev &&
1093                            !conf->mirrors[i].rdev->faulty &&
1094                            (!conf->mirrors[i].rdev->in_sync ||
1095                             sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1096                         bio->bi_rw = WRITE;
1097                         bio->bi_end_io = end_sync_write;
1098                         write_targets ++;
1099                 } else
1100                         continue;
1101                 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1102                 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1103                 bio->bi_private = r1_bio;
1104         }
1105         if (write_targets == 0) {
1106                 /* There is nowhere to write, so all non-sync
1107                  * drives must be failed - so we are finished
1108                  */
1109                 int rv = max_sector - sector_nr;
1110                 md_done_sync(mddev, rv, 1);
1111                 put_buf(r1_bio);
1112                 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1113                 return rv;
1114         }
1115
1116         nr_sectors = 0;
1117         do {
1118                 struct page *page;
1119                 int len = PAGE_SIZE;
1120                 if (sector_nr + (len>>9) > max_sector)
1121                         len = (max_sector - sector_nr) << 9;
1122                 if (len == 0)
1123                         break;
1124                 for (i=0 ; i < conf->raid_disks; i++) {
1125                         bio = r1_bio->bios[i];
1126                         if (bio->bi_end_io) {
1127                                 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1128                                 if (bio_add_page(bio, page, len, 0) == 0) {
1129                                         /* stop here */
1130                                         r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1131                                         while (i > 0) {
1132                                                 i--;
1133                                                 bio = r1_bio->bios[i];
1134                                                 if (bio->bi_end_io==NULL) continue;
1135                                                 /* remove last page from this bio */
1136                                                 bio->bi_vcnt--;
1137                                                 bio->bi_size -= len;
1138                                                 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1139                                         }
1140                                         goto bio_full;
1141                                 }
1142                         }
1143                 }
1144                 nr_sectors += len>>9;
1145                 sector_nr += len>>9;
1146         } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1147  bio_full:
1148         bio = r1_bio->bios[disk];
1149         r1_bio->sectors = nr_sectors;
1150
1151         md_sync_acct(mirror->rdev->bdev, nr_sectors);
1152
1153         generic_make_request(bio);
1154
1155         return nr_sectors;
1156 }
1157
1158 static int run(mddev_t *mddev)
1159 {
1160         conf_t *conf;
1161         int i, j, disk_idx;
1162         mirror_info_t *disk;
1163         mdk_rdev_t *rdev;
1164         struct list_head *tmp;
1165
1166         if (mddev->level != 1) {
1167                 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1168                        mdname(mddev), mddev->level);
1169                 goto out;
1170         }
1171         /*
1172          * copy the already verified devices into our private RAID1
1173          * bookkeeping area. [whatever we allocate in run(),
1174          * should be freed in stop()]
1175          */
1176         conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1177         mddev->private = conf;
1178         if (!conf)
1179                 goto out_no_mem;
1180
1181         memset(conf, 0, sizeof(*conf));
1182         conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks, 
1183                                  GFP_KERNEL);
1184         if (!conf->mirrors)
1185                 goto out_no_mem;
1186
1187         memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1188
1189         conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1190         if (!conf->poolinfo)
1191                 goto out_no_mem;
1192         conf->poolinfo->mddev = mddev;
1193         conf->poolinfo->raid_disks = mddev->raid_disks;
1194         conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1195                                           r1bio_pool_free,
1196                                           conf->poolinfo);
1197         if (!conf->r1bio_pool)
1198                 goto out_no_mem;
1199
1200         ITERATE_RDEV(mddev, rdev, tmp) {
1201                 disk_idx = rdev->raid_disk;
1202                 if (disk_idx >= mddev->raid_disks
1203                     || disk_idx < 0)
1204                         continue;
1205                 disk = conf->mirrors + disk_idx;
1206
1207                 disk->rdev = rdev;
1208
1209                 blk_queue_stack_limits(mddev->queue,
1210                                        rdev->bdev->bd_disk->queue);
1211                 /* as we don't honour merge_bvec_fn, we must never risk
1212                  * violating it, so limit ->max_sector to one PAGE, as
1213                  * a one page request is never in violation.
1214                  */
1215                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1216                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
1217                         blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1218
1219                 disk->head_position = 0;
1220                 if (!rdev->faulty && rdev->in_sync)
1221                         conf->working_disks++;
1222         }
1223         conf->raid_disks = mddev->raid_disks;
1224         conf->mddev = mddev;
1225         spin_lock_init(&conf->device_lock);
1226         INIT_LIST_HEAD(&conf->retry_list);
1227         if (conf->working_disks == 1)
1228                 mddev->recovery_cp = MaxSector;
1229
1230         spin_lock_init(&conf->resync_lock);
1231         init_waitqueue_head(&conf->wait_idle);
1232         init_waitqueue_head(&conf->wait_resume);
1233
1234         if (!conf->working_disks) {
1235                 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1236                         mdname(mddev));
1237                 goto out_free_conf;
1238         }
1239
1240         mddev->degraded = 0;
1241         for (i = 0; i < conf->raid_disks; i++) {
1242
1243                 disk = conf->mirrors + i;
1244
1245                 if (!disk->rdev) {
1246                         disk->head_position = 0;
1247                         mddev->degraded++;
1248                 }
1249         }
1250
1251         /*
1252          * find the first working one and use it as a starting point
1253          * to read balancing.
1254          */
1255         for (j = 0; j < conf->raid_disks &&
1256                      (!conf->mirrors[j].rdev ||
1257                       !conf->mirrors[j].rdev->in_sync) ; j++)
1258                 /* nothing */;
1259         conf->last_used = j;
1260
1261
1262
1263         {
1264                 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1265                 if (!mddev->thread) {
1266                         printk(KERN_ERR 
1267                                 "raid1: couldn't allocate thread for %s\n", 
1268                                 mdname(mddev));
1269                         goto out_free_conf;
1270                 }
1271         }
1272         printk(KERN_INFO 
1273                 "raid1: raid set %s active with %d out of %d mirrors\n",
1274                 mdname(mddev), mddev->raid_disks - mddev->degraded, 
1275                 mddev->raid_disks);
1276         /*
1277          * Ok, everything is just fine now
1278          */
1279         mddev->array_size = mddev->size;
1280
1281         mddev->queue->unplug_fn = raid1_unplug;
1282         mddev->queue->issue_flush_fn = raid1_issue_flush;
1283
1284         return 0;
1285
1286 out_no_mem:
1287         printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1288                mdname(mddev));
1289
1290 out_free_conf:
1291         if (conf) {
1292                 if (conf->r1bio_pool)
1293                         mempool_destroy(conf->r1bio_pool);
1294                 if (conf->mirrors)
1295                         kfree(conf->mirrors);
1296                 if (conf->poolinfo)
1297                         kfree(conf->poolinfo);
1298                 kfree(conf);
1299                 mddev->private = NULL;
1300         }
1301 out:
1302         return -EIO;
1303 }
1304
1305 static int stop(mddev_t *mddev)
1306 {
1307         conf_t *conf = mddev_to_conf(mddev);
1308
1309         md_unregister_thread(mddev->thread);
1310         mddev->thread = NULL;
1311         blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1312         if (conf->r1bio_pool)
1313                 mempool_destroy(conf->r1bio_pool);
1314         if (conf->mirrors)
1315                 kfree(conf->mirrors);
1316         if (conf->poolinfo)
1317                 kfree(conf->poolinfo);
1318         kfree(conf);
1319         mddev->private = NULL;
1320         return 0;
1321 }
1322
1323 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1324 {
1325         /* no resync is happening, and there is enough space
1326          * on all devices, so we can resize.
1327          * We need to make sure resync covers any new space.
1328          * If the array is shrinking we should possibly wait until
1329          * any io in the removed space completes, but it hardly seems
1330          * worth it.
1331          */
1332         mddev->array_size = sectors>>1;
1333         set_capacity(mddev->gendisk, mddev->array_size << 1);
1334         mddev->changed = 1;
1335         if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1336                 mddev->recovery_cp = mddev->size << 1;
1337                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1338         }
1339         mddev->size = mddev->array_size;
1340         return 0;
1341 }
1342
1343 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1344 {
1345         /* We need to:
1346          * 1/ resize the r1bio_pool
1347          * 2/ resize conf->mirrors
1348          *
1349          * We allocate a new r1bio_pool if we can.
1350          * Then raise a device barrier and wait until all IO stops.
1351          * Then resize conf->mirrors and swap in the new r1bio pool.
1352          */
1353         mempool_t *newpool, *oldpool;
1354         struct pool_info *newpoolinfo;
1355         mirror_info_t *newmirrors;
1356         conf_t *conf = mddev_to_conf(mddev);
1357
1358         int d;
1359
1360         for (d= raid_disks; d < conf->raid_disks; d++)
1361                 if (conf->mirrors[d].rdev)
1362                         return -EBUSY;
1363
1364         newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1365         if (!newpoolinfo)
1366                 return -ENOMEM;
1367         newpoolinfo->mddev = mddev;
1368         newpoolinfo->raid_disks = raid_disks;
1369
1370         newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1371                                  r1bio_pool_free, newpoolinfo);
1372         if (!newpool) {
1373                 kfree(newpoolinfo);
1374                 return -ENOMEM;
1375         }
1376         newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1377         if (!newmirrors) {
1378                 kfree(newpoolinfo);
1379                 mempool_destroy(newpool);
1380                 return -ENOMEM;
1381         }
1382         memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1383
1384         spin_lock_irq(&conf->resync_lock);
1385         conf->barrier++;
1386         wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1387                             conf->resync_lock, unplug_slaves(mddev));
1388         spin_unlock_irq(&conf->resync_lock);
1389
1390         /* ok, everything is stopped */
1391         oldpool = conf->r1bio_pool;
1392         conf->r1bio_pool = newpool;
1393         for (d=0; d < raid_disks && d < conf->raid_disks; d++)
1394                 newmirrors[d] = conf->mirrors[d];
1395         kfree(conf->mirrors);
1396         conf->mirrors = newmirrors;
1397         kfree(conf->poolinfo);
1398         conf->poolinfo = newpoolinfo;
1399
1400         mddev->degraded += (raid_disks - conf->raid_disks);
1401         conf->raid_disks = mddev->raid_disks = raid_disks;
1402
1403         spin_lock_irq(&conf->resync_lock);
1404         conf->barrier--;
1405         spin_unlock_irq(&conf->resync_lock);
1406         wake_up(&conf->wait_resume);
1407         wake_up(&conf->wait_idle);
1408
1409
1410         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1411         md_wakeup_thread(mddev->thread);
1412
1413         mempool_destroy(oldpool);
1414         return 0;
1415 }
1416
1417
1418 static mdk_personality_t raid1_personality =
1419 {
1420         .name           = "raid1",
1421         .owner          = THIS_MODULE,
1422         .make_request   = make_request,
1423         .run            = run,
1424         .stop           = stop,
1425         .status         = status,
1426         .error_handler  = error,
1427         .hot_add_disk   = raid1_add_disk,
1428         .hot_remove_disk= raid1_remove_disk,
1429         .spare_active   = raid1_spare_active,
1430         .sync_request   = sync_request,
1431         .resize         = raid1_resize,
1432         .reshape        = raid1_reshape,
1433 };
1434
1435 static int __init raid_init(void)
1436 {
1437         return register_md_personality(RAID1, &raid1_personality);
1438 }
1439
1440 static void raid_exit(void)
1441 {
1442         unregister_md_personality(RAID1);
1443 }
1444
1445 module_init(raid_init);
1446 module_exit(raid_exit);
1447 MODULE_LICENSE("GPL");
1448 MODULE_ALIAS("md-personality-3"); /* RAID1 */