/* * raid1.c : Multiple Devices driver for Linux * * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat * * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman * * RAID-1 management functions. * * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 * * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> * * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support * bitmapped intelligence in resync: * * - bitmap marked during normal i/o * - bitmap used to skip nondirty blocks during sync * * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: * - persistent bitmap code * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * You should have received a copy of the GNU General Public License * (for example /usr/src/linux/COPYING); if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/slab.h>#include <linux/delay.h>#include <linux/blkdev.h>#include <linux/module.h>#include <linux/seq_file.h>#include <linux/ratelimit.h>#include "md.h"#include "raid1.h"#include "bitmap.h" /* * Number of guaranteed r1bios in case of extreme VM load: */#define NR_RAID1_BIOS 256 /* when we get a read error on a read-only array, we redirect to another * device without failing the first device, or trying to over-write to * correct the read error. To keep track of bad blocks on a per-bio * level, we store IO_BLOCKED in the appropriate 'bios' pointer */#define IO_BLOCKED ((struct bio *)1)/* When we successfully write to a known bad-block, we need to remove the * bad-block marking which must be done from process context. So we record * the success by setting devs[n].bio to IO_MADE_GOOD */#define IO_MADE_GOOD ((struct bio *)2) #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) /* When there are this many requests queue to be written by * the raid1 thread, we become 'congested' to provide back-pressure * for writeback. */static int max_queued_requests = 1024; static void allow_barrier(struct r1conf *conf, sector_t start_next_window, sector_t bi_sector);static void lower_barrier(struct r1conf *conf); static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data){ struct pool_info *pi = data; int size = offsetof(struct r1bio, bios[pi->raid_disks]); /* allocate a r1bio with room for raid_disks entries in the bios array */ return kzalloc(size, gfp_flags);} static void r1bio_pool_free(void *r1_bio, void *data){ kfree(r1_bio);} #define RESYNC_BLOCK_SIZE (64*1024)#define RESYNC_DEPTH 32#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)#define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)#define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)#define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS) static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data){ struct pool_info *pi = data; struct r1bio *r1_bio; struct bio *bio; int i, j; r1_bio = r1bio_pool_alloc(gfp_flags, pi); if (!r1_bio) return NULL; /* * Allocate bios : 1 for reading, n-1 for writing */ for (j = pi->raid_disks ; j-- ; ) { bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); if (!bio) goto out_free_bio; r1_bio->bios[j] = bio; } /* * Allocate RESYNC_PAGES data pages and attach them to * the first bio. * If this is a user-requested check/repair, allocate * RESYNC_PAGES for each bio. */ if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) j = pi->raid_disks; else j = 1; while(j--) { bio = r1_bio->bios[j]; bio->bi_vcnt = RESYNC_PAGES; if (bio_alloc_pages(bio, gfp_flags)) goto out_free_bio; } /* If not user-requests, copy the page pointers to all bios */ if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) { for (i=0; i<RESYNC_PAGES ; i++) for (j=1; j<pi->raid_disks; j++) r1_bio->bios[j]->bi_io_vec[i].bv_page = r1_bio->bios[0]->bi_io_vec[i].bv_page; } r1_bio->master_bio = NULL; return r1_bio; out_free_bio: while (++j < pi->raid_disks) bio_put(r1_bio->bios[j]); r1bio_pool_free(r1_bio, data); return NULL;} static void r1buf_pool_free(void *__r1_bio, void *data){ struct pool_info *pi = data; int i,j; struct r1bio *r1bio = __r1_bio; for (i = 0; i < RESYNC_PAGES; i++) for (j = pi->raid_disks; j-- ;) { if (j == 0 || r1bio->bios[j]->bi_io_vec[i].bv_page != r1bio->bios[0]->bi_io_vec[i].bv_page) safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page); } for (i=0 ; i < pi->raid_disks; i++) bio_put(r1bio->bios[i]); r1bio_pool_free(r1bio, data);} static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio){ int i; for (i = 0; i < conf->raid_disks * 2; i++) { struct bio **bio = r1_bio->bios + i; if (!BIO_SPECIAL(*bio)) bio_put(*bio); *bio = NULL; }} static void free_r1bio(struct r1bio *r1_bio){ struct r1conf *conf = r1_bio->mddev->private; put_all_bios(conf, r1_bio); mempool_free(r1_bio, conf->r1bio_pool);} static void put_buf(struct r1bio *r1_bio){ struct r1conf *conf = r1_bio->mddev->private; int i; for (i = 0; i < conf->raid_disks * 2; i++) { struct bio *bio = r1_bio->bios[i]; if (bio->bi_end_io) rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev); } mempool_free(r1_bio, conf->r1buf_pool); lower_barrier(conf);} static void reschedule_retry(struct r1bio *r1_bio){ unsigned long flags; struct mddev *mddev = r1_bio->mddev; struct r1conf *conf = mddev->private; spin_lock_irqsave(&conf->device_lock, flags); list_add(&r1_bio->retry_list, &conf->retry_list); conf->nr_queued ++; spin_unlock_irqrestore(&conf->device_lock, flags); wake_up(&conf->wait_barrier); md_wakeup_thread(mddev->thread);} /* * raid_end_bio_io() is called when we have finished servicing a mirrored * operation and are ready to return a success/failure code to the buffer * cache layer. */static void call_bio_endio(struct r1bio *r1_bio){ struct bio *bio = r1_bio->master_bio; int done; struct r1conf *conf = r1_bio->mddev->private; sector_t start_next_window = r1_bio->start_next_window; sector_t bi_sector = bio->bi_sector; if (bio->bi_phys_segments) { unsigned long flags; spin_lock_irqsave(&conf->device_lock, flags); bio->bi_phys_segments--; done = (bio->bi_phys_segments == 0); spin_unlock_irqrestore(&conf->device_lock, flags); /* * make_request() might be waiting for * bi_phys_segments to decrease */ wake_up(&conf->wait_barrier); } else done = 1; if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) clear_bit(BIO_UPTODATE, &bio->bi_flags); if (done) { bio_endio(bio, 0); /* * Wake up any possible resync thread that waits for the device * to go idle. */ allow_barrier(conf, start_next_window, bi_sector); }} static void raid_end_bio_io(struct r1bio *r1_bio){ struct bio *bio = r1_bio->master_bio; /* if nobody has done the final endio yet, do it now */ if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { pr_debug("raid1: sync end %s on sectors %llu-%llu\n", (bio_data_dir(bio) == WRITE) ? "write" : "read", (unsigned long long) bio->bi_sector, (unsigned long long) bio->bi_sector + bio_sectors(bio) - 1); call_bio_endio(r1_bio); } free_r1bio(r1_bio);} /* * Update disk head position estimator based on IRQ completion info. */static inline void update_head_pos(int disk, struct r1bio *r1_bio){ struct r1conf *conf = r1_bio->mddev->private; conf->mirrors[disk].head_position = r1_bio->sector + (r1_bio->sectors);} /* * Find the disk number which triggered given bio */static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio){ int mirror; struct r1conf *conf = r1_bio->mddev->private; int raid_disks = conf->raid_disks; for (mirror = 0; mirror < raid_disks * 2; mirror++) if (r1_bio->bios[mirror] == bio) break; BUG_ON(mirror == raid_disks * 2); update_head_pos(mirror, r1_bio); return mirror;} static void raid1_end_read_request(struct bio *bio, int error){ int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct r1bio *r1_bio = bio->bi_private; int mirror; struct r1conf *conf = r1_bio->mddev->private; mirror = r1_bio->read_disk; /* * this branch is our 'one mirror IO has finished' event handler: */ update_head_pos(mirror, r1_bio); if (uptodate) set_bit(R1BIO_Uptodate, &r1_bio->state); else { /* If all other devices have failed, we want to return * the error upwards rather than fail the last device. * Here we redefine "uptodate" to mean "Don't want to retry" */ unsigned long flags; spin_lock_irqsave(&conf->device_lock, flags); if (r1_bio->mddev->degraded == conf->raid_disks || (r1_bio->mddev->degraded == conf->raid_disks-1 && !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))) uptodate = 1; spin_unlock_irqrestore(&conf->device_lock, flags); } if (uptodate) { raid_end_bio_io(r1_bio); rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); } else { /* * oops, read error: */ char b[BDEVNAME_SIZE]; printk_ratelimited( KERN_ERR "md/raid1:%s: %s: " "rescheduling sector %llu\n", mdname(conf->mddev), bdevname(conf->mirrors[mirror].rdev->bdev, b), (unsigned long long)r1_bio->sector); set_bit(R1BIO_ReadError, &r1_bio->state); reschedule_retry(r1_bio); /* don't drop the reference on read_disk yet */ }} static void close_write(struct r1bio *r1_bio){ /* it really is the end of this request */ if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { /* free extra copy of the data pages */ int i = r1_bio->behind_page_count; while (i--) safe_put_page(r1_bio->behind_bvecs[i].bv_page); kfree(r1_bio->behind_bvecs); r1_bio->behind_bvecs = NULL; } /* clear the bitmap if all writes complete successfully */ bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, r1_bio->sectors, !test_bit(R1BIO_Degraded, &r1_bio->state), test_bit(R1BIO_BehindIO, &r1_bio->state)); md_write_end(r1_bio->mddev);} static void r1_bio_write_done(struct r1bio *r1_bio){ if (!atomic_dec_and_test(&r1_bio->remaining)) return; if (test_bit(R1BIO_WriteError, &r1_bio->state)) reschedule_retry(r1_bio); else { close_write(r1_bio); if (test_bit(R1BIO_MadeGood, &r1_bio->state)) reschedule_retry(r1_bio); else raid_end_bio_io(r1_bio); }} static void raid1_end_write_request(struct bio *bio, int error){ int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct r1bio *r1_bio = bio->bi_private; int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state); struct r1conf *conf = r1_bio->mddev->private; struct bio *to_put = NULL; mirror = find_bio_disk(r1_bio, bio); /* * 'one mirror IO has finished' event handler: */ if (!uptodate) { set_bit(WriteErrorSeen, &conf->mirrors[mirror].rdev->flags); if (!test_and_set_bit(WantReplacement, &conf->mirrors[mirror].rdev->flags)) set_bit(MD_RECOVERY_NEEDED, & conf->mddev->recovery); set_bit(R1BIO_WriteError, &r1_bio->state); } else { /* * Set R1BIO_Uptodate in our master bio, so that we * will return a good error code for to the higher * levels even if IO on some other mirrored buffer * fails. * * The 'master' represents the composite IO operation * to user-side. So if something waits for IO, then it * will wait for the 'master' bio. */ sector_t first_bad; int bad_sectors; r1_bio->bios[mirror] = NULL; to_put = bio; /* * Do not set R1BIO_Uptodate if the current device is * rebuilding or Faulty. This is because we cannot use * such device for properly reading the data back (we could * potentially use it, if the current write would have felt * before rdev->recovery_offset, but for simplicity we don't * check this here. */ if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) && !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)) set_bit(R1BIO_Uptodate, &r1_bio->state); /* Maybe we can clear some bad blocks. */ if (is_badblock(conf->mirrors[mirror].rdev, r1_bio->sector, r1_bio->sectors, &first_bad, &bad_sectors)) { r1_bio->bios[mirror] = IO_MADE_GOOD; set_bit(R1BIO_MadeGood, &r1_bio->state); } } if (behind) { if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags)) atomic_dec(&r1_bio->behind_remaining); /* * In behind mode, we ACK the master bio once the I/O * has safely reached all non-writemostly * disks. Setting the Returned bit ensures that this * gets done only once -- we don't ever want to return * -EIO here, instead we'll wait */ if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && test_bit(R1BIO_Uptodate, &r1_bio->state)) { /* Maybe we can return now */ if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { struct bio *mbio = r1_bio->master_bio; pr_debug("raid1: behind end write sectors" " %llu-%llu\n", (unsigned long long) mbio->bi_sector, (unsigned long long) mbio->bi_sector + bio_sectors(mbio) - 1); call_bio_endio(r1_bio); } } } if (r1_bio->bios[mirror] == NULL) rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); /* * Let's see if all mirrored write operations have finished * already. */ r1_bio_write_done(r1_bio); if (to_put) bio_put(to_put);} /* * This routine returns the disk from which the requested read should * be done. There is a per-array 'next expected sequential IO' sector * number - if this matches on the next IO then we use the last disk. * There is also a per-disk 'last know head position' sector that is * maintained from IRQ contexts, both the normal and the resync IO * completion handlers update this position correctly. If there is no * perfect sequential match then we pick the disk whose head is closest. * * If there are 2 mirrors in the same 2 devices, performance degradesLines 1–500 of 3200. Load more
/* * raid1.c : Multiple Devices driver for Linux * * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat * * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman * * RAID-1 management functions. * * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 * * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> * * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support * bitmapped intelligence in resync: * * - bitmap marked during normal i/o * - bitmap used to skip nondirty blocks during sync * * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: * - persistent bitmap code * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * You should have received a copy of the GNU General Public License * (for example /usr/src/linux/COPYING); if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/slab.h> #include <linux/delay.h> #include <linux/blkdev.h> #include <linux/module.h> #include <linux/seq_file.h> #include <linux/ratelimit.h> #include "md.h" #include "raid1.h" #include "bitmap.h" /* * Number of guaranteed r1bios in case of extreme VM load: */ #define NR_RAID1_BIOS 256 /* when we get a read error on a read-only array, we redirect to another * device without failing the first device, or trying to over-write to * correct the read error. To keep track of bad blocks on a per-bio * level, we store IO_BLOCKED in the appropriate 'bios' pointer */ #define IO_BLOCKED ((struct bio *)1) /* When we successfully write to a known bad-block, we need to remove the * bad-block marking which must be done from process context. So we record * the success by setting devs[n].bio to IO_MADE_GOOD */ #define IO_MADE_GOOD ((struct bio *)2) #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) /* When there are this many requests queue to be written by * the raid1 thread, we become 'congested' to provide back-pressure * for writeback. */ static int max_queued_requests = 1024; static void allow_barrier(struct r1conf *conf, sector_t start_next_window, sector_t bi_sector); static void lower_barrier(struct r1conf *conf); static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data) { struct pool_info *pi = data; int size = offsetof(struct r1bio, bios[pi->raid_disks]); /* allocate a r1bio with room for raid_disks entries in the bios array */ return kzalloc(size, gfp_flags); } static void r1bio_pool_free(void *r1_bio, void *data) { kfree(r1_bio); } #define RESYNC_BLOCK_SIZE (64*1024) #define RESYNC_DEPTH 32 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH) #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9) #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS) static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) { struct pool_info *pi = data; struct r1bio *r1_bio; struct bio *bio; int i, j; r1_bio = r1bio_pool_alloc(gfp_flags, pi); if (!r1_bio) return NULL; /* * Allocate bios : 1 for reading, n-1 for writing */ for (j = pi->raid_disks ; j-- ; ) { bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); if (!bio) goto out_free_bio; r1_bio->bios[j] = bio; } /* * Allocate RESYNC_PAGES data pages and attach them to * the first bio. * If this is a user-requested check/repair, allocate * RESYNC_PAGES for each bio. */ if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) j = pi->raid_disks; else j = 1; while(j--) { bio = r1_bio->bios[j]; bio->bi_vcnt = RESYNC_PAGES; if (bio_alloc_pages(bio, gfp_flags)) goto out_free_bio; } /* If not user-requests, copy the page pointers to all bios */ if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) { for (i=0; i<RESYNC_PAGES ; i++) for (j=1; j<pi->raid_disks; j++) r1_bio->bios[j]->bi_io_vec[i].bv_page = r1_bio->bios[0]->bi_io_vec[i].bv_page; } r1_bio->master_bio = NULL; return r1_bio; out_free_bio: while (++j < pi->raid_disks) bio_put(r1_bio->bios[j]); r1bio_pool_free(r1_bio, data); return NULL; } static void r1buf_pool_free(void *__r1_bio, void *data) { struct pool_info *pi = data; int i,j; struct r1bio *r1bio = __r1_bio; for (i = 0; i < RESYNC_PAGES; i++) for (j = pi->raid_disks; j-- ;) { if (j == 0 || r1bio->bios[j]->bi_io_vec[i].bv_page != r1bio->bios[0]->bi_io_vec[i].bv_page) safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page); } for (i=0 ; i < pi->raid_disks; i++) bio_put(r1bio->bios[i]); r1bio_pool_free(r1bio, data); } static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio) { int i; for (i = 0; i < conf->raid_disks * 2; i++) { struct bio **bio = r1_bio->bios + i; if (!BIO_SPECIAL(*bio)) bio_put(*bio); *bio = NULL; } } static void free_r1bio(struct r1bio *r1_bio) { struct r1conf *conf = r1_bio->mddev->private; put_all_bios(conf, r1_bio); mempool_free(r1_bio, conf->r1bio_pool); } static void put_buf(struct r1bio *r1_bio) { struct r1conf *conf = r1_bio->mddev->private; int i; for (i = 0; i < conf->raid_disks * 2; i++) { struct bio *bio = r1_bio->bios[i]; if (bio->bi_end_io) rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev); } mempool_free(r1_bio, conf->r1buf_pool); lower_barrier(conf); } static void reschedule_retry(struct r1bio *r1_bio) { unsigned long flags; struct mddev *mddev = r1_bio->mddev; struct r1conf *conf = mddev->private; spin_lock_irqsave(&conf->device_lock, flags); list_add(&r1_bio->retry_list, &conf->retry_list); conf->nr_queued ++; spin_unlock_irqrestore(&conf->device_lock, flags); wake_up(&conf->wait_barrier); md_wakeup_thread(mddev->thread); } /* * raid_end_bio_io() is called when we have finished servicing a mirrored * operation and are ready to return a success/failure code to the buffer * cache layer. */ static void call_bio_endio(struct r1bio *r1_bio) { struct bio *bio = r1_bio->master_bio; int done; struct r1conf *conf = r1_bio->mddev->private; sector_t start_next_window = r1_bio->start_next_window; sector_t bi_sector = bio->bi_sector; if (bio->bi_phys_segments) { unsigned long flags; spin_lock_irqsave(&conf->device_lock, flags); bio->bi_phys_segments--; done = (bio->bi_phys_segments == 0); spin_unlock_irqrestore(&conf->device_lock, flags); /* * make_request() might be waiting for * bi_phys_segments to decrease */ wake_up(&conf->wait_barrier); } else done = 1; if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) clear_bit(BIO_UPTODATE, &bio->bi_flags); if (done) { bio_endio(bio, 0); /* * Wake up any possible resync thread that waits for the device * to go idle. */ allow_barrier(conf, start_next_window, bi_sector); } } static void raid_end_bio_io(struct r1bio *r1_bio) { struct bio *bio = r1_bio->master_bio; /* if nobody has done the final endio yet, do it now */ if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { pr_debug("raid1: sync end %s on sectors %llu-%llu\n", (bio_data_dir(bio) == WRITE) ? "write" : "read", (unsigned long long) bio->bi_sector, (unsigned long long) bio->bi_sector + bio_sectors(bio) - 1); call_bio_endio(r1_bio); } free_r1bio(r1_bio); } /* * Update disk head position estimator based on IRQ completion info. */ static inline void update_head_pos(int disk, struct r1bio *r1_bio) { struct r1conf *conf = r1_bio->mddev->private; conf->mirrors[disk].head_position = r1_bio->sector + (r1_bio->sectors); } /* * Find the disk number which triggered given bio */ static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio) { int mirror; struct r1conf *conf = r1_bio->mddev->private; int raid_disks = conf->raid_disks; for (mirror = 0; mirror < raid_disks * 2; mirror++) if (r1_bio->bios[mirror] == bio) break; BUG_ON(mirror == raid_disks * 2); update_head_pos(mirror, r1_bio); return mirror; } static void raid1_end_read_request(struct bio *bio, int error) { int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct r1bio *r1_bio = bio->bi_private; int mirror; struct r1conf *conf = r1_bio->mddev->private; mirror = r1_bio->read_disk; /* * this branch is our 'one mirror IO has finished' event handler: */ update_head_pos(mirror, r1_bio); if (uptodate) set_bit(R1BIO_Uptodate, &r1_bio->state); else { /* If all other devices have failed, we want to return * the error upwards rather than fail the last device. * Here we redefine "uptodate" to mean "Don't want to retry" */ unsigned long flags; spin_lock_irqsave(&conf->device_lock, flags); if (r1_bio->mddev->degraded == conf->raid_disks || (r1_bio->mddev->degraded == conf->raid_disks-1 && !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))) uptodate = 1; spin_unlock_irqrestore(&conf->device_lock, flags); } if (uptodate) { raid_end_bio_io(r1_bio); rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); } else { /* * oops, read error: */ char b[BDEVNAME_SIZE]; printk_ratelimited( KERN_ERR "md/raid1:%s: %s: " "rescheduling sector %llu\n", mdname(conf->mddev), bdevname(conf->mirrors[mirror].rdev->bdev, b), (unsigned long long)r1_bio->sector); set_bit(R1BIO_ReadError, &r1_bio->state); reschedule_retry(r1_bio); /* don't drop the reference on read_disk yet */ } } static void close_write(struct r1bio *r1_bio) { /* it really is the end of this request */ if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { /* free extra copy of the data pages */ int i = r1_bio->behind_page_count; while (i--) safe_put_page(r1_bio->behind_bvecs[i].bv_page); kfree(r1_bio->behind_bvecs); r1_bio->behind_bvecs = NULL; } /* clear the bitmap if all writes complete successfully */ bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, r1_bio->sectors, !test_bit(R1BIO_Degraded, &r1_bio->state), test_bit(R1BIO_BehindIO, &r1_bio->state)); md_write_end(r1_bio->mddev); } static void r1_bio_write_done(struct r1bio *r1_bio) { if (!atomic_dec_and_test(&r1_bio->remaining)) return; if (test_bit(R1BIO_WriteError, &r1_bio->state)) reschedule_retry(r1_bio); else { close_write(r1_bio); if (test_bit(R1BIO_MadeGood, &r1_bio->state)) reschedule_retry(r1_bio); else raid_end_bio_io(r1_bio); } } static void raid1_end_write_request(struct bio *bio, int error) { int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct r1bio *r1_bio = bio->bi_private; int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state); struct r1conf *conf = r1_bio->mddev->private; struct bio *to_put = NULL; mirror = find_bio_disk(r1_bio, bio); /* * 'one mirror IO has finished' event handler: */ if (!uptodate) { set_bit(WriteErrorSeen, &conf->mirrors[mirror].rdev->flags); if (!test_and_set_bit(WantReplacement, &conf->mirrors[mirror].rdev->flags)) set_bit(MD_RECOVERY_NEEDED, & conf->mddev->recovery); set_bit(R1BIO_WriteError, &r1_bio->state); } else { /* * Set R1BIO_Uptodate in our master bio, so that we * will return a good error code for to the higher * levels even if IO on some other mirrored buffer * fails. * * The 'master' represents the composite IO operation * to user-side. So if something waits for IO, then it * will wait for the 'master' bio. */ sector_t first_bad; int bad_sectors; r1_bio->bios[mirror] = NULL; to_put = bio; /* * Do not set R1BIO_Uptodate if the current device is * rebuilding or Faulty. This is because we cannot use * such device for properly reading the data back (we could * potentially use it, if the current write would have felt * before rdev->recovery_offset, but for simplicity we don't * check this here. */ if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) && !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)) set_bit(R1BIO_Uptodate, &r1_bio->state); /* Maybe we can clear some bad blocks. */ if (is_badblock(conf->mirrors[mirror].rdev, r1_bio->sector, r1_bio->sectors, &first_bad, &bad_sectors)) { r1_bio->bios[mirror] = IO_MADE_GOOD; set_bit(R1BIO_MadeGood, &r1_bio->state); } } if (behind) { if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags)) atomic_dec(&r1_bio->behind_remaining); /* * In behind mode, we ACK the master bio once the I/O * has safely reached all non-writemostly * disks. Setting the Returned bit ensures that this * gets done only once -- we don't ever want to return * -EIO here, instead we'll wait */ if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && test_bit(R1BIO_Uptodate, &r1_bio->state)) { /* Maybe we can return now */ if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { struct bio *mbio = r1_bio->master_bio; pr_debug("raid1: behind end write sectors" " %llu-%llu\n", (unsigned long long) mbio->bi_sector, (unsigned long long) mbio->bi_sector + bio_sectors(mbio) - 1); call_bio_endio(r1_bio); } } } if (r1_bio->bios[mirror] == NULL) rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); /* * Let's see if all mirrored write operations have finished * already. */ r1_bio_write_done(r1_bio); if (to_put) bio_put(to_put); } /* * This routine returns the disk from which the requested read should * be done. There is a per-array 'next expected sequential IO' sector * number - if this matches on the next IO then we use the last disk. * There is also a per-disk 'last know head position' sector that is * maintained from IRQ contexts, both the normal and the resync IO * completion handlers update this position correctly. If there is no * perfect sequential match then we pick the disk whose head is closest. * * If there are 2 mirrors in the same 2 devices, performance degrades