]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/md/raid1.c
[PATCH] md: handle errors when read-only
[linux-2.6-omap-h63xx.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  * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16  * bitmapped intelligence in resync:
17  *
18  *      - bitmap marked during normal i/o
19  *      - bitmap used to skip nondirty blocks during sync
20  *
21  * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22  * - persistent bitmap code
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2, or (at your option)
27  * any later version.
28  *
29  * You should have received a copy of the GNU General Public License
30  * (for example /usr/src/linux/COPYING); if not, write to the Free
31  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33
34 #include "dm-bio-list.h"
35 #include <linux/raid/raid1.h>
36 #include <linux/raid/bitmap.h>
37
38 #define DEBUG 0
39 #if DEBUG
40 #define PRINTK(x...) printk(x)
41 #else
42 #define PRINTK(x...)
43 #endif
44
45 /*
46  * Number of guaranteed r1bios in case of extreme VM load:
47  */
48 #define NR_RAID1_BIOS 256
49
50 static mdk_personality_t raid1_personality;
51
52 static void unplug_slaves(mddev_t *mddev);
53
54 static void allow_barrier(conf_t *conf);
55 static void lower_barrier(conf_t *conf);
56
57 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
58 {
59         struct pool_info *pi = data;
60         r1bio_t *r1_bio;
61         int size = offsetof(r1bio_t, bios[pi->raid_disks]);
62
63         /* allocate a r1bio with room for raid_disks entries in the bios array */
64         r1_bio = kmalloc(size, gfp_flags);
65         if (r1_bio)
66                 memset(r1_bio, 0, size);
67         else
68                 unplug_slaves(pi->mddev);
69
70         return r1_bio;
71 }
72
73 static void r1bio_pool_free(void *r1_bio, void *data)
74 {
75         kfree(r1_bio);
76 }
77
78 #define RESYNC_BLOCK_SIZE (64*1024)
79 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
80 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
81 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
82 #define RESYNC_WINDOW (2048*1024)
83
84 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
85 {
86         struct pool_info *pi = data;
87         struct page *page;
88         r1bio_t *r1_bio;
89         struct bio *bio;
90         int i, j;
91
92         r1_bio = r1bio_pool_alloc(gfp_flags, pi);
93         if (!r1_bio) {
94                 unplug_slaves(pi->mddev);
95                 return NULL;
96         }
97
98         /*
99          * Allocate bios : 1 for reading, n-1 for writing
100          */
101         for (j = pi->raid_disks ; j-- ; ) {
102                 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
103                 if (!bio)
104                         goto out_free_bio;
105                 r1_bio->bios[j] = bio;
106         }
107         /*
108          * Allocate RESYNC_PAGES data pages and attach them to
109          * the first bio;
110          */
111         bio = r1_bio->bios[0];
112         for (i = 0; i < RESYNC_PAGES; i++) {
113                 page = alloc_page(gfp_flags);
114                 if (unlikely(!page))
115                         goto out_free_pages;
116
117                 bio->bi_io_vec[i].bv_page = page;
118         }
119
120         r1_bio->master_bio = NULL;
121
122         return r1_bio;
123
124 out_free_pages:
125         for ( ; i > 0 ; i--)
126                 __free_page(bio->bi_io_vec[i-1].bv_page);
127 out_free_bio:
128         while ( ++j < pi->raid_disks )
129                 bio_put(r1_bio->bios[j]);
130         r1bio_pool_free(r1_bio, data);
131         return NULL;
132 }
133
134 static void r1buf_pool_free(void *__r1_bio, void *data)
135 {
136         struct pool_info *pi = data;
137         int i;
138         r1bio_t *r1bio = __r1_bio;
139         struct bio *bio = r1bio->bios[0];
140
141         for (i = 0; i < RESYNC_PAGES; i++) {
142                 __free_page(bio->bi_io_vec[i].bv_page);
143                 bio->bi_io_vec[i].bv_page = NULL;
144         }
145         for (i=0 ; i < pi->raid_disks; i++)
146                 bio_put(r1bio->bios[i]);
147
148         r1bio_pool_free(r1bio, data);
149 }
150
151 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
152 {
153         int i;
154
155         for (i = 0; i < conf->raid_disks; i++) {
156                 struct bio **bio = r1_bio->bios + i;
157                 if (*bio && *bio != IO_BLOCKED)
158                         bio_put(*bio);
159                 *bio = NULL;
160         }
161 }
162
163 static inline void free_r1bio(r1bio_t *r1_bio)
164 {
165         conf_t *conf = mddev_to_conf(r1_bio->mddev);
166
167         /*
168          * Wake up any possible resync thread that waits for the device
169          * to go idle.
170          */
171         allow_barrier(conf);
172
173         put_all_bios(conf, r1_bio);
174         mempool_free(r1_bio, conf->r1bio_pool);
175 }
176
177 static inline void put_buf(r1bio_t *r1_bio)
178 {
179         conf_t *conf = mddev_to_conf(r1_bio->mddev);
180         int i;
181
182         for (i=0; i<conf->raid_disks; i++) {
183                 struct bio *bio = r1_bio->bios[i];
184                 if (bio->bi_end_io)
185                         rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
186         }
187
188         mempool_free(r1_bio, conf->r1buf_pool);
189
190         lower_barrier(conf);
191 }
192
193 static void reschedule_retry(r1bio_t *r1_bio)
194 {
195         unsigned long flags;
196         mddev_t *mddev = r1_bio->mddev;
197         conf_t *conf = mddev_to_conf(mddev);
198
199         spin_lock_irqsave(&conf->device_lock, flags);
200         list_add(&r1_bio->retry_list, &conf->retry_list);
201         conf->nr_queued ++;
202         spin_unlock_irqrestore(&conf->device_lock, flags);
203
204         wake_up(&conf->wait_barrier);
205         md_wakeup_thread(mddev->thread);
206 }
207
208 /*
209  * raid_end_bio_io() is called when we have finished servicing a mirrored
210  * operation and are ready to return a success/failure code to the buffer
211  * cache layer.
212  */
213 static void raid_end_bio_io(r1bio_t *r1_bio)
214 {
215         struct bio *bio = r1_bio->master_bio;
216
217         /* if nobody has done the final endio yet, do it now */
218         if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
219                 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
220                         (bio_data_dir(bio) == WRITE) ? "write" : "read",
221                         (unsigned long long) bio->bi_sector,
222                         (unsigned long long) bio->bi_sector +
223                                 (bio->bi_size >> 9) - 1);
224
225                 bio_endio(bio, bio->bi_size,
226                         test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
227         }
228         free_r1bio(r1_bio);
229 }
230
231 /*
232  * Update disk head position estimator based on IRQ completion info.
233  */
234 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
235 {
236         conf_t *conf = mddev_to_conf(r1_bio->mddev);
237
238         conf->mirrors[disk].head_position =
239                 r1_bio->sector + (r1_bio->sectors);
240 }
241
242 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
243 {
244         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
245         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
246         int mirror;
247         conf_t *conf = mddev_to_conf(r1_bio->mddev);
248
249         if (bio->bi_size)
250                 return 1;
251         
252         mirror = r1_bio->read_disk;
253         /*
254          * this branch is our 'one mirror IO has finished' event handler:
255          */
256         update_head_pos(mirror, r1_bio);
257
258         if (uptodate || conf->working_disks <= 1) {
259                 /*
260                  * Set R1BIO_Uptodate in our master bio, so that
261                  * we will return a good error code for to the higher
262                  * levels even if IO on some other mirrored buffer fails.
263                  *
264                  * The 'master' represents the composite IO operation to
265                  * user-side. So if something waits for IO, then it will
266                  * wait for the 'master' bio.
267                  */
268                 set_bit(R1BIO_Uptodate, &r1_bio->state);
269
270                 raid_end_bio_io(r1_bio);
271         } else {
272                 /*
273                  * oops, read error:
274                  */
275                 char b[BDEVNAME_SIZE];
276                 if (printk_ratelimit())
277                         printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
278                                bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
279                 reschedule_retry(r1_bio);
280         }
281
282         rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
283         return 0;
284 }
285
286 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
287 {
288         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
289         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
290         int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
291         conf_t *conf = mddev_to_conf(r1_bio->mddev);
292
293         if (bio->bi_size)
294                 return 1;
295
296         for (mirror = 0; mirror < conf->raid_disks; mirror++)
297                 if (r1_bio->bios[mirror] == bio)
298                         break;
299
300         if (error == -ENOTSUPP && test_bit(R1BIO_Barrier, &r1_bio->state)) {
301                 set_bit(BarriersNotsupp, &conf->mirrors[mirror].rdev->flags);
302                 set_bit(R1BIO_BarrierRetry, &r1_bio->state);
303                 r1_bio->mddev->barriers_work = 0;
304         } else {
305                 /*
306                  * this branch is our 'one mirror IO has finished' event handler:
307                  */
308                 r1_bio->bios[mirror] = NULL;
309                 if (!uptodate) {
310                         md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
311                         /* an I/O failed, we can't clear the bitmap */
312                         set_bit(R1BIO_Degraded, &r1_bio->state);
313                 } else
314                         /*
315                          * Set R1BIO_Uptodate in our master bio, so that
316                          * we will return a good error code for to the higher
317                          * levels even if IO on some other mirrored buffer fails.
318                          *
319                          * The 'master' represents the composite IO operation to
320                          * user-side. So if something waits for IO, then it will
321                          * wait for the 'master' bio.
322                          */
323                         set_bit(R1BIO_Uptodate, &r1_bio->state);
324
325                 update_head_pos(mirror, r1_bio);
326
327                 if (behind) {
328                         if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
329                                 atomic_dec(&r1_bio->behind_remaining);
330
331                         /* In behind mode, we ACK the master bio once the I/O has safely
332                          * reached all non-writemostly disks. Setting the Returned bit
333                          * ensures that this gets done only once -- we don't ever want to
334                          * return -EIO here, instead we'll wait */
335
336                         if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
337                             test_bit(R1BIO_Uptodate, &r1_bio->state)) {
338                                 /* Maybe we can return now */
339                                 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
340                                         struct bio *mbio = r1_bio->master_bio;
341                                         PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
342                                                (unsigned long long) mbio->bi_sector,
343                                                (unsigned long long) mbio->bi_sector +
344                                                (mbio->bi_size >> 9) - 1);
345                                         bio_endio(mbio, mbio->bi_size, 0);
346                                 }
347                         }
348                 }
349         }
350         /*
351          *
352          * Let's see if all mirrored write operations have finished
353          * already.
354          */
355         if (atomic_dec_and_test(&r1_bio->remaining)) {
356                 if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
357                         reschedule_retry(r1_bio);
358                         /* Don't dec_pending yet, we want to hold
359                          * the reference over the retry
360                          */
361                         return 0;
362                 }
363                 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
364                         /* free extra copy of the data pages */
365                         int i = bio->bi_vcnt;
366                         while (i--)
367                                 __free_page(bio->bi_io_vec[i].bv_page);
368                 }
369                 /* clear the bitmap if all writes complete successfully */
370                 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
371                                 r1_bio->sectors,
372                                 !test_bit(R1BIO_Degraded, &r1_bio->state),
373                                 behind);
374                 md_write_end(r1_bio->mddev);
375                 raid_end_bio_io(r1_bio);
376         }
377
378         if (r1_bio->bios[mirror]==NULL)
379                 bio_put(bio);
380
381         rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
382         return 0;
383 }
384
385
386 /*
387  * This routine returns the disk from which the requested read should
388  * be done. There is a per-array 'next expected sequential IO' sector
389  * number - if this matches on the next IO then we use the last disk.
390  * There is also a per-disk 'last know head position' sector that is
391  * maintained from IRQ contexts, both the normal and the resync IO
392  * completion handlers update this position correctly. If there is no
393  * perfect sequential match then we pick the disk whose head is closest.
394  *
395  * If there are 2 mirrors in the same 2 devices, performance degrades
396  * because position is mirror, not device based.
397  *
398  * The rdev for the device selected will have nr_pending incremented.
399  */
400 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
401 {
402         const unsigned long this_sector = r1_bio->sector;
403         int new_disk = conf->last_used, disk = new_disk;
404         int wonly_disk = -1;
405         const int sectors = r1_bio->sectors;
406         sector_t new_distance, current_distance;
407         mdk_rdev_t *rdev;
408
409         rcu_read_lock();
410         /*
411          * Check if we can balance. We can balance on the whole
412          * device if no resync is going on, or below the resync window.
413          * We take the first readable disk when above the resync window.
414          */
415  retry:
416         if (conf->mddev->recovery_cp < MaxSector &&
417             (this_sector + sectors >= conf->next_resync)) {
418                 /* Choose the first operation device, for consistancy */
419                 new_disk = 0;
420
421                 for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
422                      r1_bio->bios[new_disk] == IO_BLOCKED ||
423                      !rdev || !test_bit(In_sync, &rdev->flags)
424                              || test_bit(WriteMostly, &rdev->flags);
425                      rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
426
427                         if (rdev && test_bit(In_sync, &rdev->flags) &&
428                                 r1_bio->bios[new_disk] != IO_BLOCKED)
429                                 wonly_disk = new_disk;
430
431                         if (new_disk == conf->raid_disks - 1) {
432                                 new_disk = wonly_disk;
433                                 break;
434                         }
435                 }
436                 goto rb_out;
437         }
438
439
440         /* make sure the disk is operational */
441         for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
442              r1_bio->bios[new_disk] == IO_BLOCKED ||
443              !rdev || !test_bit(In_sync, &rdev->flags) ||
444                      test_bit(WriteMostly, &rdev->flags);
445              rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
446
447                 if (rdev && test_bit(In_sync, &rdev->flags) &&
448                     r1_bio->bios[new_disk] != IO_BLOCKED)
449                         wonly_disk = new_disk;
450
451                 if (new_disk <= 0)
452                         new_disk = conf->raid_disks;
453                 new_disk--;
454                 if (new_disk == disk) {
455                         new_disk = wonly_disk;
456                         break;
457                 }
458         }
459
460         if (new_disk < 0)
461                 goto rb_out;
462
463         disk = new_disk;
464         /* now disk == new_disk == starting point for search */
465
466         /*
467          * Don't change to another disk for sequential reads:
468          */
469         if (conf->next_seq_sect == this_sector)
470                 goto rb_out;
471         if (this_sector == conf->mirrors[new_disk].head_position)
472                 goto rb_out;
473
474         current_distance = abs(this_sector - conf->mirrors[disk].head_position);
475
476         /* Find the disk whose head is closest */
477
478         do {
479                 if (disk <= 0)
480                         disk = conf->raid_disks;
481                 disk--;
482
483                 rdev = rcu_dereference(conf->mirrors[disk].rdev);
484
485                 if (!rdev || r1_bio->bios[disk] == IO_BLOCKED ||
486                     !test_bit(In_sync, &rdev->flags) ||
487                     test_bit(WriteMostly, &rdev->flags))
488                         continue;
489
490                 if (!atomic_read(&rdev->nr_pending)) {
491                         new_disk = disk;
492                         break;
493                 }
494                 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
495                 if (new_distance < current_distance) {
496                         current_distance = new_distance;
497                         new_disk = disk;
498                 }
499         } while (disk != conf->last_used);
500
501  rb_out:
502
503
504         if (new_disk >= 0) {
505                 rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
506                 if (!rdev)
507                         goto retry;
508                 atomic_inc(&rdev->nr_pending);
509                 if (!test_bit(In_sync, &rdev->flags)) {
510                         /* cannot risk returning a device that failed
511                          * before we inc'ed nr_pending
512                          */
513                         atomic_dec(&rdev->nr_pending);
514                         goto retry;
515                 }
516                 conf->next_seq_sect = this_sector + sectors;
517                 conf->last_used = new_disk;
518         }
519         rcu_read_unlock();
520
521         return new_disk;
522 }
523
524 static void unplug_slaves(mddev_t *mddev)
525 {
526         conf_t *conf = mddev_to_conf(mddev);
527         int i;
528
529         rcu_read_lock();
530         for (i=0; i<mddev->raid_disks; i++) {
531                 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
532                 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
533                         request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
534
535                         atomic_inc(&rdev->nr_pending);
536                         rcu_read_unlock();
537
538                         if (r_queue->unplug_fn)
539                                 r_queue->unplug_fn(r_queue);
540
541                         rdev_dec_pending(rdev, mddev);
542                         rcu_read_lock();
543                 }
544         }
545         rcu_read_unlock();
546 }
547
548 static void raid1_unplug(request_queue_t *q)
549 {
550         mddev_t *mddev = q->queuedata;
551
552         unplug_slaves(mddev);
553         md_wakeup_thread(mddev->thread);
554 }
555
556 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
557                              sector_t *error_sector)
558 {
559         mddev_t *mddev = q->queuedata;
560         conf_t *conf = mddev_to_conf(mddev);
561         int i, ret = 0;
562
563         rcu_read_lock();
564         for (i=0; i<mddev->raid_disks && ret == 0; i++) {
565                 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
566                 if (rdev && !test_bit(Faulty, &rdev->flags)) {
567                         struct block_device *bdev = rdev->bdev;
568                         request_queue_t *r_queue = bdev_get_queue(bdev);
569
570                         if (!r_queue->issue_flush_fn)
571                                 ret = -EOPNOTSUPP;
572                         else {
573                                 atomic_inc(&rdev->nr_pending);
574                                 rcu_read_unlock();
575                                 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
576                                                               error_sector);
577                                 rdev_dec_pending(rdev, mddev);
578                                 rcu_read_lock();
579                         }
580                 }
581         }
582         rcu_read_unlock();
583         return ret;
584 }
585
586 /* Barriers....
587  * Sometimes we need to suspend IO while we do something else,
588  * either some resync/recovery, or reconfigure the array.
589  * To do this we raise a 'barrier'.
590  * The 'barrier' is a counter that can be raised multiple times
591  * to count how many activities are happening which preclude
592  * normal IO.
593  * We can only raise the barrier if there is no pending IO.
594  * i.e. if nr_pending == 0.
595  * We choose only to raise the barrier if no-one is waiting for the
596  * barrier to go down.  This means that as soon as an IO request
597  * is ready, no other operations which require a barrier will start
598  * until the IO request has had a chance.
599  *
600  * So: regular IO calls 'wait_barrier'.  When that returns there
601  *    is no backgroup IO happening,  It must arrange to call
602  *    allow_barrier when it has finished its IO.
603  * backgroup IO calls must call raise_barrier.  Once that returns
604  *    there is no normal IO happeing.  It must arrange to call
605  *    lower_barrier when the particular background IO completes.
606  */
607 #define RESYNC_DEPTH 32
608
609 static void raise_barrier(conf_t *conf)
610 {
611         spin_lock_irq(&conf->resync_lock);
612
613         /* Wait until no block IO is waiting */
614         wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
615                             conf->resync_lock,
616                             raid1_unplug(conf->mddev->queue));
617
618         /* block any new IO from starting */
619         conf->barrier++;
620
621         /* No wait for all pending IO to complete */
622         wait_event_lock_irq(conf->wait_barrier,
623                             !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
624                             conf->resync_lock,
625                             raid1_unplug(conf->mddev->queue));
626
627         spin_unlock_irq(&conf->resync_lock);
628 }
629
630 static void lower_barrier(conf_t *conf)
631 {
632         unsigned long flags;
633         spin_lock_irqsave(&conf->resync_lock, flags);
634         conf->barrier--;
635         spin_unlock_irqrestore(&conf->resync_lock, flags);
636         wake_up(&conf->wait_barrier);
637 }
638
639 static void wait_barrier(conf_t *conf)
640 {
641         spin_lock_irq(&conf->resync_lock);
642         if (conf->barrier) {
643                 conf->nr_waiting++;
644                 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
645                                     conf->resync_lock,
646                                     raid1_unplug(conf->mddev->queue));
647                 conf->nr_waiting--;
648         }
649         conf->nr_pending++;
650         spin_unlock_irq(&conf->resync_lock);
651 }
652
653 static void allow_barrier(conf_t *conf)
654 {
655         unsigned long flags;
656         spin_lock_irqsave(&conf->resync_lock, flags);
657         conf->nr_pending--;
658         spin_unlock_irqrestore(&conf->resync_lock, flags);
659         wake_up(&conf->wait_barrier);
660 }
661
662 static void freeze_array(conf_t *conf)
663 {
664         /* stop syncio and normal IO and wait for everything to
665          * go quite.
666          * We increment barrier and nr_waiting, and then
667          * wait until barrier+nr_pending match nr_queued+2
668          */
669         spin_lock_irq(&conf->resync_lock);
670         conf->barrier++;
671         conf->nr_waiting++;
672         wait_event_lock_irq(conf->wait_barrier,
673                             conf->barrier+conf->nr_pending == conf->nr_queued+2,
674                             conf->resync_lock,
675                             raid1_unplug(conf->mddev->queue));
676         spin_unlock_irq(&conf->resync_lock);
677 }
678 static void unfreeze_array(conf_t *conf)
679 {
680         /* reverse the effect of the freeze */
681         spin_lock_irq(&conf->resync_lock);
682         conf->barrier--;
683         conf->nr_waiting--;
684         wake_up(&conf->wait_barrier);
685         spin_unlock_irq(&conf->resync_lock);
686 }
687
688
689 /* duplicate the data pages for behind I/O */
690 static struct page **alloc_behind_pages(struct bio *bio)
691 {
692         int i;
693         struct bio_vec *bvec;
694         struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
695                                         GFP_NOIO);
696         if (unlikely(!pages))
697                 goto do_sync_io;
698
699         memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
700
701         bio_for_each_segment(bvec, bio, i) {
702                 pages[i] = alloc_page(GFP_NOIO);
703                 if (unlikely(!pages[i]))
704                         goto do_sync_io;
705                 memcpy(kmap(pages[i]) + bvec->bv_offset,
706                         kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
707                 kunmap(pages[i]);
708                 kunmap(bvec->bv_page);
709         }
710
711         return pages;
712
713 do_sync_io:
714         if (pages)
715                 for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
716                         __free_page(pages[i]);
717         kfree(pages);
718         PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
719         return NULL;
720 }
721
722 static int make_request(request_queue_t *q, struct bio * bio)
723 {
724         mddev_t *mddev = q->queuedata;
725         conf_t *conf = mddev_to_conf(mddev);
726         mirror_info_t *mirror;
727         r1bio_t *r1_bio;
728         struct bio *read_bio;
729         int i, targets = 0, disks;
730         mdk_rdev_t *rdev;
731         struct bitmap *bitmap = mddev->bitmap;
732         unsigned long flags;
733         struct bio_list bl;
734         struct page **behind_pages = NULL;
735         const int rw = bio_data_dir(bio);
736         int do_barriers;
737
738         if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
739                 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
740                 return 0;
741         }
742
743         /*
744          * Register the new request and wait if the reconstruction
745          * thread has put up a bar for new requests.
746          * Continue immediately if no resync is active currently.
747          */
748         md_write_start(mddev, bio); /* wait on superblock update early */
749
750         wait_barrier(conf);
751
752         disk_stat_inc(mddev->gendisk, ios[rw]);
753         disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
754
755         /*
756          * make_request() can abort the operation when READA is being
757          * used and no empty request is available.
758          *
759          */
760         r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
761
762         r1_bio->master_bio = bio;
763         r1_bio->sectors = bio->bi_size >> 9;
764         r1_bio->state = 0;
765         r1_bio->mddev = mddev;
766         r1_bio->sector = bio->bi_sector;
767
768         if (rw == READ) {
769                 /*
770                  * read balancing logic:
771                  */
772                 int rdisk = read_balance(conf, r1_bio);
773
774                 if (rdisk < 0) {
775                         /* couldn't find anywhere to read from */
776                         raid_end_bio_io(r1_bio);
777                         return 0;
778                 }
779                 mirror = conf->mirrors + rdisk;
780
781                 r1_bio->read_disk = rdisk;
782
783                 read_bio = bio_clone(bio, GFP_NOIO);
784
785                 r1_bio->bios[rdisk] = read_bio;
786
787                 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
788                 read_bio->bi_bdev = mirror->rdev->bdev;
789                 read_bio->bi_end_io = raid1_end_read_request;
790                 read_bio->bi_rw = READ;
791                 read_bio->bi_private = r1_bio;
792
793                 generic_make_request(read_bio);
794                 return 0;
795         }
796
797         /*
798          * WRITE:
799          */
800         /* first select target devices under spinlock and
801          * inc refcount on their rdev.  Record them by setting
802          * bios[x] to bio
803          */
804         disks = conf->raid_disks;
805 #if 0
806         { static int first=1;
807         if (first) printk("First Write sector %llu disks %d\n",
808                           (unsigned long long)r1_bio->sector, disks);
809         first = 0;
810         }
811 #endif
812         rcu_read_lock();
813         for (i = 0;  i < disks; i++) {
814                 if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
815                     !test_bit(Faulty, &rdev->flags)) {
816                         atomic_inc(&rdev->nr_pending);
817                         if (test_bit(Faulty, &rdev->flags)) {
818                                 atomic_dec(&rdev->nr_pending);
819                                 r1_bio->bios[i] = NULL;
820                         } else
821                                 r1_bio->bios[i] = bio;
822                         targets++;
823                 } else
824                         r1_bio->bios[i] = NULL;
825         }
826         rcu_read_unlock();
827
828         BUG_ON(targets == 0); /* we never fail the last device */
829
830         if (targets < conf->raid_disks) {
831                 /* array is degraded, we will not clear the bitmap
832                  * on I/O completion (see raid1_end_write_request) */
833                 set_bit(R1BIO_Degraded, &r1_bio->state);
834         }
835
836         /* do behind I/O ? */
837         if (bitmap &&
838             atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
839             (behind_pages = alloc_behind_pages(bio)) != NULL)
840                 set_bit(R1BIO_BehindIO, &r1_bio->state);
841
842         atomic_set(&r1_bio->remaining, 0);
843         atomic_set(&r1_bio->behind_remaining, 0);
844
845         do_barriers = bio->bi_rw & BIO_RW_BARRIER;
846         if (do_barriers)
847                 set_bit(R1BIO_Barrier, &r1_bio->state);
848
849         bio_list_init(&bl);
850         for (i = 0; i < disks; i++) {
851                 struct bio *mbio;
852                 if (!r1_bio->bios[i])
853                         continue;
854
855                 mbio = bio_clone(bio, GFP_NOIO);
856                 r1_bio->bios[i] = mbio;
857
858                 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
859                 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
860                 mbio->bi_end_io = raid1_end_write_request;
861                 mbio->bi_rw = WRITE | do_barriers;
862                 mbio->bi_private = r1_bio;
863
864                 if (behind_pages) {
865                         struct bio_vec *bvec;
866                         int j;
867
868                         /* Yes, I really want the '__' version so that
869                          * we clear any unused pointer in the io_vec, rather
870                          * than leave them unchanged.  This is important
871                          * because when we come to free the pages, we won't
872                          * know the originial bi_idx, so we just free
873                          * them all
874                          */
875                         __bio_for_each_segment(bvec, mbio, j, 0)
876                                 bvec->bv_page = behind_pages[j];
877                         if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
878                                 atomic_inc(&r1_bio->behind_remaining);
879                 }
880
881                 atomic_inc(&r1_bio->remaining);
882
883                 bio_list_add(&bl, mbio);
884         }
885         kfree(behind_pages); /* the behind pages are attached to the bios now */
886
887         bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
888                                 test_bit(R1BIO_BehindIO, &r1_bio->state));
889         spin_lock_irqsave(&conf->device_lock, flags);
890         bio_list_merge(&conf->pending_bio_list, &bl);
891         bio_list_init(&bl);
892
893         blk_plug_device(mddev->queue);
894         spin_unlock_irqrestore(&conf->device_lock, flags);
895
896 #if 0
897         while ((bio = bio_list_pop(&bl)) != NULL)
898                 generic_make_request(bio);
899 #endif
900
901         return 0;
902 }
903
904 static void status(struct seq_file *seq, mddev_t *mddev)
905 {
906         conf_t *conf = mddev_to_conf(mddev);
907         int i;
908
909         seq_printf(seq, " [%d/%d] [", conf->raid_disks,
910                                                 conf->working_disks);
911         for (i = 0; i < conf->raid_disks; i++)
912                 seq_printf(seq, "%s",
913                               conf->mirrors[i].rdev &&
914                               test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
915         seq_printf(seq, "]");
916 }
917
918
919 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
920 {
921         char b[BDEVNAME_SIZE];
922         conf_t *conf = mddev_to_conf(mddev);
923
924         /*
925          * If it is not operational, then we have already marked it as dead
926          * else if it is the last working disks, ignore the error, let the
927          * next level up know.
928          * else mark the drive as failed
929          */
930         if (test_bit(In_sync, &rdev->flags)
931             && conf->working_disks == 1)
932                 /*
933                  * Don't fail the drive, act as though we were just a
934                  * normal single drive
935                  */
936                 return;
937         if (test_bit(In_sync, &rdev->flags)) {
938                 mddev->degraded++;
939                 conf->working_disks--;
940                 /*
941                  * if recovery is running, make sure it aborts.
942                  */
943                 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
944         }
945         clear_bit(In_sync, &rdev->flags);
946         set_bit(Faulty, &rdev->flags);
947         mddev->sb_dirty = 1;
948         printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
949                 "       Operation continuing on %d devices\n",
950                 bdevname(rdev->bdev,b), conf->working_disks);
951 }
952
953 static void print_conf(conf_t *conf)
954 {
955         int i;
956         mirror_info_t *tmp;
957
958         printk("RAID1 conf printout:\n");
959         if (!conf) {
960                 printk("(!conf)\n");
961                 return;
962         }
963         printk(" --- wd:%d rd:%d\n", conf->working_disks,
964                 conf->raid_disks);
965
966         for (i = 0; i < conf->raid_disks; i++) {
967                 char b[BDEVNAME_SIZE];
968                 tmp = conf->mirrors + i;
969                 if (tmp->rdev)
970                         printk(" disk %d, wo:%d, o:%d, dev:%s\n",
971                                 i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
972                                 bdevname(tmp->rdev->bdev,b));
973         }
974 }
975
976 static void close_sync(conf_t *conf)
977 {
978         wait_barrier(conf);
979         allow_barrier(conf);
980
981         mempool_destroy(conf->r1buf_pool);
982         conf->r1buf_pool = NULL;
983 }
984
985 static int raid1_spare_active(mddev_t *mddev)
986 {
987         int i;
988         conf_t *conf = mddev->private;
989         mirror_info_t *tmp;
990
991         /*
992          * Find all failed disks within the RAID1 configuration 
993          * and mark them readable
994          */
995         for (i = 0; i < conf->raid_disks; i++) {
996                 tmp = conf->mirrors + i;
997                 if (tmp->rdev 
998                     && !test_bit(Faulty, &tmp->rdev->flags)
999                     && !test_bit(In_sync, &tmp->rdev->flags)) {
1000                         conf->working_disks++;
1001                         mddev->degraded--;
1002                         set_bit(In_sync, &tmp->rdev->flags);
1003                 }
1004         }
1005
1006         print_conf(conf);
1007         return 0;
1008 }
1009
1010
1011 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1012 {
1013         conf_t *conf = mddev->private;
1014         int found = 0;
1015         int mirror = 0;
1016         mirror_info_t *p;
1017
1018         for (mirror=0; mirror < mddev->raid_disks; mirror++)
1019                 if ( !(p=conf->mirrors+mirror)->rdev) {
1020
1021                         blk_queue_stack_limits(mddev->queue,
1022                                                rdev->bdev->bd_disk->queue);
1023                         /* as we don't honour merge_bvec_fn, we must never risk
1024                          * violating it, so limit ->max_sector to one PAGE, as
1025                          * a one page request is never in violation.
1026                          */
1027                         if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1028                             mddev->queue->max_sectors > (PAGE_SIZE>>9))
1029                                 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1030
1031                         p->head_position = 0;
1032                         rdev->raid_disk = mirror;
1033                         found = 1;
1034                         /* As all devices are equivalent, we don't need a full recovery
1035                          * if this was recently any drive of the array
1036                          */
1037                         if (rdev->saved_raid_disk < 0)
1038                                 conf->fullsync = 1;
1039                         rcu_assign_pointer(p->rdev, rdev);
1040                         break;
1041                 }
1042
1043         print_conf(conf);
1044         return found;
1045 }
1046
1047 static int raid1_remove_disk(mddev_t *mddev, int number)
1048 {
1049         conf_t *conf = mddev->private;
1050         int err = 0;
1051         mdk_rdev_t *rdev;
1052         mirror_info_t *p = conf->mirrors+ number;
1053
1054         print_conf(conf);
1055         rdev = p->rdev;
1056         if (rdev) {
1057                 if (test_bit(In_sync, &rdev->flags) ||
1058                     atomic_read(&rdev->nr_pending)) {
1059                         err = -EBUSY;
1060                         goto abort;
1061                 }
1062                 p->rdev = NULL;
1063                 synchronize_rcu();
1064                 if (atomic_read(&rdev->nr_pending)) {
1065                         /* lost the race, try later */
1066                         err = -EBUSY;
1067                         p->rdev = rdev;
1068                 }
1069         }
1070 abort:
1071
1072         print_conf(conf);
1073         return err;
1074 }
1075
1076
1077 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1078 {
1079         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1080
1081         if (bio->bi_size)
1082                 return 1;
1083
1084         if (r1_bio->bios[r1_bio->read_disk] != bio)
1085                 BUG();
1086         update_head_pos(r1_bio->read_disk, r1_bio);
1087         /*
1088          * we have read a block, now it needs to be re-written,
1089          * or re-read if the read failed.
1090          * We don't do much here, just schedule handling by raid1d
1091          */
1092         if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1093                 set_bit(R1BIO_Uptodate, &r1_bio->state);
1094         reschedule_retry(r1_bio);
1095         return 0;
1096 }
1097
1098 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1099 {
1100         int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1101         r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
1102         mddev_t *mddev = r1_bio->mddev;
1103         conf_t *conf = mddev_to_conf(mddev);
1104         int i;
1105         int mirror=0;
1106
1107         if (bio->bi_size)
1108                 return 1;
1109
1110         for (i = 0; i < conf->raid_disks; i++)
1111                 if (r1_bio->bios[i] == bio) {
1112                         mirror = i;
1113                         break;
1114                 }
1115         if (!uptodate)
1116                 md_error(mddev, conf->mirrors[mirror].rdev);
1117
1118         update_head_pos(mirror, r1_bio);
1119
1120         if (atomic_dec_and_test(&r1_bio->remaining)) {
1121                 md_done_sync(mddev, r1_bio->sectors, uptodate);
1122                 put_buf(r1_bio);
1123         }
1124         return 0;
1125 }
1126
1127 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1128 {
1129         conf_t *conf = mddev_to_conf(mddev);
1130         int i;
1131         int disks = conf->raid_disks;
1132         struct bio *bio, *wbio;
1133
1134         bio = r1_bio->bios[r1_bio->read_disk];
1135
1136
1137         /*
1138          * schedule writes
1139          */
1140         if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
1141                 /* ouch - failed to read all of that.
1142                  * Try some synchronous reads of other devices to get
1143                  * good data, much like with normal read errors.  Only
1144                  * read into the pages we already have so they we don't
1145                  * need to re-issue the read request.
1146                  * We don't need to freeze the array, because being in an
1147                  * active sync request, there is no normal IO, and
1148                  * no overlapping syncs.
1149                  */
1150                 sector_t sect = r1_bio->sector;
1151                 int sectors = r1_bio->sectors;
1152                 int idx = 0;
1153
1154                 while(sectors) {
1155                         int s = sectors;
1156                         int d = r1_bio->read_disk;
1157                         int success = 0;
1158                         mdk_rdev_t *rdev;
1159
1160                         if (s > (PAGE_SIZE>>9))
1161                                 s = PAGE_SIZE >> 9;
1162                         do {
1163                                 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1164                                         rdev = conf->mirrors[d].rdev;
1165                                         if (sync_page_io(rdev->bdev,
1166                                                          sect + rdev->data_offset,
1167                                                          s<<9,
1168                                                          bio->bi_io_vec[idx].bv_page,
1169                                                          READ)) {
1170                                                 success = 1;
1171                                                 break;
1172                                         }
1173                                 }
1174                                 d++;
1175                                 if (d == conf->raid_disks)
1176                                         d = 0;
1177                         } while (!success && d != r1_bio->read_disk);
1178
1179                         if (success) {
1180                                 /* write it back and re-read */
1181                                 set_bit(R1BIO_Uptodate, &r1_bio->state);
1182                                 while (d != r1_bio->read_disk) {
1183                                         if (d == 0)
1184                                                 d = conf->raid_disks;
1185                                         d--;
1186                                         if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1187                                                 continue;
1188                                         rdev = conf->mirrors[d].rdev;
1189                                         if (sync_page_io(rdev->bdev,
1190                                                          sect + rdev->data_offset,
1191                                                          s<<9,
1192                                                          bio->bi_io_vec[idx].bv_page,
1193                                                          WRITE) == 0 ||
1194                                             sync_page_io(rdev->bdev,
1195                                                          sect + rdev->data_offset,
1196                                                          s<<9,
1197                                                          bio->bi_io_vec[idx].bv_page,
1198                                                          READ) == 0) {
1199                                                 md_error(mddev, rdev);
1200                                         }
1201                                 }
1202                         } else {
1203                                 char b[BDEVNAME_SIZE];
1204                                 /* Cannot read from anywhere, array is toast */
1205                                 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1206                                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
1207                                        " for block %llu\n",
1208                                        bdevname(bio->bi_bdev,b),
1209                                        (unsigned long long)r1_bio->sector);
1210                                 md_done_sync(mddev, r1_bio->sectors, 0);
1211                                 put_buf(r1_bio);
1212                                 return;
1213                         }
1214                         sectors -= s;
1215                         sect += s;
1216                         idx ++;
1217                 }
1218         }
1219         atomic_set(&r1_bio->remaining, 1);
1220         for (i = 0; i < disks ; i++) {
1221                 wbio = r1_bio->bios[i];
1222                 if (wbio->bi_end_io == NULL ||
1223                     (wbio->bi_end_io == end_sync_read &&
1224                      (i == r1_bio->read_disk ||
1225                       !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1226                         continue;
1227
1228                 wbio->bi_rw = WRITE;
1229                 wbio->bi_end_io = end_sync_write;
1230                 atomic_inc(&r1_bio->remaining);
1231                 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1232
1233                 generic_make_request(wbio);
1234         }
1235
1236         if (atomic_dec_and_test(&r1_bio->remaining)) {
1237                 /* if we're here, all write(s) have completed, so clean up */
1238                 md_done_sync(mddev, r1_bio->sectors, 1);
1239                 put_buf(r1_bio);
1240         }
1241 }
1242
1243 /*
1244  * This is a kernel thread which:
1245  *
1246  *      1.      Retries failed read operations on working mirrors.
1247  *      2.      Updates the raid superblock when problems encounter.
1248  *      3.      Performs writes following reads for array syncronising.
1249  */
1250
1251 static void raid1d(mddev_t *mddev)
1252 {
1253         r1bio_t *r1_bio;
1254         struct bio *bio;
1255         unsigned long flags;
1256         conf_t *conf = mddev_to_conf(mddev);
1257         struct list_head *head = &conf->retry_list;
1258         int unplug=0;
1259         mdk_rdev_t *rdev;
1260
1261         md_check_recovery(mddev);
1262         
1263         for (;;) {
1264                 char b[BDEVNAME_SIZE];
1265                 spin_lock_irqsave(&conf->device_lock, flags);
1266
1267                 if (conf->pending_bio_list.head) {
1268                         bio = bio_list_get(&conf->pending_bio_list);
1269                         blk_remove_plug(mddev->queue);
1270                         spin_unlock_irqrestore(&conf->device_lock, flags);
1271                         /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1272                         if (bitmap_unplug(mddev->bitmap) != 0)
1273                                 printk("%s: bitmap file write failed!\n", mdname(mddev));
1274
1275                         while (bio) { /* submit pending writes */
1276                                 struct bio *next = bio->bi_next;
1277                                 bio->bi_next = NULL;
1278                                 generic_make_request(bio);
1279                                 bio = next;
1280                         }
1281                         unplug = 1;
1282
1283                         continue;
1284                 }
1285
1286                 if (list_empty(head))
1287                         break;
1288                 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1289                 list_del(head->prev);
1290                 conf->nr_queued--;
1291                 spin_unlock_irqrestore(&conf->device_lock, flags);
1292
1293                 mddev = r1_bio->mddev;
1294                 conf = mddev_to_conf(mddev);
1295                 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1296                         sync_request_write(mddev, r1_bio);
1297                         unplug = 1;
1298                 } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
1299                         /* some requests in the r1bio were BIO_RW_BARRIER
1300                          * requests which failed with -ENOTSUPP.  Hohumm..
1301                          * Better resubmit without the barrier.
1302                          * We know which devices to resubmit for, because
1303                          * all others have had their bios[] entry cleared.
1304                          */
1305                         int i;
1306                         clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
1307                         clear_bit(R1BIO_Barrier, &r1_bio->state);
1308                         for (i=0; i < conf->raid_disks; i++)
1309                                 if (r1_bio->bios[i]) {
1310                                         struct bio_vec *bvec;
1311                                         int j;
1312
1313                                         bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1314                                         /* copy pages from the failed bio, as
1315                                          * this might be a write-behind device */
1316                                         __bio_for_each_segment(bvec, bio, j, 0)
1317                                                 bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
1318                                         bio_put(r1_bio->bios[i]);
1319                                         bio->bi_sector = r1_bio->sector +
1320                                                 conf->mirrors[i].rdev->data_offset;
1321                                         bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1322                                         bio->bi_end_io = raid1_end_write_request;
1323                                         bio->bi_rw = WRITE;
1324                                         bio->bi_private = r1_bio;
1325                                         r1_bio->bios[i] = bio;
1326                                         generic_make_request(bio);
1327                                 }
1328                 } else {
1329                         int disk;
1330
1331                         /* we got a read error. Maybe the drive is bad.  Maybe just
1332                          * the block and we can fix it.
1333                          * We freeze all other IO, and try reading the block from
1334                          * other devices.  When we find one, we re-write
1335                          * and check it that fixes the read error.
1336                          * This is all done synchronously while the array is
1337                          * frozen
1338                          */
1339                         sector_t sect = r1_bio->sector;
1340                         int sectors = r1_bio->sectors;
1341                         freeze_array(conf);
1342                         if (mddev->ro == 0) while(sectors) {
1343                                 int s = sectors;
1344                                 int d = r1_bio->read_disk;
1345                                 int success = 0;
1346
1347                                 if (s > (PAGE_SIZE>>9))
1348                                         s = PAGE_SIZE >> 9;
1349
1350                                 do {
1351                                         rdev = conf->mirrors[d].rdev;
1352                                         if (rdev &&
1353                                             test_bit(In_sync, &rdev->flags) &&
1354                                             sync_page_io(rdev->bdev,
1355                                                          sect + rdev->data_offset,
1356                                                          s<<9,
1357                                                          conf->tmppage, READ))
1358                                                 success = 1;
1359                                         else {
1360                                                 d++;
1361                                                 if (d == conf->raid_disks)
1362                                                         d = 0;
1363                                         }
1364                                 } while (!success && d != r1_bio->read_disk);
1365
1366                                 if (success) {
1367                                         /* write it back and re-read */
1368                                         while (d != r1_bio->read_disk) {
1369                                                 if (d==0)
1370                                                         d = conf->raid_disks;
1371                                                 d--;
1372                                                 rdev = conf->mirrors[d].rdev;
1373                                                 if (rdev &&
1374                                                     test_bit(In_sync, &rdev->flags)) {
1375                                                         if (sync_page_io(rdev->bdev,
1376                                                                          sect + rdev->data_offset,
1377                                                                          s<<9, conf->tmppage, WRITE) == 0 ||
1378                                                             sync_page_io(rdev->bdev,
1379                                                                          sect + rdev->data_offset,
1380                                                                          s<<9, conf->tmppage, READ) == 0) {
1381                                                                 /* Well, this device is dead */
1382                                                                 md_error(mddev, rdev);
1383                                                         }
1384                                                 }
1385                                         }
1386                                 } else {
1387                                         /* Cannot read from anywhere -- bye bye array */
1388                                         md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1389                                         break;
1390                                 }
1391                                 sectors -= s;
1392                                 sect += s;
1393                         }
1394
1395                         unfreeze_array(conf);
1396
1397                         bio = r1_bio->bios[r1_bio->read_disk];
1398                         if ((disk=read_balance(conf, r1_bio)) == -1) {
1399                                 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1400                                        " read error for block %llu\n",
1401                                        bdevname(bio->bi_bdev,b),
1402                                        (unsigned long long)r1_bio->sector);
1403                                 raid_end_bio_io(r1_bio);
1404                         } else {
1405                                 r1_bio->bios[r1_bio->read_disk] =
1406                                         mddev->ro ? IO_BLOCKED : NULL;
1407                                 r1_bio->read_disk = disk;
1408                                 bio_put(bio);
1409                                 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1410                                 r1_bio->bios[r1_bio->read_disk] = bio;
1411                                 rdev = conf->mirrors[disk].rdev;
1412                                 if (printk_ratelimit())
1413                                         printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1414                                                " another mirror\n",
1415                                                bdevname(rdev->bdev,b),
1416                                                (unsigned long long)r1_bio->sector);
1417                                 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1418                                 bio->bi_bdev = rdev->bdev;
1419                                 bio->bi_end_io = raid1_end_read_request;
1420                                 bio->bi_rw = READ;
1421                                 bio->bi_private = r1_bio;
1422                                 unplug = 1;
1423                                 generic_make_request(bio);
1424                         }
1425                 }
1426         }
1427         spin_unlock_irqrestore(&conf->device_lock, flags);
1428         if (unplug)
1429                 unplug_slaves(mddev);
1430 }
1431
1432
1433 static int init_resync(conf_t *conf)
1434 {
1435         int buffs;
1436
1437         buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1438         if (conf->r1buf_pool)
1439                 BUG();
1440         conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1441                                           conf->poolinfo);
1442         if (!conf->r1buf_pool)
1443                 return -ENOMEM;
1444         conf->next_resync = 0;
1445         return 0;
1446 }
1447
1448 /*
1449  * perform a "sync" on one "block"
1450  *
1451  * We need to make sure that no normal I/O request - particularly write
1452  * requests - conflict with active sync requests.
1453  *
1454  * This is achieved by tracking pending requests and a 'barrier' concept
1455  * that can be installed to exclude normal IO requests.
1456  */
1457
1458 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1459 {
1460         conf_t *conf = mddev_to_conf(mddev);
1461         r1bio_t *r1_bio;
1462         struct bio *bio;
1463         sector_t max_sector, nr_sectors;
1464         int disk = -1;
1465         int i;
1466         int wonly = -1;
1467         int write_targets = 0, read_targets = 0;
1468         int sync_blocks;
1469         int still_degraded = 0;
1470
1471         if (!conf->r1buf_pool)
1472         {
1473 /*
1474                 printk("sync start - bitmap %p\n", mddev->bitmap);
1475 */
1476                 if (init_resync(conf))
1477                         return 0;
1478         }
1479
1480         max_sector = mddev->size << 1;
1481         if (sector_nr >= max_sector) {
1482                 /* If we aborted, we need to abort the
1483                  * sync on the 'current' bitmap chunk (there will
1484                  * only be one in raid1 resync.
1485                  * We can find the current addess in mddev->curr_resync
1486                  */
1487                 if (mddev->curr_resync < max_sector) /* aborted */
1488                         bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1489                                                 &sync_blocks, 1);
1490                 else /* completed sync */
1491                         conf->fullsync = 0;
1492
1493                 bitmap_close_sync(mddev->bitmap);
1494                 close_sync(conf);
1495                 return 0;
1496         }
1497
1498         /* before building a request, check if we can skip these blocks..
1499          * This call the bitmap_start_sync doesn't actually record anything
1500          */
1501         if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1502             !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1503                 /* We can skip this block, and probably several more */
1504                 *skipped = 1;
1505                 return sync_blocks;
1506         }
1507         /*
1508          * If there is non-resync activity waiting for a turn,
1509          * and resync is going fast enough,
1510          * then let it though before starting on this new sync request.
1511          */
1512         if (!go_faster && conf->nr_waiting)
1513                 msleep_interruptible(1000);
1514
1515         raise_barrier(conf);
1516
1517         conf->next_resync = sector_nr;
1518
1519         r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1520         rcu_read_lock();
1521         /*
1522          * If we get a correctably read error during resync or recovery,
1523          * we might want to read from a different device.  So we
1524          * flag all drives that could conceivably be read from for READ,
1525          * and any others (which will be non-In_sync devices) for WRITE.
1526          * If a read fails, we try reading from something else for which READ
1527          * is OK.
1528          */
1529
1530         r1_bio->mddev = mddev;
1531         r1_bio->sector = sector_nr;
1532         r1_bio->state = 0;
1533         set_bit(R1BIO_IsSync, &r1_bio->state);
1534
1535         for (i=0; i < conf->raid_disks; i++) {
1536                 mdk_rdev_t *rdev;
1537                 bio = r1_bio->bios[i];
1538
1539                 /* take from bio_init */
1540                 bio->bi_next = NULL;
1541                 bio->bi_flags |= 1 << BIO_UPTODATE;
1542                 bio->bi_rw = 0;
1543                 bio->bi_vcnt = 0;
1544                 bio->bi_idx = 0;
1545                 bio->bi_phys_segments = 0;
1546                 bio->bi_hw_segments = 0;
1547                 bio->bi_size = 0;
1548                 bio->bi_end_io = NULL;
1549                 bio->bi_private = NULL;
1550
1551                 rdev = rcu_dereference(conf->mirrors[i].rdev);
1552                 if (rdev == NULL ||
1553                            test_bit(Faulty, &rdev->flags)) {
1554                         still_degraded = 1;
1555                         continue;
1556                 } else if (!test_bit(In_sync, &rdev->flags)) {
1557                         bio->bi_rw = WRITE;
1558                         bio->bi_end_io = end_sync_write;
1559                         write_targets ++;
1560                 } else {
1561                         /* may need to read from here */
1562                         bio->bi_rw = READ;
1563                         bio->bi_end_io = end_sync_read;
1564                         if (test_bit(WriteMostly, &rdev->flags)) {
1565                                 if (wonly < 0)
1566                                         wonly = i;
1567                         } else {
1568                                 if (disk < 0)
1569                                         disk = i;
1570                         }
1571                         read_targets++;
1572                 }
1573                 atomic_inc(&rdev->nr_pending);
1574                 bio->bi_sector = sector_nr + rdev->data_offset;
1575                 bio->bi_bdev = rdev->bdev;
1576                 bio->bi_private = r1_bio;
1577         }
1578         rcu_read_unlock();
1579         if (disk < 0)
1580                 disk = wonly;
1581         r1_bio->read_disk = disk;
1582
1583         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
1584                 /* extra read targets are also write targets */
1585                 write_targets += read_targets-1;
1586
1587         if (write_targets == 0 || read_targets == 0) {
1588                 /* There is nowhere to write, so all non-sync
1589                  * drives must be failed - so we are finished
1590                  */
1591                 sector_t rv = max_sector - sector_nr;
1592                 *skipped = 1;
1593                 put_buf(r1_bio);
1594                 return rv;
1595         }
1596
1597         nr_sectors = 0;
1598         sync_blocks = 0;
1599         do {
1600                 struct page *page;
1601                 int len = PAGE_SIZE;
1602                 if (sector_nr + (len>>9) > max_sector)
1603                         len = (max_sector - sector_nr) << 9;
1604                 if (len == 0)
1605                         break;
1606                 if (sync_blocks == 0) {
1607                         if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1608                                                &sync_blocks, still_degraded) &&
1609                             !conf->fullsync &&
1610                             !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1611                                 break;
1612                         if (sync_blocks < (PAGE_SIZE>>9))
1613                                 BUG();
1614                         if (len > (sync_blocks<<9))
1615                                 len = sync_blocks<<9;
1616                 }
1617
1618                 for (i=0 ; i < conf->raid_disks; i++) {
1619                         bio = r1_bio->bios[i];
1620                         if (bio->bi_end_io) {
1621                                 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1622                                 if (bio_add_page(bio, page, len, 0) == 0) {
1623                                         /* stop here */
1624                                         r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1625                                         while (i > 0) {
1626                                                 i--;
1627                                                 bio = r1_bio->bios[i];
1628                                                 if (bio->bi_end_io==NULL)
1629                                                         continue;
1630                                                 /* remove last page from this bio */
1631                                                 bio->bi_vcnt--;
1632                                                 bio->bi_size -= len;
1633                                                 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1634                                         }
1635                                         goto bio_full;
1636                                 }
1637                         }
1638                 }
1639                 nr_sectors += len>>9;
1640                 sector_nr += len>>9;
1641                 sync_blocks -= (len>>9);
1642         } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1643  bio_full:
1644         bio = r1_bio->bios[r1_bio->read_disk];
1645         r1_bio->sectors = nr_sectors;
1646
1647         md_sync_acct(conf->mirrors[r1_bio->read_disk].rdev->bdev, nr_sectors);
1648
1649         generic_make_request(bio);
1650
1651         return nr_sectors;
1652 }
1653
1654 static int run(mddev_t *mddev)
1655 {
1656         conf_t *conf;
1657         int i, j, disk_idx;
1658         mirror_info_t *disk;
1659         mdk_rdev_t *rdev;
1660         struct list_head *tmp;
1661
1662         if (mddev->level != 1) {
1663                 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1664                        mdname(mddev), mddev->level);
1665                 goto out;
1666         }
1667         /*
1668          * copy the already verified devices into our private RAID1
1669          * bookkeeping area. [whatever we allocate in run(),
1670          * should be freed in stop()]
1671          */
1672         conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1673         mddev->private = conf;
1674         if (!conf)
1675                 goto out_no_mem;
1676
1677         memset(conf, 0, sizeof(*conf));
1678         conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks, 
1679                                  GFP_KERNEL);
1680         if (!conf->mirrors)
1681                 goto out_no_mem;
1682
1683         memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1684
1685         conf->tmppage = alloc_page(GFP_KERNEL);
1686         if (!conf->tmppage)
1687                 goto out_no_mem;
1688
1689         conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1690         if (!conf->poolinfo)
1691                 goto out_no_mem;
1692         conf->poolinfo->mddev = mddev;
1693         conf->poolinfo->raid_disks = mddev->raid_disks;
1694         conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1695                                           r1bio_pool_free,
1696                                           conf->poolinfo);
1697         if (!conf->r1bio_pool)
1698                 goto out_no_mem;
1699
1700         ITERATE_RDEV(mddev, rdev, tmp) {
1701                 disk_idx = rdev->raid_disk;
1702                 if (disk_idx >= mddev->raid_disks
1703                     || disk_idx < 0)
1704                         continue;
1705                 disk = conf->mirrors + disk_idx;
1706
1707                 disk->rdev = rdev;
1708
1709                 blk_queue_stack_limits(mddev->queue,
1710                                        rdev->bdev->bd_disk->queue);
1711                 /* as we don't honour merge_bvec_fn, we must never risk
1712                  * violating it, so limit ->max_sector to one PAGE, as
1713                  * a one page request is never in violation.
1714                  */
1715                 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1716                     mddev->queue->max_sectors > (PAGE_SIZE>>9))
1717                         blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1718
1719                 disk->head_position = 0;
1720                 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1721                         conf->working_disks++;
1722         }
1723         conf->raid_disks = mddev->raid_disks;
1724         conf->mddev = mddev;
1725         spin_lock_init(&conf->device_lock);
1726         INIT_LIST_HEAD(&conf->retry_list);
1727         if (conf->working_disks == 1)
1728                 mddev->recovery_cp = MaxSector;
1729
1730         spin_lock_init(&conf->resync_lock);
1731         init_waitqueue_head(&conf->wait_barrier);
1732
1733         bio_list_init(&conf->pending_bio_list);
1734         bio_list_init(&conf->flushing_bio_list);
1735
1736         if (!conf->working_disks) {
1737                 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1738                         mdname(mddev));
1739                 goto out_free_conf;
1740         }
1741
1742         mddev->degraded = 0;
1743         for (i = 0; i < conf->raid_disks; i++) {
1744
1745                 disk = conf->mirrors + i;
1746
1747                 if (!disk->rdev) {
1748                         disk->head_position = 0;
1749                         mddev->degraded++;
1750                 }
1751         }
1752
1753         /*
1754          * find the first working one and use it as a starting point
1755          * to read balancing.
1756          */
1757         for (j = 0; j < conf->raid_disks &&
1758                      (!conf->mirrors[j].rdev ||
1759                       !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
1760                 /* nothing */;
1761         conf->last_used = j;
1762
1763
1764         mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1765         if (!mddev->thread) {
1766                 printk(KERN_ERR
1767                        "raid1: couldn't allocate thread for %s\n",
1768                        mdname(mddev));
1769                 goto out_free_conf;
1770         }
1771
1772         printk(KERN_INFO 
1773                 "raid1: raid set %s active with %d out of %d mirrors\n",
1774                 mdname(mddev), mddev->raid_disks - mddev->degraded, 
1775                 mddev->raid_disks);
1776         /*
1777          * Ok, everything is just fine now
1778          */
1779         mddev->array_size = mddev->size;
1780
1781         mddev->queue->unplug_fn = raid1_unplug;
1782         mddev->queue->issue_flush_fn = raid1_issue_flush;
1783
1784         return 0;
1785
1786 out_no_mem:
1787         printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1788                mdname(mddev));
1789
1790 out_free_conf:
1791         if (conf) {
1792                 if (conf->r1bio_pool)
1793                         mempool_destroy(conf->r1bio_pool);
1794                 kfree(conf->mirrors);
1795                 __free_page(conf->tmppage);
1796                 kfree(conf->poolinfo);
1797                 kfree(conf);
1798                 mddev->private = NULL;
1799         }
1800 out:
1801         return -EIO;
1802 }
1803
1804 static int stop(mddev_t *mddev)
1805 {
1806         conf_t *conf = mddev_to_conf(mddev);
1807         struct bitmap *bitmap = mddev->bitmap;
1808         int behind_wait = 0;
1809
1810         /* wait for behind writes to complete */
1811         while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1812                 behind_wait++;
1813                 printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
1814                 set_current_state(TASK_UNINTERRUPTIBLE);
1815                 schedule_timeout(HZ); /* wait a second */
1816                 /* need to kick something here to make sure I/O goes? */
1817         }
1818
1819         md_unregister_thread(mddev->thread);
1820         mddev->thread = NULL;
1821         blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1822         if (conf->r1bio_pool)
1823                 mempool_destroy(conf->r1bio_pool);
1824         kfree(conf->mirrors);
1825         kfree(conf->poolinfo);
1826         kfree(conf);
1827         mddev->private = NULL;
1828         return 0;
1829 }
1830
1831 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1832 {
1833         /* no resync is happening, and there is enough space
1834          * on all devices, so we can resize.
1835          * We need to make sure resync covers any new space.
1836          * If the array is shrinking we should possibly wait until
1837          * any io in the removed space completes, but it hardly seems
1838          * worth it.
1839          */
1840         mddev->array_size = sectors>>1;
1841         set_capacity(mddev->gendisk, mddev->array_size << 1);
1842         mddev->changed = 1;
1843         if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1844                 mddev->recovery_cp = mddev->size << 1;
1845                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1846         }
1847         mddev->size = mddev->array_size;
1848         mddev->resync_max_sectors = sectors;
1849         return 0;
1850 }
1851
1852 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1853 {
1854         /* We need to:
1855          * 1/ resize the r1bio_pool
1856          * 2/ resize conf->mirrors
1857          *
1858          * We allocate a new r1bio_pool if we can.
1859          * Then raise a device barrier and wait until all IO stops.
1860          * Then resize conf->mirrors and swap in the new r1bio pool.
1861          *
1862          * At the same time, we "pack" the devices so that all the missing
1863          * devices have the higher raid_disk numbers.
1864          */
1865         mempool_t *newpool, *oldpool;
1866         struct pool_info *newpoolinfo;
1867         mirror_info_t *newmirrors;
1868         conf_t *conf = mddev_to_conf(mddev);
1869         int cnt;
1870
1871         int d, d2;
1872
1873         if (raid_disks < conf->raid_disks) {
1874                 cnt=0;
1875                 for (d= 0; d < conf->raid_disks; d++)
1876                         if (conf->mirrors[d].rdev)
1877                                 cnt++;
1878                 if (cnt > raid_disks)
1879                         return -EBUSY;
1880         }
1881
1882         newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1883         if (!newpoolinfo)
1884                 return -ENOMEM;
1885         newpoolinfo->mddev = mddev;
1886         newpoolinfo->raid_disks = raid_disks;
1887
1888         newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1889                                  r1bio_pool_free, newpoolinfo);
1890         if (!newpool) {
1891                 kfree(newpoolinfo);
1892                 return -ENOMEM;
1893         }
1894         newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1895         if (!newmirrors) {
1896                 kfree(newpoolinfo);
1897                 mempool_destroy(newpool);
1898                 return -ENOMEM;
1899         }
1900         memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1901
1902         raise_barrier(conf);
1903
1904         /* ok, everything is stopped */
1905         oldpool = conf->r1bio_pool;
1906         conf->r1bio_pool = newpool;
1907
1908         for (d=d2=0; d < conf->raid_disks; d++)
1909                 if (conf->mirrors[d].rdev) {
1910                         conf->mirrors[d].rdev->raid_disk = d2;
1911                         newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1912                 }
1913         kfree(conf->mirrors);
1914         conf->mirrors = newmirrors;
1915         kfree(conf->poolinfo);
1916         conf->poolinfo = newpoolinfo;
1917
1918         mddev->degraded += (raid_disks - conf->raid_disks);
1919         conf->raid_disks = mddev->raid_disks = raid_disks;
1920
1921         conf->last_used = 0; /* just make sure it is in-range */
1922         lower_barrier(conf);
1923
1924         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1925         md_wakeup_thread(mddev->thread);
1926
1927         mempool_destroy(oldpool);
1928         return 0;
1929 }
1930
1931 static void raid1_quiesce(mddev_t *mddev, int state)
1932 {
1933         conf_t *conf = mddev_to_conf(mddev);
1934
1935         switch(state) {
1936         case 1:
1937                 raise_barrier(conf);
1938                 break;
1939         case 0:
1940                 lower_barrier(conf);
1941                 break;
1942         }
1943 }
1944
1945
1946 static mdk_personality_t raid1_personality =
1947 {
1948         .name           = "raid1",
1949         .owner          = THIS_MODULE,
1950         .make_request   = make_request,
1951         .run            = run,
1952         .stop           = stop,
1953         .status         = status,
1954         .error_handler  = error,
1955         .hot_add_disk   = raid1_add_disk,
1956         .hot_remove_disk= raid1_remove_disk,
1957         .spare_active   = raid1_spare_active,
1958         .sync_request   = sync_request,
1959         .resize         = raid1_resize,
1960         .reshape        = raid1_reshape,
1961         .quiesce        = raid1_quiesce,
1962 };
1963
1964 static int __init raid_init(void)
1965 {
1966         return register_md_personality(RAID1, &raid1_personality);
1967 }
1968
1969 static void raid_exit(void)
1970 {
1971         unregister_md_personality(RAID1);
1972 }
1973
1974 module_init(raid_init);
1975 module_exit(raid_exit);
1976 MODULE_LICENSE("GPL");
1977 MODULE_ALIAS("md-personality-3"); /* RAID1 */