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