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