patch-2_6_7-vs1_9_1_12
[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 0;
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->in_sync && (this_sector + sectors >= conf->next_resync)) {
380                 /* make sure that disk is operational */
381                 new_disk = 0;
382
383                 while (!conf->mirrors[new_disk].rdev ||
384                        !conf->mirrors[new_disk].rdev->in_sync) {
385                         new_disk++;
386                         if (new_disk == conf->raid_disks) {
387                                 new_disk = 0;
388                                 break;
389                         }
390                 }
391                 goto rb_out;
392         }
393
394
395         /* make sure the disk is operational */
396         while (!conf->mirrors[new_disk].rdev ||
397                !conf->mirrors[new_disk].rdev->in_sync) {
398                 if (new_disk <= 0)
399                         new_disk = conf->raid_disks;
400                 new_disk--;
401                 if (new_disk == disk) {
402                         new_disk = conf->last_used;
403                         goto rb_out;
404                 }
405         }
406         disk = new_disk;
407         /* now disk == new_disk == starting point for search */
408
409         /*
410          * Don't change to another disk for sequential reads:
411          */
412         if (conf->next_seq_sect == this_sector)
413                 goto rb_out;
414         if (this_sector == conf->mirrors[new_disk].head_position)
415                 goto rb_out;
416
417         current_distance = abs(this_sector - conf->mirrors[disk].head_position);
418
419         /* Find the disk whose head is closest */
420
421         do {
422                 if (disk <= 0)
423                         disk = conf->raid_disks;
424                 disk--;
425
426                 if (!conf->mirrors[disk].rdev ||
427                     !conf->mirrors[disk].rdev->in_sync)
428                         continue;
429
430                 if (!atomic_read(&conf->mirrors[disk].rdev->nr_pending)) {
431                         new_disk = disk;
432                         break;
433                 }
434                 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
435                 if (new_distance < current_distance) {
436                         current_distance = new_distance;
437                         new_disk = disk;
438                 }
439         } while (disk != conf->last_used);
440
441 rb_out:
442         r1_bio->read_disk = new_disk;
443         conf->next_seq_sect = this_sector + sectors;
444
445         conf->last_used = new_disk;
446
447         if (conf->mirrors[new_disk].rdev)
448                 atomic_inc(&conf->mirrors[new_disk].rdev->nr_pending);
449         spin_unlock_irq(&conf->device_lock);
450
451         return new_disk;
452 }
453
454 static void unplug_slaves(mddev_t *mddev)
455 {
456         conf_t *conf = mddev_to_conf(mddev);
457         int i;
458         unsigned long flags;
459
460         spin_lock_irqsave(&conf->device_lock, flags);
461         for (i=0; i<mddev->raid_disks; i++) {
462                 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
463                 if (rdev && atomic_read(&rdev->nr_pending)) {
464                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
465
466                         atomic_inc(&rdev->nr_pending);
467                         spin_unlock_irqrestore(&conf->device_lock, flags);
468
469                         if (r_queue->unplug_fn)
470                                 r_queue->unplug_fn(r_queue);
471
472                         spin_lock_irqsave(&conf->device_lock, flags);
473                         atomic_dec(&rdev->nr_pending);
474                 }
475         }
476         spin_unlock_irqrestore(&conf->device_lock, flags);
477 }
478 static void raid1_unplug(request_queue_t *q)
479 {
480         unplug_slaves(q->queuedata);
481 }
482
483 /*
484  * Throttle resync depth, so that we can both get proper overlapping of
485  * requests, but are still able to handle normal requests quickly.
486  */
487 #define RESYNC_DEPTH 32
488
489 static void device_barrier(conf_t *conf, sector_t sect)
490 {
491         spin_lock_irq(&conf->resync_lock);
492         wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
493                             conf->resync_lock, unplug_slaves(conf->mddev));
494         
495         if (!conf->barrier++) {
496                 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
497                                     conf->resync_lock, unplug_slaves(conf->mddev));
498                 if (conf->nr_pending)
499                         BUG();
500         }
501         wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
502                             conf->resync_lock, unplug_slaves(conf->mddev));
503         conf->next_resync = sect;
504         spin_unlock_irq(&conf->resync_lock);
505 }
506
507 static int make_request(request_queue_t *q, struct bio * bio)
508 {
509         mddev_t *mddev = q->queuedata;
510         conf_t *conf = mddev_to_conf(mddev);
511         mirror_info_t *mirror;
512         r1bio_t *r1_bio;
513         struct bio *read_bio;
514         int i, disks;
515
516         /*
517          * Register the new request and wait if the reconstruction
518          * thread has put up a bar for new requests.
519          * Continue immediately if no resync is active currently.
520          */
521         spin_lock_irq(&conf->resync_lock);
522         wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
523         conf->nr_pending++;
524         spin_unlock_irq(&conf->resync_lock);
525
526         if (bio_data_dir(bio)==WRITE) {
527                 disk_stat_inc(mddev->gendisk, writes);
528                 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
529         } else {
530                 disk_stat_inc(mddev->gendisk, reads);
531                 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
532         }
533
534         /*
535          * make_request() can abort the operation when READA is being
536          * used and no empty request is available.
537          *
538          */
539         r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
540
541         r1_bio->master_bio = bio;
542         r1_bio->sectors = bio->bi_size >> 9;
543
544         r1_bio->mddev = mddev;
545         r1_bio->sector = bio->bi_sector;
546
547         if (bio_data_dir(bio) == READ) {
548                 /*
549                  * read balancing logic:
550                  */
551                 mirror = conf->mirrors + read_balance(conf, bio, r1_bio);
552
553                 read_bio = bio_clone(bio, GFP_NOIO);
554
555                 r1_bio->bios[r1_bio->read_disk] = read_bio;
556
557                 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
558                 read_bio->bi_bdev = mirror->rdev->bdev;
559                 read_bio->bi_end_io = raid1_end_read_request;
560                 read_bio->bi_rw = READ;
561                 read_bio->bi_private = r1_bio;
562
563                 generic_make_request(read_bio);
564                 return 0;
565         }
566
567         /*
568          * WRITE:
569          */
570         /* first select target devices under spinlock and
571          * inc refcount on their rdev.  Record them by setting
572          * bios[x] to bio
573          */
574         disks = conf->raid_disks;
575         spin_lock_irq(&conf->device_lock);
576         for (i = 0;  i < disks; i++) {
577                 if (conf->mirrors[i].rdev &&
578                     !conf->mirrors[i].rdev->faulty) {
579                         atomic_inc(&conf->mirrors[i].rdev->nr_pending);
580                         r1_bio->bios[i] = bio;
581                 } else
582                         r1_bio->bios[i] = NULL;
583         }
584         spin_unlock_irq(&conf->device_lock);
585
586         atomic_set(&r1_bio->remaining, 1);
587         md_write_start(mddev);
588         for (i = 0; i < disks; i++) {
589                 struct bio *mbio;
590                 if (!r1_bio->bios[i])
591                         continue;
592
593                 mbio = bio_clone(bio, GFP_NOIO);
594                 r1_bio->bios[i] = mbio;
595
596                 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
597                 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
598                 mbio->bi_end_io = raid1_end_write_request;
599                 mbio->bi_rw = WRITE;
600                 mbio->bi_private = r1_bio;
601
602                 atomic_inc(&r1_bio->remaining);
603                 generic_make_request(mbio);
604         }
605
606         if (atomic_dec_and_test(&r1_bio->remaining)) {
607                 md_write_end(mddev);
608                 raid_end_bio_io(r1_bio);
609         }
610
611         return 0;
612 }
613
614 static void status(struct seq_file *seq, mddev_t *mddev)
615 {
616         conf_t *conf = mddev_to_conf(mddev);
617         int i;
618
619         seq_printf(seq, " [%d/%d] [", conf->raid_disks,
620                                                 conf->working_disks);
621         for (i = 0; i < conf->raid_disks; i++)
622                 seq_printf(seq, "%s",
623                               conf->mirrors[i].rdev &&
624                               conf->mirrors[i].rdev->in_sync ? "U" : "_");
625         seq_printf(seq, "]");
626 }
627
628
629 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
630 {
631         char b[BDEVNAME_SIZE];
632         conf_t *conf = mddev_to_conf(mddev);
633
634         /*
635          * If it is not operational, then we have already marked it as dead
636          * else if it is the last working disks, ignore the error, let the
637          * next level up know.
638          * else mark the drive as failed
639          */
640         if (rdev->in_sync
641             && conf->working_disks == 1)
642                 /*
643                  * Don't fail the drive, act as though we were just a
644                  * normal single drive
645                  */
646                 return;
647         if (rdev->in_sync) {
648                 mddev->degraded++;
649                 conf->working_disks--;
650                 /*
651                  * if recovery is running, make sure it aborts.
652                  */
653                 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
654         }
655         rdev->in_sync = 0;
656         rdev->faulty = 1;
657         mddev->sb_dirty = 1;
658         printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
659                 "       Operation continuing on %d devices\n",
660                 bdevname(rdev->bdev,b), conf->working_disks);
661 }
662
663 static void print_conf(conf_t *conf)
664 {
665         int i;
666         mirror_info_t *tmp;
667
668         printk("RAID1 conf printout:\n");
669         if (!conf) {
670                 printk("(!conf)\n");
671                 return;
672         }
673         printk(" --- wd:%d rd:%d\n", conf->working_disks,
674                 conf->raid_disks);
675
676         for (i = 0; i < conf->raid_disks; i++) {
677                 char b[BDEVNAME_SIZE];
678                 tmp = conf->mirrors + i;
679                 if (tmp->rdev)
680                         printk(" disk %d, wo:%d, o:%d, dev:%s\n",
681                                 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
682                                 bdevname(tmp->rdev->bdev,b));
683         }
684 }
685
686 static void close_sync(conf_t *conf)
687 {
688         spin_lock_irq(&conf->resync_lock);
689         wait_event_lock_irq(conf->wait_resume, !conf->barrier,
690                             conf->resync_lock,  unplug_slaves(conf->mddev));
691         spin_unlock_irq(&conf->resync_lock);
692
693         if (conf->barrier) BUG();
694         if (waitqueue_active(&conf->wait_idle)) BUG();
695
696         mempool_destroy(conf->r1buf_pool);
697         conf->r1buf_pool = NULL;
698 }
699
700 static int raid1_spare_active(mddev_t *mddev)
701 {
702         int i;
703         conf_t *conf = mddev->private;
704         mirror_info_t *tmp;
705
706         spin_lock_irq(&conf->device_lock);
707         /*
708          * Find all failed disks within the RAID1 configuration 
709          * and mark them readable
710          */
711         for (i = 0; i < conf->raid_disks; i++) {
712                 tmp = conf->mirrors + i;
713                 if (tmp->rdev 
714                     && !tmp->rdev->faulty
715                     && !tmp->rdev->in_sync) {
716                         conf->working_disks++;
717                         mddev->degraded--;
718                         tmp->rdev->in_sync = 1;
719                 }
720         }
721         spin_unlock_irq(&conf->device_lock);
722
723         print_conf(conf);
724         return 0;
725 }
726
727
728 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
729 {
730         conf_t *conf = mddev->private;
731         int found = 0;
732         int mirror;
733         mirror_info_t *p;
734
735         spin_lock_irq(&conf->device_lock);
736         for (mirror=0; mirror < mddev->raid_disks; mirror++)
737                 if ( !(p=conf->mirrors+mirror)->rdev) {
738                         p->rdev = rdev;
739
740                         blk_queue_stack_limits(mddev->queue,
741                                                rdev->bdev->bd_disk->queue);
742                         /* as we don't honour merge_bvec_fn, we must never risk
743                          * violating it, so limit ->max_sector to one PAGE, as
744                          * a one page request is never in violation.
745                          */
746                         if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
747                             mddev->queue->max_sectors > (PAGE_SIZE>>9))
748                                 mddev->queue->max_sectors = (PAGE_SIZE>>9);
749
750                         p->head_position = 0;
751                         rdev->raid_disk = mirror;
752                         found = 1;
753                         break;
754                 }
755         spin_unlock_irq(&conf->device_lock);
756
757         print_conf(conf);
758         return found;
759 }
760
761 static int raid1_remove_disk(mddev_t *mddev, int number)
762 {
763         conf_t *conf = mddev->private;
764         int err = 1;
765         mirror_info_t *p = conf->mirrors+ number;
766
767         print_conf(conf);
768         spin_lock_irq(&conf->device_lock);
769         if (p->rdev) {
770                 if (p->rdev->in_sync ||
771                     atomic_read(&p->rdev->nr_pending)) {
772                         err = -EBUSY;
773                         goto abort;
774                 }
775                 p->rdev = NULL;
776                 err = 0;
777         }
778         if (err)
779                 MD_BUG();
780 abort:
781         spin_unlock_irq(&conf->device_lock);
782
783         print_conf(conf);
784         return err;
785 }
786
787
788 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
789 {
790         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
791         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
792         conf_t *conf = mddev_to_conf(r1_bio->mddev);
793
794         if (bio->bi_size)
795                 return 1;
796
797         if (r1_bio->bios[r1_bio->read_disk] != bio)
798                 BUG();
799         update_head_pos(r1_bio->read_disk, r1_bio);
800         /*
801          * we have read a block, now it needs to be re-written,
802          * or re-read if the read failed.
803          * We don't do much here, just schedule handling by raid1d
804          */
805         if (!uptodate)
806                 md_error(r1_bio->mddev,
807                          conf->mirrors[r1_bio->read_disk].rdev);
808         else
809                 set_bit(R1BIO_Uptodate, &r1_bio->state);
810         rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
811         reschedule_retry(r1_bio);
812         return 0;
813 }
814
815 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
816 {
817         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
818         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
819         mddev_t *mddev = r1_bio->mddev;
820         conf_t *conf = mddev_to_conf(mddev);
821         int i;
822         int mirror=0;
823
824         if (bio->bi_size)
825                 return 1;
826
827         for (i = 0; i < conf->raid_disks; i++)
828                 if (r1_bio->bios[i] == bio) {
829                         mirror = i;
830                         break;
831                 }
832         if (!uptodate)
833                 md_error(mddev, conf->mirrors[mirror].rdev);
834         update_head_pos(mirror, r1_bio);
835
836         if (atomic_dec_and_test(&r1_bio->remaining)) {
837                 md_done_sync(mddev, r1_bio->sectors, uptodate);
838                 put_buf(r1_bio);
839         }
840         rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
841         return 0;
842 }
843
844 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
845 {
846         conf_t *conf = mddev_to_conf(mddev);
847         int i;
848         int disks = conf->raid_disks;
849         struct bio *bio, *wbio;
850
851         bio = r1_bio->bios[r1_bio->read_disk];
852
853         /*
854          * schedule writes
855          */
856         if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
857                 /*
858                  * There is no point trying a read-for-reconstruct as
859                  * reconstruct is about to be aborted
860                  */
861                 char b[BDEVNAME_SIZE];
862                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
863                         " for block %llu\n",
864                         bdevname(bio->bi_bdev,b), 
865                         (unsigned long long)r1_bio->sector);
866                 md_done_sync(mddev, r1_bio->sectors, 0);
867                 put_buf(r1_bio);
868                 return;
869         }
870
871         atomic_set(&r1_bio->remaining, 1);
872         for (i = 0; i < disks ; i++) {
873                 wbio = r1_bio->bios[i];
874                 if (wbio->bi_end_io != end_sync_write)
875                         continue;
876
877                 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
878                 atomic_inc(&r1_bio->remaining);
879                 md_sync_acct(conf->mirrors[i].rdev, wbio->bi_size >> 9);
880                 generic_make_request(wbio);
881         }
882
883         if (atomic_dec_and_test(&r1_bio->remaining)) {
884                 md_done_sync(mddev, r1_bio->sectors, 1);
885                 put_buf(r1_bio);
886         }
887 }
888
889 /*
890  * This is a kernel thread which:
891  *
892  *      1.      Retries failed read operations on working mirrors.
893  *      2.      Updates the raid superblock when problems encounter.
894  *      3.      Performs writes following reads for array syncronising.
895  */
896
897 static void raid1d(mddev_t *mddev)
898 {
899         struct list_head *head = &retry_list_head;
900         r1bio_t *r1_bio;
901         struct bio *bio;
902         unsigned long flags;
903         conf_t *conf = mddev_to_conf(mddev);
904         int unplug=0;
905         mdk_rdev_t *rdev;
906
907         md_check_recovery(mddev);
908         md_handle_safemode(mddev);
909         
910         for (;;) {
911                 char b[BDEVNAME_SIZE];
912                 spin_lock_irqsave(&retry_list_lock, flags);
913                 if (list_empty(head))
914                         break;
915                 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
916                 list_del(head->prev);
917                 spin_unlock_irqrestore(&retry_list_lock, flags);
918
919                 mddev = r1_bio->mddev;
920                 conf = mddev_to_conf(mddev);
921                 bio = r1_bio->master_bio;
922                 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
923                         sync_request_write(mddev, r1_bio);
924                         unplug = 1;
925                 } else {
926                         if (map(mddev, &rdev) == -1) {
927                                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
928                                        " read error for block %llu\n",
929                                        bdevname(bio->bi_bdev,b),
930                                        (unsigned long long)r1_bio->sector);
931                                 raid_end_bio_io(r1_bio);
932                         } else {
933                                 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
934                                        " another mirror\n",
935                                        bdevname(rdev->bdev,b),
936                                        (unsigned long long)r1_bio->sector);
937                                 bio->bi_bdev = rdev->bdev;
938                                 bio->bi_sector = r1_bio->sector + rdev->data_offset;
939                                 bio->bi_rw = READ;
940                                 unplug = 1;
941                                 generic_make_request(bio);
942                         }
943                 }
944         }
945         spin_unlock_irqrestore(&retry_list_lock, flags);
946         if (unplug)
947                 unplug_slaves(mddev);
948 }
949
950
951 static int init_resync(conf_t *conf)
952 {
953         int buffs;
954
955         buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
956         if (conf->r1buf_pool)
957                 BUG();
958         conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
959                                           conf->poolinfo);
960         if (!conf->r1buf_pool)
961                 return -ENOMEM;
962         conf->next_resync = 0;
963         return 0;
964 }
965
966 /*
967  * perform a "sync" on one "block"
968  *
969  * We need to make sure that no normal I/O request - particularly write
970  * requests - conflict with active sync requests.
971  *
972  * This is achieved by tracking pending requests and a 'barrier' concept
973  * that can be installed to exclude normal IO requests.
974  */
975
976 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
977 {
978         conf_t *conf = mddev_to_conf(mddev);
979         mirror_info_t *mirror;
980         r1bio_t *r1_bio;
981         struct bio *bio;
982         sector_t max_sector, nr_sectors;
983         int disk;
984         int i;
985         int write_targets = 0;
986
987         if (!conf->r1buf_pool)
988                 if (init_resync(conf))
989                         return -ENOMEM;
990
991         max_sector = mddev->size << 1;
992         if (sector_nr >= max_sector) {
993                 close_sync(conf);
994                 return 0;
995         }
996
997         /*
998          * If there is non-resync activity waiting for us then
999          * put in a delay to throttle resync.
1000          */
1001         if (!go_faster && waitqueue_active(&conf->wait_resume))
1002                 schedule_timeout(HZ);
1003         device_barrier(conf, sector_nr + RESYNC_SECTORS);
1004
1005         /*
1006          * If reconstructing, and >1 working disc,
1007          * could dedicate one to rebuild and others to
1008          * service read requests ..
1009          */
1010         disk = conf->last_used;
1011         /* make sure disk is operational */
1012         spin_lock_irq(&conf->device_lock);
1013         while (conf->mirrors[disk].rdev == NULL ||
1014                !conf->mirrors[disk].rdev->in_sync) {
1015                 if (disk <= 0)
1016                         disk = conf->raid_disks;
1017                 disk--;
1018                 if (disk == conf->last_used)
1019                         break;
1020         }
1021         conf->last_used = disk;
1022         atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1023         spin_unlock_irq(&conf->device_lock);
1024
1025         mirror = conf->mirrors + disk;
1026
1027         r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1028
1029         spin_lock_irq(&conf->resync_lock);
1030         conf->nr_pending++;
1031         spin_unlock_irq(&conf->resync_lock);
1032
1033         r1_bio->mddev = mddev;
1034         r1_bio->sector = sector_nr;
1035         set_bit(R1BIO_IsSync, &r1_bio->state);
1036         r1_bio->read_disk = disk;
1037
1038         for (i=0; i < conf->raid_disks; i++) {
1039                 bio = r1_bio->bios[i];
1040
1041                 /* take from bio_init */
1042                 bio->bi_next = NULL;
1043                 bio->bi_flags |= 1 << BIO_UPTODATE;
1044                 bio->bi_rw = 0;
1045                 bio->bi_vcnt = 0;
1046                 bio->bi_idx = 0;
1047                 bio->bi_phys_segments = 0;
1048                 bio->bi_hw_segments = 0;
1049                 bio->bi_size = 0;
1050                 bio->bi_end_io = NULL;
1051                 bio->bi_private = NULL;
1052
1053                 if (i == disk) {
1054                         bio->bi_rw = READ;
1055                         bio->bi_end_io = end_sync_read;
1056                 } else if (conf->mirrors[i].rdev &&
1057                            !conf->mirrors[i].rdev->faulty &&
1058                            (!conf->mirrors[i].rdev->in_sync ||
1059                             sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1060                         bio->bi_rw = WRITE;
1061                         bio->bi_end_io = end_sync_write;
1062                         write_targets ++;
1063                 } else
1064                         continue;
1065                 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1066                 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1067                 bio->bi_private = r1_bio;
1068         }
1069         if (write_targets == 0) {
1070                 /* There is nowhere to write, so all non-sync
1071                  * drives must be failed - so we are finished
1072                  */
1073                 int rv = max_sector - sector_nr;
1074                 md_done_sync(mddev, rv, 1);
1075                 put_buf(r1_bio);
1076                 atomic_dec(&conf->mirrors[disk].rdev->nr_pending);
1077                 return rv;
1078         }
1079
1080         nr_sectors = 0;
1081         do {
1082                 struct page *page;
1083                 int len = PAGE_SIZE;
1084                 if (sector_nr + (len>>9) > max_sector)
1085                         len = (max_sector - sector_nr) << 9;
1086                 if (len == 0)
1087                         break;
1088                 for (i=0 ; i < conf->raid_disks; i++) {
1089                         bio = r1_bio->bios[i];
1090                         if (bio->bi_end_io) {
1091                                 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1092                                 if (bio_add_page(bio, page, len, 0) == 0) {
1093                                         /* stop here */
1094                                         r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1095                                         while (i > 0) {
1096                                                 i--;
1097                                                 bio = r1_bio->bios[i];
1098                                                 if (bio->bi_end_io==NULL) continue;
1099                                                 /* remove last page from this bio */
1100                                                 bio->bi_vcnt--;
1101                                                 bio->bi_size -= len;
1102                                                 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1103                                         }
1104                                         goto bio_full;
1105                                 }
1106                         }
1107                 }
1108                 nr_sectors += len>>9;
1109                 sector_nr += len>>9;
1110         } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1111  bio_full:
1112         bio = r1_bio->bios[disk];
1113         r1_bio->sectors = nr_sectors;
1114
1115         md_sync_acct(mirror->rdev, nr_sectors);
1116
1117         generic_make_request(bio);
1118
1119         return nr_sectors;
1120 }
1121
1122 static int run(mddev_t *mddev)
1123 {
1124         conf_t *conf;
1125         int i, j, disk_idx;
1126         mirror_info_t *disk;
1127         mdk_rdev_t *rdev;
1128         struct list_head *tmp;
1129
1130         if (mddev->level != 1) {
1131                 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1132                        mdname(mddev), mddev->level);
1133                 goto out;
1134         }
1135         /*
1136          * copy the already verified devices into our private RAID1
1137          * bookkeeping area. [whatever we allocate in run(),
1138          * should be freed in stop()]
1139          */
1140         conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1141         mddev->private = conf;
1142         if (!conf)
1143                 goto out_no_mem;
1144
1145         memset(conf, 0, sizeof(*conf));
1146         conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks, 
1147                                  GFP_KERNEL);
1148         if (!conf->mirrors)
1149                 goto out_no_mem;
1150
1151         memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1152
1153         conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1154         if (!conf->poolinfo)
1155                 goto out_no_mem;
1156         conf->poolinfo->mddev = mddev;
1157         conf->poolinfo->raid_disks = mddev->raid_disks;
1158         conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1159                                           r1bio_pool_free,
1160                                           conf->poolinfo);
1161         if (!conf->r1bio_pool)
1162                 goto out_no_mem;
1163
1164         mddev->queue->unplug_fn = raid1_unplug;
1165
1166
1167         ITERATE_RDEV(mddev, rdev, tmp) {
1168                 disk_idx = rdev->raid_disk;
1169                 if (disk_idx >= mddev->raid_disks
1170                     || disk_idx < 0)
1171                         continue;
1172                 disk = conf->mirrors + disk_idx;
1173
1174                 disk->rdev = rdev;
1175
1176                 blk_queue_stack_limits(mddev->queue,
1177                                        rdev->bdev->bd_disk->queue);
1178                 /* as we don't honour merge_bvec_fn, we must never risk
1179                  * violating it, so limit ->max_sector to one PAGE, as
1180                  * a one page request is never in violation.
1181                  */
1182                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1183                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
1184                         mddev->queue->max_sectors = (PAGE_SIZE>>9);
1185
1186                 disk->head_position = 0;
1187                 if (!rdev->faulty && rdev->in_sync)
1188                         conf->working_disks++;
1189         }
1190         conf->raid_disks = mddev->raid_disks;
1191         conf->mddev = mddev;
1192         conf->device_lock = SPIN_LOCK_UNLOCKED;
1193         if (conf->working_disks == 1)
1194                 mddev->recovery_cp = MaxSector;
1195
1196         conf->resync_lock = SPIN_LOCK_UNLOCKED;
1197         init_waitqueue_head(&conf->wait_idle);
1198         init_waitqueue_head(&conf->wait_resume);
1199
1200         if (!conf->working_disks) {
1201                 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1202                         mdname(mddev));
1203                 goto out_free_conf;
1204         }
1205
1206         mddev->degraded = 0;
1207         for (i = 0; i < conf->raid_disks; i++) {
1208
1209                 disk = conf->mirrors + i;
1210
1211                 if (!disk->rdev) {
1212                         disk->head_position = 0;
1213                         mddev->degraded++;
1214                 }
1215         }
1216
1217         /*
1218          * find the first working one and use it as a starting point
1219          * to read balancing.
1220          */
1221         for (j = 0; j < conf->raid_disks &&
1222                      (!conf->mirrors[j].rdev ||
1223                       !conf->mirrors[j].rdev->in_sync) ; j++)
1224                 /* nothing */;
1225         conf->last_used = j;
1226
1227
1228
1229         {
1230                 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1231                 if (!mddev->thread) {
1232                         printk(KERN_ERR 
1233                                 "raid1: couldn't allocate thread for %s\n", 
1234                                 mdname(mddev));
1235                         goto out_free_conf;
1236                 }
1237         }
1238         printk(KERN_INFO 
1239                 "raid1: raid set %s active with %d out of %d mirrors\n",
1240                 mdname(mddev), mddev->raid_disks - mddev->degraded, 
1241                 mddev->raid_disks);
1242         /*
1243          * Ok, everything is just fine now
1244          */
1245         mddev->array_size = mddev->size;
1246
1247         return 0;
1248
1249 out_no_mem:
1250         printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1251                mdname(mddev));
1252
1253 out_free_conf:
1254         if (conf) {
1255                 if (conf->r1bio_pool)
1256                         mempool_destroy(conf->r1bio_pool);
1257                 if (conf->mirrors)
1258                         kfree(conf->mirrors);
1259                 if (conf->poolinfo)
1260                         kfree(conf->poolinfo);
1261                 kfree(conf);
1262                 mddev->private = NULL;
1263         }
1264 out:
1265         return -EIO;
1266 }
1267
1268 static int stop(mddev_t *mddev)
1269 {
1270         conf_t *conf = mddev_to_conf(mddev);
1271
1272         md_unregister_thread(mddev->thread);
1273         mddev->thread = NULL;
1274         if (conf->r1bio_pool)
1275                 mempool_destroy(conf->r1bio_pool);
1276         if (conf->mirrors)
1277                 kfree(conf->mirrors);
1278         if (conf->poolinfo)
1279                 kfree(conf->poolinfo);
1280         kfree(conf);
1281         mddev->private = NULL;
1282         return 0;
1283 }
1284
1285 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1286 {
1287         /* no resync is happening, and there is enough space
1288          * on all devices, so we can resize.
1289          * We need to make sure resync covers any new space.
1290          * If the array is shrinking we should possibly wait until
1291          * any io in the removed space completes, but it hardly seems
1292          * worth it.
1293          */
1294         mddev->array_size = sectors>>1;
1295         set_capacity(mddev->gendisk, mddev->array_size << 1);
1296         mddev->changed = 1;
1297         if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1298                 mddev->recovery_cp = mddev->size << 1;
1299                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1300         }
1301         mddev->size = mddev->array_size;
1302         return 0;
1303 }
1304
1305 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1306 {
1307         /* We need to:
1308          * 1/ resize the r1bio_pool
1309          * 2/ resize conf->mirrors
1310          *
1311          * We allocate a new r1bio_pool if we can.
1312          * Then raise a device barrier and wait until all IO stops.
1313          * Then resize conf->mirrors and swap in the new r1bio pool.
1314          */
1315         mempool_t *newpool, *oldpool;
1316         struct pool_info *newpoolinfo;
1317         mirror_info_t *newmirrors;
1318         conf_t *conf = mddev_to_conf(mddev);
1319
1320         int d;
1321
1322         for (d= raid_disks; d < conf->raid_disks; d++)
1323                 if (conf->mirrors[d].rdev)
1324                         return -EBUSY;
1325
1326         newpoolinfo = kmalloc(sizeof(newpoolinfo), GFP_KERNEL);
1327         if (!newpoolinfo)
1328                 return -ENOMEM;
1329         newpoolinfo->mddev = mddev;
1330         newpoolinfo->raid_disks = raid_disks;
1331
1332         newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1333                                  r1bio_pool_free, newpoolinfo);
1334         if (!newpool) {
1335                 kfree(newpoolinfo);
1336                 return -ENOMEM;
1337         }
1338         newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1339         if (!newmirrors) {
1340                 kfree(newpoolinfo);
1341                 mempool_destroy(newpool);
1342                 return -ENOMEM;
1343         }
1344         memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1345
1346         spin_lock_irq(&conf->resync_lock);
1347         conf->barrier++;
1348         wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1349                             conf->resync_lock, unplug_slaves(mddev));
1350         spin_unlock_irq(&conf->resync_lock);
1351
1352         /* ok, everything is stopped */
1353         oldpool = conf->r1bio_pool;
1354         conf->r1bio_pool = newpool;
1355         for (d=0; d < raid_disks && d < conf->raid_disks; d++)
1356                 newmirrors[d] = conf->mirrors[d];
1357         kfree(conf->mirrors);
1358         conf->mirrors = newmirrors;
1359         kfree(conf->poolinfo);
1360         conf->poolinfo = newpoolinfo;
1361
1362         mddev->degraded += (raid_disks - conf->raid_disks);
1363         conf->raid_disks = mddev->raid_disks = raid_disks;
1364
1365         spin_lock_irq(&conf->resync_lock);
1366         conf->barrier--;
1367         spin_unlock_irq(&conf->resync_lock);
1368         wake_up(&conf->wait_resume);
1369         wake_up(&conf->wait_idle);
1370
1371
1372         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1373         md_wakeup_thread(mddev->thread);
1374
1375         mempool_destroy(oldpool);
1376         return 0;
1377 }
1378
1379
1380 static mdk_personality_t raid1_personality =
1381 {
1382         .name           = "raid1",
1383         .owner          = THIS_MODULE,
1384         .make_request   = make_request,
1385         .run            = run,
1386         .stop           = stop,
1387         .status         = status,
1388         .error_handler  = error,
1389         .hot_add_disk   = raid1_add_disk,
1390         .hot_remove_disk= raid1_remove_disk,
1391         .spare_active   = raid1_spare_active,
1392         .sync_request   = sync_request,
1393         .resize         = raid1_resize,
1394         .reshape        = raid1_reshape,
1395 };
1396
1397 static int __init raid_init(void)
1398 {
1399         return register_md_personality(RAID1, &raid1_personality);
1400 }
1401
1402 static void raid_exit(void)
1403 {
1404         unregister_md_personality(RAID1);
1405 }
1406
1407 module_init(raid_init);
1408 module_exit(raid_exit);
1409 MODULE_LICENSE("GPL");
1410 MODULE_ALIAS("md-personality-3"); /* RAID1 */