2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
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)
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.
34 #include "dm-bio-list.h"
35 #include <linux/raid/raid1.h>
36 #include <linux/raid/bitmap.h>
40 #define PRINTK(x...) printk(x)
46 * Number of guaranteed r1bios in case of extreme VM load:
48 #define NR_RAID1_BIOS 256
50 static mdk_personality_t raid1_personality
;
52 static void unplug_slaves(mddev_t
*mddev
);
54 static void allow_barrier(conf_t
*conf
);
55 static void lower_barrier(conf_t
*conf
);
57 static void * r1bio_pool_alloc(gfp_t gfp_flags
, void *data
)
59 struct pool_info
*pi
= data
;
61 int size
= offsetof(r1bio_t
, bios
[pi
->raid_disks
]);
63 /* allocate a r1bio with room for raid_disks entries in the bios array */
64 r1_bio
= kmalloc(size
, gfp_flags
);
66 memset(r1_bio
, 0, size
);
68 unplug_slaves(pi
->mddev
);
73 static void r1bio_pool_free(void *r1_bio
, void *data
)
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)
84 static void * r1buf_pool_alloc(gfp_t gfp_flags
, void *data
)
86 struct pool_info
*pi
= data
;
92 r1_bio
= r1bio_pool_alloc(gfp_flags
, pi
);
94 unplug_slaves(pi
->mddev
);
99 * Allocate bios : 1 for reading, n-1 for writing
101 for (j
= pi
->raid_disks
; j
-- ; ) {
102 bio
= bio_alloc(gfp_flags
, RESYNC_PAGES
);
105 r1_bio
->bios
[j
] = bio
;
108 * Allocate RESYNC_PAGES data pages and attach them to
111 bio
= r1_bio
->bios
[0];
112 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
113 page
= alloc_page(gfp_flags
);
117 bio
->bi_io_vec
[i
].bv_page
= page
;
120 r1_bio
->master_bio
= NULL
;
126 __free_page(bio
->bi_io_vec
[i
-1].bv_page
);
128 while ( ++j
< pi
->raid_disks
)
129 bio_put(r1_bio
->bios
[j
]);
130 r1bio_pool_free(r1_bio
, data
);
134 static void r1buf_pool_free(void *__r1_bio
, void *data
)
136 struct pool_info
*pi
= data
;
138 r1bio_t
*r1bio
= __r1_bio
;
139 struct bio
*bio
= r1bio
->bios
[0];
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
;
145 for (i
=0 ; i
< pi
->raid_disks
; i
++)
146 bio_put(r1bio
->bios
[i
]);
148 r1bio_pool_free(r1bio
, data
);
151 static void put_all_bios(conf_t
*conf
, r1bio_t
*r1_bio
)
155 for (i
= 0; i
< conf
->raid_disks
; i
++) {
156 struct bio
**bio
= r1_bio
->bios
+ i
;
157 if (*bio
&& *bio
!= IO_BLOCKED
)
163 static inline void free_r1bio(r1bio_t
*r1_bio
)
165 conf_t
*conf
= mddev_to_conf(r1_bio
->mddev
);
168 * Wake up any possible resync thread that waits for the device
173 put_all_bios(conf
, r1_bio
);
174 mempool_free(r1_bio
, conf
->r1bio_pool
);
177 static inline void put_buf(r1bio_t
*r1_bio
)
179 conf_t
*conf
= mddev_to_conf(r1_bio
->mddev
);
182 for (i
=0; i
<conf
->raid_disks
; i
++) {
183 struct bio
*bio
= r1_bio
->bios
[i
];
185 rdev_dec_pending(conf
->mirrors
[i
].rdev
, r1_bio
->mddev
);
188 mempool_free(r1_bio
, conf
->r1buf_pool
);
193 static void reschedule_retry(r1bio_t
*r1_bio
)
196 mddev_t
*mddev
= r1_bio
->mddev
;
197 conf_t
*conf
= mddev_to_conf(mddev
);
199 spin_lock_irqsave(&conf
->device_lock
, flags
);
200 list_add(&r1_bio
->retry_list
, &conf
->retry_list
);
202 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
204 wake_up(&conf
->wait_barrier
);
205 md_wakeup_thread(mddev
->thread
);
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
213 static void raid_end_bio_io(r1bio_t
*r1_bio
)
215 struct bio
*bio
= r1_bio
->master_bio
;
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);
225 bio_endio(bio
, bio
->bi_size
,
226 test_bit(R1BIO_Uptodate
, &r1_bio
->state
) ? 0 : -EIO
);
232 * Update disk head position estimator based on IRQ completion info.
234 static inline void update_head_pos(int disk
, r1bio_t
*r1_bio
)
236 conf_t
*conf
= mddev_to_conf(r1_bio
->mddev
);
238 conf
->mirrors
[disk
].head_position
=
239 r1_bio
->sector
+ (r1_bio
->sectors
);
242 static int raid1_end_read_request(struct bio
*bio
, unsigned int bytes_done
, int error
)
244 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
245 r1bio_t
* r1_bio
= (r1bio_t
*)(bio
->bi_private
);
247 conf_t
*conf
= mddev_to_conf(r1_bio
->mddev
);
252 mirror
= r1_bio
->read_disk
;
254 * this branch is our 'one mirror IO has finished' event handler:
256 update_head_pos(mirror
, r1_bio
);
258 if (uptodate
|| conf
->working_disks
<= 1) {
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.
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.
268 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
270 raid_end_bio_io(r1_bio
);
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
);
282 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, conf
->mddev
);
286 static int raid1_end_write_request(struct bio
*bio
, unsigned int bytes_done
, int error
)
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
);
296 for (mirror
= 0; mirror
< conf
->raid_disks
; mirror
++)
297 if (r1_bio
->bios
[mirror
] == bio
)
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;
306 * this branch is our 'one mirror IO has finished' event handler:
308 r1_bio
->bios
[mirror
] = NULL
;
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
);
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.
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.
323 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
325 update_head_pos(mirror
, r1_bio
);
328 if (test_bit(WriteMostly
, &conf
->mirrors
[mirror
].rdev
->flags
))
329 atomic_dec(&r1_bio
->behind_remaining
);
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 */
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);
352 * Let's see if all mirrored write operations have finished
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
363 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
364 /* free extra copy of the data pages */
365 int i
= bio
->bi_vcnt
;
367 __free_page(bio
->bi_io_vec
[i
].bv_page
);
369 /* clear the bitmap if all writes complete successfully */
370 bitmap_endwrite(r1_bio
->mddev
->bitmap
, r1_bio
->sector
,
372 !test_bit(R1BIO_Degraded
, &r1_bio
->state
),
374 md_write_end(r1_bio
->mddev
);
375 raid_end_bio_io(r1_bio
);
378 if (r1_bio
->bios
[mirror
]==NULL
)
381 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, conf
->mddev
);
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.
395 * If there are 2 mirrors in the same 2 devices, performance degrades
396 * because position is mirror, not device based.
398 * The rdev for the device selected will have nr_pending incremented.
400 static int read_balance(conf_t
*conf
, r1bio_t
*r1_bio
)
402 const unsigned long this_sector
= r1_bio
->sector
;
403 int new_disk
= conf
->last_used
, disk
= new_disk
;
405 const int sectors
= r1_bio
->sectors
;
406 sector_t new_distance
, current_distance
;
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.
416 if (conf
->mddev
->recovery_cp
< MaxSector
&&
417 (this_sector
+ sectors
>= conf
->next_resync
)) {
418 /* Choose the first operation device, for consistancy */
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
)) {
427 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
428 r1_bio
->bios
[new_disk
] != IO_BLOCKED
)
429 wonly_disk
= new_disk
;
431 if (new_disk
== conf
->raid_disks
- 1) {
432 new_disk
= wonly_disk
;
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
)) {
447 if (rdev
&& test_bit(In_sync
, &rdev
->flags
) &&
448 r1_bio
->bios
[new_disk
] != IO_BLOCKED
)
449 wonly_disk
= new_disk
;
452 new_disk
= conf
->raid_disks
;
454 if (new_disk
== disk
) {
455 new_disk
= wonly_disk
;
464 /* now disk == new_disk == starting point for search */
467 * Don't change to another disk for sequential reads:
469 if (conf
->next_seq_sect
== this_sector
)
471 if (this_sector
== conf
->mirrors
[new_disk
].head_position
)
474 current_distance
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
476 /* Find the disk whose head is closest */
480 disk
= conf
->raid_disks
;
483 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
485 if (!rdev
|| r1_bio
->bios
[disk
] == IO_BLOCKED
||
486 !test_bit(In_sync
, &rdev
->flags
) ||
487 test_bit(WriteMostly
, &rdev
->flags
))
490 if (!atomic_read(&rdev
->nr_pending
)) {
494 new_distance
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
495 if (new_distance
< current_distance
) {
496 current_distance
= new_distance
;
499 } while (disk
!= conf
->last_used
);
505 rdev
= rcu_dereference(conf
->mirrors
[new_disk
].rdev
);
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
513 atomic_dec(&rdev
->nr_pending
);
516 conf
->next_seq_sect
= this_sector
+ sectors
;
517 conf
->last_used
= new_disk
;
524 static void unplug_slaves(mddev_t
*mddev
)
526 conf_t
*conf
= mddev_to_conf(mddev
);
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
);
535 atomic_inc(&rdev
->nr_pending
);
538 if (r_queue
->unplug_fn
)
539 r_queue
->unplug_fn(r_queue
);
541 rdev_dec_pending(rdev
, mddev
);
548 static void raid1_unplug(request_queue_t
*q
)
550 mddev_t
*mddev
= q
->queuedata
;
552 unplug_slaves(mddev
);
553 md_wakeup_thread(mddev
->thread
);
556 static int raid1_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
557 sector_t
*error_sector
)
559 mddev_t
*mddev
= q
->queuedata
;
560 conf_t
*conf
= mddev_to_conf(mddev
);
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
);
570 if (!r_queue
->issue_flush_fn
)
573 atomic_inc(&rdev
->nr_pending
);
575 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
577 rdev_dec_pending(rdev
, mddev
);
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
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.
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.
607 #define RESYNC_DEPTH 32
609 static void raise_barrier(conf_t
*conf
)
611 spin_lock_irq(&conf
->resync_lock
);
613 /* Wait until no block IO is waiting */
614 wait_event_lock_irq(conf
->wait_barrier
, !conf
->nr_waiting
,
616 raid1_unplug(conf
->mddev
->queue
));
618 /* block any new IO from starting */
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
,
625 raid1_unplug(conf
->mddev
->queue
));
627 spin_unlock_irq(&conf
->resync_lock
);
630 static void lower_barrier(conf_t
*conf
)
633 spin_lock_irqsave(&conf
->resync_lock
, flags
);
635 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
636 wake_up(&conf
->wait_barrier
);
639 static void wait_barrier(conf_t
*conf
)
641 spin_lock_irq(&conf
->resync_lock
);
644 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
646 raid1_unplug(conf
->mddev
->queue
));
650 spin_unlock_irq(&conf
->resync_lock
);
653 static void allow_barrier(conf_t
*conf
)
656 spin_lock_irqsave(&conf
->resync_lock
, flags
);
658 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
659 wake_up(&conf
->wait_barrier
);
662 static void freeze_array(conf_t
*conf
)
664 /* stop syncio and normal IO and wait for everything to
666 * We increment barrier and nr_waiting, and then
667 * wait until barrier+nr_pending match nr_queued+2
669 spin_lock_irq(&conf
->resync_lock
);
672 wait_event_lock_irq(conf
->wait_barrier
,
673 conf
->barrier
+conf
->nr_pending
== conf
->nr_queued
+2,
675 raid1_unplug(conf
->mddev
->queue
));
676 spin_unlock_irq(&conf
->resync_lock
);
678 static void unfreeze_array(conf_t
*conf
)
680 /* reverse the effect of the freeze */
681 spin_lock_irq(&conf
->resync_lock
);
684 wake_up(&conf
->wait_barrier
);
685 spin_unlock_irq(&conf
->resync_lock
);
689 /* duplicate the data pages for behind I/O */
690 static struct page
**alloc_behind_pages(struct bio
*bio
)
693 struct bio_vec
*bvec
;
694 struct page
**pages
= kmalloc(bio
->bi_vcnt
* sizeof(struct page
*),
696 if (unlikely(!pages
))
699 memset(pages
, 0, bio
->bi_vcnt
* sizeof(struct page
*));
701 bio_for_each_segment(bvec
, bio
, i
) {
702 pages
[i
] = alloc_page(GFP_NOIO
);
703 if (unlikely(!pages
[i
]))
705 memcpy(kmap(pages
[i
]) + bvec
->bv_offset
,
706 kmap(bvec
->bv_page
) + bvec
->bv_offset
, bvec
->bv_len
);
708 kunmap(bvec
->bv_page
);
715 for (i
= 0; i
< bio
->bi_vcnt
&& pages
[i
]; i
++)
716 __free_page(pages
[i
]);
718 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio
->bi_size
);
722 static int make_request(request_queue_t
*q
, struct bio
* bio
)
724 mddev_t
*mddev
= q
->queuedata
;
725 conf_t
*conf
= mddev_to_conf(mddev
);
726 mirror_info_t
*mirror
;
728 struct bio
*read_bio
;
729 int i
, targets
= 0, disks
;
731 struct bitmap
*bitmap
= mddev
->bitmap
;
734 struct page
**behind_pages
= NULL
;
735 const int rw
= bio_data_dir(bio
);
738 if (unlikely(!mddev
->barriers_work
&& bio_barrier(bio
))) {
739 bio_endio(bio
, bio
->bi_size
, -EOPNOTSUPP
);
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.
748 md_write_start(mddev
, bio
); /* wait on superblock update early */
752 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
753 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bio
));
756 * make_request() can abort the operation when READA is being
757 * used and no empty request is available.
760 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
762 r1_bio
->master_bio
= bio
;
763 r1_bio
->sectors
= bio
->bi_size
>> 9;
765 r1_bio
->mddev
= mddev
;
766 r1_bio
->sector
= bio
->bi_sector
;
770 * read balancing logic:
772 int rdisk
= read_balance(conf
, r1_bio
);
775 /* couldn't find anywhere to read from */
776 raid_end_bio_io(r1_bio
);
779 mirror
= conf
->mirrors
+ rdisk
;
781 r1_bio
->read_disk
= rdisk
;
783 read_bio
= bio_clone(bio
, GFP_NOIO
);
785 r1_bio
->bios
[rdisk
] = read_bio
;
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
;
793 generic_make_request(read_bio
);
800 /* first select target devices under spinlock and
801 * inc refcount on their rdev. Record them by setting
804 disks
= conf
->raid_disks
;
806 { static int first
=1;
807 if (first
) printk("First Write sector %llu disks %d\n",
808 (unsigned long long)r1_bio
->sector
, disks
);
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
;
821 r1_bio
->bios
[i
] = bio
;
824 r1_bio
->bios
[i
] = NULL
;
828 BUG_ON(targets
== 0); /* we never fail the last device */
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
);
836 /* do behind I/O ? */
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
);
842 atomic_set(&r1_bio
->remaining
, 0);
843 atomic_set(&r1_bio
->behind_remaining
, 0);
845 do_barriers
= bio
->bi_rw
& BIO_RW_BARRIER
;
847 set_bit(R1BIO_Barrier
, &r1_bio
->state
);
850 for (i
= 0; i
< disks
; i
++) {
852 if (!r1_bio
->bios
[i
])
855 mbio
= bio_clone(bio
, GFP_NOIO
);
856 r1_bio
->bios
[i
] = mbio
;
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
;
865 struct bio_vec
*bvec
;
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
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
);
881 atomic_inc(&r1_bio
->remaining
);
883 bio_list_add(&bl
, mbio
);
885 kfree(behind_pages
); /* the behind pages are attached to the bios now */
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
);
893 blk_plug_device(mddev
->queue
);
894 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
897 while ((bio
= bio_list_pop(&bl
)) != NULL
)
898 generic_make_request(bio
);
904 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
906 conf_t
*conf
= mddev_to_conf(mddev
);
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
, "]");
919 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
921 char b
[BDEVNAME_SIZE
];
922 conf_t
*conf
= mddev_to_conf(mddev
);
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
930 if (test_bit(In_sync
, &rdev
->flags
)
931 && conf
->working_disks
== 1)
933 * Don't fail the drive, act as though we were just a
934 * normal single drive
937 if (test_bit(In_sync
, &rdev
->flags
)) {
939 conf
->working_disks
--;
941 * if recovery is running, make sure it aborts.
943 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
945 clear_bit(In_sync
, &rdev
->flags
);
946 set_bit(Faulty
, &rdev
->flags
);
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
);
953 static void print_conf(conf_t
*conf
)
958 printk("RAID1 conf printout:\n");
963 printk(" --- wd:%d rd:%d\n", conf
->working_disks
,
966 for (i
= 0; i
< conf
->raid_disks
; i
++) {
967 char b
[BDEVNAME_SIZE
];
968 tmp
= conf
->mirrors
+ i
;
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
));
976 static void close_sync(conf_t
*conf
)
981 mempool_destroy(conf
->r1buf_pool
);
982 conf
->r1buf_pool
= NULL
;
985 static int raid1_spare_active(mddev_t
*mddev
)
988 conf_t
*conf
= mddev
->private;
992 * Find all failed disks within the RAID1 configuration
993 * and mark them readable
995 for (i
= 0; i
< conf
->raid_disks
; i
++) {
996 tmp
= conf
->mirrors
+ i
;
998 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
999 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
1000 conf
->working_disks
++;
1002 set_bit(In_sync
, &tmp
->rdev
->flags
);
1011 static int raid1_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1013 conf_t
*conf
= mddev
->private;
1018 for (mirror
=0; mirror
< mddev
->raid_disks
; mirror
++)
1019 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
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.
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);
1031 p
->head_position
= 0;
1032 rdev
->raid_disk
= mirror
;
1034 /* As all devices are equivalent, we don't need a full recovery
1035 * if this was recently any drive of the array
1037 if (rdev
->saved_raid_disk
< 0)
1039 rcu_assign_pointer(p
->rdev
, rdev
);
1047 static int raid1_remove_disk(mddev_t
*mddev
, int number
)
1049 conf_t
*conf
= mddev
->private;
1052 mirror_info_t
*p
= conf
->mirrors
+ number
;
1057 if (test_bit(In_sync
, &rdev
->flags
) ||
1058 atomic_read(&rdev
->nr_pending
)) {
1064 if (atomic_read(&rdev
->nr_pending
)) {
1065 /* lost the race, try later */
1077 static int end_sync_read(struct bio
*bio
, unsigned int bytes_done
, int error
)
1079 r1bio_t
* r1_bio
= (r1bio_t
*)(bio
->bi_private
);
1084 if (r1_bio
->bios
[r1_bio
->read_disk
] != bio
)
1086 update_head_pos(r1_bio
->read_disk
, r1_bio
);
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
1092 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1093 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1094 reschedule_retry(r1_bio
);
1098 static int end_sync_write(struct bio
*bio
, unsigned int bytes_done
, int error
)
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
);
1110 for (i
= 0; i
< conf
->raid_disks
; i
++)
1111 if (r1_bio
->bios
[i
] == bio
) {
1116 md_error(mddev
, conf
->mirrors
[mirror
].rdev
);
1118 update_head_pos(mirror
, r1_bio
);
1120 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
1121 md_done_sync(mddev
, r1_bio
->sectors
, uptodate
);
1127 static void sync_request_write(mddev_t
*mddev
, r1bio_t
*r1_bio
)
1129 conf_t
*conf
= mddev_to_conf(mddev
);
1131 int disks
= conf
->raid_disks
;
1132 struct bio
*bio
, *wbio
;
1134 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
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.
1150 sector_t sect
= r1_bio
->sector
;
1151 int sectors
= r1_bio
->sectors
;
1156 int d
= r1_bio
->read_disk
;
1160 if (s
> (PAGE_SIZE
>>9))
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
,
1168 bio
->bi_io_vec
[idx
].bv_page
,
1175 if (d
== conf
->raid_disks
)
1177 } while (!success
&& d
!= r1_bio
->read_disk
);
1180 /* write it back and re-read */
1181 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1182 while (d
!= r1_bio
->read_disk
) {
1184 d
= conf
->raid_disks
;
1186 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1188 rdev
= conf
->mirrors
[d
].rdev
;
1189 if (sync_page_io(rdev
->bdev
,
1190 sect
+ rdev
->data_offset
,
1192 bio
->bi_io_vec
[idx
].bv_page
,
1194 sync_page_io(rdev
->bdev
,
1195 sect
+ rdev
->data_offset
,
1197 bio
->bi_io_vec
[idx
].bv_page
,
1199 md_error(mddev
, rdev
);
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);
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
))))
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);
1233 generic_make_request(wbio
);
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);
1244 * This is a kernel thread which:
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.
1251 static void raid1d(mddev_t
*mddev
)
1255 unsigned long flags
;
1256 conf_t
*conf
= mddev_to_conf(mddev
);
1257 struct list_head
*head
= &conf
->retry_list
;
1261 md_check_recovery(mddev
);
1264 char b
[BDEVNAME_SIZE
];
1265 spin_lock_irqsave(&conf
->device_lock
, flags
);
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
));
1275 while (bio
) { /* submit pending writes */
1276 struct bio
*next
= bio
->bi_next
;
1277 bio
->bi_next
= NULL
;
1278 generic_make_request(bio
);
1286 if (list_empty(head
))
1288 r1_bio
= list_entry(head
->prev
, r1bio_t
, retry_list
);
1289 list_del(head
->prev
);
1291 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
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
);
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.
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
;
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
;
1324 bio
->bi_private
= r1_bio
;
1325 r1_bio
->bios
[i
] = bio
;
1326 generic_make_request(bio
);
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
1339 sector_t sect
= r1_bio
->sector
;
1340 int sectors
= r1_bio
->sectors
;
1342 if (mddev
->ro
== 0) while(sectors
) {
1344 int d
= r1_bio
->read_disk
;
1347 if (s
> (PAGE_SIZE
>>9))
1351 rdev
= conf
->mirrors
[d
].rdev
;
1353 test_bit(In_sync
, &rdev
->flags
) &&
1354 sync_page_io(rdev
->bdev
,
1355 sect
+ rdev
->data_offset
,
1357 conf
->tmppage
, READ
))
1361 if (d
== conf
->raid_disks
)
1364 } while (!success
&& d
!= r1_bio
->read_disk
);
1367 /* write it back and re-read */
1368 while (d
!= r1_bio
->read_disk
) {
1370 d
= conf
->raid_disks
;
1372 rdev
= conf
->mirrors
[d
].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
);
1387 /* Cannot read from anywhere -- bye bye array */
1388 md_error(mddev
, conf
->mirrors
[r1_bio
->read_disk
].rdev
);
1395 unfreeze_array(conf
);
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
);
1405 r1_bio
->bios
[r1_bio
->read_disk
] =
1406 mddev
->ro
? IO_BLOCKED
: NULL
;
1407 r1_bio
->read_disk
= disk
;
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
;
1421 bio
->bi_private
= r1_bio
;
1423 generic_make_request(bio
);
1427 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1429 unplug_slaves(mddev
);
1433 static int init_resync(conf_t
*conf
)
1437 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1438 if (conf
->r1buf_pool
)
1440 conf
->r1buf_pool
= mempool_create(buffs
, r1buf_pool_alloc
, r1buf_pool_free
,
1442 if (!conf
->r1buf_pool
)
1444 conf
->next_resync
= 0;
1449 * perform a "sync" on one "block"
1451 * We need to make sure that no normal I/O request - particularly write
1452 * requests - conflict with active sync requests.
1454 * This is achieved by tracking pending requests and a 'barrier' concept
1455 * that can be installed to exclude normal IO requests.
1458 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1460 conf_t
*conf
= mddev_to_conf(mddev
);
1463 sector_t max_sector
, nr_sectors
;
1467 int write_targets
= 0, read_targets
= 0;
1469 int still_degraded
= 0;
1471 if (!conf
->r1buf_pool
)
1474 printk("sync start - bitmap %p\n", mddev->bitmap);
1476 if (init_resync(conf
))
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
1487 if (mddev
->curr_resync
< max_sector
) /* aborted */
1488 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1490 else /* completed sync */
1493 bitmap_close_sync(mddev
->bitmap
);
1498 /* before building a request, check if we can skip these blocks..
1499 * This call the bitmap_start_sync doesn't actually record anything
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 */
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.
1512 if (!go_faster
&& conf
->nr_waiting
)
1513 msleep_interruptible(1000);
1515 raise_barrier(conf
);
1517 conf
->next_resync
= sector_nr
;
1519 r1_bio
= mempool_alloc(conf
->r1buf_pool
, GFP_NOIO
);
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
1530 r1_bio
->mddev
= mddev
;
1531 r1_bio
->sector
= sector_nr
;
1533 set_bit(R1BIO_IsSync
, &r1_bio
->state
);
1535 for (i
=0; i
< conf
->raid_disks
; i
++) {
1537 bio
= r1_bio
->bios
[i
];
1539 /* take from bio_init */
1540 bio
->bi_next
= NULL
;
1541 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
1545 bio
->bi_phys_segments
= 0;
1546 bio
->bi_hw_segments
= 0;
1548 bio
->bi_end_io
= NULL
;
1549 bio
->bi_private
= NULL
;
1551 rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1553 test_bit(Faulty
, &rdev
->flags
)) {
1556 } else if (!test_bit(In_sync
, &rdev
->flags
)) {
1558 bio
->bi_end_io
= end_sync_write
;
1561 /* may need to read from here */
1563 bio
->bi_end_io
= end_sync_read
;
1564 if (test_bit(WriteMostly
, &rdev
->flags
)) {
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
;
1581 r1_bio
->read_disk
= disk
;
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;
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
1591 sector_t rv
= max_sector
- sector_nr
;
1601 int len
= PAGE_SIZE
;
1602 if (sector_nr
+ (len
>>9) > max_sector
)
1603 len
= (max_sector
- sector_nr
) << 9;
1606 if (sync_blocks
== 0) {
1607 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
1608 &sync_blocks
, still_degraded
) &&
1610 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
1612 if (sync_blocks
< (PAGE_SIZE
>>9))
1614 if (len
> (sync_blocks
<<9))
1615 len
= sync_blocks
<<9;
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) {
1624 r1_bio
->bios
[0]->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
1627 bio
= r1_bio
->bios
[i
];
1628 if (bio
->bi_end_io
==NULL
)
1630 /* remove last page from this bio */
1632 bio
->bi_size
-= len
;
1633 bio
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
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
);
1644 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
1645 r1_bio
->sectors
= nr_sectors
;
1647 md_sync_acct(conf
->mirrors
[r1_bio
->read_disk
].rdev
->bdev
, nr_sectors
);
1649 generic_make_request(bio
);
1654 static int run(mddev_t
*mddev
)
1658 mirror_info_t
*disk
;
1660 struct list_head
*tmp
;
1662 if (mddev
->level
!= 1) {
1663 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1664 mdname(mddev
), mddev
->level
);
1668 * copy the already verified devices into our private RAID1
1669 * bookkeeping area. [whatever we allocate in run(),
1670 * should be freed in stop()]
1672 conf
= kmalloc(sizeof(conf_t
), GFP_KERNEL
);
1673 mddev
->private = conf
;
1677 memset(conf
, 0, sizeof(*conf
));
1678 conf
->mirrors
= kmalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
1683 memset(conf
->mirrors
, 0, sizeof(struct mirror_info
)*mddev
->raid_disks
);
1685 conf
->tmppage
= alloc_page(GFP_KERNEL
);
1689 conf
->poolinfo
= kmalloc(sizeof(*conf
->poolinfo
), GFP_KERNEL
);
1690 if (!conf
->poolinfo
)
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
,
1697 if (!conf
->r1bio_pool
)
1700 ITERATE_RDEV(mddev
, rdev
, tmp
) {
1701 disk_idx
= rdev
->raid_disk
;
1702 if (disk_idx
>= mddev
->raid_disks
1705 disk
= conf
->mirrors
+ disk_idx
;
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.
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);
1719 disk
->head_position
= 0;
1720 if (!test_bit(Faulty
, &rdev
->flags
) && test_bit(In_sync
, &rdev
->flags
))
1721 conf
->working_disks
++;
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
;
1730 spin_lock_init(&conf
->resync_lock
);
1731 init_waitqueue_head(&conf
->wait_barrier
);
1733 bio_list_init(&conf
->pending_bio_list
);
1734 bio_list_init(&conf
->flushing_bio_list
);
1736 if (!conf
->working_disks
) {
1737 printk(KERN_ERR
"raid1: no operational mirrors for %s\n",
1742 mddev
->degraded
= 0;
1743 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1745 disk
= conf
->mirrors
+ i
;
1748 disk
->head_position
= 0;
1754 * find the first working one and use it as a starting point
1755 * to read balancing.
1757 for (j
= 0; j
< conf
->raid_disks
&&
1758 (!conf
->mirrors
[j
].rdev
||
1759 !test_bit(In_sync
, &conf
->mirrors
[j
].rdev
->flags
)) ; j
++)
1761 conf
->last_used
= j
;
1764 mddev
->thread
= md_register_thread(raid1d
, mddev
, "%s_raid1");
1765 if (!mddev
->thread
) {
1767 "raid1: couldn't allocate thread for %s\n",
1773 "raid1: raid set %s active with %d out of %d mirrors\n",
1774 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
1777 * Ok, everything is just fine now
1779 mddev
->array_size
= mddev
->size
;
1781 mddev
->queue
->unplug_fn
= raid1_unplug
;
1782 mddev
->queue
->issue_flush_fn
= raid1_issue_flush
;
1787 printk(KERN_ERR
"raid1: couldn't allocate memory for %s\n",
1792 if (conf
->r1bio_pool
)
1793 mempool_destroy(conf
->r1bio_pool
);
1794 kfree(conf
->mirrors
);
1795 __free_page(conf
->tmppage
);
1796 kfree(conf
->poolinfo
);
1798 mddev
->private = NULL
;
1804 static int stop(mddev_t
*mddev
)
1806 conf_t
*conf
= mddev_to_conf(mddev
);
1807 struct bitmap
*bitmap
= mddev
->bitmap
;
1808 int behind_wait
= 0;
1810 /* wait for behind writes to complete */
1811 while (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
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? */
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
);
1827 mddev
->private = NULL
;
1831 static int raid1_resize(mddev_t
*mddev
, sector_t sectors
)
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
1840 mddev
->array_size
= sectors
>>1;
1841 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 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
);
1847 mddev
->size
= mddev
->array_size
;
1848 mddev
->resync_max_sectors
= sectors
;
1852 static int raid1_reshape(mddev_t
*mddev
, int raid_disks
)
1855 * 1/ resize the r1bio_pool
1856 * 2/ resize conf->mirrors
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.
1862 * At the same time, we "pack" the devices so that all the missing
1863 * devices have the higher raid_disk numbers.
1865 mempool_t
*newpool
, *oldpool
;
1866 struct pool_info
*newpoolinfo
;
1867 mirror_info_t
*newmirrors
;
1868 conf_t
*conf
= mddev_to_conf(mddev
);
1873 if (raid_disks
< conf
->raid_disks
) {
1875 for (d
= 0; d
< conf
->raid_disks
; d
++)
1876 if (conf
->mirrors
[d
].rdev
)
1878 if (cnt
> raid_disks
)
1882 newpoolinfo
= kmalloc(sizeof(*newpoolinfo
), GFP_KERNEL
);
1885 newpoolinfo
->mddev
= mddev
;
1886 newpoolinfo
->raid_disks
= raid_disks
;
1888 newpool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
1889 r1bio_pool_free
, newpoolinfo
);
1894 newmirrors
= kmalloc(sizeof(struct mirror_info
) * raid_disks
, GFP_KERNEL
);
1897 mempool_destroy(newpool
);
1900 memset(newmirrors
, 0, sizeof(struct mirror_info
)*raid_disks
);
1902 raise_barrier(conf
);
1904 /* ok, everything is stopped */
1905 oldpool
= conf
->r1bio_pool
;
1906 conf
->r1bio_pool
= newpool
;
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
;
1913 kfree(conf
->mirrors
);
1914 conf
->mirrors
= newmirrors
;
1915 kfree(conf
->poolinfo
);
1916 conf
->poolinfo
= newpoolinfo
;
1918 mddev
->degraded
+= (raid_disks
- conf
->raid_disks
);
1919 conf
->raid_disks
= mddev
->raid_disks
= raid_disks
;
1921 conf
->last_used
= 0; /* just make sure it is in-range */
1922 lower_barrier(conf
);
1924 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1925 md_wakeup_thread(mddev
->thread
);
1927 mempool_destroy(oldpool
);
1931 static void raid1_quiesce(mddev_t
*mddev
, int state
)
1933 conf_t
*conf
= mddev_to_conf(mddev
);
1937 raise_barrier(conf
);
1940 lower_barrier(conf
);
1946 static mdk_personality_t raid1_personality
=
1949 .owner
= THIS_MODULE
,
1950 .make_request
= make_request
,
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
,
1964 static int __init
raid_init(void)
1966 return register_md_personality(RAID1
, &raid1_personality
);
1969 static void raid_exit(void)
1971 unregister_md_personality(RAID1
);
1974 module_init(raid_init
);
1975 module_exit(raid_exit
);
1976 MODULE_LICENSE("GPL");
1977 MODULE_ALIAS("md-personality-3"); /* RAID1 */