md/raid1: perform bad-block tests for WriteMostly devices too.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / raid1.c
blob606fc04fd76a25aa8d3530e612fcb477cc8e660c
1 /*
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)
27 * any later version.
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 <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/seq_file.h>
38 #include <linux/ratelimit.h>
39 #include "md.h"
40 #include "raid1.h"
41 #include "bitmap.h"
43 #define DEBUG 0
44 #define PRINTK(x...) do { if (DEBUG) printk(x); } while (0)
47 * Number of guaranteed r1bios in case of extreme VM load:
49 #define NR_RAID1_BIOS 256
52 static void allow_barrier(conf_t *conf);
53 static void lower_barrier(conf_t *conf);
55 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
57 struct pool_info *pi = data;
58 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
60 /* allocate a r1bio with room for raid_disks entries in the bios array */
61 return kzalloc(size, gfp_flags);
64 static void r1bio_pool_free(void *r1_bio, void *data)
66 kfree(r1_bio);
69 #define RESYNC_BLOCK_SIZE (64*1024)
70 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
71 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
72 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
73 #define RESYNC_WINDOW (2048*1024)
75 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
77 struct pool_info *pi = data;
78 struct page *page;
79 r1bio_t *r1_bio;
80 struct bio *bio;
81 int i, j;
83 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
84 if (!r1_bio)
85 return NULL;
88 * Allocate bios : 1 for reading, n-1 for writing
90 for (j = pi->raid_disks ; j-- ; ) {
91 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
92 if (!bio)
93 goto out_free_bio;
94 r1_bio->bios[j] = bio;
97 * Allocate RESYNC_PAGES data pages and attach them to
98 * the first bio.
99 * If this is a user-requested check/repair, allocate
100 * RESYNC_PAGES for each bio.
102 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
103 j = pi->raid_disks;
104 else
105 j = 1;
106 while(j--) {
107 bio = r1_bio->bios[j];
108 for (i = 0; i < RESYNC_PAGES; i++) {
109 page = alloc_page(gfp_flags);
110 if (unlikely(!page))
111 goto out_free_pages;
113 bio->bi_io_vec[i].bv_page = page;
114 bio->bi_vcnt = i+1;
117 /* If not user-requests, copy the page pointers to all bios */
118 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
119 for (i=0; i<RESYNC_PAGES ; i++)
120 for (j=1; j<pi->raid_disks; j++)
121 r1_bio->bios[j]->bi_io_vec[i].bv_page =
122 r1_bio->bios[0]->bi_io_vec[i].bv_page;
125 r1_bio->master_bio = NULL;
127 return r1_bio;
129 out_free_pages:
130 for (j=0 ; j < pi->raid_disks; j++)
131 for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
132 put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
133 j = -1;
134 out_free_bio:
135 while ( ++j < pi->raid_disks )
136 bio_put(r1_bio->bios[j]);
137 r1bio_pool_free(r1_bio, data);
138 return NULL;
141 static void r1buf_pool_free(void *__r1_bio, void *data)
143 struct pool_info *pi = data;
144 int i,j;
145 r1bio_t *r1bio = __r1_bio;
147 for (i = 0; i < RESYNC_PAGES; i++)
148 for (j = pi->raid_disks; j-- ;) {
149 if (j == 0 ||
150 r1bio->bios[j]->bi_io_vec[i].bv_page !=
151 r1bio->bios[0]->bi_io_vec[i].bv_page)
152 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
154 for (i=0 ; i < pi->raid_disks; i++)
155 bio_put(r1bio->bios[i]);
157 r1bio_pool_free(r1bio, data);
160 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
162 int i;
164 for (i = 0; i < conf->raid_disks; i++) {
165 struct bio **bio = r1_bio->bios + i;
166 if (!BIO_SPECIAL(*bio))
167 bio_put(*bio);
168 *bio = NULL;
172 static void free_r1bio(r1bio_t *r1_bio)
174 conf_t *conf = r1_bio->mddev->private;
176 put_all_bios(conf, r1_bio);
177 mempool_free(r1_bio, conf->r1bio_pool);
180 static void put_buf(r1bio_t *r1_bio)
182 conf_t *conf = r1_bio->mddev->private;
183 int i;
185 for (i=0; i<conf->raid_disks; i++) {
186 struct bio *bio = r1_bio->bios[i];
187 if (bio->bi_end_io)
188 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
191 mempool_free(r1_bio, conf->r1buf_pool);
193 lower_barrier(conf);
196 static void reschedule_retry(r1bio_t *r1_bio)
198 unsigned long flags;
199 mddev_t *mddev = r1_bio->mddev;
200 conf_t *conf = mddev->private;
202 spin_lock_irqsave(&conf->device_lock, flags);
203 list_add(&r1_bio->retry_list, &conf->retry_list);
204 conf->nr_queued ++;
205 spin_unlock_irqrestore(&conf->device_lock, flags);
207 wake_up(&conf->wait_barrier);
208 md_wakeup_thread(mddev->thread);
212 * raid_end_bio_io() is called when we have finished servicing a mirrored
213 * operation and are ready to return a success/failure code to the buffer
214 * cache layer.
216 static void call_bio_endio(r1bio_t *r1_bio)
218 struct bio *bio = r1_bio->master_bio;
219 int done;
220 conf_t *conf = r1_bio->mddev->private;
222 if (bio->bi_phys_segments) {
223 unsigned long flags;
224 spin_lock_irqsave(&conf->device_lock, flags);
225 bio->bi_phys_segments--;
226 done = (bio->bi_phys_segments == 0);
227 spin_unlock_irqrestore(&conf->device_lock, flags);
228 } else
229 done = 1;
231 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
232 clear_bit(BIO_UPTODATE, &bio->bi_flags);
233 if (done) {
234 bio_endio(bio, 0);
236 * Wake up any possible resync thread that waits for the device
237 * to go idle.
239 allow_barrier(conf);
243 static void raid_end_bio_io(r1bio_t *r1_bio)
245 struct bio *bio = r1_bio->master_bio;
247 /* if nobody has done the final endio yet, do it now */
248 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
249 PRINTK(KERN_DEBUG "raid1: sync end %s on sectors %llu-%llu\n",
250 (bio_data_dir(bio) == WRITE) ? "write" : "read",
251 (unsigned long long) bio->bi_sector,
252 (unsigned long long) bio->bi_sector +
253 (bio->bi_size >> 9) - 1);
255 call_bio_endio(r1_bio);
257 free_r1bio(r1_bio);
261 * Update disk head position estimator based on IRQ completion info.
263 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
265 conf_t *conf = r1_bio->mddev->private;
267 conf->mirrors[disk].head_position =
268 r1_bio->sector + (r1_bio->sectors);
271 static void raid1_end_read_request(struct bio *bio, int error)
273 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
274 r1bio_t *r1_bio = bio->bi_private;
275 int mirror;
276 conf_t *conf = r1_bio->mddev->private;
278 mirror = r1_bio->read_disk;
280 * this branch is our 'one mirror IO has finished' event handler:
282 update_head_pos(mirror, r1_bio);
284 if (uptodate)
285 set_bit(R1BIO_Uptodate, &r1_bio->state);
286 else {
287 /* If all other devices have failed, we want to return
288 * the error upwards rather than fail the last device.
289 * Here we redefine "uptodate" to mean "Don't want to retry"
291 unsigned long flags;
292 spin_lock_irqsave(&conf->device_lock, flags);
293 if (r1_bio->mddev->degraded == conf->raid_disks ||
294 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
295 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
296 uptodate = 1;
297 spin_unlock_irqrestore(&conf->device_lock, flags);
300 if (uptodate)
301 raid_end_bio_io(r1_bio);
302 else {
304 * oops, read error:
306 char b[BDEVNAME_SIZE];
307 printk_ratelimited(
308 KERN_ERR "md/raid1:%s: %s: "
309 "rescheduling sector %llu\n",
310 mdname(conf->mddev),
311 bdevname(conf->mirrors[mirror].rdev->bdev,
313 (unsigned long long)r1_bio->sector);
314 set_bit(R1BIO_ReadError, &r1_bio->state);
315 reschedule_retry(r1_bio);
318 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
321 static void close_write(r1bio_t *r1_bio)
323 /* it really is the end of this request */
324 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
325 /* free extra copy of the data pages */
326 int i = r1_bio->behind_page_count;
327 while (i--)
328 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
329 kfree(r1_bio->behind_bvecs);
330 r1_bio->behind_bvecs = NULL;
332 /* clear the bitmap if all writes complete successfully */
333 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
334 r1_bio->sectors,
335 !test_bit(R1BIO_Degraded, &r1_bio->state),
336 test_bit(R1BIO_BehindIO, &r1_bio->state));
337 md_write_end(r1_bio->mddev);
340 static void r1_bio_write_done(r1bio_t *r1_bio)
342 if (!atomic_dec_and_test(&r1_bio->remaining))
343 return;
345 if (test_bit(R1BIO_WriteError, &r1_bio->state))
346 reschedule_retry(r1_bio);
347 else {
348 close_write(r1_bio);
349 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
350 reschedule_retry(r1_bio);
351 else
352 raid_end_bio_io(r1_bio);
356 static void raid1_end_write_request(struct bio *bio, int error)
358 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
359 r1bio_t *r1_bio = bio->bi_private;
360 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
361 conf_t *conf = r1_bio->mddev->private;
362 struct bio *to_put = NULL;
365 for (mirror = 0; mirror < conf->raid_disks; mirror++)
366 if (r1_bio->bios[mirror] == bio)
367 break;
370 * 'one mirror IO has finished' event handler:
372 if (!uptodate) {
373 set_bit(WriteErrorSeen,
374 &conf->mirrors[mirror].rdev->flags);
375 set_bit(R1BIO_WriteError, &r1_bio->state);
376 } else {
378 * Set R1BIO_Uptodate in our master bio, so that we
379 * will return a good error code for to the higher
380 * levels even if IO on some other mirrored buffer
381 * fails.
383 * The 'master' represents the composite IO operation
384 * to user-side. So if something waits for IO, then it
385 * will wait for the 'master' bio.
387 sector_t first_bad;
388 int bad_sectors;
390 r1_bio->bios[mirror] = NULL;
391 to_put = bio;
392 set_bit(R1BIO_Uptodate, &r1_bio->state);
394 /* Maybe we can clear some bad blocks. */
395 if (is_badblock(conf->mirrors[mirror].rdev,
396 r1_bio->sector, r1_bio->sectors,
397 &first_bad, &bad_sectors)) {
398 r1_bio->bios[mirror] = IO_MADE_GOOD;
399 set_bit(R1BIO_MadeGood, &r1_bio->state);
403 update_head_pos(mirror, r1_bio);
405 if (behind) {
406 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
407 atomic_dec(&r1_bio->behind_remaining);
410 * In behind mode, we ACK the master bio once the I/O
411 * has safely reached all non-writemostly
412 * disks. Setting the Returned bit ensures that this
413 * gets done only once -- we don't ever want to return
414 * -EIO here, instead we'll wait
416 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
417 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
418 /* Maybe we can return now */
419 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
420 struct bio *mbio = r1_bio->master_bio;
421 PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n",
422 (unsigned long long) mbio->bi_sector,
423 (unsigned long long) mbio->bi_sector +
424 (mbio->bi_size >> 9) - 1);
425 call_bio_endio(r1_bio);
429 if (r1_bio->bios[mirror] == NULL)
430 rdev_dec_pending(conf->mirrors[mirror].rdev,
431 conf->mddev);
434 * Let's see if all mirrored write operations have finished
435 * already.
437 r1_bio_write_done(r1_bio);
439 if (to_put)
440 bio_put(to_put);
445 * This routine returns the disk from which the requested read should
446 * be done. There is a per-array 'next expected sequential IO' sector
447 * number - if this matches on the next IO then we use the last disk.
448 * There is also a per-disk 'last know head position' sector that is
449 * maintained from IRQ contexts, both the normal and the resync IO
450 * completion handlers update this position correctly. If there is no
451 * perfect sequential match then we pick the disk whose head is closest.
453 * If there are 2 mirrors in the same 2 devices, performance degrades
454 * because position is mirror, not device based.
456 * The rdev for the device selected will have nr_pending incremented.
458 static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors)
460 const sector_t this_sector = r1_bio->sector;
461 int sectors;
462 int best_good_sectors;
463 int start_disk;
464 int best_disk;
465 int i;
466 sector_t best_dist;
467 mdk_rdev_t *rdev;
468 int choose_first;
470 rcu_read_lock();
472 * Check if we can balance. We can balance on the whole
473 * device if no resync is going on, or below the resync window.
474 * We take the first readable disk when above the resync window.
476 retry:
477 sectors = r1_bio->sectors;
478 best_disk = -1;
479 best_dist = MaxSector;
480 best_good_sectors = 0;
482 if (conf->mddev->recovery_cp < MaxSector &&
483 (this_sector + sectors >= conf->next_resync)) {
484 choose_first = 1;
485 start_disk = 0;
486 } else {
487 choose_first = 0;
488 start_disk = conf->last_used;
491 for (i = 0 ; i < conf->raid_disks ; i++) {
492 sector_t dist;
493 sector_t first_bad;
494 int bad_sectors;
496 int disk = start_disk + i;
497 if (disk >= conf->raid_disks)
498 disk -= conf->raid_disks;
500 rdev = rcu_dereference(conf->mirrors[disk].rdev);
501 if (r1_bio->bios[disk] == IO_BLOCKED
502 || rdev == NULL
503 || test_bit(Faulty, &rdev->flags))
504 continue;
505 if (!test_bit(In_sync, &rdev->flags) &&
506 rdev->recovery_offset < this_sector + sectors)
507 continue;
508 if (test_bit(WriteMostly, &rdev->flags)) {
509 /* Don't balance among write-mostly, just
510 * use the first as a last resort */
511 if (best_disk < 0) {
512 if (is_badblock(rdev, this_sector, sectors,
513 &first_bad, &bad_sectors)) {
514 if (first_bad < this_sector)
515 /* Cannot use this */
516 continue;
517 best_good_sectors = first_bad - this_sector;
518 } else
519 best_good_sectors = sectors;
520 best_disk = disk;
522 continue;
524 /* This is a reasonable device to use. It might
525 * even be best.
527 if (is_badblock(rdev, this_sector, sectors,
528 &first_bad, &bad_sectors)) {
529 if (best_dist < MaxSector)
530 /* already have a better device */
531 continue;
532 if (first_bad <= this_sector) {
533 /* cannot read here. If this is the 'primary'
534 * device, then we must not read beyond
535 * bad_sectors from another device..
537 bad_sectors -= (this_sector - first_bad);
538 if (choose_first && sectors > bad_sectors)
539 sectors = bad_sectors;
540 if (best_good_sectors > sectors)
541 best_good_sectors = sectors;
543 } else {
544 sector_t good_sectors = first_bad - this_sector;
545 if (good_sectors > best_good_sectors) {
546 best_good_sectors = good_sectors;
547 best_disk = disk;
549 if (choose_first)
550 break;
552 continue;
553 } else
554 best_good_sectors = sectors;
556 dist = abs(this_sector - conf->mirrors[disk].head_position);
557 if (choose_first
558 /* Don't change to another disk for sequential reads */
559 || conf->next_seq_sect == this_sector
560 || dist == 0
561 /* If device is idle, use it */
562 || atomic_read(&rdev->nr_pending) == 0) {
563 best_disk = disk;
564 break;
566 if (dist < best_dist) {
567 best_dist = dist;
568 best_disk = disk;
572 if (best_disk >= 0) {
573 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
574 if (!rdev)
575 goto retry;
576 atomic_inc(&rdev->nr_pending);
577 if (test_bit(Faulty, &rdev->flags)) {
578 /* cannot risk returning a device that failed
579 * before we inc'ed nr_pending
581 rdev_dec_pending(rdev, conf->mddev);
582 goto retry;
584 sectors = best_good_sectors;
585 conf->next_seq_sect = this_sector + sectors;
586 conf->last_used = best_disk;
588 rcu_read_unlock();
589 *max_sectors = sectors;
591 return best_disk;
594 int md_raid1_congested(mddev_t *mddev, int bits)
596 conf_t *conf = mddev->private;
597 int i, ret = 0;
599 rcu_read_lock();
600 for (i = 0; i < mddev->raid_disks; i++) {
601 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
602 if (rdev && !test_bit(Faulty, &rdev->flags)) {
603 struct request_queue *q = bdev_get_queue(rdev->bdev);
605 BUG_ON(!q);
607 /* Note the '|| 1' - when read_balance prefers
608 * non-congested targets, it can be removed
610 if ((bits & (1<<BDI_async_congested)) || 1)
611 ret |= bdi_congested(&q->backing_dev_info, bits);
612 else
613 ret &= bdi_congested(&q->backing_dev_info, bits);
616 rcu_read_unlock();
617 return ret;
619 EXPORT_SYMBOL_GPL(md_raid1_congested);
621 static int raid1_congested(void *data, int bits)
623 mddev_t *mddev = data;
625 return mddev_congested(mddev, bits) ||
626 md_raid1_congested(mddev, bits);
629 static void flush_pending_writes(conf_t *conf)
631 /* Any writes that have been queued but are awaiting
632 * bitmap updates get flushed here.
634 spin_lock_irq(&conf->device_lock);
636 if (conf->pending_bio_list.head) {
637 struct bio *bio;
638 bio = bio_list_get(&conf->pending_bio_list);
639 spin_unlock_irq(&conf->device_lock);
640 /* flush any pending bitmap writes to
641 * disk before proceeding w/ I/O */
642 bitmap_unplug(conf->mddev->bitmap);
644 while (bio) { /* submit pending writes */
645 struct bio *next = bio->bi_next;
646 bio->bi_next = NULL;
647 generic_make_request(bio);
648 bio = next;
650 } else
651 spin_unlock_irq(&conf->device_lock);
654 /* Barriers....
655 * Sometimes we need to suspend IO while we do something else,
656 * either some resync/recovery, or reconfigure the array.
657 * To do this we raise a 'barrier'.
658 * The 'barrier' is a counter that can be raised multiple times
659 * to count how many activities are happening which preclude
660 * normal IO.
661 * We can only raise the barrier if there is no pending IO.
662 * i.e. if nr_pending == 0.
663 * We choose only to raise the barrier if no-one is waiting for the
664 * barrier to go down. This means that as soon as an IO request
665 * is ready, no other operations which require a barrier will start
666 * until the IO request has had a chance.
668 * So: regular IO calls 'wait_barrier'. When that returns there
669 * is no backgroup IO happening, It must arrange to call
670 * allow_barrier when it has finished its IO.
671 * backgroup IO calls must call raise_barrier. Once that returns
672 * there is no normal IO happeing. It must arrange to call
673 * lower_barrier when the particular background IO completes.
675 #define RESYNC_DEPTH 32
677 static void raise_barrier(conf_t *conf)
679 spin_lock_irq(&conf->resync_lock);
681 /* Wait until no block IO is waiting */
682 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
683 conf->resync_lock, );
685 /* block any new IO from starting */
686 conf->barrier++;
688 /* Now wait for all pending IO to complete */
689 wait_event_lock_irq(conf->wait_barrier,
690 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
691 conf->resync_lock, );
693 spin_unlock_irq(&conf->resync_lock);
696 static void lower_barrier(conf_t *conf)
698 unsigned long flags;
699 BUG_ON(conf->barrier <= 0);
700 spin_lock_irqsave(&conf->resync_lock, flags);
701 conf->barrier--;
702 spin_unlock_irqrestore(&conf->resync_lock, flags);
703 wake_up(&conf->wait_barrier);
706 static void wait_barrier(conf_t *conf)
708 spin_lock_irq(&conf->resync_lock);
709 if (conf->barrier) {
710 conf->nr_waiting++;
711 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
712 conf->resync_lock,
714 conf->nr_waiting--;
716 conf->nr_pending++;
717 spin_unlock_irq(&conf->resync_lock);
720 static void allow_barrier(conf_t *conf)
722 unsigned long flags;
723 spin_lock_irqsave(&conf->resync_lock, flags);
724 conf->nr_pending--;
725 spin_unlock_irqrestore(&conf->resync_lock, flags);
726 wake_up(&conf->wait_barrier);
729 static void freeze_array(conf_t *conf)
731 /* stop syncio and normal IO and wait for everything to
732 * go quite.
733 * We increment barrier and nr_waiting, and then
734 * wait until nr_pending match nr_queued+1
735 * This is called in the context of one normal IO request
736 * that has failed. Thus any sync request that might be pending
737 * will be blocked by nr_pending, and we need to wait for
738 * pending IO requests to complete or be queued for re-try.
739 * Thus the number queued (nr_queued) plus this request (1)
740 * must match the number of pending IOs (nr_pending) before
741 * we continue.
743 spin_lock_irq(&conf->resync_lock);
744 conf->barrier++;
745 conf->nr_waiting++;
746 wait_event_lock_irq(conf->wait_barrier,
747 conf->nr_pending == conf->nr_queued+1,
748 conf->resync_lock,
749 flush_pending_writes(conf));
750 spin_unlock_irq(&conf->resync_lock);
752 static void unfreeze_array(conf_t *conf)
754 /* reverse the effect of the freeze */
755 spin_lock_irq(&conf->resync_lock);
756 conf->barrier--;
757 conf->nr_waiting--;
758 wake_up(&conf->wait_barrier);
759 spin_unlock_irq(&conf->resync_lock);
763 /* duplicate the data pages for behind I/O
765 static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio)
767 int i;
768 struct bio_vec *bvec;
769 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
770 GFP_NOIO);
771 if (unlikely(!bvecs))
772 return;
774 bio_for_each_segment(bvec, bio, i) {
775 bvecs[i] = *bvec;
776 bvecs[i].bv_page = alloc_page(GFP_NOIO);
777 if (unlikely(!bvecs[i].bv_page))
778 goto do_sync_io;
779 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
780 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
781 kunmap(bvecs[i].bv_page);
782 kunmap(bvec->bv_page);
784 r1_bio->behind_bvecs = bvecs;
785 r1_bio->behind_page_count = bio->bi_vcnt;
786 set_bit(R1BIO_BehindIO, &r1_bio->state);
787 return;
789 do_sync_io:
790 for (i = 0; i < bio->bi_vcnt; i++)
791 if (bvecs[i].bv_page)
792 put_page(bvecs[i].bv_page);
793 kfree(bvecs);
794 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
797 static int make_request(mddev_t *mddev, struct bio * bio)
799 conf_t *conf = mddev->private;
800 mirror_info_t *mirror;
801 r1bio_t *r1_bio;
802 struct bio *read_bio;
803 int i, disks;
804 struct bitmap *bitmap;
805 unsigned long flags;
806 const int rw = bio_data_dir(bio);
807 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
808 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
809 mdk_rdev_t *blocked_rdev;
810 int plugged;
811 int first_clone;
812 int sectors_handled;
813 int max_sectors;
816 * Register the new request and wait if the reconstruction
817 * thread has put up a bar for new requests.
818 * Continue immediately if no resync is active currently.
821 md_write_start(mddev, bio); /* wait on superblock update early */
823 if (bio_data_dir(bio) == WRITE &&
824 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
825 bio->bi_sector < mddev->suspend_hi) {
826 /* As the suspend_* range is controlled by
827 * userspace, we want an interruptible
828 * wait.
830 DEFINE_WAIT(w);
831 for (;;) {
832 flush_signals(current);
833 prepare_to_wait(&conf->wait_barrier,
834 &w, TASK_INTERRUPTIBLE);
835 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
836 bio->bi_sector >= mddev->suspend_hi)
837 break;
838 schedule();
840 finish_wait(&conf->wait_barrier, &w);
843 wait_barrier(conf);
845 bitmap = mddev->bitmap;
848 * make_request() can abort the operation when READA is being
849 * used and no empty request is available.
852 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
854 r1_bio->master_bio = bio;
855 r1_bio->sectors = bio->bi_size >> 9;
856 r1_bio->state = 0;
857 r1_bio->mddev = mddev;
858 r1_bio->sector = bio->bi_sector;
860 /* We might need to issue multiple reads to different
861 * devices if there are bad blocks around, so we keep
862 * track of the number of reads in bio->bi_phys_segments.
863 * If this is 0, there is only one r1_bio and no locking
864 * will be needed when requests complete. If it is
865 * non-zero, then it is the number of not-completed requests.
867 bio->bi_phys_segments = 0;
868 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
870 if (rw == READ) {
872 * read balancing logic:
874 int rdisk;
876 read_again:
877 rdisk = read_balance(conf, r1_bio, &max_sectors);
879 if (rdisk < 0) {
880 /* couldn't find anywhere to read from */
881 raid_end_bio_io(r1_bio);
882 return 0;
884 mirror = conf->mirrors + rdisk;
886 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
887 bitmap) {
888 /* Reading from a write-mostly device must
889 * take care not to over-take any writes
890 * that are 'behind'
892 wait_event(bitmap->behind_wait,
893 atomic_read(&bitmap->behind_writes) == 0);
895 r1_bio->read_disk = rdisk;
897 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
898 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
899 max_sectors);
901 r1_bio->bios[rdisk] = read_bio;
903 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
904 read_bio->bi_bdev = mirror->rdev->bdev;
905 read_bio->bi_end_io = raid1_end_read_request;
906 read_bio->bi_rw = READ | do_sync;
907 read_bio->bi_private = r1_bio;
909 if (max_sectors < r1_bio->sectors) {
910 /* could not read all from this device, so we will
911 * need another r1_bio.
914 sectors_handled = (r1_bio->sector + max_sectors
915 - bio->bi_sector);
916 r1_bio->sectors = max_sectors;
917 spin_lock_irq(&conf->device_lock);
918 if (bio->bi_phys_segments == 0)
919 bio->bi_phys_segments = 2;
920 else
921 bio->bi_phys_segments++;
922 spin_unlock_irq(&conf->device_lock);
923 /* Cannot call generic_make_request directly
924 * as that will be queued in __make_request
925 * and subsequent mempool_alloc might block waiting
926 * for it. So hand bio over to raid1d.
928 reschedule_retry(r1_bio);
930 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
932 r1_bio->master_bio = bio;
933 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
934 r1_bio->state = 0;
935 r1_bio->mddev = mddev;
936 r1_bio->sector = bio->bi_sector + sectors_handled;
937 goto read_again;
938 } else
939 generic_make_request(read_bio);
940 return 0;
944 * WRITE:
946 /* first select target devices under rcu_lock and
947 * inc refcount on their rdev. Record them by setting
948 * bios[x] to bio
949 * If there are known/acknowledged bad blocks on any device on
950 * which we have seen a write error, we want to avoid writing those
951 * blocks.
952 * This potentially requires several writes to write around
953 * the bad blocks. Each set of writes gets it's own r1bio
954 * with a set of bios attached.
956 plugged = mddev_check_plugged(mddev);
958 disks = conf->raid_disks;
959 retry_write:
960 blocked_rdev = NULL;
961 rcu_read_lock();
962 max_sectors = r1_bio->sectors;
963 for (i = 0; i < disks; i++) {
964 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
965 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
966 atomic_inc(&rdev->nr_pending);
967 blocked_rdev = rdev;
968 break;
970 r1_bio->bios[i] = NULL;
971 if (!rdev || test_bit(Faulty, &rdev->flags)) {
972 set_bit(R1BIO_Degraded, &r1_bio->state);
973 continue;
976 atomic_inc(&rdev->nr_pending);
977 if (test_bit(WriteErrorSeen, &rdev->flags)) {
978 sector_t first_bad;
979 int bad_sectors;
980 int is_bad;
982 is_bad = is_badblock(rdev, r1_bio->sector,
983 max_sectors,
984 &first_bad, &bad_sectors);
985 if (is_bad < 0) {
986 /* mustn't write here until the bad block is
987 * acknowledged*/
988 set_bit(BlockedBadBlocks, &rdev->flags);
989 blocked_rdev = rdev;
990 break;
992 if (is_bad && first_bad <= r1_bio->sector) {
993 /* Cannot write here at all */
994 bad_sectors -= (r1_bio->sector - first_bad);
995 if (bad_sectors < max_sectors)
996 /* mustn't write more than bad_sectors
997 * to other devices yet
999 max_sectors = bad_sectors;
1000 rdev_dec_pending(rdev, mddev);
1001 /* We don't set R1BIO_Degraded as that
1002 * only applies if the disk is
1003 * missing, so it might be re-added,
1004 * and we want to know to recover this
1005 * chunk.
1006 * In this case the device is here,
1007 * and the fact that this chunk is not
1008 * in-sync is recorded in the bad
1009 * block log
1011 continue;
1013 if (is_bad) {
1014 int good_sectors = first_bad - r1_bio->sector;
1015 if (good_sectors < max_sectors)
1016 max_sectors = good_sectors;
1019 r1_bio->bios[i] = bio;
1021 rcu_read_unlock();
1023 if (unlikely(blocked_rdev)) {
1024 /* Wait for this device to become unblocked */
1025 int j;
1027 for (j = 0; j < i; j++)
1028 if (r1_bio->bios[j])
1029 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1030 r1_bio->state = 0;
1031 allow_barrier(conf);
1032 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1033 wait_barrier(conf);
1034 goto retry_write;
1037 if (max_sectors < r1_bio->sectors) {
1038 /* We are splitting this write into multiple parts, so
1039 * we need to prepare for allocating another r1_bio.
1041 r1_bio->sectors = max_sectors;
1042 spin_lock_irq(&conf->device_lock);
1043 if (bio->bi_phys_segments == 0)
1044 bio->bi_phys_segments = 2;
1045 else
1046 bio->bi_phys_segments++;
1047 spin_unlock_irq(&conf->device_lock);
1049 sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
1051 atomic_set(&r1_bio->remaining, 1);
1052 atomic_set(&r1_bio->behind_remaining, 0);
1054 first_clone = 1;
1055 for (i = 0; i < disks; i++) {
1056 struct bio *mbio;
1057 if (!r1_bio->bios[i])
1058 continue;
1060 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1061 md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
1063 if (first_clone) {
1064 /* do behind I/O ?
1065 * Not if there are too many, or cannot
1066 * allocate memory, or a reader on WriteMostly
1067 * is waiting for behind writes to flush */
1068 if (bitmap &&
1069 (atomic_read(&bitmap->behind_writes)
1070 < mddev->bitmap_info.max_write_behind) &&
1071 !waitqueue_active(&bitmap->behind_wait))
1072 alloc_behind_pages(mbio, r1_bio);
1074 bitmap_startwrite(bitmap, r1_bio->sector,
1075 r1_bio->sectors,
1076 test_bit(R1BIO_BehindIO,
1077 &r1_bio->state));
1078 first_clone = 0;
1080 if (r1_bio->behind_bvecs) {
1081 struct bio_vec *bvec;
1082 int j;
1084 /* Yes, I really want the '__' version so that
1085 * we clear any unused pointer in the io_vec, rather
1086 * than leave them unchanged. This is important
1087 * because when we come to free the pages, we won't
1088 * know the original bi_idx, so we just free
1089 * them all
1091 __bio_for_each_segment(bvec, mbio, j, 0)
1092 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
1093 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1094 atomic_inc(&r1_bio->behind_remaining);
1097 r1_bio->bios[i] = mbio;
1099 mbio->bi_sector = (r1_bio->sector +
1100 conf->mirrors[i].rdev->data_offset);
1101 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1102 mbio->bi_end_io = raid1_end_write_request;
1103 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
1104 mbio->bi_private = r1_bio;
1106 atomic_inc(&r1_bio->remaining);
1107 spin_lock_irqsave(&conf->device_lock, flags);
1108 bio_list_add(&conf->pending_bio_list, mbio);
1109 spin_unlock_irqrestore(&conf->device_lock, flags);
1111 /* Mustn't call r1_bio_write_done before this next test,
1112 * as it could result in the bio being freed.
1114 if (sectors_handled < (bio->bi_size >> 9)) {
1115 r1_bio_write_done(r1_bio);
1116 /* We need another r1_bio. It has already been counted
1117 * in bio->bi_phys_segments
1119 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1120 r1_bio->master_bio = bio;
1121 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1122 r1_bio->state = 0;
1123 r1_bio->mddev = mddev;
1124 r1_bio->sector = bio->bi_sector + sectors_handled;
1125 goto retry_write;
1128 r1_bio_write_done(r1_bio);
1130 /* In case raid1d snuck in to freeze_array */
1131 wake_up(&conf->wait_barrier);
1133 if (do_sync || !bitmap || !plugged)
1134 md_wakeup_thread(mddev->thread);
1136 return 0;
1139 static void status(struct seq_file *seq, mddev_t *mddev)
1141 conf_t *conf = mddev->private;
1142 int i;
1144 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1145 conf->raid_disks - mddev->degraded);
1146 rcu_read_lock();
1147 for (i = 0; i < conf->raid_disks; i++) {
1148 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1149 seq_printf(seq, "%s",
1150 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1152 rcu_read_unlock();
1153 seq_printf(seq, "]");
1157 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1159 char b[BDEVNAME_SIZE];
1160 conf_t *conf = mddev->private;
1163 * If it is not operational, then we have already marked it as dead
1164 * else if it is the last working disks, ignore the error, let the
1165 * next level up know.
1166 * else mark the drive as failed
1168 if (test_bit(In_sync, &rdev->flags)
1169 && (conf->raid_disks - mddev->degraded) == 1) {
1171 * Don't fail the drive, act as though we were just a
1172 * normal single drive.
1173 * However don't try a recovery from this drive as
1174 * it is very likely to fail.
1176 conf->recovery_disabled = mddev->recovery_disabled;
1177 return;
1179 set_bit(Blocked, &rdev->flags);
1180 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1181 unsigned long flags;
1182 spin_lock_irqsave(&conf->device_lock, flags);
1183 mddev->degraded++;
1184 set_bit(Faulty, &rdev->flags);
1185 spin_unlock_irqrestore(&conf->device_lock, flags);
1187 * if recovery is running, make sure it aborts.
1189 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1190 } else
1191 set_bit(Faulty, &rdev->flags);
1192 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1193 printk(KERN_ALERT
1194 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1195 "md/raid1:%s: Operation continuing on %d devices.\n",
1196 mdname(mddev), bdevname(rdev->bdev, b),
1197 mdname(mddev), conf->raid_disks - mddev->degraded);
1200 static void print_conf(conf_t *conf)
1202 int i;
1204 printk(KERN_DEBUG "RAID1 conf printout:\n");
1205 if (!conf) {
1206 printk(KERN_DEBUG "(!conf)\n");
1207 return;
1209 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1210 conf->raid_disks);
1212 rcu_read_lock();
1213 for (i = 0; i < conf->raid_disks; i++) {
1214 char b[BDEVNAME_SIZE];
1215 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
1216 if (rdev)
1217 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1218 i, !test_bit(In_sync, &rdev->flags),
1219 !test_bit(Faulty, &rdev->flags),
1220 bdevname(rdev->bdev,b));
1222 rcu_read_unlock();
1225 static void close_sync(conf_t *conf)
1227 wait_barrier(conf);
1228 allow_barrier(conf);
1230 mempool_destroy(conf->r1buf_pool);
1231 conf->r1buf_pool = NULL;
1234 static int raid1_spare_active(mddev_t *mddev)
1236 int i;
1237 conf_t *conf = mddev->private;
1238 int count = 0;
1239 unsigned long flags;
1242 * Find all failed disks within the RAID1 configuration
1243 * and mark them readable.
1244 * Called under mddev lock, so rcu protection not needed.
1246 for (i = 0; i < conf->raid_disks; i++) {
1247 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1248 if (rdev
1249 && !test_bit(Faulty, &rdev->flags)
1250 && !test_and_set_bit(In_sync, &rdev->flags)) {
1251 count++;
1252 sysfs_notify_dirent_safe(rdev->sysfs_state);
1255 spin_lock_irqsave(&conf->device_lock, flags);
1256 mddev->degraded -= count;
1257 spin_unlock_irqrestore(&conf->device_lock, flags);
1259 print_conf(conf);
1260 return count;
1264 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1266 conf_t *conf = mddev->private;
1267 int err = -EEXIST;
1268 int mirror = 0;
1269 mirror_info_t *p;
1270 int first = 0;
1271 int last = mddev->raid_disks - 1;
1273 if (mddev->recovery_disabled == conf->recovery_disabled)
1274 return -EBUSY;
1276 if (rdev->raid_disk >= 0)
1277 first = last = rdev->raid_disk;
1279 for (mirror = first; mirror <= last; mirror++)
1280 if ( !(p=conf->mirrors+mirror)->rdev) {
1282 disk_stack_limits(mddev->gendisk, rdev->bdev,
1283 rdev->data_offset << 9);
1284 /* as we don't honour merge_bvec_fn, we must
1285 * never risk violating it, so limit
1286 * ->max_segments to one lying with a single
1287 * page, as a one page request is never in
1288 * violation.
1290 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1291 blk_queue_max_segments(mddev->queue, 1);
1292 blk_queue_segment_boundary(mddev->queue,
1293 PAGE_CACHE_SIZE - 1);
1296 p->head_position = 0;
1297 rdev->raid_disk = mirror;
1298 err = 0;
1299 /* As all devices are equivalent, we don't need a full recovery
1300 * if this was recently any drive of the array
1302 if (rdev->saved_raid_disk < 0)
1303 conf->fullsync = 1;
1304 rcu_assign_pointer(p->rdev, rdev);
1305 break;
1307 md_integrity_add_rdev(rdev, mddev);
1308 print_conf(conf);
1309 return err;
1312 static int raid1_remove_disk(mddev_t *mddev, int number)
1314 conf_t *conf = mddev->private;
1315 int err = 0;
1316 mdk_rdev_t *rdev;
1317 mirror_info_t *p = conf->mirrors+ number;
1319 print_conf(conf);
1320 rdev = p->rdev;
1321 if (rdev) {
1322 if (test_bit(In_sync, &rdev->flags) ||
1323 atomic_read(&rdev->nr_pending)) {
1324 err = -EBUSY;
1325 goto abort;
1327 /* Only remove non-faulty devices if recovery
1328 * is not possible.
1330 if (!test_bit(Faulty, &rdev->flags) &&
1331 mddev->recovery_disabled != conf->recovery_disabled &&
1332 mddev->degraded < conf->raid_disks) {
1333 err = -EBUSY;
1334 goto abort;
1336 p->rdev = NULL;
1337 synchronize_rcu();
1338 if (atomic_read(&rdev->nr_pending)) {
1339 /* lost the race, try later */
1340 err = -EBUSY;
1341 p->rdev = rdev;
1342 goto abort;
1344 err = md_integrity_register(mddev);
1346 abort:
1348 print_conf(conf);
1349 return err;
1353 static void end_sync_read(struct bio *bio, int error)
1355 r1bio_t *r1_bio = bio->bi_private;
1356 int i;
1358 for (i=r1_bio->mddev->raid_disks; i--; )
1359 if (r1_bio->bios[i] == bio)
1360 break;
1361 BUG_ON(i < 0);
1362 update_head_pos(i, r1_bio);
1364 * we have read a block, now it needs to be re-written,
1365 * or re-read if the read failed.
1366 * We don't do much here, just schedule handling by raid1d
1368 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1369 set_bit(R1BIO_Uptodate, &r1_bio->state);
1371 if (atomic_dec_and_test(&r1_bio->remaining))
1372 reschedule_retry(r1_bio);
1375 static void end_sync_write(struct bio *bio, int error)
1377 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1378 r1bio_t *r1_bio = bio->bi_private;
1379 mddev_t *mddev = r1_bio->mddev;
1380 conf_t *conf = mddev->private;
1381 int i;
1382 int mirror=0;
1383 sector_t first_bad;
1384 int bad_sectors;
1386 for (i = 0; i < conf->raid_disks; i++)
1387 if (r1_bio->bios[i] == bio) {
1388 mirror = i;
1389 break;
1391 if (!uptodate) {
1392 sector_t sync_blocks = 0;
1393 sector_t s = r1_bio->sector;
1394 long sectors_to_go = r1_bio->sectors;
1395 /* make sure these bits doesn't get cleared. */
1396 do {
1397 bitmap_end_sync(mddev->bitmap, s,
1398 &sync_blocks, 1);
1399 s += sync_blocks;
1400 sectors_to_go -= sync_blocks;
1401 } while (sectors_to_go > 0);
1402 set_bit(WriteErrorSeen,
1403 &conf->mirrors[mirror].rdev->flags);
1404 set_bit(R1BIO_WriteError, &r1_bio->state);
1405 } else if (is_badblock(conf->mirrors[mirror].rdev,
1406 r1_bio->sector,
1407 r1_bio->sectors,
1408 &first_bad, &bad_sectors) &&
1409 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1410 r1_bio->sector,
1411 r1_bio->sectors,
1412 &first_bad, &bad_sectors)
1414 set_bit(R1BIO_MadeGood, &r1_bio->state);
1416 update_head_pos(mirror, r1_bio);
1418 if (atomic_dec_and_test(&r1_bio->remaining)) {
1419 int s = r1_bio->sectors;
1420 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1421 test_bit(R1BIO_WriteError, &r1_bio->state))
1422 reschedule_retry(r1_bio);
1423 else {
1424 put_buf(r1_bio);
1425 md_done_sync(mddev, s, uptodate);
1430 static int r1_sync_page_io(mdk_rdev_t *rdev, sector_t sector,
1431 int sectors, struct page *page, int rw)
1433 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1434 /* success */
1435 return 1;
1436 if (rw == WRITE)
1437 set_bit(WriteErrorSeen, &rdev->flags);
1438 /* need to record an error - either for the block or the device */
1439 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1440 md_error(rdev->mddev, rdev);
1441 return 0;
1444 static int fix_sync_read_error(r1bio_t *r1_bio)
1446 /* Try some synchronous reads of other devices to get
1447 * good data, much like with normal read errors. Only
1448 * read into the pages we already have so we don't
1449 * need to re-issue the read request.
1450 * We don't need to freeze the array, because being in an
1451 * active sync request, there is no normal IO, and
1452 * no overlapping syncs.
1453 * We don't need to check is_badblock() again as we
1454 * made sure that anything with a bad block in range
1455 * will have bi_end_io clear.
1457 mddev_t *mddev = r1_bio->mddev;
1458 conf_t *conf = mddev->private;
1459 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1460 sector_t sect = r1_bio->sector;
1461 int sectors = r1_bio->sectors;
1462 int idx = 0;
1464 while(sectors) {
1465 int s = sectors;
1466 int d = r1_bio->read_disk;
1467 int success = 0;
1468 mdk_rdev_t *rdev;
1469 int start;
1471 if (s > (PAGE_SIZE>>9))
1472 s = PAGE_SIZE >> 9;
1473 do {
1474 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1475 /* No rcu protection needed here devices
1476 * can only be removed when no resync is
1477 * active, and resync is currently active
1479 rdev = conf->mirrors[d].rdev;
1480 if (sync_page_io(rdev, sect, s<<9,
1481 bio->bi_io_vec[idx].bv_page,
1482 READ, false)) {
1483 success = 1;
1484 break;
1487 d++;
1488 if (d == conf->raid_disks)
1489 d = 0;
1490 } while (!success && d != r1_bio->read_disk);
1492 if (!success) {
1493 char b[BDEVNAME_SIZE];
1494 int abort = 0;
1495 /* Cannot read from anywhere, this block is lost.
1496 * Record a bad block on each device. If that doesn't
1497 * work just disable and interrupt the recovery.
1498 * Don't fail devices as that won't really help.
1500 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1501 " for block %llu\n",
1502 mdname(mddev),
1503 bdevname(bio->bi_bdev, b),
1504 (unsigned long long)r1_bio->sector);
1505 for (d = 0; d < conf->raid_disks; d++) {
1506 rdev = conf->mirrors[d].rdev;
1507 if (!rdev || test_bit(Faulty, &rdev->flags))
1508 continue;
1509 if (!rdev_set_badblocks(rdev, sect, s, 0))
1510 abort = 1;
1512 if (abort) {
1513 mddev->recovery_disabled = 1;
1514 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1515 md_done_sync(mddev, r1_bio->sectors, 0);
1516 put_buf(r1_bio);
1517 return 0;
1519 /* Try next page */
1520 sectors -= s;
1521 sect += s;
1522 idx++;
1523 continue;
1526 start = d;
1527 /* write it back and re-read */
1528 while (d != r1_bio->read_disk) {
1529 if (d == 0)
1530 d = conf->raid_disks;
1531 d--;
1532 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1533 continue;
1534 rdev = conf->mirrors[d].rdev;
1535 if (r1_sync_page_io(rdev, sect, s,
1536 bio->bi_io_vec[idx].bv_page,
1537 WRITE) == 0) {
1538 r1_bio->bios[d]->bi_end_io = NULL;
1539 rdev_dec_pending(rdev, mddev);
1542 d = start;
1543 while (d != r1_bio->read_disk) {
1544 if (d == 0)
1545 d = conf->raid_disks;
1546 d--;
1547 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1548 continue;
1549 rdev = conf->mirrors[d].rdev;
1550 if (r1_sync_page_io(rdev, sect, s,
1551 bio->bi_io_vec[idx].bv_page,
1552 READ) != 0)
1553 atomic_add(s, &rdev->corrected_errors);
1555 sectors -= s;
1556 sect += s;
1557 idx ++;
1559 set_bit(R1BIO_Uptodate, &r1_bio->state);
1560 set_bit(BIO_UPTODATE, &bio->bi_flags);
1561 return 1;
1564 static int process_checks(r1bio_t *r1_bio)
1566 /* We have read all readable devices. If we haven't
1567 * got the block, then there is no hope left.
1568 * If we have, then we want to do a comparison
1569 * and skip the write if everything is the same.
1570 * If any blocks failed to read, then we need to
1571 * attempt an over-write
1573 mddev_t *mddev = r1_bio->mddev;
1574 conf_t *conf = mddev->private;
1575 int primary;
1576 int i;
1578 for (primary = 0; primary < conf->raid_disks; primary++)
1579 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1580 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1581 r1_bio->bios[primary]->bi_end_io = NULL;
1582 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1583 break;
1585 r1_bio->read_disk = primary;
1586 for (i = 0; i < conf->raid_disks; i++) {
1587 int j;
1588 int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
1589 struct bio *pbio = r1_bio->bios[primary];
1590 struct bio *sbio = r1_bio->bios[i];
1591 int size;
1593 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1594 continue;
1596 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1597 for (j = vcnt; j-- ; ) {
1598 struct page *p, *s;
1599 p = pbio->bi_io_vec[j].bv_page;
1600 s = sbio->bi_io_vec[j].bv_page;
1601 if (memcmp(page_address(p),
1602 page_address(s),
1603 PAGE_SIZE))
1604 break;
1606 } else
1607 j = 0;
1608 if (j >= 0)
1609 mddev->resync_mismatches += r1_bio->sectors;
1610 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1611 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1612 /* No need to write to this device. */
1613 sbio->bi_end_io = NULL;
1614 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1615 continue;
1617 /* fixup the bio for reuse */
1618 sbio->bi_vcnt = vcnt;
1619 sbio->bi_size = r1_bio->sectors << 9;
1620 sbio->bi_idx = 0;
1621 sbio->bi_phys_segments = 0;
1622 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1623 sbio->bi_flags |= 1 << BIO_UPTODATE;
1624 sbio->bi_next = NULL;
1625 sbio->bi_sector = r1_bio->sector +
1626 conf->mirrors[i].rdev->data_offset;
1627 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1628 size = sbio->bi_size;
1629 for (j = 0; j < vcnt ; j++) {
1630 struct bio_vec *bi;
1631 bi = &sbio->bi_io_vec[j];
1632 bi->bv_offset = 0;
1633 if (size > PAGE_SIZE)
1634 bi->bv_len = PAGE_SIZE;
1635 else
1636 bi->bv_len = size;
1637 size -= PAGE_SIZE;
1638 memcpy(page_address(bi->bv_page),
1639 page_address(pbio->bi_io_vec[j].bv_page),
1640 PAGE_SIZE);
1643 return 0;
1646 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
1648 conf_t *conf = mddev->private;
1649 int i;
1650 int disks = conf->raid_disks;
1651 struct bio *bio, *wbio;
1653 bio = r1_bio->bios[r1_bio->read_disk];
1655 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1656 /* ouch - failed to read all of that. */
1657 if (!fix_sync_read_error(r1_bio))
1658 return;
1660 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1661 if (process_checks(r1_bio) < 0)
1662 return;
1664 * schedule writes
1666 atomic_set(&r1_bio->remaining, 1);
1667 for (i = 0; i < disks ; i++) {
1668 wbio = r1_bio->bios[i];
1669 if (wbio->bi_end_io == NULL ||
1670 (wbio->bi_end_io == end_sync_read &&
1671 (i == r1_bio->read_disk ||
1672 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1673 continue;
1675 wbio->bi_rw = WRITE;
1676 wbio->bi_end_io = end_sync_write;
1677 atomic_inc(&r1_bio->remaining);
1678 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1680 generic_make_request(wbio);
1683 if (atomic_dec_and_test(&r1_bio->remaining)) {
1684 /* if we're here, all write(s) have completed, so clean up */
1685 md_done_sync(mddev, r1_bio->sectors, 1);
1686 put_buf(r1_bio);
1691 * This is a kernel thread which:
1693 * 1. Retries failed read operations on working mirrors.
1694 * 2. Updates the raid superblock when problems encounter.
1695 * 3. Performs writes following reads for array synchronising.
1698 static void fix_read_error(conf_t *conf, int read_disk,
1699 sector_t sect, int sectors)
1701 mddev_t *mddev = conf->mddev;
1702 while(sectors) {
1703 int s = sectors;
1704 int d = read_disk;
1705 int success = 0;
1706 int start;
1707 mdk_rdev_t *rdev;
1709 if (s > (PAGE_SIZE>>9))
1710 s = PAGE_SIZE >> 9;
1712 do {
1713 /* Note: no rcu protection needed here
1714 * as this is synchronous in the raid1d thread
1715 * which is the thread that might remove
1716 * a device. If raid1d ever becomes multi-threaded....
1718 sector_t first_bad;
1719 int bad_sectors;
1721 rdev = conf->mirrors[d].rdev;
1722 if (rdev &&
1723 test_bit(In_sync, &rdev->flags) &&
1724 is_badblock(rdev, sect, s,
1725 &first_bad, &bad_sectors) == 0 &&
1726 sync_page_io(rdev, sect, s<<9,
1727 conf->tmppage, READ, false))
1728 success = 1;
1729 else {
1730 d++;
1731 if (d == conf->raid_disks)
1732 d = 0;
1734 } while (!success && d != read_disk);
1736 if (!success) {
1737 /* Cannot read from anywhere - mark it bad */
1738 mdk_rdev_t *rdev = conf->mirrors[read_disk].rdev;
1739 if (!rdev_set_badblocks(rdev, sect, s, 0))
1740 md_error(mddev, rdev);
1741 break;
1743 /* write it back and re-read */
1744 start = d;
1745 while (d != read_disk) {
1746 if (d==0)
1747 d = conf->raid_disks;
1748 d--;
1749 rdev = conf->mirrors[d].rdev;
1750 if (rdev &&
1751 test_bit(In_sync, &rdev->flags))
1752 r1_sync_page_io(rdev, sect, s,
1753 conf->tmppage, WRITE);
1755 d = start;
1756 while (d != read_disk) {
1757 char b[BDEVNAME_SIZE];
1758 if (d==0)
1759 d = conf->raid_disks;
1760 d--;
1761 rdev = conf->mirrors[d].rdev;
1762 if (rdev &&
1763 test_bit(In_sync, &rdev->flags)) {
1764 if (r1_sync_page_io(rdev, sect, s,
1765 conf->tmppage, READ)) {
1766 atomic_add(s, &rdev->corrected_errors);
1767 printk(KERN_INFO
1768 "md/raid1:%s: read error corrected "
1769 "(%d sectors at %llu on %s)\n",
1770 mdname(mddev), s,
1771 (unsigned long long)(sect +
1772 rdev->data_offset),
1773 bdevname(rdev->bdev, b));
1777 sectors -= s;
1778 sect += s;
1782 static void bi_complete(struct bio *bio, int error)
1784 complete((struct completion *)bio->bi_private);
1787 static int submit_bio_wait(int rw, struct bio *bio)
1789 struct completion event;
1790 rw |= REQ_SYNC;
1792 init_completion(&event);
1793 bio->bi_private = &event;
1794 bio->bi_end_io = bi_complete;
1795 submit_bio(rw, bio);
1796 wait_for_completion(&event);
1798 return test_bit(BIO_UPTODATE, &bio->bi_flags);
1801 static int narrow_write_error(r1bio_t *r1_bio, int i)
1803 mddev_t *mddev = r1_bio->mddev;
1804 conf_t *conf = mddev->private;
1805 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
1806 int vcnt, idx;
1807 struct bio_vec *vec;
1809 /* bio has the data to be written to device 'i' where
1810 * we just recently had a write error.
1811 * We repeatedly clone the bio and trim down to one block,
1812 * then try the write. Where the write fails we record
1813 * a bad block.
1814 * It is conceivable that the bio doesn't exactly align with
1815 * blocks. We must handle this somehow.
1817 * We currently own a reference on the rdev.
1820 int block_sectors;
1821 sector_t sector;
1822 int sectors;
1823 int sect_to_write = r1_bio->sectors;
1824 int ok = 1;
1826 if (rdev->badblocks.shift < 0)
1827 return 0;
1829 block_sectors = 1 << rdev->badblocks.shift;
1830 sector = r1_bio->sector;
1831 sectors = ((sector + block_sectors)
1832 & ~(sector_t)(block_sectors - 1))
1833 - sector;
1835 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
1836 vcnt = r1_bio->behind_page_count;
1837 vec = r1_bio->behind_bvecs;
1838 idx = 0;
1839 while (vec[idx].bv_page == NULL)
1840 idx++;
1841 } else {
1842 vcnt = r1_bio->master_bio->bi_vcnt;
1843 vec = r1_bio->master_bio->bi_io_vec;
1844 idx = r1_bio->master_bio->bi_idx;
1846 while (sect_to_write) {
1847 struct bio *wbio;
1848 if (sectors > sect_to_write)
1849 sectors = sect_to_write;
1850 /* Write at 'sector' for 'sectors'*/
1852 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
1853 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
1854 wbio->bi_sector = r1_bio->sector;
1855 wbio->bi_rw = WRITE;
1856 wbio->bi_vcnt = vcnt;
1857 wbio->bi_size = r1_bio->sectors << 9;
1858 wbio->bi_idx = idx;
1860 md_trim_bio(wbio, sector - r1_bio->sector, sectors);
1861 wbio->bi_sector += rdev->data_offset;
1862 wbio->bi_bdev = rdev->bdev;
1863 if (submit_bio_wait(WRITE, wbio) == 0)
1864 /* failure! */
1865 ok = rdev_set_badblocks(rdev, sector,
1866 sectors, 0)
1867 && ok;
1869 bio_put(wbio);
1870 sect_to_write -= sectors;
1871 sector += sectors;
1872 sectors = block_sectors;
1874 return ok;
1877 static void handle_sync_write_finished(conf_t *conf, r1bio_t *r1_bio)
1879 int m;
1880 int s = r1_bio->sectors;
1881 for (m = 0; m < conf->raid_disks ; m++) {
1882 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1883 struct bio *bio = r1_bio->bios[m];
1884 if (bio->bi_end_io == NULL)
1885 continue;
1886 if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1887 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
1888 rdev_clear_badblocks(rdev, r1_bio->sector, s);
1890 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
1891 test_bit(R1BIO_WriteError, &r1_bio->state)) {
1892 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
1893 md_error(conf->mddev, rdev);
1896 put_buf(r1_bio);
1897 md_done_sync(conf->mddev, s, 1);
1900 static void handle_write_finished(conf_t *conf, r1bio_t *r1_bio)
1902 int m;
1903 for (m = 0; m < conf->raid_disks ; m++)
1904 if (r1_bio->bios[m] == IO_MADE_GOOD) {
1905 mdk_rdev_t *rdev = conf->mirrors[m].rdev;
1906 rdev_clear_badblocks(rdev,
1907 r1_bio->sector,
1908 r1_bio->sectors);
1909 rdev_dec_pending(rdev, conf->mddev);
1910 } else if (r1_bio->bios[m] != NULL) {
1911 /* This drive got a write error. We need to
1912 * narrow down and record precise write
1913 * errors.
1915 if (!narrow_write_error(r1_bio, m)) {
1916 md_error(conf->mddev,
1917 conf->mirrors[m].rdev);
1918 /* an I/O failed, we can't clear the bitmap */
1919 set_bit(R1BIO_Degraded, &r1_bio->state);
1921 rdev_dec_pending(conf->mirrors[m].rdev,
1922 conf->mddev);
1924 if (test_bit(R1BIO_WriteError, &r1_bio->state))
1925 close_write(r1_bio);
1926 raid_end_bio_io(r1_bio);
1929 static void handle_read_error(conf_t *conf, r1bio_t *r1_bio)
1931 int disk;
1932 int max_sectors;
1933 mddev_t *mddev = conf->mddev;
1934 struct bio *bio;
1935 char b[BDEVNAME_SIZE];
1936 mdk_rdev_t *rdev;
1938 clear_bit(R1BIO_ReadError, &r1_bio->state);
1939 /* we got a read error. Maybe the drive is bad. Maybe just
1940 * the block and we can fix it.
1941 * We freeze all other IO, and try reading the block from
1942 * other devices. When we find one, we re-write
1943 * and check it that fixes the read error.
1944 * This is all done synchronously while the array is
1945 * frozen
1947 if (mddev->ro == 0) {
1948 freeze_array(conf);
1949 fix_read_error(conf, r1_bio->read_disk,
1950 r1_bio->sector, r1_bio->sectors);
1951 unfreeze_array(conf);
1952 } else
1953 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
1955 bio = r1_bio->bios[r1_bio->read_disk];
1956 bdevname(bio->bi_bdev, b);
1957 read_more:
1958 disk = read_balance(conf, r1_bio, &max_sectors);
1959 if (disk == -1) {
1960 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
1961 " read error for block %llu\n",
1962 mdname(mddev), b, (unsigned long long)r1_bio->sector);
1963 raid_end_bio_io(r1_bio);
1964 } else {
1965 const unsigned long do_sync
1966 = r1_bio->master_bio->bi_rw & REQ_SYNC;
1967 if (bio) {
1968 r1_bio->bios[r1_bio->read_disk] =
1969 mddev->ro ? IO_BLOCKED : NULL;
1970 bio_put(bio);
1972 r1_bio->read_disk = disk;
1973 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
1974 md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
1975 r1_bio->bios[r1_bio->read_disk] = bio;
1976 rdev = conf->mirrors[disk].rdev;
1977 printk_ratelimited(KERN_ERR
1978 "md/raid1:%s: redirecting sector %llu"
1979 " to other mirror: %s\n",
1980 mdname(mddev),
1981 (unsigned long long)r1_bio->sector,
1982 bdevname(rdev->bdev, b));
1983 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1984 bio->bi_bdev = rdev->bdev;
1985 bio->bi_end_io = raid1_end_read_request;
1986 bio->bi_rw = READ | do_sync;
1987 bio->bi_private = r1_bio;
1988 if (max_sectors < r1_bio->sectors) {
1989 /* Drat - have to split this up more */
1990 struct bio *mbio = r1_bio->master_bio;
1991 int sectors_handled = (r1_bio->sector + max_sectors
1992 - mbio->bi_sector);
1993 r1_bio->sectors = max_sectors;
1994 spin_lock_irq(&conf->device_lock);
1995 if (mbio->bi_phys_segments == 0)
1996 mbio->bi_phys_segments = 2;
1997 else
1998 mbio->bi_phys_segments++;
1999 spin_unlock_irq(&conf->device_lock);
2000 generic_make_request(bio);
2001 bio = NULL;
2003 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2005 r1_bio->master_bio = mbio;
2006 r1_bio->sectors = (mbio->bi_size >> 9)
2007 - sectors_handled;
2008 r1_bio->state = 0;
2009 set_bit(R1BIO_ReadError, &r1_bio->state);
2010 r1_bio->mddev = mddev;
2011 r1_bio->sector = mbio->bi_sector + sectors_handled;
2013 goto read_more;
2014 } else
2015 generic_make_request(bio);
2019 static void raid1d(mddev_t *mddev)
2021 r1bio_t *r1_bio;
2022 unsigned long flags;
2023 conf_t *conf = mddev->private;
2024 struct list_head *head = &conf->retry_list;
2025 struct blk_plug plug;
2027 md_check_recovery(mddev);
2029 blk_start_plug(&plug);
2030 for (;;) {
2032 if (atomic_read(&mddev->plug_cnt) == 0)
2033 flush_pending_writes(conf);
2035 spin_lock_irqsave(&conf->device_lock, flags);
2036 if (list_empty(head)) {
2037 spin_unlock_irqrestore(&conf->device_lock, flags);
2038 break;
2040 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
2041 list_del(head->prev);
2042 conf->nr_queued--;
2043 spin_unlock_irqrestore(&conf->device_lock, flags);
2045 mddev = r1_bio->mddev;
2046 conf = mddev->private;
2047 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2048 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2049 test_bit(R1BIO_WriteError, &r1_bio->state))
2050 handle_sync_write_finished(conf, r1_bio);
2051 else
2052 sync_request_write(mddev, r1_bio);
2053 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2054 test_bit(R1BIO_WriteError, &r1_bio->state))
2055 handle_write_finished(conf, r1_bio);
2056 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2057 handle_read_error(conf, r1_bio);
2058 else
2059 /* just a partial read to be scheduled from separate
2060 * context
2062 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
2064 cond_resched();
2065 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2066 md_check_recovery(mddev);
2068 blk_finish_plug(&plug);
2072 static int init_resync(conf_t *conf)
2074 int buffs;
2076 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2077 BUG_ON(conf->r1buf_pool);
2078 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2079 conf->poolinfo);
2080 if (!conf->r1buf_pool)
2081 return -ENOMEM;
2082 conf->next_resync = 0;
2083 return 0;
2087 * perform a "sync" on one "block"
2089 * We need to make sure that no normal I/O request - particularly write
2090 * requests - conflict with active sync requests.
2092 * This is achieved by tracking pending requests and a 'barrier' concept
2093 * that can be installed to exclude normal IO requests.
2096 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2098 conf_t *conf = mddev->private;
2099 r1bio_t *r1_bio;
2100 struct bio *bio;
2101 sector_t max_sector, nr_sectors;
2102 int disk = -1;
2103 int i;
2104 int wonly = -1;
2105 int write_targets = 0, read_targets = 0;
2106 sector_t sync_blocks;
2107 int still_degraded = 0;
2108 int good_sectors = RESYNC_SECTORS;
2109 int min_bad = 0; /* number of sectors that are bad in all devices */
2111 if (!conf->r1buf_pool)
2112 if (init_resync(conf))
2113 return 0;
2115 max_sector = mddev->dev_sectors;
2116 if (sector_nr >= max_sector) {
2117 /* If we aborted, we need to abort the
2118 * sync on the 'current' bitmap chunk (there will
2119 * only be one in raid1 resync.
2120 * We can find the current addess in mddev->curr_resync
2122 if (mddev->curr_resync < max_sector) /* aborted */
2123 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2124 &sync_blocks, 1);
2125 else /* completed sync */
2126 conf->fullsync = 0;
2128 bitmap_close_sync(mddev->bitmap);
2129 close_sync(conf);
2130 return 0;
2133 if (mddev->bitmap == NULL &&
2134 mddev->recovery_cp == MaxSector &&
2135 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2136 conf->fullsync == 0) {
2137 *skipped = 1;
2138 return max_sector - sector_nr;
2140 /* before building a request, check if we can skip these blocks..
2141 * This call the bitmap_start_sync doesn't actually record anything
2143 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2144 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2145 /* We can skip this block, and probably several more */
2146 *skipped = 1;
2147 return sync_blocks;
2150 * If there is non-resync activity waiting for a turn,
2151 * and resync is going fast enough,
2152 * then let it though before starting on this new sync request.
2154 if (!go_faster && conf->nr_waiting)
2155 msleep_interruptible(1000);
2157 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2158 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2159 raise_barrier(conf);
2161 conf->next_resync = sector_nr;
2163 rcu_read_lock();
2165 * If we get a correctably read error during resync or recovery,
2166 * we might want to read from a different device. So we
2167 * flag all drives that could conceivably be read from for READ,
2168 * and any others (which will be non-In_sync devices) for WRITE.
2169 * If a read fails, we try reading from something else for which READ
2170 * is OK.
2173 r1_bio->mddev = mddev;
2174 r1_bio->sector = sector_nr;
2175 r1_bio->state = 0;
2176 set_bit(R1BIO_IsSync, &r1_bio->state);
2178 for (i=0; i < conf->raid_disks; i++) {
2179 mdk_rdev_t *rdev;
2180 bio = r1_bio->bios[i];
2182 /* take from bio_init */
2183 bio->bi_next = NULL;
2184 bio->bi_flags &= ~(BIO_POOL_MASK-1);
2185 bio->bi_flags |= 1 << BIO_UPTODATE;
2186 bio->bi_comp_cpu = -1;
2187 bio->bi_rw = READ;
2188 bio->bi_vcnt = 0;
2189 bio->bi_idx = 0;
2190 bio->bi_phys_segments = 0;
2191 bio->bi_size = 0;
2192 bio->bi_end_io = NULL;
2193 bio->bi_private = NULL;
2195 rdev = rcu_dereference(conf->mirrors[i].rdev);
2196 if (rdev == NULL ||
2197 test_bit(Faulty, &rdev->flags)) {
2198 still_degraded = 1;
2199 } else if (!test_bit(In_sync, &rdev->flags)) {
2200 bio->bi_rw = WRITE;
2201 bio->bi_end_io = end_sync_write;
2202 write_targets ++;
2203 } else {
2204 /* may need to read from here */
2205 sector_t first_bad = MaxSector;
2206 int bad_sectors;
2208 if (is_badblock(rdev, sector_nr, good_sectors,
2209 &first_bad, &bad_sectors)) {
2210 if (first_bad > sector_nr)
2211 good_sectors = first_bad - sector_nr;
2212 else {
2213 bad_sectors -= (sector_nr - first_bad);
2214 if (min_bad == 0 ||
2215 min_bad > bad_sectors)
2216 min_bad = bad_sectors;
2219 if (sector_nr < first_bad) {
2220 if (test_bit(WriteMostly, &rdev->flags)) {
2221 if (wonly < 0)
2222 wonly = i;
2223 } else {
2224 if (disk < 0)
2225 disk = i;
2227 bio->bi_rw = READ;
2228 bio->bi_end_io = end_sync_read;
2229 read_targets++;
2232 if (bio->bi_end_io) {
2233 atomic_inc(&rdev->nr_pending);
2234 bio->bi_sector = sector_nr + rdev->data_offset;
2235 bio->bi_bdev = rdev->bdev;
2236 bio->bi_private = r1_bio;
2239 rcu_read_unlock();
2240 if (disk < 0)
2241 disk = wonly;
2242 r1_bio->read_disk = disk;
2244 if (read_targets == 0 && min_bad > 0) {
2245 /* These sectors are bad on all InSync devices, so we
2246 * need to mark them bad on all write targets
2248 int ok = 1;
2249 for (i = 0 ; i < conf->raid_disks ; i++)
2250 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2251 mdk_rdev_t *rdev =
2252 rcu_dereference(conf->mirrors[i].rdev);
2253 ok = rdev_set_badblocks(rdev, sector_nr,
2254 min_bad, 0
2255 ) && ok;
2257 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2258 *skipped = 1;
2259 put_buf(r1_bio);
2261 if (!ok) {
2262 /* Cannot record the badblocks, so need to
2263 * abort the resync.
2264 * If there are multiple read targets, could just
2265 * fail the really bad ones ???
2267 conf->recovery_disabled = mddev->recovery_disabled;
2268 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2269 return 0;
2270 } else
2271 return min_bad;
2274 if (min_bad > 0 && min_bad < good_sectors) {
2275 /* only resync enough to reach the next bad->good
2276 * transition */
2277 good_sectors = min_bad;
2280 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2281 /* extra read targets are also write targets */
2282 write_targets += read_targets-1;
2284 if (write_targets == 0 || read_targets == 0) {
2285 /* There is nowhere to write, so all non-sync
2286 * drives must be failed - so we are finished
2288 sector_t rv = max_sector - sector_nr;
2289 *skipped = 1;
2290 put_buf(r1_bio);
2291 return rv;
2294 if (max_sector > mddev->resync_max)
2295 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2296 if (max_sector > sector_nr + good_sectors)
2297 max_sector = sector_nr + good_sectors;
2298 nr_sectors = 0;
2299 sync_blocks = 0;
2300 do {
2301 struct page *page;
2302 int len = PAGE_SIZE;
2303 if (sector_nr + (len>>9) > max_sector)
2304 len = (max_sector - sector_nr) << 9;
2305 if (len == 0)
2306 break;
2307 if (sync_blocks == 0) {
2308 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2309 &sync_blocks, still_degraded) &&
2310 !conf->fullsync &&
2311 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2312 break;
2313 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
2314 if ((len >> 9) > sync_blocks)
2315 len = sync_blocks<<9;
2318 for (i=0 ; i < conf->raid_disks; i++) {
2319 bio = r1_bio->bios[i];
2320 if (bio->bi_end_io) {
2321 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2322 if (bio_add_page(bio, page, len, 0) == 0) {
2323 /* stop here */
2324 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2325 while (i > 0) {
2326 i--;
2327 bio = r1_bio->bios[i];
2328 if (bio->bi_end_io==NULL)
2329 continue;
2330 /* remove last page from this bio */
2331 bio->bi_vcnt--;
2332 bio->bi_size -= len;
2333 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2335 goto bio_full;
2339 nr_sectors += len>>9;
2340 sector_nr += len>>9;
2341 sync_blocks -= (len>>9);
2342 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2343 bio_full:
2344 r1_bio->sectors = nr_sectors;
2346 /* For a user-requested sync, we read all readable devices and do a
2347 * compare
2349 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2350 atomic_set(&r1_bio->remaining, read_targets);
2351 for (i=0; i<conf->raid_disks; i++) {
2352 bio = r1_bio->bios[i];
2353 if (bio->bi_end_io == end_sync_read) {
2354 md_sync_acct(bio->bi_bdev, nr_sectors);
2355 generic_make_request(bio);
2358 } else {
2359 atomic_set(&r1_bio->remaining, 1);
2360 bio = r1_bio->bios[r1_bio->read_disk];
2361 md_sync_acct(bio->bi_bdev, nr_sectors);
2362 generic_make_request(bio);
2365 return nr_sectors;
2368 static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2370 if (sectors)
2371 return sectors;
2373 return mddev->dev_sectors;
2376 static conf_t *setup_conf(mddev_t *mddev)
2378 conf_t *conf;
2379 int i;
2380 mirror_info_t *disk;
2381 mdk_rdev_t *rdev;
2382 int err = -ENOMEM;
2384 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2385 if (!conf)
2386 goto abort;
2388 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2389 GFP_KERNEL);
2390 if (!conf->mirrors)
2391 goto abort;
2393 conf->tmppage = alloc_page(GFP_KERNEL);
2394 if (!conf->tmppage)
2395 goto abort;
2397 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2398 if (!conf->poolinfo)
2399 goto abort;
2400 conf->poolinfo->raid_disks = mddev->raid_disks;
2401 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2402 r1bio_pool_free,
2403 conf->poolinfo);
2404 if (!conf->r1bio_pool)
2405 goto abort;
2407 conf->poolinfo->mddev = mddev;
2409 spin_lock_init(&conf->device_lock);
2410 list_for_each_entry(rdev, &mddev->disks, same_set) {
2411 int disk_idx = rdev->raid_disk;
2412 if (disk_idx >= mddev->raid_disks
2413 || disk_idx < 0)
2414 continue;
2415 disk = conf->mirrors + disk_idx;
2417 disk->rdev = rdev;
2419 disk->head_position = 0;
2421 conf->raid_disks = mddev->raid_disks;
2422 conf->mddev = mddev;
2423 INIT_LIST_HEAD(&conf->retry_list);
2425 spin_lock_init(&conf->resync_lock);
2426 init_waitqueue_head(&conf->wait_barrier);
2428 bio_list_init(&conf->pending_bio_list);
2430 conf->last_used = -1;
2431 for (i = 0; i < conf->raid_disks; i++) {
2433 disk = conf->mirrors + i;
2435 if (!disk->rdev ||
2436 !test_bit(In_sync, &disk->rdev->flags)) {
2437 disk->head_position = 0;
2438 if (disk->rdev)
2439 conf->fullsync = 1;
2440 } else if (conf->last_used < 0)
2442 * The first working device is used as a
2443 * starting point to read balancing.
2445 conf->last_used = i;
2448 err = -EIO;
2449 if (conf->last_used < 0) {
2450 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
2451 mdname(mddev));
2452 goto abort;
2454 err = -ENOMEM;
2455 conf->thread = md_register_thread(raid1d, mddev, NULL);
2456 if (!conf->thread) {
2457 printk(KERN_ERR
2458 "md/raid1:%s: couldn't allocate thread\n",
2459 mdname(mddev));
2460 goto abort;
2463 return conf;
2465 abort:
2466 if (conf) {
2467 if (conf->r1bio_pool)
2468 mempool_destroy(conf->r1bio_pool);
2469 kfree(conf->mirrors);
2470 safe_put_page(conf->tmppage);
2471 kfree(conf->poolinfo);
2472 kfree(conf);
2474 return ERR_PTR(err);
2477 static int run(mddev_t *mddev)
2479 conf_t *conf;
2480 int i;
2481 mdk_rdev_t *rdev;
2483 if (mddev->level != 1) {
2484 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
2485 mdname(mddev), mddev->level);
2486 return -EIO;
2488 if (mddev->reshape_position != MaxSector) {
2489 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
2490 mdname(mddev));
2491 return -EIO;
2494 * copy the already verified devices into our private RAID1
2495 * bookkeeping area. [whatever we allocate in run(),
2496 * should be freed in stop()]
2498 if (mddev->private == NULL)
2499 conf = setup_conf(mddev);
2500 else
2501 conf = mddev->private;
2503 if (IS_ERR(conf))
2504 return PTR_ERR(conf);
2506 list_for_each_entry(rdev, &mddev->disks, same_set) {
2507 if (!mddev->gendisk)
2508 continue;
2509 disk_stack_limits(mddev->gendisk, rdev->bdev,
2510 rdev->data_offset << 9);
2511 /* as we don't honour merge_bvec_fn, we must never risk
2512 * violating it, so limit ->max_segments to 1 lying within
2513 * a single page, as a one page request is never in violation.
2515 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2516 blk_queue_max_segments(mddev->queue, 1);
2517 blk_queue_segment_boundary(mddev->queue,
2518 PAGE_CACHE_SIZE - 1);
2522 mddev->degraded = 0;
2523 for (i=0; i < conf->raid_disks; i++)
2524 if (conf->mirrors[i].rdev == NULL ||
2525 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2526 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2527 mddev->degraded++;
2529 if (conf->raid_disks - mddev->degraded == 1)
2530 mddev->recovery_cp = MaxSector;
2532 if (mddev->recovery_cp != MaxSector)
2533 printk(KERN_NOTICE "md/raid1:%s: not clean"
2534 " -- starting background reconstruction\n",
2535 mdname(mddev));
2536 printk(KERN_INFO
2537 "md/raid1:%s: active with %d out of %d mirrors\n",
2538 mdname(mddev), mddev->raid_disks - mddev->degraded,
2539 mddev->raid_disks);
2542 * Ok, everything is just fine now
2544 mddev->thread = conf->thread;
2545 conf->thread = NULL;
2546 mddev->private = conf;
2548 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
2550 if (mddev->queue) {
2551 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2552 mddev->queue->backing_dev_info.congested_data = mddev;
2554 return md_integrity_register(mddev);
2557 static int stop(mddev_t *mddev)
2559 conf_t *conf = mddev->private;
2560 struct bitmap *bitmap = mddev->bitmap;
2562 /* wait for behind writes to complete */
2563 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
2564 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2565 mdname(mddev));
2566 /* need to kick something here to make sure I/O goes? */
2567 wait_event(bitmap->behind_wait,
2568 atomic_read(&bitmap->behind_writes) == 0);
2571 raise_barrier(conf);
2572 lower_barrier(conf);
2574 md_unregister_thread(&mddev->thread);
2575 if (conf->r1bio_pool)
2576 mempool_destroy(conf->r1bio_pool);
2577 kfree(conf->mirrors);
2578 kfree(conf->poolinfo);
2579 kfree(conf);
2580 mddev->private = NULL;
2581 return 0;
2584 static int raid1_resize(mddev_t *mddev, sector_t sectors)
2586 /* no resync is happening, and there is enough space
2587 * on all devices, so we can resize.
2588 * We need to make sure resync covers any new space.
2589 * If the array is shrinking we should possibly wait until
2590 * any io in the removed space completes, but it hardly seems
2591 * worth it.
2593 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
2594 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2595 return -EINVAL;
2596 set_capacity(mddev->gendisk, mddev->array_sectors);
2597 revalidate_disk(mddev->gendisk);
2598 if (sectors > mddev->dev_sectors &&
2599 mddev->recovery_cp > mddev->dev_sectors) {
2600 mddev->recovery_cp = mddev->dev_sectors;
2601 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2603 mddev->dev_sectors = sectors;
2604 mddev->resync_max_sectors = sectors;
2605 return 0;
2608 static int raid1_reshape(mddev_t *mddev)
2610 /* We need to:
2611 * 1/ resize the r1bio_pool
2612 * 2/ resize conf->mirrors
2614 * We allocate a new r1bio_pool if we can.
2615 * Then raise a device barrier and wait until all IO stops.
2616 * Then resize conf->mirrors and swap in the new r1bio pool.
2618 * At the same time, we "pack" the devices so that all the missing
2619 * devices have the higher raid_disk numbers.
2621 mempool_t *newpool, *oldpool;
2622 struct pool_info *newpoolinfo;
2623 mirror_info_t *newmirrors;
2624 conf_t *conf = mddev->private;
2625 int cnt, raid_disks;
2626 unsigned long flags;
2627 int d, d2, err;
2629 /* Cannot change chunk_size, layout, or level */
2630 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
2631 mddev->layout != mddev->new_layout ||
2632 mddev->level != mddev->new_level) {
2633 mddev->new_chunk_sectors = mddev->chunk_sectors;
2634 mddev->new_layout = mddev->layout;
2635 mddev->new_level = mddev->level;
2636 return -EINVAL;
2639 err = md_allow_write(mddev);
2640 if (err)
2641 return err;
2643 raid_disks = mddev->raid_disks + mddev->delta_disks;
2645 if (raid_disks < conf->raid_disks) {
2646 cnt=0;
2647 for (d= 0; d < conf->raid_disks; d++)
2648 if (conf->mirrors[d].rdev)
2649 cnt++;
2650 if (cnt > raid_disks)
2651 return -EBUSY;
2654 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2655 if (!newpoolinfo)
2656 return -ENOMEM;
2657 newpoolinfo->mddev = mddev;
2658 newpoolinfo->raid_disks = raid_disks;
2660 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2661 r1bio_pool_free, newpoolinfo);
2662 if (!newpool) {
2663 kfree(newpoolinfo);
2664 return -ENOMEM;
2666 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
2667 if (!newmirrors) {
2668 kfree(newpoolinfo);
2669 mempool_destroy(newpool);
2670 return -ENOMEM;
2673 raise_barrier(conf);
2675 /* ok, everything is stopped */
2676 oldpool = conf->r1bio_pool;
2677 conf->r1bio_pool = newpool;
2679 for (d = d2 = 0; d < conf->raid_disks; d++) {
2680 mdk_rdev_t *rdev = conf->mirrors[d].rdev;
2681 if (rdev && rdev->raid_disk != d2) {
2682 sysfs_unlink_rdev(mddev, rdev);
2683 rdev->raid_disk = d2;
2684 sysfs_unlink_rdev(mddev, rdev);
2685 if (sysfs_link_rdev(mddev, rdev))
2686 printk(KERN_WARNING
2687 "md/raid1:%s: cannot register rd%d\n",
2688 mdname(mddev), rdev->raid_disk);
2690 if (rdev)
2691 newmirrors[d2++].rdev = rdev;
2693 kfree(conf->mirrors);
2694 conf->mirrors = newmirrors;
2695 kfree(conf->poolinfo);
2696 conf->poolinfo = newpoolinfo;
2698 spin_lock_irqsave(&conf->device_lock, flags);
2699 mddev->degraded += (raid_disks - conf->raid_disks);
2700 spin_unlock_irqrestore(&conf->device_lock, flags);
2701 conf->raid_disks = mddev->raid_disks = raid_disks;
2702 mddev->delta_disks = 0;
2704 conf->last_used = 0; /* just make sure it is in-range */
2705 lower_barrier(conf);
2707 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2708 md_wakeup_thread(mddev->thread);
2710 mempool_destroy(oldpool);
2711 return 0;
2714 static void raid1_quiesce(mddev_t *mddev, int state)
2716 conf_t *conf = mddev->private;
2718 switch(state) {
2719 case 2: /* wake for suspend */
2720 wake_up(&conf->wait_barrier);
2721 break;
2722 case 1:
2723 raise_barrier(conf);
2724 break;
2725 case 0:
2726 lower_barrier(conf);
2727 break;
2731 static void *raid1_takeover(mddev_t *mddev)
2733 /* raid1 can take over:
2734 * raid5 with 2 devices, any layout or chunk size
2736 if (mddev->level == 5 && mddev->raid_disks == 2) {
2737 conf_t *conf;
2738 mddev->new_level = 1;
2739 mddev->new_layout = 0;
2740 mddev->new_chunk_sectors = 0;
2741 conf = setup_conf(mddev);
2742 if (!IS_ERR(conf))
2743 conf->barrier = 1;
2744 return conf;
2746 return ERR_PTR(-EINVAL);
2749 static struct mdk_personality raid1_personality =
2751 .name = "raid1",
2752 .level = 1,
2753 .owner = THIS_MODULE,
2754 .make_request = make_request,
2755 .run = run,
2756 .stop = stop,
2757 .status = status,
2758 .error_handler = error,
2759 .hot_add_disk = raid1_add_disk,
2760 .hot_remove_disk= raid1_remove_disk,
2761 .spare_active = raid1_spare_active,
2762 .sync_request = sync_request,
2763 .resize = raid1_resize,
2764 .size = raid1_size,
2765 .check_reshape = raid1_reshape,
2766 .quiesce = raid1_quiesce,
2767 .takeover = raid1_takeover,
2770 static int __init raid_init(void)
2772 return register_md_personality(&raid1_personality);
2775 static void raid_exit(void)
2777 unregister_md_personality(&raid1_personality);
2780 module_init(raid_init);
2781 module_exit(raid_exit);
2782 MODULE_LICENSE("GPL");
2783 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
2784 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2785 MODULE_ALIAS("md-raid1");
2786 MODULE_ALIAS("md-level-1");