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