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