Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid1.c : Multiple Devices driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat |
| 5 | * |
| 6 | * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 7 | * |
| 8 | * RAID-1 management functions. |
| 9 | * |
| 10 | * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 |
| 11 | * |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 12 | * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> |
| 14 | * |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 15 | * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support |
| 16 | * bitmapped intelligence in resync: |
| 17 | * |
| 18 | * - bitmap marked during normal i/o |
| 19 | * - bitmap used to skip nondirty blocks during sync |
| 20 | * |
| 21 | * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: |
| 22 | * - persistent bitmap code |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License as published by |
| 26 | * the Free Software Foundation; either version 2, or (at your option) |
| 27 | * any later version. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * (for example /usr/src/linux/COPYING); if not, write to the Free |
| 31 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 32 | */ |
| 33 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 35 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 37 | #include <linux/seq_file.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 38 | #include <linux/ratelimit.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 39 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 40 | #include "raid1.h" |
| 41 | #include "bitmap.h" |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 42 | |
| 43 | #define DEBUG 0 |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 44 | #define PRINTK(x...) do { if (DEBUG) printk(x); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Number of guaranteed r1bios in case of extreme VM load: |
| 48 | */ |
| 49 | #define NR_RAID1_BIOS 256 |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 52 | static void allow_barrier(conf_t *conf); |
| 53 | static void lower_barrier(conf_t *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 55 | static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | struct pool_info *pi = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | int size = offsetof(r1bio_t, bios[pi->raid_disks]); |
| 59 | |
| 60 | /* allocate a r1bio with room for raid_disks entries in the bios array */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 61 | return kzalloc(size, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void r1bio_pool_free(void *r1_bio, void *data) |
| 65 | { |
| 66 | kfree(r1_bio); |
| 67 | } |
| 68 | |
| 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) |
| 74 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 75 | static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | struct pool_info *pi = data; |
| 78 | struct page *page; |
| 79 | r1bio_t *r1_bio; |
| 80 | struct bio *bio; |
| 81 | int i, j; |
| 82 | |
| 83 | r1_bio = r1bio_pool_alloc(gfp_flags, pi); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 84 | if (!r1_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Allocate bios : 1 for reading, n-1 for writing |
| 89 | */ |
| 90 | for (j = pi->raid_disks ; j-- ; ) { |
NeilBrown | 6746557 | 2010-10-26 17:33:54 +1100 | [diff] [blame] | 91 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | if (!bio) |
| 93 | goto out_free_bio; |
| 94 | r1_bio->bios[j] = bio; |
| 95 | } |
| 96 | /* |
| 97 | * Allocate RESYNC_PAGES data pages and attach them to |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 98 | * the first bio. |
| 99 | * If this is a user-requested check/repair, allocate |
| 100 | * RESYNC_PAGES for each bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | */ |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 113 | bio->bi_io_vec[i].bv_page = page; |
NeilBrown | 303a0e1 | 2009-04-06 14:40:38 +1000 | [diff] [blame] | 114 | bio->bi_vcnt = i+1; |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | r1_bio->master_bio = NULL; |
| 126 | |
| 127 | return r1_bio; |
| 128 | |
| 129 | out_free_pages: |
NeilBrown | 303a0e1 | 2009-04-06 14:40:38 +1000 | [diff] [blame] | 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); |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 133 | j = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 139 | } |
| 140 | |
| 141 | static void r1buf_pool_free(void *__r1_bio, void *data) |
| 142 | { |
| 143 | struct pool_info *pi = data; |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 144 | int i,j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | r1bio_t *r1bio = __r1_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 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) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 152 | safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page); |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | for (i=0 ; i < pi->raid_disks; i++) |
| 155 | bio_put(r1bio->bios[i]); |
| 156 | |
| 157 | r1bio_pool_free(r1bio, data); |
| 158 | } |
| 159 | |
| 160 | static void put_all_bios(conf_t *conf, r1bio_t *r1_bio) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < conf->raid_disks; i++) { |
| 165 | struct bio **bio = r1_bio->bios + i; |
NeilBrown | cf30a47 | 2006-01-06 00:20:23 -0800 | [diff] [blame] | 166 | if (*bio && *bio != IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | bio_put(*bio); |
| 168 | *bio = NULL; |
| 169 | } |
| 170 | } |
| 171 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 172 | static void free_r1bio(r1bio_t *r1_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 174 | conf_t *conf = r1_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | put_all_bios(conf, r1_bio); |
| 177 | mempool_free(r1_bio, conf->r1bio_pool); |
| 178 | } |
| 179 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 180 | static void put_buf(r1bio_t *r1_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 182 | conf_t *conf = r1_bio->mddev->private; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 183 | int i; |
| 184 | |
| 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); |
| 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | mempool_free(r1_bio, conf->r1buf_pool); |
| 192 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 193 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void reschedule_retry(r1bio_t *r1_bio) |
| 197 | { |
| 198 | unsigned long flags; |
| 199 | mddev_t *mddev = r1_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 200 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | spin_lock_irqsave(&conf->device_lock, flags); |
| 203 | list_add(&r1_bio->retry_list, &conf->retry_list); |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 204 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 206 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 207 | wake_up(&conf->wait_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | md_wakeup_thread(mddev->thread); |
| 209 | } |
| 210 | |
| 211 | /* |
| 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. |
| 215 | */ |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 216 | static void call_bio_endio(r1bio_t *r1_bio) |
| 217 | { |
| 218 | struct bio *bio = r1_bio->master_bio; |
| 219 | int done; |
| 220 | conf_t *conf = r1_bio->mddev->private; |
| 221 | |
| 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; |
| 230 | |
| 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); |
| 235 | /* |
| 236 | * Wake up any possible resync thread that waits for the device |
| 237 | * to go idle. |
| 238 | */ |
| 239 | allow_barrier(conf); |
| 240 | } |
| 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static void raid_end_bio_io(r1bio_t *r1_bio) |
| 244 | { |
| 245 | struct bio *bio = r1_bio->master_bio; |
| 246 | |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 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); |
| 254 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 255 | call_bio_endio(r1_bio); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | free_r1bio(r1_bio); |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Update disk head position estimator based on IRQ completion info. |
| 262 | */ |
| 263 | static inline void update_head_pos(int disk, r1bio_t *r1_bio) |
| 264 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 265 | conf_t *conf = r1_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | conf->mirrors[disk].head_position = |
| 268 | r1_bio->sector + (r1_bio->sectors); |
| 269 | } |
| 270 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 271 | static void raid1_end_read_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 274 | r1bio_t *r1_bio = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | int mirror; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 276 | conf_t *conf = r1_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | mirror = r1_bio->read_disk; |
| 279 | /* |
| 280 | * this branch is our 'one mirror IO has finished' event handler: |
| 281 | */ |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 282 | update_head_pos(mirror, r1_bio); |
| 283 | |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | */ |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 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); |
| 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 300 | if (uptodate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | raid_end_bio_io(r1_bio); |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 302 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* |
| 304 | * oops, read error: |
| 305 | */ |
| 306 | char b[BDEVNAME_SIZE]; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 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, |
| 312 | b), |
| 313 | (unsigned long long)r1_bio->sector); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 314 | set_bit(R1BIO_ReadError, &r1_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | reschedule_retry(r1_bio); |
| 316 | } |
| 317 | |
| 318 | rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 321 | static void r1_bio_write_done(r1bio_t *r1_bio) |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 322 | { |
| 323 | if (atomic_dec_and_test(&r1_bio->remaining)) |
| 324 | { |
| 325 | /* it really is the end of this request */ |
| 326 | if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { |
| 327 | /* free extra copy of the data pages */ |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 328 | int i = r1_bio->behind_page_count; |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 329 | while (i--) |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 330 | safe_put_page(r1_bio->behind_pages[i]); |
| 331 | kfree(r1_bio->behind_pages); |
| 332 | r1_bio->behind_pages = NULL; |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 333 | } |
| 334 | /* clear the bitmap if all writes complete successfully */ |
| 335 | bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, |
| 336 | r1_bio->sectors, |
| 337 | !test_bit(R1BIO_Degraded, &r1_bio->state), |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 338 | test_bit(R1BIO_BehindIO, &r1_bio->state)); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 339 | md_write_end(r1_bio->mddev); |
| 340 | raid_end_bio_io(r1_bio); |
| 341 | } |
| 342 | } |
| 343 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 344 | static void raid1_end_write_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 347 | r1bio_t *r1_bio = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 348 | int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state); |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 349 | conf_t *conf = r1_bio->mddev->private; |
NeilBrown | 04b857f | 2006-03-09 17:33:46 -0800 | [diff] [blame] | 350 | struct bio *to_put = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | for (mirror = 0; mirror < conf->raid_disks; mirror++) |
| 354 | if (r1_bio->bios[mirror] == bio) |
| 355 | break; |
| 356 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 357 | /* |
| 358 | * 'one mirror IO has finished' event handler: |
| 359 | */ |
| 360 | r1_bio->bios[mirror] = NULL; |
| 361 | to_put = bio; |
| 362 | if (!uptodate) { |
| 363 | md_error(r1_bio->mddev, conf->mirrors[mirror].rdev); |
| 364 | /* an I/O failed, we can't clear the bitmap */ |
| 365 | set_bit(R1BIO_Degraded, &r1_bio->state); |
| 366 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 368 | * Set R1BIO_Uptodate in our master bio, so that we |
| 369 | * will return a good error code for to the higher |
| 370 | * levels even if IO on some other mirrored buffer |
| 371 | * fails. |
| 372 | * |
| 373 | * The 'master' represents the composite IO operation |
| 374 | * to user-side. So if something waits for IO, then it |
| 375 | * will wait for the 'master' bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 377 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 379 | update_head_pos(mirror, r1_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 381 | if (behind) { |
| 382 | if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags)) |
| 383 | atomic_dec(&r1_bio->behind_remaining); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 384 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 385 | /* |
| 386 | * In behind mode, we ACK the master bio once the I/O |
| 387 | * has safely reached all non-writemostly |
| 388 | * disks. Setting the Returned bit ensures that this |
| 389 | * gets done only once -- we don't ever want to return |
| 390 | * -EIO here, instead we'll wait |
| 391 | */ |
| 392 | if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && |
| 393 | test_bit(R1BIO_Uptodate, &r1_bio->state)) { |
| 394 | /* Maybe we can return now */ |
| 395 | if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { |
| 396 | struct bio *mbio = r1_bio->master_bio; |
| 397 | PRINTK(KERN_DEBUG "raid1: behind end write sectors %llu-%llu\n", |
| 398 | (unsigned long long) mbio->bi_sector, |
| 399 | (unsigned long long) mbio->bi_sector + |
| 400 | (mbio->bi_size >> 9) - 1); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 401 | call_bio_endio(r1_bio); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 405 | rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | * Let's see if all mirrored write operations have finished |
| 409 | * already. |
| 410 | */ |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 411 | r1_bio_write_done(r1_bio); |
NeilBrown | c70810b | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 412 | |
NeilBrown | 04b857f | 2006-03-09 17:33:46 -0800 | [diff] [blame] | 413 | if (to_put) |
| 414 | bio_put(to_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | /* |
| 419 | * This routine returns the disk from which the requested read should |
| 420 | * be done. There is a per-array 'next expected sequential IO' sector |
| 421 | * number - if this matches on the next IO then we use the last disk. |
| 422 | * There is also a per-disk 'last know head position' sector that is |
| 423 | * maintained from IRQ contexts, both the normal and the resync IO |
| 424 | * completion handlers update this position correctly. If there is no |
| 425 | * perfect sequential match then we pick the disk whose head is closest. |
| 426 | * |
| 427 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 428 | * because position is mirror, not device based. |
| 429 | * |
| 430 | * The rdev for the device selected will have nr_pending incremented. |
| 431 | */ |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 432 | static int read_balance(conf_t *conf, r1bio_t *r1_bio, int *max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
NeilBrown | af3a2cd | 2010-05-08 08:20:17 +1000 | [diff] [blame] | 434 | const sector_t this_sector = r1_bio->sector; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 435 | int sectors; |
| 436 | int best_good_sectors; |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 437 | int start_disk; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 438 | int best_disk; |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 439 | int i; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 440 | sector_t best_dist; |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 441 | mdk_rdev_t *rdev; |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 442 | int choose_first; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | rcu_read_lock(); |
| 445 | /* |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 446 | * Check if we can balance. We can balance on the whole |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | * device if no resync is going on, or below the resync window. |
| 448 | * We take the first readable disk when above the resync window. |
| 449 | */ |
| 450 | retry: |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 451 | sectors = r1_bio->sectors; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 452 | best_disk = -1; |
| 453 | best_dist = MaxSector; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 454 | best_good_sectors = 0; |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (conf->mddev->recovery_cp < MaxSector && |
| 457 | (this_sector + sectors >= conf->next_resync)) { |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 458 | choose_first = 1; |
| 459 | start_disk = 0; |
| 460 | } else { |
| 461 | choose_first = 0; |
| 462 | start_disk = conf->last_used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 465 | for (i = 0 ; i < conf->raid_disks ; i++) { |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 466 | sector_t dist; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 467 | sector_t first_bad; |
| 468 | int bad_sectors; |
| 469 | |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 470 | int disk = start_disk + i; |
| 471 | if (disk >= conf->raid_disks) |
| 472 | disk -= conf->raid_disks; |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 473 | |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 474 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
| 475 | if (r1_bio->bios[disk] == IO_BLOCKED |
| 476 | || rdev == NULL |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 477 | || test_bit(Faulty, &rdev->flags)) |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 478 | continue; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 479 | if (!test_bit(In_sync, &rdev->flags) && |
| 480 | rdev->recovery_offset < this_sector + sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | continue; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 482 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 483 | /* Don't balance among write-mostly, just |
| 484 | * use the first as a last resort */ |
| 485 | if (best_disk < 0) |
| 486 | best_disk = disk; |
| 487 | continue; |
| 488 | } |
| 489 | /* This is a reasonable device to use. It might |
| 490 | * even be best. |
| 491 | */ |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 492 | if (is_badblock(rdev, this_sector, sectors, |
| 493 | &first_bad, &bad_sectors)) { |
| 494 | if (best_dist < MaxSector) |
| 495 | /* already have a better device */ |
| 496 | continue; |
| 497 | if (first_bad <= this_sector) { |
| 498 | /* cannot read here. If this is the 'primary' |
| 499 | * device, then we must not read beyond |
| 500 | * bad_sectors from another device.. |
| 501 | */ |
| 502 | bad_sectors -= (this_sector - first_bad); |
| 503 | if (choose_first && sectors > bad_sectors) |
| 504 | sectors = bad_sectors; |
| 505 | if (best_good_sectors > sectors) |
| 506 | best_good_sectors = sectors; |
| 507 | |
| 508 | } else { |
| 509 | sector_t good_sectors = first_bad - this_sector; |
| 510 | if (good_sectors > best_good_sectors) { |
| 511 | best_good_sectors = good_sectors; |
| 512 | best_disk = disk; |
| 513 | } |
| 514 | if (choose_first) |
| 515 | break; |
| 516 | } |
| 517 | continue; |
| 518 | } else |
| 519 | best_good_sectors = sectors; |
| 520 | |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 521 | dist = abs(this_sector - conf->mirrors[disk].head_position); |
| 522 | if (choose_first |
| 523 | /* Don't change to another disk for sequential reads */ |
| 524 | || conf->next_seq_sect == this_sector |
| 525 | || dist == 0 |
| 526 | /* If device is idle, use it */ |
| 527 | || atomic_read(&rdev->nr_pending) == 0) { |
| 528 | best_disk = disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | break; |
| 530 | } |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 531 | if (dist < best_dist) { |
| 532 | best_dist = dist; |
| 533 | best_disk = disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
NeilBrown | f3ac8bf | 2010-09-06 14:10:08 +1000 | [diff] [blame] | 535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 537 | if (best_disk >= 0) { |
| 538 | rdev = rcu_dereference(conf->mirrors[best_disk].rdev); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 539 | if (!rdev) |
| 540 | goto retry; |
| 541 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 542 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /* cannot risk returning a device that failed |
| 544 | * before we inc'ed nr_pending |
| 545 | */ |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 546 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | goto retry; |
| 548 | } |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 549 | sectors = best_good_sectors; |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 550 | conf->next_seq_sect = this_sector + sectors; |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 551 | conf->last_used = best_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | rcu_read_unlock(); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 554 | *max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
NeilBrown | 7607305 | 2011-05-11 14:34:56 +1000 | [diff] [blame] | 556 | return best_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 559 | int md_raid1_congested(mddev_t *mddev, int bits) |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 560 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 561 | conf_t *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 562 | int i, ret = 0; |
| 563 | |
| 564 | rcu_read_lock(); |
| 565 | for (i = 0; i < mddev->raid_disks; i++) { |
| 566 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 567 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 568 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 569 | |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 570 | BUG_ON(!q); |
| 571 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 572 | /* Note the '|| 1' - when read_balance prefers |
| 573 | * non-congested targets, it can be removed |
| 574 | */ |
Alexander Beregalov | 91a9e99 | 2009-04-07 01:35:56 +0400 | [diff] [blame] | 575 | if ((bits & (1<<BDI_async_congested)) || 1) |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 576 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 577 | else |
| 578 | ret &= bdi_congested(&q->backing_dev_info, bits); |
| 579 | } |
| 580 | } |
| 581 | rcu_read_unlock(); |
| 582 | return ret; |
| 583 | } |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 584 | EXPORT_SYMBOL_GPL(md_raid1_congested); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 585 | |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 586 | static int raid1_congested(void *data, int bits) |
| 587 | { |
| 588 | mddev_t *mddev = data; |
| 589 | |
| 590 | return mddev_congested(mddev, bits) || |
| 591 | md_raid1_congested(mddev, bits); |
| 592 | } |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 593 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 594 | static void flush_pending_writes(conf_t *conf) |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 595 | { |
| 596 | /* Any writes that have been queued but are awaiting |
| 597 | * bitmap updates get flushed here. |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 598 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 599 | spin_lock_irq(&conf->device_lock); |
| 600 | |
| 601 | if (conf->pending_bio_list.head) { |
| 602 | struct bio *bio; |
| 603 | bio = bio_list_get(&conf->pending_bio_list); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 604 | spin_unlock_irq(&conf->device_lock); |
| 605 | /* flush any pending bitmap writes to |
| 606 | * disk before proceeding w/ I/O */ |
| 607 | bitmap_unplug(conf->mddev->bitmap); |
| 608 | |
| 609 | while (bio) { /* submit pending writes */ |
| 610 | struct bio *next = bio->bi_next; |
| 611 | bio->bi_next = NULL; |
| 612 | generic_make_request(bio); |
| 613 | bio = next; |
| 614 | } |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 615 | } else |
| 616 | spin_unlock_irq(&conf->device_lock); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 617 | } |
| 618 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 619 | /* Barriers.... |
| 620 | * Sometimes we need to suspend IO while we do something else, |
| 621 | * either some resync/recovery, or reconfigure the array. |
| 622 | * To do this we raise a 'barrier'. |
| 623 | * The 'barrier' is a counter that can be raised multiple times |
| 624 | * to count how many activities are happening which preclude |
| 625 | * normal IO. |
| 626 | * We can only raise the barrier if there is no pending IO. |
| 627 | * i.e. if nr_pending == 0. |
| 628 | * We choose only to raise the barrier if no-one is waiting for the |
| 629 | * barrier to go down. This means that as soon as an IO request |
| 630 | * is ready, no other operations which require a barrier will start |
| 631 | * until the IO request has had a chance. |
| 632 | * |
| 633 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 634 | * is no backgroup IO happening, It must arrange to call |
| 635 | * allow_barrier when it has finished its IO. |
| 636 | * backgroup IO calls must call raise_barrier. Once that returns |
| 637 | * there is no normal IO happeing. It must arrange to call |
| 638 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | */ |
| 640 | #define RESYNC_DEPTH 32 |
| 641 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 642 | static void raise_barrier(conf_t *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | spin_lock_irq(&conf->resync_lock); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 645 | |
| 646 | /* Wait until no block IO is waiting */ |
| 647 | wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 648 | conf->resync_lock, ); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 649 | |
| 650 | /* block any new IO from starting */ |
| 651 | conf->barrier++; |
| 652 | |
NeilBrown | 046abee | 2010-10-26 15:46:20 +1100 | [diff] [blame] | 653 | /* Now wait for all pending IO to complete */ |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 654 | wait_event_lock_irq(conf->wait_barrier, |
| 655 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 656 | conf->resync_lock, ); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | spin_unlock_irq(&conf->resync_lock); |
| 659 | } |
| 660 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 661 | static void lower_barrier(conf_t *conf) |
| 662 | { |
| 663 | unsigned long flags; |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 664 | BUG_ON(conf->barrier <= 0); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 665 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 666 | conf->barrier--; |
| 667 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 668 | wake_up(&conf->wait_barrier); |
| 669 | } |
| 670 | |
| 671 | static void wait_barrier(conf_t *conf) |
| 672 | { |
| 673 | spin_lock_irq(&conf->resync_lock); |
| 674 | if (conf->barrier) { |
| 675 | conf->nr_waiting++; |
| 676 | wait_event_lock_irq(conf->wait_barrier, !conf->barrier, |
| 677 | conf->resync_lock, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 678 | ); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 679 | conf->nr_waiting--; |
| 680 | } |
| 681 | conf->nr_pending++; |
| 682 | spin_unlock_irq(&conf->resync_lock); |
| 683 | } |
| 684 | |
| 685 | static void allow_barrier(conf_t *conf) |
| 686 | { |
| 687 | unsigned long flags; |
| 688 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 689 | conf->nr_pending--; |
| 690 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 691 | wake_up(&conf->wait_barrier); |
| 692 | } |
| 693 | |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 694 | static void freeze_array(conf_t *conf) |
| 695 | { |
| 696 | /* stop syncio and normal IO and wait for everything to |
| 697 | * go quite. |
| 698 | * We increment barrier and nr_waiting, and then |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 699 | * wait until nr_pending match nr_queued+1 |
| 700 | * This is called in the context of one normal IO request |
| 701 | * that has failed. Thus any sync request that might be pending |
| 702 | * will be blocked by nr_pending, and we need to wait for |
| 703 | * pending IO requests to complete or be queued for re-try. |
| 704 | * Thus the number queued (nr_queued) plus this request (1) |
| 705 | * must match the number of pending IOs (nr_pending) before |
| 706 | * we continue. |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 707 | */ |
| 708 | spin_lock_irq(&conf->resync_lock); |
| 709 | conf->barrier++; |
| 710 | conf->nr_waiting++; |
| 711 | wait_event_lock_irq(conf->wait_barrier, |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 712 | conf->nr_pending == conf->nr_queued+1, |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 713 | conf->resync_lock, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 714 | flush_pending_writes(conf)); |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 715 | spin_unlock_irq(&conf->resync_lock); |
| 716 | } |
| 717 | static void unfreeze_array(conf_t *conf) |
| 718 | { |
| 719 | /* reverse the effect of the freeze */ |
| 720 | spin_lock_irq(&conf->resync_lock); |
| 721 | conf->barrier--; |
| 722 | conf->nr_waiting--; |
| 723 | wake_up(&conf->wait_barrier); |
| 724 | spin_unlock_irq(&conf->resync_lock); |
| 725 | } |
| 726 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 727 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 728 | /* duplicate the data pages for behind I/O |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 729 | */ |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 730 | static void alloc_behind_pages(struct bio *bio, r1bio_t *r1_bio) |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 731 | { |
| 732 | int i; |
| 733 | struct bio_vec *bvec; |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 734 | struct page **pages = kzalloc(bio->bi_vcnt * sizeof(struct page*), |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 735 | GFP_NOIO); |
| 736 | if (unlikely(!pages)) |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 737 | return; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 738 | |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 739 | bio_for_each_segment(bvec, bio, i) { |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 740 | pages[i] = alloc_page(GFP_NOIO); |
| 741 | if (unlikely(!pages[i])) |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 742 | goto do_sync_io; |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 743 | memcpy(kmap(pages[i]) + bvec->bv_offset, |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 744 | kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len); |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 745 | kunmap(pages[i]); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 746 | kunmap(bvec->bv_page); |
| 747 | } |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 748 | r1_bio->behind_pages = pages; |
| 749 | r1_bio->behind_page_count = bio->bi_vcnt; |
| 750 | set_bit(R1BIO_BehindIO, &r1_bio->state); |
| 751 | return; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 752 | |
| 753 | do_sync_io: |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 754 | for (i = 0; i < bio->bi_vcnt; i++) |
| 755 | if (pages[i]) |
| 756 | put_page(pages[i]); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 757 | kfree(pages); |
| 758 | PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 759 | } |
| 760 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 761 | static int make_request(mddev_t *mddev, struct bio * bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 763 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | mirror_info_t *mirror; |
| 765 | r1bio_t *r1_bio; |
| 766 | struct bio *read_bio; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 767 | int i, targets = 0, disks; |
NeilBrown | 84255d1 | 2008-05-23 13:04:32 -0700 | [diff] [blame] | 768 | struct bitmap *bitmap; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 769 | unsigned long flags; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 770 | const int rw = bio_data_dir(bio); |
NeilBrown | 2c7d46e | 2010-08-18 16:16:05 +1000 | [diff] [blame] | 771 | const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 772 | const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA)); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 773 | mdk_rdev_t *blocked_rdev; |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 774 | int plugged; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | /* |
| 777 | * Register the new request and wait if the reconstruction |
| 778 | * thread has put up a bar for new requests. |
| 779 | * Continue immediately if no resync is active currently. |
| 780 | */ |
NeilBrown | 62de608 | 2006-05-01 12:15:47 -0700 | [diff] [blame] | 781 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 782 | md_write_start(mddev, bio); /* wait on superblock update early */ |
| 783 | |
NeilBrown | 6eef4b2 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 784 | if (bio_data_dir(bio) == WRITE && |
| 785 | bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo && |
| 786 | bio->bi_sector < mddev->suspend_hi) { |
| 787 | /* As the suspend_* range is controlled by |
| 788 | * userspace, we want an interruptible |
| 789 | * wait. |
| 790 | */ |
| 791 | DEFINE_WAIT(w); |
| 792 | for (;;) { |
| 793 | flush_signals(current); |
| 794 | prepare_to_wait(&conf->wait_barrier, |
| 795 | &w, TASK_INTERRUPTIBLE); |
| 796 | if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo || |
| 797 | bio->bi_sector >= mddev->suspend_hi) |
| 798 | break; |
| 799 | schedule(); |
| 800 | } |
| 801 | finish_wait(&conf->wait_barrier, &w); |
| 802 | } |
NeilBrown | 62de608 | 2006-05-01 12:15:47 -0700 | [diff] [blame] | 803 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 804 | wait_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
NeilBrown | 84255d1 | 2008-05-23 13:04:32 -0700 | [diff] [blame] | 806 | bitmap = mddev->bitmap; |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | /* |
| 809 | * make_request() can abort the operation when READA is being |
| 810 | * used and no empty request is available. |
| 811 | * |
| 812 | */ |
| 813 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); |
| 814 | |
| 815 | r1_bio->master_bio = bio; |
| 816 | r1_bio->sectors = bio->bi_size >> 9; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 817 | r1_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | r1_bio->mddev = mddev; |
| 819 | r1_bio->sector = bio->bi_sector; |
| 820 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 821 | /* We might need to issue multiple reads to different |
| 822 | * devices if there are bad blocks around, so we keep |
| 823 | * track of the number of reads in bio->bi_phys_segments. |
| 824 | * If this is 0, there is only one r1_bio and no locking |
| 825 | * will be needed when requests complete. If it is |
| 826 | * non-zero, then it is the number of not-completed requests. |
| 827 | */ |
| 828 | bio->bi_phys_segments = 0; |
| 829 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 830 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 831 | if (rw == READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | /* |
| 833 | * read balancing logic: |
| 834 | */ |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 835 | int max_sectors; |
| 836 | int rdisk; |
| 837 | |
| 838 | read_again: |
| 839 | rdisk = read_balance(conf, r1_bio, &max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | if (rdisk < 0) { |
| 842 | /* couldn't find anywhere to read from */ |
| 843 | raid_end_bio_io(r1_bio); |
| 844 | return 0; |
| 845 | } |
| 846 | mirror = conf->mirrors + rdisk; |
| 847 | |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 848 | if (test_bit(WriteMostly, &mirror->rdev->flags) && |
| 849 | bitmap) { |
| 850 | /* Reading from a write-mostly device must |
| 851 | * take care not to over-take any writes |
| 852 | * that are 'behind' |
| 853 | */ |
| 854 | wait_event(bitmap->behind_wait, |
| 855 | atomic_read(&bitmap->behind_writes) == 0); |
| 856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | r1_bio->read_disk = rdisk; |
| 858 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 859 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 860 | md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector, |
| 861 | max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | r1_bio->bios[rdisk] = read_bio; |
| 864 | |
| 865 | read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset; |
| 866 | read_bio->bi_bdev = mirror->rdev->bdev; |
| 867 | read_bio->bi_end_io = raid1_end_read_request; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 868 | read_bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | read_bio->bi_private = r1_bio; |
| 870 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 871 | if (max_sectors < r1_bio->sectors) { |
| 872 | /* could not read all from this device, so we will |
| 873 | * need another r1_bio. |
| 874 | */ |
| 875 | int sectors_handled; |
| 876 | |
| 877 | sectors_handled = (r1_bio->sector + max_sectors |
| 878 | - bio->bi_sector); |
| 879 | r1_bio->sectors = max_sectors; |
| 880 | spin_lock_irq(&conf->device_lock); |
| 881 | if (bio->bi_phys_segments == 0) |
| 882 | bio->bi_phys_segments = 2; |
| 883 | else |
| 884 | bio->bi_phys_segments++; |
| 885 | spin_unlock_irq(&conf->device_lock); |
| 886 | /* Cannot call generic_make_request directly |
| 887 | * as that will be queued in __make_request |
| 888 | * and subsequent mempool_alloc might block waiting |
| 889 | * for it. So hand bio over to raid1d. |
| 890 | */ |
| 891 | reschedule_retry(r1_bio); |
| 892 | |
| 893 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); |
| 894 | |
| 895 | r1_bio->master_bio = bio; |
| 896 | r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled; |
| 897 | r1_bio->state = 0; |
| 898 | r1_bio->mddev = mddev; |
| 899 | r1_bio->sector = bio->bi_sector + sectors_handled; |
| 900 | goto read_again; |
| 901 | } else |
| 902 | generic_make_request(read_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * WRITE: |
| 908 | */ |
| 909 | /* first select target devices under spinlock and |
| 910 | * inc refcount on their rdev. Record them by setting |
| 911 | * bios[x] to bio |
| 912 | */ |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 913 | plugged = mddev_check_plugged(mddev); |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | disks = conf->raid_disks; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 916 | retry_write: |
| 917 | blocked_rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | rcu_read_lock(); |
| 919 | for (i = 0; i < disks; i++) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 920 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 921 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 922 | atomic_inc(&rdev->nr_pending); |
| 923 | blocked_rdev = rdev; |
| 924 | break; |
| 925 | } |
| 926 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | atomic_inc(&rdev->nr_pending); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 928 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 929 | rdev_dec_pending(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | r1_bio->bios[i] = NULL; |
NeilBrown | 964147d | 2010-05-18 15:27:13 +1000 | [diff] [blame] | 931 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | r1_bio->bios[i] = bio; |
NeilBrown | 964147d | 2010-05-18 15:27:13 +1000 | [diff] [blame] | 933 | targets++; |
| 934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } else |
| 936 | r1_bio->bios[i] = NULL; |
| 937 | } |
| 938 | rcu_read_unlock(); |
| 939 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 940 | if (unlikely(blocked_rdev)) { |
| 941 | /* Wait for this device to become unblocked */ |
| 942 | int j; |
| 943 | |
| 944 | for (j = 0; j < i; j++) |
| 945 | if (r1_bio->bios[j]) |
| 946 | rdev_dec_pending(conf->mirrors[j].rdev, mddev); |
| 947 | |
| 948 | allow_barrier(conf); |
| 949 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
| 950 | wait_barrier(conf); |
| 951 | goto retry_write; |
| 952 | } |
| 953 | |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 954 | if (targets < conf->raid_disks) { |
| 955 | /* array is degraded, we will not clear the bitmap |
| 956 | * on I/O completion (see raid1_end_write_request) */ |
| 957 | set_bit(R1BIO_Degraded, &r1_bio->state); |
| 958 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 959 | |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 960 | /* do behind I/O ? |
| 961 | * Not if there are too many, or cannot allocate memory, |
| 962 | * or a reader on WriteMostly is waiting for behind writes |
| 963 | * to flush */ |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 964 | if (bitmap && |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 965 | (atomic_read(&bitmap->behind_writes) |
| 966 | < mddev->bitmap_info.max_write_behind) && |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 967 | !waitqueue_active(&bitmap->behind_wait)) |
| 968 | alloc_behind_pages(bio, r1_bio); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 969 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 970 | atomic_set(&r1_bio->remaining, 1); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 971 | atomic_set(&r1_bio->behind_remaining, 0); |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 972 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 973 | bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors, |
| 974 | test_bit(R1BIO_BehindIO, &r1_bio->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | for (i = 0; i < disks; i++) { |
| 976 | struct bio *mbio; |
| 977 | if (!r1_bio->bios[i]) |
| 978 | continue; |
| 979 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 980 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | r1_bio->bios[i] = mbio; |
| 982 | |
| 983 | mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset; |
| 984 | mbio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 985 | mbio->bi_end_io = raid1_end_write_request; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 986 | mbio->bi_rw = WRITE | do_flush_fua | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | mbio->bi_private = r1_bio; |
| 988 | |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 989 | if (r1_bio->behind_pages) { |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 990 | struct bio_vec *bvec; |
| 991 | int j; |
| 992 | |
| 993 | /* Yes, I really want the '__' version so that |
| 994 | * we clear any unused pointer in the io_vec, rather |
| 995 | * than leave them unchanged. This is important |
| 996 | * because when we come to free the pages, we won't |
NeilBrown | 046abee | 2010-10-26 15:46:20 +1100 | [diff] [blame] | 997 | * know the original bi_idx, so we just free |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 998 | * them all |
| 999 | */ |
| 1000 | __bio_for_each_segment(bvec, mbio, j, 0) |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 1001 | bvec->bv_page = r1_bio->behind_pages[j]; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 1002 | if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags)) |
| 1003 | atomic_inc(&r1_bio->behind_remaining); |
| 1004 | } |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | atomic_inc(&r1_bio->remaining); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1007 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1008 | bio_list_add(&conf->pending_bio_list, mbio); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1009 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
NeilBrown | af6d7b7 | 2011-05-11 14:51:19 +1000 | [diff] [blame] | 1011 | r1_bio_write_done(r1_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1013 | /* In case raid1d snuck in to freeze_array */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1014 | wake_up(&conf->wait_barrier); |
| 1015 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1016 | if (do_sync || !bitmap || !plugged) |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 1017 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static void status(struct seq_file *seq, mddev_t *mddev) |
| 1023 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1024 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | int i; |
| 1026 | |
| 1027 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
NeilBrown | 11ce99e | 2006-10-03 01:15:52 -0700 | [diff] [blame] | 1028 | conf->raid_disks - mddev->degraded); |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1029 | rcu_read_lock(); |
| 1030 | for (i = 0; i < conf->raid_disks; i++) { |
| 1031 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | seq_printf(seq, "%s", |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1033 | rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 1034 | } |
| 1035 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | seq_printf(seq, "]"); |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1041 | { |
| 1042 | char b[BDEVNAME_SIZE]; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1043 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
| 1045 | /* |
| 1046 | * If it is not operational, then we have already marked it as dead |
| 1047 | * else if it is the last working disks, ignore the error, let the |
| 1048 | * next level up know. |
| 1049 | * else mark the drive as failed |
| 1050 | */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1051 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1052 | && (conf->raid_disks - mddev->degraded) == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * Don't fail the drive, act as though we were just a |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1055 | * normal single drive. |
| 1056 | * However don't try a recovery from this drive as |
| 1057 | * it is very likely to fail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1059 | conf->recovery_disabled = mddev->recovery_disabled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return; |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1061 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame^] | 1062 | set_bit(Blocked, &rdev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1063 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1064 | unsigned long flags; |
| 1065 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | mddev->degraded++; |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 1067 | set_bit(Faulty, &rdev->flags); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1068 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | /* |
| 1070 | * if recovery is running, make sure it aborts. |
| 1071 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1072 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | dd00a99 | 2007-05-10 03:15:50 -0700 | [diff] [blame] | 1073 | } else |
| 1074 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1075 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Joe Perches | 067032b | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1076 | printk(KERN_ALERT |
| 1077 | "md/raid1:%s: Disk failure on %s, disabling device.\n" |
| 1078 | "md/raid1:%s: Operation continuing on %d devices.\n", |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1079 | mdname(mddev), bdevname(rdev->bdev, b), |
| 1080 | mdname(mddev), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static void print_conf(conf_t *conf) |
| 1084 | { |
| 1085 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1087 | printk(KERN_DEBUG "RAID1 conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | if (!conf) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1089 | printk(KERN_DEBUG "(!conf)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | return; |
| 1091 | } |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1092 | printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | conf->raid_disks); |
| 1094 | |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1095 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | for (i = 0; i < conf->raid_disks; i++) { |
| 1097 | char b[BDEVNAME_SIZE]; |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1098 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 1099 | if (rdev) |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1100 | printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1101 | i, !test_bit(In_sync, &rdev->flags), |
| 1102 | !test_bit(Faulty, &rdev->flags), |
| 1103 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1105 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static void close_sync(conf_t *conf) |
| 1109 | { |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 1110 | wait_barrier(conf); |
| 1111 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | |
| 1113 | mempool_destroy(conf->r1buf_pool); |
| 1114 | conf->r1buf_pool = NULL; |
| 1115 | } |
| 1116 | |
| 1117 | static int raid1_spare_active(mddev_t *mddev) |
| 1118 | { |
| 1119 | int i; |
| 1120 | conf_t *conf = mddev->private; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1121 | int count = 0; |
| 1122 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* |
| 1125 | * Find all failed disks within the RAID1 configuration |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1126 | * and mark them readable. |
| 1127 | * Called under mddev lock, so rcu protection not needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | */ |
| 1129 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 1130 | mdk_rdev_t *rdev = conf->mirrors[i].rdev; |
| 1131 | if (rdev |
| 1132 | && !test_bit(Faulty, &rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1133 | && !test_and_set_bit(In_sync, &rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1134 | count++; |
Jonathan Brassow | 654e8b5 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1135 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1138 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1139 | mddev->degraded -= count; |
| 1140 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
| 1142 | print_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1143 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1148 | { |
| 1149 | conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1150 | int err = -EEXIST; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1151 | int mirror = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | mirror_info_t *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1153 | int first = 0; |
| 1154 | int last = mddev->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1156 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 1157 | return -EBUSY; |
| 1158 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1159 | if (rdev->raid_disk >= 0) |
| 1160 | first = last = rdev->raid_disk; |
| 1161 | |
| 1162 | for (mirror = first; mirror <= last; mirror++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | if ( !(p=conf->mirrors+mirror)->rdev) { |
| 1164 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 1165 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1166 | rdev->data_offset << 9); |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 1167 | /* as we don't honour merge_bvec_fn, we must |
| 1168 | * never risk violating it, so limit |
| 1169 | * ->max_segments to one lying with a single |
| 1170 | * page, as a one page request is never in |
| 1171 | * violation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | */ |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 1173 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { |
| 1174 | blk_queue_max_segments(mddev->queue, 1); |
| 1175 | blk_queue_segment_boundary(mddev->queue, |
| 1176 | PAGE_CACHE_SIZE - 1); |
| 1177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
| 1179 | p->head_position = 0; |
| 1180 | rdev->raid_disk = mirror; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1181 | err = 0; |
NeilBrown | 6aea114a | 2005-11-28 13:44:13 -0800 | [diff] [blame] | 1182 | /* As all devices are equivalent, we don't need a full recovery |
| 1183 | * if this was recently any drive of the array |
| 1184 | */ |
| 1185 | if (rdev->saved_raid_disk < 0) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1186 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 1187 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | break; |
| 1189 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1190 | md_integrity_add_rdev(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | print_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1192 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static int raid1_remove_disk(mddev_t *mddev, int number) |
| 1196 | { |
| 1197 | conf_t *conf = mddev->private; |
| 1198 | int err = 0; |
| 1199 | mdk_rdev_t *rdev; |
| 1200 | mirror_info_t *p = conf->mirrors+ number; |
| 1201 | |
| 1202 | print_conf(conf); |
| 1203 | rdev = p->rdev; |
| 1204 | if (rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1205 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | atomic_read(&rdev->nr_pending)) { |
| 1207 | err = -EBUSY; |
| 1208 | goto abort; |
| 1209 | } |
NeilBrown | 046abee | 2010-10-26 15:46:20 +1100 | [diff] [blame] | 1210 | /* Only remove non-faulty devices if recovery |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1211 | * is not possible. |
| 1212 | */ |
| 1213 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1214 | mddev->recovery_disabled != conf->recovery_disabled && |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1215 | mddev->degraded < conf->raid_disks) { |
| 1216 | err = -EBUSY; |
| 1217 | goto abort; |
| 1218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 1220 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | if (atomic_read(&rdev->nr_pending)) { |
| 1222 | /* lost the race, try later */ |
| 1223 | err = -EBUSY; |
| 1224 | p->rdev = rdev; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1225 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1227 | err = md_integrity_register(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | } |
| 1229 | abort: |
| 1230 | |
| 1231 | print_conf(conf); |
| 1232 | return err; |
| 1233 | } |
| 1234 | |
| 1235 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1236 | static void end_sync_read(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1238 | r1bio_t *r1_bio = bio->bi_private; |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 1239 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 1241 | for (i=r1_bio->mddev->raid_disks; i--; ) |
| 1242 | if (r1_bio->bios[i] == bio) |
| 1243 | break; |
| 1244 | BUG_ON(i < 0); |
| 1245 | update_head_pos(i, r1_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * we have read a block, now it needs to be re-written, |
| 1248 | * or re-read if the read failed. |
| 1249 | * We don't do much here, just schedule handling by raid1d |
| 1250 | */ |
NeilBrown | 69382e8 | 2006-01-06 00:20:22 -0800 | [diff] [blame] | 1251 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 1253 | |
| 1254 | if (atomic_dec_and_test(&r1_bio->remaining)) |
| 1255 | reschedule_retry(r1_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1258 | static void end_sync_write(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | { |
| 1260 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1261 | r1bio_t *r1_bio = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | mddev_t *mddev = r1_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1263 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | int i; |
| 1265 | int mirror=0; |
| 1266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | for (i = 0; i < conf->raid_disks; i++) |
| 1268 | if (r1_bio->bios[i] == bio) { |
| 1269 | mirror = i; |
| 1270 | break; |
| 1271 | } |
NeilBrown | 6b1117d | 2006-03-31 02:31:57 -0800 | [diff] [blame] | 1272 | if (!uptodate) { |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1273 | sector_t sync_blocks = 0; |
NeilBrown | 6b1117d | 2006-03-31 02:31:57 -0800 | [diff] [blame] | 1274 | sector_t s = r1_bio->sector; |
| 1275 | long sectors_to_go = r1_bio->sectors; |
| 1276 | /* make sure these bits doesn't get cleared. */ |
| 1277 | do { |
NeilBrown | 5e3db64 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1278 | bitmap_end_sync(mddev->bitmap, s, |
NeilBrown | 6b1117d | 2006-03-31 02:31:57 -0800 | [diff] [blame] | 1279 | &sync_blocks, 1); |
| 1280 | s += sync_blocks; |
| 1281 | sectors_to_go -= sync_blocks; |
| 1282 | } while (sectors_to_go > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | md_error(mddev, conf->mirrors[mirror].rdev); |
NeilBrown | 6b1117d | 2006-03-31 02:31:57 -0800 | [diff] [blame] | 1284 | } |
NeilBrown | e3b9703 | 2005-08-04 12:53:34 -0700 | [diff] [blame] | 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | update_head_pos(mirror, r1_bio); |
| 1287 | |
| 1288 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1289 | sector_t s = r1_bio->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | put_buf(r1_bio); |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1291 | md_done_sync(mddev, s, uptodate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1295 | static int fix_sync_read_error(r1bio_t *r1_bio) |
| 1296 | { |
| 1297 | /* Try some synchronous reads of other devices to get |
| 1298 | * good data, much like with normal read errors. Only |
| 1299 | * read into the pages we already have so we don't |
| 1300 | * need to re-issue the read request. |
| 1301 | * We don't need to freeze the array, because being in an |
| 1302 | * active sync request, there is no normal IO, and |
| 1303 | * no overlapping syncs. |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1304 | * We don't need to check is_badblock() again as we |
| 1305 | * made sure that anything with a bad block in range |
| 1306 | * will have bi_end_io clear. |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1307 | */ |
| 1308 | mddev_t *mddev = r1_bio->mddev; |
| 1309 | conf_t *conf = mddev->private; |
| 1310 | struct bio *bio = r1_bio->bios[r1_bio->read_disk]; |
| 1311 | sector_t sect = r1_bio->sector; |
| 1312 | int sectors = r1_bio->sectors; |
| 1313 | int idx = 0; |
| 1314 | |
| 1315 | while(sectors) { |
| 1316 | int s = sectors; |
| 1317 | int d = r1_bio->read_disk; |
| 1318 | int success = 0; |
| 1319 | mdk_rdev_t *rdev; |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1320 | int start; |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1321 | |
| 1322 | if (s > (PAGE_SIZE>>9)) |
| 1323 | s = PAGE_SIZE >> 9; |
| 1324 | do { |
| 1325 | if (r1_bio->bios[d]->bi_end_io == end_sync_read) { |
| 1326 | /* No rcu protection needed here devices |
| 1327 | * can only be removed when no resync is |
| 1328 | * active, and resync is currently active |
| 1329 | */ |
| 1330 | rdev = conf->mirrors[d].rdev; |
Namhyung Kim | 9d3d801 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1331 | if (sync_page_io(rdev, sect, s<<9, |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1332 | bio->bi_io_vec[idx].bv_page, |
| 1333 | READ, false)) { |
| 1334 | success = 1; |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
| 1338 | d++; |
| 1339 | if (d == conf->raid_disks) |
| 1340 | d = 0; |
| 1341 | } while (!success && d != r1_bio->read_disk); |
| 1342 | |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1343 | if (!success) { |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1344 | char b[BDEVNAME_SIZE]; |
| 1345 | /* Cannot read from anywhere, array is toast */ |
| 1346 | md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev); |
| 1347 | printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error" |
| 1348 | " for block %llu\n", |
| 1349 | mdname(mddev), |
| 1350 | bdevname(bio->bi_bdev, b), |
| 1351 | (unsigned long long)r1_bio->sector); |
| 1352 | md_done_sync(mddev, r1_bio->sectors, 0); |
| 1353 | put_buf(r1_bio); |
| 1354 | return 0; |
| 1355 | } |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1356 | |
| 1357 | start = d; |
| 1358 | /* write it back and re-read */ |
| 1359 | while (d != r1_bio->read_disk) { |
| 1360 | if (d == 0) |
| 1361 | d = conf->raid_disks; |
| 1362 | d--; |
| 1363 | if (r1_bio->bios[d]->bi_end_io != end_sync_read) |
| 1364 | continue; |
| 1365 | rdev = conf->mirrors[d].rdev; |
Namhyung Kim | 9d3d801 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1366 | if (sync_page_io(rdev, sect, s<<9, |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1367 | bio->bi_io_vec[idx].bv_page, |
| 1368 | WRITE, false) == 0) { |
| 1369 | r1_bio->bios[d]->bi_end_io = NULL; |
| 1370 | rdev_dec_pending(rdev, mddev); |
| 1371 | md_error(mddev, rdev); |
Namhyung Kim | 9d3d801 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1372 | } |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1373 | } |
| 1374 | d = start; |
| 1375 | while (d != r1_bio->read_disk) { |
| 1376 | if (d == 0) |
| 1377 | d = conf->raid_disks; |
| 1378 | d--; |
| 1379 | if (r1_bio->bios[d]->bi_end_io != end_sync_read) |
| 1380 | continue; |
| 1381 | rdev = conf->mirrors[d].rdev; |
Namhyung Kim | 9d3d801 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1382 | if (sync_page_io(rdev, sect, s<<9, |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1383 | bio->bi_io_vec[idx].bv_page, |
| 1384 | READ, false) == 0) |
| 1385 | md_error(mddev, rdev); |
Namhyung Kim | 9d3d801 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1386 | else |
| 1387 | atomic_add(s, &rdev->corrected_errors); |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1388 | } |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1389 | sectors -= s; |
| 1390 | sect += s; |
| 1391 | idx ++; |
| 1392 | } |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1393 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
NeilBrown | 7ca78d5 | 2011-05-11 14:50:37 +1000 | [diff] [blame] | 1394 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1395 | return 1; |
| 1396 | } |
| 1397 | |
| 1398 | static int process_checks(r1bio_t *r1_bio) |
| 1399 | { |
| 1400 | /* We have read all readable devices. If we haven't |
| 1401 | * got the block, then there is no hope left. |
| 1402 | * If we have, then we want to do a comparison |
| 1403 | * and skip the write if everything is the same. |
| 1404 | * If any blocks failed to read, then we need to |
| 1405 | * attempt an over-write |
| 1406 | */ |
| 1407 | mddev_t *mddev = r1_bio->mddev; |
| 1408 | conf_t *conf = mddev->private; |
| 1409 | int primary; |
| 1410 | int i; |
| 1411 | |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1412 | for (primary = 0; primary < conf->raid_disks; primary++) |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1413 | if (r1_bio->bios[primary]->bi_end_io == end_sync_read && |
| 1414 | test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) { |
| 1415 | r1_bio->bios[primary]->bi_end_io = NULL; |
| 1416 | rdev_dec_pending(conf->mirrors[primary].rdev, mddev); |
| 1417 | break; |
| 1418 | } |
| 1419 | r1_bio->read_disk = primary; |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1420 | for (i = 0; i < conf->raid_disks; i++) { |
| 1421 | int j; |
| 1422 | int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9); |
| 1423 | struct bio *pbio = r1_bio->bios[primary]; |
| 1424 | struct bio *sbio = r1_bio->bios[i]; |
| 1425 | int size; |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1426 | |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1427 | if (r1_bio->bios[i]->bi_end_io != end_sync_read) |
| 1428 | continue; |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1429 | |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1430 | if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) { |
| 1431 | for (j = vcnt; j-- ; ) { |
| 1432 | struct page *p, *s; |
| 1433 | p = pbio->bi_io_vec[j].bv_page; |
| 1434 | s = sbio->bi_io_vec[j].bv_page; |
| 1435 | if (memcmp(page_address(p), |
| 1436 | page_address(s), |
| 1437 | PAGE_SIZE)) |
| 1438 | break; |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1439 | } |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1440 | } else |
| 1441 | j = 0; |
| 1442 | if (j >= 0) |
| 1443 | mddev->resync_mismatches += r1_bio->sectors; |
| 1444 | if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery) |
| 1445 | && test_bit(BIO_UPTODATE, &sbio->bi_flags))) { |
| 1446 | /* No need to write to this device. */ |
| 1447 | sbio->bi_end_io = NULL; |
| 1448 | rdev_dec_pending(conf->mirrors[i].rdev, mddev); |
| 1449 | continue; |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1450 | } |
NeilBrown | 78d7f5f | 2011-05-11 14:48:56 +1000 | [diff] [blame] | 1451 | /* fixup the bio for reuse */ |
| 1452 | sbio->bi_vcnt = vcnt; |
| 1453 | sbio->bi_size = r1_bio->sectors << 9; |
| 1454 | sbio->bi_idx = 0; |
| 1455 | sbio->bi_phys_segments = 0; |
| 1456 | sbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 1457 | sbio->bi_flags |= 1 << BIO_UPTODATE; |
| 1458 | sbio->bi_next = NULL; |
| 1459 | sbio->bi_sector = r1_bio->sector + |
| 1460 | conf->mirrors[i].rdev->data_offset; |
| 1461 | sbio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 1462 | size = sbio->bi_size; |
| 1463 | for (j = 0; j < vcnt ; j++) { |
| 1464 | struct bio_vec *bi; |
| 1465 | bi = &sbio->bi_io_vec[j]; |
| 1466 | bi->bv_offset = 0; |
| 1467 | if (size > PAGE_SIZE) |
| 1468 | bi->bv_len = PAGE_SIZE; |
| 1469 | else |
| 1470 | bi->bv_len = size; |
| 1471 | size -= PAGE_SIZE; |
| 1472 | memcpy(page_address(bi->bv_page), |
| 1473 | page_address(pbio->bi_io_vec[j].bv_page), |
| 1474 | PAGE_SIZE); |
| 1475 | } |
| 1476 | } |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio) |
| 1481 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1482 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | int i; |
| 1484 | int disks = conf->raid_disks; |
| 1485 | struct bio *bio, *wbio; |
| 1486 | |
| 1487 | bio = r1_bio->bios[r1_bio->read_disk]; |
| 1488 | |
NeilBrown | a68e587 | 2011-05-11 14:40:44 +1000 | [diff] [blame] | 1489 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) |
| 1490 | /* ouch - failed to read all of that. */ |
| 1491 | if (!fix_sync_read_error(r1_bio)) |
| 1492 | return; |
NeilBrown | 7ca78d5 | 2011-05-11 14:50:37 +1000 | [diff] [blame] | 1493 | |
| 1494 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 1495 | if (process_checks(r1_bio) < 0) |
| 1496 | return; |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 1497 | /* |
| 1498 | * schedule writes |
| 1499 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | atomic_set(&r1_bio->remaining, 1); |
| 1501 | for (i = 0; i < disks ; i++) { |
| 1502 | wbio = r1_bio->bios[i]; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1503 | if (wbio->bi_end_io == NULL || |
| 1504 | (wbio->bi_end_io == end_sync_read && |
| 1505 | (i == r1_bio->read_disk || |
| 1506 | !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | continue; |
| 1508 | |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1509 | wbio->bi_rw = WRITE; |
| 1510 | wbio->bi_end_io = end_sync_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | atomic_inc(&r1_bio->remaining); |
| 1512 | md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9); |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | generic_make_request(wbio); |
| 1515 | } |
| 1516 | |
| 1517 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1518 | /* if we're here, all write(s) have completed, so clean up */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | md_done_sync(mddev, r1_bio->sectors, 1); |
| 1520 | put_buf(r1_bio); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * This is a kernel thread which: |
| 1526 | * |
| 1527 | * 1. Retries failed read operations on working mirrors. |
| 1528 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1529 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | */ |
| 1531 | |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1532 | static void fix_read_error(conf_t *conf, int read_disk, |
| 1533 | sector_t sect, int sectors) |
| 1534 | { |
| 1535 | mddev_t *mddev = conf->mddev; |
| 1536 | while(sectors) { |
| 1537 | int s = sectors; |
| 1538 | int d = read_disk; |
| 1539 | int success = 0; |
| 1540 | int start; |
| 1541 | mdk_rdev_t *rdev; |
| 1542 | |
| 1543 | if (s > (PAGE_SIZE>>9)) |
| 1544 | s = PAGE_SIZE >> 9; |
| 1545 | |
| 1546 | do { |
| 1547 | /* Note: no rcu protection needed here |
| 1548 | * as this is synchronous in the raid1d thread |
| 1549 | * which is the thread that might remove |
| 1550 | * a device. If raid1d ever becomes multi-threaded.... |
| 1551 | */ |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1552 | sector_t first_bad; |
| 1553 | int bad_sectors; |
| 1554 | |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1555 | rdev = conf->mirrors[d].rdev; |
| 1556 | if (rdev && |
| 1557 | test_bit(In_sync, &rdev->flags) && |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1558 | is_badblock(rdev, sect, s, |
| 1559 | &first_bad, &bad_sectors) == 0 && |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1560 | sync_page_io(rdev, sect, s<<9, |
| 1561 | conf->tmppage, READ, false)) |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1562 | success = 1; |
| 1563 | else { |
| 1564 | d++; |
| 1565 | if (d == conf->raid_disks) |
| 1566 | d = 0; |
| 1567 | } |
| 1568 | } while (!success && d != read_disk); |
| 1569 | |
| 1570 | if (!success) { |
| 1571 | /* Cannot read from anywhere -- bye bye array */ |
| 1572 | md_error(mddev, conf->mirrors[read_disk].rdev); |
| 1573 | break; |
| 1574 | } |
| 1575 | /* write it back and re-read */ |
| 1576 | start = d; |
| 1577 | while (d != read_disk) { |
| 1578 | if (d==0) |
| 1579 | d = conf->raid_disks; |
| 1580 | d--; |
| 1581 | rdev = conf->mirrors[d].rdev; |
| 1582 | if (rdev && |
| 1583 | test_bit(In_sync, &rdev->flags)) { |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1584 | if (sync_page_io(rdev, sect, s<<9, |
| 1585 | conf->tmppage, WRITE, false) |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1586 | == 0) |
| 1587 | /* Well, this device is dead */ |
| 1588 | md_error(mddev, rdev); |
| 1589 | } |
| 1590 | } |
| 1591 | d = start; |
| 1592 | while (d != read_disk) { |
| 1593 | char b[BDEVNAME_SIZE]; |
| 1594 | if (d==0) |
| 1595 | d = conf->raid_disks; |
| 1596 | d--; |
| 1597 | rdev = conf->mirrors[d].rdev; |
| 1598 | if (rdev && |
| 1599 | test_bit(In_sync, &rdev->flags)) { |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1600 | if (sync_page_io(rdev, sect, s<<9, |
| 1601 | conf->tmppage, READ, false) |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1602 | == 0) |
| 1603 | /* Well, this device is dead */ |
| 1604 | md_error(mddev, rdev); |
| 1605 | else { |
| 1606 | atomic_add(s, &rdev->corrected_errors); |
| 1607 | printk(KERN_INFO |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1608 | "md/raid1:%s: read error corrected " |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1609 | "(%d sectors at %llu on %s)\n", |
| 1610 | mdname(mddev), s, |
Randy Dunlap | 969b755 | 2006-10-28 10:38:32 -0700 | [diff] [blame] | 1611 | (unsigned long long)(sect + |
| 1612 | rdev->data_offset), |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1613 | bdevname(rdev->bdev, b)); |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | sectors -= s; |
| 1618 | sect += s; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | static void raid1d(mddev_t *mddev) |
| 1623 | { |
| 1624 | r1bio_t *r1_bio; |
| 1625 | struct bio *bio; |
| 1626 | unsigned long flags; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1627 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | struct list_head *head = &conf->retry_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | mdk_rdev_t *rdev; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1630 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
| 1632 | md_check_recovery(mddev); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1633 | |
| 1634 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | for (;;) { |
| 1636 | char b[BDEVNAME_SIZE]; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1637 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1638 | if (atomic_read(&mddev->plug_cnt) == 0) |
| 1639 | flush_pending_writes(conf); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1642 | if (list_empty(head)) { |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1643 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | break; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | r1_bio = list_entry(head->prev, r1bio_t, retry_list); |
| 1647 | list_del(head->prev); |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 1648 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1650 | |
| 1651 | mddev = r1_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1652 | conf = mddev->private; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1653 | if (test_bit(R1BIO_IsSync, &r1_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | sync_request_write(mddev, r1_bio); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1655 | else if (test_bit(R1BIO_ReadError, &r1_bio->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | int disk; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1657 | int max_sectors; |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 1658 | |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1659 | clear_bit(R1BIO_ReadError, &r1_bio->state); |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 1660 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 1661 | * the block and we can fix it. |
| 1662 | * We freeze all other IO, and try reading the block from |
| 1663 | * other devices. When we find one, we re-write |
| 1664 | * and check it that fixes the read error. |
| 1665 | * This is all done synchronously while the array is |
| 1666 | * frozen |
| 1667 | */ |
NeilBrown | 867868f | 2006-10-03 01:15:51 -0700 | [diff] [blame] | 1668 | if (mddev->ro == 0) { |
| 1669 | freeze_array(conf); |
| 1670 | fix_read_error(conf, r1_bio->read_disk, |
| 1671 | r1_bio->sector, |
| 1672 | r1_bio->sectors); |
| 1673 | unfreeze_array(conf); |
NeilBrown | d0e2607 | 2009-12-01 17:30:59 +1100 | [diff] [blame] | 1674 | } else |
| 1675 | md_error(mddev, |
| 1676 | conf->mirrors[r1_bio->read_disk].rdev); |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 1677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | bio = r1_bio->bios[r1_bio->read_disk]; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1679 | bdevname(bio->bi_bdev, b); |
| 1680 | read_more: |
| 1681 | disk = read_balance(conf, r1_bio, &max_sectors); |
| 1682 | if (disk == -1) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 1683 | printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | " read error for block %llu\n", |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1685 | mdname(mddev), b, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | (unsigned long long)r1_bio->sector); |
| 1687 | raid_end_bio_io(r1_bio); |
| 1688 | } else { |
NeilBrown | 2c7d46e | 2010-08-18 16:16:05 +1000 | [diff] [blame] | 1689 | const unsigned long do_sync = r1_bio->master_bio->bi_rw & REQ_SYNC; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1690 | if (bio) { |
| 1691 | r1_bio->bios[r1_bio->read_disk] = |
| 1692 | mddev->ro ? IO_BLOCKED : NULL; |
| 1693 | bio_put(bio); |
| 1694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | r1_bio->read_disk = disk; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 1696 | bio = bio_clone_mddev(r1_bio->master_bio, |
| 1697 | GFP_NOIO, mddev); |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1698 | md_trim_bio(bio, |
| 1699 | r1_bio->sector - bio->bi_sector, |
| 1700 | max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | r1_bio->bios[r1_bio->read_disk] = bio; |
| 1702 | rdev = conf->mirrors[disk].rdev; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1703 | printk_ratelimited( |
| 1704 | KERN_ERR |
| 1705 | "md/raid1:%s: redirecting sector %llu" |
| 1706 | " to other mirror: %s\n", |
| 1707 | mdname(mddev), |
| 1708 | (unsigned long long)r1_bio->sector, |
| 1709 | bdevname(rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | bio->bi_sector = r1_bio->sector + rdev->data_offset; |
| 1711 | bio->bi_bdev = rdev->bdev; |
| 1712 | bio->bi_end_io = raid1_end_read_request; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1713 | bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | bio->bi_private = r1_bio; |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1715 | if (max_sectors < r1_bio->sectors) { |
| 1716 | /* Drat - have to split this up more */ |
| 1717 | struct bio *mbio = r1_bio->master_bio; |
| 1718 | int sectors_handled = |
| 1719 | r1_bio->sector + max_sectors |
| 1720 | - mbio->bi_sector; |
| 1721 | r1_bio->sectors = max_sectors; |
| 1722 | spin_lock_irq(&conf->device_lock); |
| 1723 | if (mbio->bi_phys_segments == 0) |
| 1724 | mbio->bi_phys_segments = 2; |
| 1725 | else |
| 1726 | mbio->bi_phys_segments++; |
| 1727 | spin_unlock_irq(&conf->device_lock); |
| 1728 | generic_make_request(bio); |
| 1729 | bio = NULL; |
| 1730 | |
| 1731 | r1_bio = mempool_alloc(conf->r1bio_pool, |
| 1732 | GFP_NOIO); |
| 1733 | |
| 1734 | r1_bio->master_bio = mbio; |
| 1735 | r1_bio->sectors = (mbio->bi_size >> 9) |
| 1736 | - sectors_handled; |
| 1737 | r1_bio->state = 0; |
| 1738 | set_bit(R1BIO_ReadError, |
| 1739 | &r1_bio->state); |
| 1740 | r1_bio->mddev = mddev; |
| 1741 | r1_bio->sector = mbio->bi_sector |
| 1742 | + sectors_handled; |
| 1743 | |
| 1744 | goto read_more; |
| 1745 | } else |
| 1746 | generic_make_request(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
NeilBrown | d2eb35a | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1748 | } else { |
| 1749 | /* just a partial read to be scheduled from separate |
| 1750 | * context |
| 1751 | */ |
| 1752 | generic_make_request(r1_bio->bios[r1_bio->read_disk]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | } |
NeilBrown | 1d9d524 | 2009-10-16 15:55:32 +1100 | [diff] [blame] | 1754 | cond_resched(); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame^] | 1755 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 1756 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | } |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1758 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | static int init_resync(conf_t *conf) |
| 1763 | { |
| 1764 | int buffs; |
| 1765 | |
| 1766 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Eric Sesterhenn | 9e77c48 | 2006-04-01 01:08:49 +0200 | [diff] [blame] | 1767 | BUG_ON(conf->r1buf_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, |
| 1769 | conf->poolinfo); |
| 1770 | if (!conf->r1buf_pool) |
| 1771 | return -ENOMEM; |
| 1772 | conf->next_resync = 0; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * perform a "sync" on one "block" |
| 1778 | * |
| 1779 | * We need to make sure that no normal I/O request - particularly write |
| 1780 | * requests - conflict with active sync requests. |
| 1781 | * |
| 1782 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 1783 | * that can be installed to exclude normal IO requests. |
| 1784 | */ |
| 1785 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1786 | static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1788 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | r1bio_t *r1_bio; |
| 1790 | struct bio *bio; |
| 1791 | sector_t max_sector, nr_sectors; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1792 | int disk = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | int i; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1794 | int wonly = -1; |
| 1795 | int write_targets = 0, read_targets = 0; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1796 | sector_t sync_blocks; |
NeilBrown | e3b9703 | 2005-08-04 12:53:34 -0700 | [diff] [blame] | 1797 | int still_degraded = 0; |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1798 | int good_sectors = RESYNC_SECTORS; |
| 1799 | int min_bad = 0; /* number of sectors that are bad in all devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | |
| 1801 | if (!conf->r1buf_pool) |
| 1802 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1803 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1805 | max_sector = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | if (sector_nr >= max_sector) { |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1807 | /* If we aborted, we need to abort the |
| 1808 | * sync on the 'current' bitmap chunk (there will |
| 1809 | * only be one in raid1 resync. |
| 1810 | * We can find the current addess in mddev->curr_resync |
| 1811 | */ |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1812 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 1813 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1814 | &sync_blocks, 1); |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1815 | else /* completed sync */ |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1816 | conf->fullsync = 0; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1817 | |
| 1818 | bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | close_sync(conf); |
| 1820 | return 0; |
| 1821 | } |
| 1822 | |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1823 | if (mddev->bitmap == NULL && |
| 1824 | mddev->recovery_cp == MaxSector && |
NeilBrown | 6394cca | 2006-08-27 01:23:50 -0700 | [diff] [blame] | 1825 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1826 | conf->fullsync == 0) { |
| 1827 | *skipped = 1; |
| 1828 | return max_sector - sector_nr; |
| 1829 | } |
NeilBrown | 6394cca | 2006-08-27 01:23:50 -0700 | [diff] [blame] | 1830 | /* before building a request, check if we can skip these blocks.. |
| 1831 | * This call the bitmap_start_sync doesn't actually record anything |
| 1832 | */ |
NeilBrown | e3b9703 | 2005-08-04 12:53:34 -0700 | [diff] [blame] | 1833 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | e5de485f | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 1834 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1835 | /* We can skip this block, and probably several more */ |
| 1836 | *skipped = 1; |
| 1837 | return sync_blocks; |
| 1838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | /* |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 1840 | * If there is non-resync activity waiting for a turn, |
| 1841 | * and resync is going fast enough, |
| 1842 | * then let it though before starting on this new sync request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | */ |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 1844 | if (!go_faster && conf->nr_waiting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | msleep_interruptible(1000); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 1846 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 1847 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
NeilBrown | 1c4588e | 2010-10-26 17:41:22 +1100 | [diff] [blame] | 1848 | r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 1849 | raise_barrier(conf); |
| 1850 | |
| 1851 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1853 | rcu_read_lock(); |
| 1854 | /* |
| 1855 | * If we get a correctably read error during resync or recovery, |
| 1856 | * we might want to read from a different device. So we |
| 1857 | * flag all drives that could conceivably be read from for READ, |
| 1858 | * and any others (which will be non-In_sync devices) for WRITE. |
| 1859 | * If a read fails, we try reading from something else for which READ |
| 1860 | * is OK. |
| 1861 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | r1_bio->mddev = mddev; |
| 1864 | r1_bio->sector = sector_nr; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1865 | r1_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | set_bit(R1BIO_IsSync, &r1_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | |
| 1868 | for (i=0; i < conf->raid_disks; i++) { |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1869 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | bio = r1_bio->bios[i]; |
| 1871 | |
| 1872 | /* take from bio_init */ |
| 1873 | bio->bi_next = NULL; |
NeilBrown | db8d9d3 | 2010-10-07 12:00:50 +1100 | [diff] [blame] | 1874 | bio->bi_flags &= ~(BIO_POOL_MASK-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | bio->bi_flags |= 1 << BIO_UPTODATE; |
NeilBrown | db8d9d3 | 2010-10-07 12:00:50 +1100 | [diff] [blame] | 1876 | bio->bi_comp_cpu = -1; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 1877 | bio->bi_rw = READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | bio->bi_vcnt = 0; |
| 1879 | bio->bi_idx = 0; |
| 1880 | bio->bi_phys_segments = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | bio->bi_size = 0; |
| 1882 | bio->bi_end_io = NULL; |
| 1883 | bio->bi_private = NULL; |
| 1884 | |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1885 | rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 1886 | if (rdev == NULL || |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1887 | test_bit(Faulty, &rdev->flags)) { |
NeilBrown | e3b9703 | 2005-08-04 12:53:34 -0700 | [diff] [blame] | 1888 | still_degraded = 1; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1889 | } else if (!test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | bio->bi_rw = WRITE; |
| 1891 | bio->bi_end_io = end_sync_write; |
| 1892 | write_targets ++; |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1893 | } else { |
| 1894 | /* may need to read from here */ |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1895 | sector_t first_bad = MaxSector; |
| 1896 | int bad_sectors; |
| 1897 | |
| 1898 | if (is_badblock(rdev, sector_nr, good_sectors, |
| 1899 | &first_bad, &bad_sectors)) { |
| 1900 | if (first_bad > sector_nr) |
| 1901 | good_sectors = first_bad - sector_nr; |
| 1902 | else { |
| 1903 | bad_sectors -= (sector_nr - first_bad); |
| 1904 | if (min_bad == 0 || |
| 1905 | min_bad > bad_sectors) |
| 1906 | min_bad = bad_sectors; |
| 1907 | } |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1908 | } |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1909 | if (sector_nr < first_bad) { |
| 1910 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1911 | if (wonly < 0) |
| 1912 | wonly = i; |
| 1913 | } else { |
| 1914 | if (disk < 0) |
| 1915 | disk = i; |
| 1916 | } |
| 1917 | bio->bi_rw = READ; |
| 1918 | bio->bi_end_io = end_sync_read; |
| 1919 | read_targets++; |
| 1920 | } |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1921 | } |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1922 | if (bio->bi_end_io) { |
| 1923 | atomic_inc(&rdev->nr_pending); |
| 1924 | bio->bi_sector = sector_nr + rdev->data_offset; |
| 1925 | bio->bi_bdev = rdev->bdev; |
| 1926 | bio->bi_private = r1_bio; |
| 1927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | } |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1929 | rcu_read_unlock(); |
| 1930 | if (disk < 0) |
| 1931 | disk = wonly; |
| 1932 | r1_bio->read_disk = disk; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 1933 | |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1934 | if (read_targets == 0 && min_bad > 0) { |
| 1935 | /* These sectors are bad on all InSync devices, so we |
| 1936 | * need to mark them bad on all write targets |
| 1937 | */ |
| 1938 | int ok = 1; |
| 1939 | for (i = 0 ; i < conf->raid_disks ; i++) |
| 1940 | if (r1_bio->bios[i]->bi_end_io == end_sync_write) { |
| 1941 | mdk_rdev_t *rdev = |
| 1942 | rcu_dereference(conf->mirrors[i].rdev); |
| 1943 | ok = rdev_set_badblocks(rdev, sector_nr, |
| 1944 | min_bad, 0 |
| 1945 | ) && ok; |
| 1946 | } |
| 1947 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1948 | *skipped = 1; |
| 1949 | put_buf(r1_bio); |
| 1950 | |
| 1951 | if (!ok) { |
| 1952 | /* Cannot record the badblocks, so need to |
| 1953 | * abort the resync. |
| 1954 | * If there are multiple read targets, could just |
| 1955 | * fail the really bad ones ??? |
| 1956 | */ |
| 1957 | conf->recovery_disabled = mddev->recovery_disabled; |
| 1958 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1959 | return 0; |
| 1960 | } else |
| 1961 | return min_bad; |
| 1962 | |
| 1963 | } |
| 1964 | if (min_bad > 0 && min_bad < good_sectors) { |
| 1965 | /* only resync enough to reach the next bad->good |
| 1966 | * transition */ |
| 1967 | good_sectors = min_bad; |
| 1968 | } |
| 1969 | |
NeilBrown | 3e198f7 | 2006-01-06 00:20:21 -0800 | [diff] [blame] | 1970 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0) |
| 1971 | /* extra read targets are also write targets */ |
| 1972 | write_targets += read_targets-1; |
| 1973 | |
| 1974 | if (write_targets == 0 || read_targets == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | /* There is nowhere to write, so all non-sync |
| 1976 | * drives must be failed - so we are finished |
| 1977 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1978 | sector_t rv = max_sector - sector_nr; |
| 1979 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | put_buf(r1_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | return rv; |
| 1982 | } |
| 1983 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 1984 | if (max_sector > mddev->resync_max) |
| 1985 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ |
NeilBrown | 06f6038 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1986 | if (max_sector > sector_nr + good_sectors) |
| 1987 | max_sector = sector_nr + good_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | nr_sectors = 0; |
NeilBrown | 289e99e | 2005-06-21 17:17:24 -0700 | [diff] [blame] | 1989 | sync_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | do { |
| 1991 | struct page *page; |
| 1992 | int len = PAGE_SIZE; |
| 1993 | if (sector_nr + (len>>9) > max_sector) |
| 1994 | len = (max_sector - sector_nr) << 9; |
| 1995 | if (len == 0) |
| 1996 | break; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 1997 | if (sync_blocks == 0) { |
| 1998 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
NeilBrown | e5de485f | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 1999 | &sync_blocks, still_degraded) && |
| 2000 | !conf->fullsync && |
| 2001 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 2002 | break; |
Eric Sesterhenn | 9e77c48 | 2006-04-01 01:08:49 +0200 | [diff] [blame] | 2003 | BUG_ON(sync_blocks < (PAGE_SIZE>>9)); |
NeilBrown | 7571ae8 | 2010-10-07 11:54:46 +1100 | [diff] [blame] | 2004 | if ((len >> 9) > sync_blocks) |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 2005 | len = sync_blocks<<9; |
NeilBrown | ab7a30c | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2006 | } |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | for (i=0 ; i < conf->raid_disks; i++) { |
| 2009 | bio = r1_bio->bios[i]; |
| 2010 | if (bio->bi_end_io) { |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2011 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | if (bio_add_page(bio, page, len, 0) == 0) { |
| 2013 | /* stop here */ |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2014 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | while (i > 0) { |
| 2016 | i--; |
| 2017 | bio = r1_bio->bios[i]; |
NeilBrown | 6a806c5 | 2005-07-15 03:56:35 -0700 | [diff] [blame] | 2018 | if (bio->bi_end_io==NULL) |
| 2019 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | /* remove last page from this bio */ |
| 2021 | bio->bi_vcnt--; |
| 2022 | bio->bi_size -= len; |
| 2023 | bio->bi_flags &= ~(1<< BIO_SEG_VALID); |
| 2024 | } |
| 2025 | goto bio_full; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | nr_sectors += len>>9; |
| 2030 | sector_nr += len>>9; |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2031 | sync_blocks -= (len>>9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES); |
| 2033 | bio_full: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | r1_bio->sectors = nr_sectors; |
| 2035 | |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2036 | /* For a user-requested sync, we read all readable devices and do a |
| 2037 | * compare |
| 2038 | */ |
| 2039 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 2040 | atomic_set(&r1_bio->remaining, read_targets); |
| 2041 | for (i=0; i<conf->raid_disks; i++) { |
| 2042 | bio = r1_bio->bios[i]; |
| 2043 | if (bio->bi_end_io == end_sync_read) { |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 2044 | md_sync_acct(bio->bi_bdev, nr_sectors); |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2045 | generic_make_request(bio); |
| 2046 | } |
| 2047 | } |
| 2048 | } else { |
| 2049 | atomic_set(&r1_bio->remaining, 1); |
| 2050 | bio = r1_bio->bios[r1_bio->read_disk]; |
NeilBrown | ddac7c7 | 2006-08-31 21:27:36 -0700 | [diff] [blame] | 2051 | md_sync_acct(bio->bi_bdev, nr_sectors); |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2052 | generic_make_request(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
NeilBrown | d11c171 | 2006-01-06 00:20:26 -0800 | [diff] [blame] | 2054 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | return nr_sectors; |
| 2056 | } |
| 2057 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2058 | static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 2059 | { |
| 2060 | if (sectors) |
| 2061 | return sectors; |
| 2062 | |
| 2063 | return mddev->dev_sectors; |
| 2064 | } |
| 2065 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2066 | static conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | { |
| 2068 | conf_t *conf; |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2069 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | mirror_info_t *disk; |
| 2071 | mdk_rdev_t *rdev; |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2072 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2074 | conf = kzalloc(sizeof(conf_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | if (!conf) |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2076 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2078 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | GFP_KERNEL); |
| 2080 | if (!conf->mirrors) |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2081 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 2083 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 2084 | if (!conf->tmppage) |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2085 | goto abort; |
NeilBrown | ddaf22a | 2006-01-06 00:20:19 -0800 | [diff] [blame] | 2086 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2087 | conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | if (!conf->poolinfo) |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2089 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | conf->poolinfo->raid_disks = mddev->raid_disks; |
| 2091 | conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, |
| 2092 | r1bio_pool_free, |
| 2093 | conf->poolinfo); |
| 2094 | if (!conf->r1bio_pool) |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2095 | goto abort; |
| 2096 | |
NeilBrown | ed9bfdf | 2009-10-16 15:55:44 +1100 | [diff] [blame] | 2097 | conf->poolinfo->mddev = mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 2099 | spin_lock_init(&conf->device_lock); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2100 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2101 | int disk_idx = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | if (disk_idx >= mddev->raid_disks |
| 2103 | || disk_idx < 0) |
| 2104 | continue; |
| 2105 | disk = conf->mirrors + disk_idx; |
| 2106 | |
| 2107 | disk->rdev = rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
| 2109 | disk->head_position = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } |
| 2111 | conf->raid_disks = mddev->raid_disks; |
| 2112 | conf->mddev = mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | INIT_LIST_HEAD(&conf->retry_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
| 2115 | spin_lock_init(&conf->resync_lock); |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 2116 | init_waitqueue_head(&conf->wait_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2118 | bio_list_init(&conf->pending_bio_list); |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2119 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2120 | conf->last_used = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | for (i = 0; i < conf->raid_disks; i++) { |
| 2122 | |
| 2123 | disk = conf->mirrors + i; |
| 2124 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2125 | if (!disk->rdev || |
| 2126 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | disk->head_position = 0; |
NeilBrown | 918f023 | 2007-08-22 14:01:52 -0700 | [diff] [blame] | 2128 | if (disk->rdev) |
| 2129 | conf->fullsync = 1; |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2130 | } else if (conf->last_used < 0) |
| 2131 | /* |
| 2132 | * The first working device is used as a |
| 2133 | * starting point to read balancing. |
| 2134 | */ |
| 2135 | conf->last_used = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2137 | |
| 2138 | err = -EIO; |
| 2139 | if (conf->last_used < 0) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2140 | printk(KERN_ERR "md/raid1:%s: no operational mirrors\n", |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2141 | mdname(mddev)); |
| 2142 | goto abort; |
NeilBrown | 11ce99e | 2006-10-03 01:15:52 -0700 | [diff] [blame] | 2143 | } |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2144 | err = -ENOMEM; |
| 2145 | conf->thread = md_register_thread(raid1d, mddev, NULL); |
| 2146 | if (!conf->thread) { |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2147 | printk(KERN_ERR |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2148 | "md/raid1:%s: couldn't allocate thread\n", |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2149 | mdname(mddev)); |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2150 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | } |
NeilBrown | 191ea9b | 2005-06-21 17:17:23 -0700 | [diff] [blame] | 2152 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2153 | return conf; |
| 2154 | |
| 2155 | abort: |
| 2156 | if (conf) { |
| 2157 | if (conf->r1bio_pool) |
| 2158 | mempool_destroy(conf->r1bio_pool); |
| 2159 | kfree(conf->mirrors); |
| 2160 | safe_put_page(conf->tmppage); |
| 2161 | kfree(conf->poolinfo); |
| 2162 | kfree(conf); |
| 2163 | } |
| 2164 | return ERR_PTR(err); |
| 2165 | } |
| 2166 | |
| 2167 | static int run(mddev_t *mddev) |
| 2168 | { |
| 2169 | conf_t *conf; |
| 2170 | int i; |
| 2171 | mdk_rdev_t *rdev; |
| 2172 | |
| 2173 | if (mddev->level != 1) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2174 | printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n", |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2175 | mdname(mddev), mddev->level); |
| 2176 | return -EIO; |
| 2177 | } |
| 2178 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2179 | printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n", |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2180 | mdname(mddev)); |
| 2181 | return -EIO; |
| 2182 | } |
| 2183 | /* |
| 2184 | * copy the already verified devices into our private RAID1 |
| 2185 | * bookkeeping area. [whatever we allocate in run(), |
| 2186 | * should be freed in stop()] |
| 2187 | */ |
| 2188 | if (mddev->private == NULL) |
| 2189 | conf = setup_conf(mddev); |
| 2190 | else |
| 2191 | conf = mddev->private; |
| 2192 | |
| 2193 | if (IS_ERR(conf)) |
| 2194 | return PTR_ERR(conf); |
| 2195 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2196 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 2197 | if (!mddev->gendisk) |
| 2198 | continue; |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2199 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 2200 | rdev->data_offset << 9); |
| 2201 | /* as we don't honour merge_bvec_fn, we must never risk |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 2202 | * violating it, so limit ->max_segments to 1 lying within |
| 2203 | * a single page, as a one page request is never in violation. |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2204 | */ |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 2205 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { |
| 2206 | blk_queue_max_segments(mddev->queue, 1); |
| 2207 | blk_queue_segment_boundary(mddev->queue, |
| 2208 | PAGE_CACHE_SIZE - 1); |
| 2209 | } |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | mddev->degraded = 0; |
| 2213 | for (i=0; i < conf->raid_disks; i++) |
| 2214 | if (conf->mirrors[i].rdev == NULL || |
| 2215 | !test_bit(In_sync, &conf->mirrors[i].rdev->flags) || |
| 2216 | test_bit(Faulty, &conf->mirrors[i].rdev->flags)) |
| 2217 | mddev->degraded++; |
| 2218 | |
| 2219 | if (conf->raid_disks - mddev->degraded == 1) |
| 2220 | mddev->recovery_cp = MaxSector; |
| 2221 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 2222 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2223 | printk(KERN_NOTICE "md/raid1:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 2224 | " -- starting background reconstruction\n", |
| 2225 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | printk(KERN_INFO |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2227 | "md/raid1:%s: active with %d out of %d mirrors\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 2229 | mddev->raid_disks); |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | /* |
| 2232 | * Ok, everything is just fine now |
| 2233 | */ |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2234 | mddev->thread = conf->thread; |
| 2235 | conf->thread = NULL; |
| 2236 | mddev->private = conf; |
| 2237 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 2238 | md_set_array_sectors(mddev, raid1_size(mddev, 0, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | |
Jonathan Brassow | 1ed7242 | 2011-06-07 17:50:35 -0500 | [diff] [blame] | 2240 | if (mddev->queue) { |
| 2241 | mddev->queue->backing_dev_info.congested_fn = raid1_congested; |
| 2242 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 2243 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2244 | return md_integrity_register(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | static int stop(mddev_t *mddev) |
| 2248 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2249 | conf_t *conf = mddev->private; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 2250 | struct bitmap *bitmap = mddev->bitmap; |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 2251 | |
| 2252 | /* wait for behind writes to complete */ |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 2253 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
NeilBrown | 9dd1e2f | 2010-05-03 14:30:35 +1000 | [diff] [blame] | 2254 | printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n", |
| 2255 | mdname(mddev)); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 2256 | /* need to kick something here to make sure I/O goes? */ |
NeilBrown | e555190 | 2010-03-31 11:21:44 +1100 | [diff] [blame] | 2257 | wait_event(bitmap->behind_wait, |
| 2258 | atomic_read(&bitmap->behind_writes) == 0); |
NeilBrown | 4b6d287 | 2005-09-09 16:23:47 -0700 | [diff] [blame] | 2259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2261 | raise_barrier(conf); |
| 2262 | lower_barrier(conf); |
| 2263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | md_unregister_thread(mddev->thread); |
| 2265 | mddev->thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | if (conf->r1bio_pool) |
| 2267 | mempool_destroy(conf->r1bio_pool); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 2268 | kfree(conf->mirrors); |
| 2269 | kfree(conf->poolinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | kfree(conf); |
| 2271 | mddev->private = NULL; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | static int raid1_resize(mddev_t *mddev, sector_t sectors) |
| 2276 | { |
| 2277 | /* no resync is happening, and there is enough space |
| 2278 | * on all devices, so we can resize. |
| 2279 | * We need to make sure resync covers any new space. |
| 2280 | * If the array is shrinking we should possibly wait until |
| 2281 | * any io in the removed space completes, but it hardly seems |
| 2282 | * worth it. |
| 2283 | */ |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 2284 | md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0)); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2285 | if (mddev->array_sectors > raid1_size(mddev, sectors, 0)) |
| 2286 | return -EINVAL; |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 2287 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 2288 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2289 | if (sectors > mddev->dev_sectors && |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 2290 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2291 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2293 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2294 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 2295 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2299 | static int raid1_reshape(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | { |
| 2301 | /* We need to: |
| 2302 | * 1/ resize the r1bio_pool |
| 2303 | * 2/ resize conf->mirrors |
| 2304 | * |
| 2305 | * We allocate a new r1bio_pool if we can. |
| 2306 | * Then raise a device barrier and wait until all IO stops. |
| 2307 | * Then resize conf->mirrors and swap in the new r1bio pool. |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2308 | * |
| 2309 | * At the same time, we "pack" the devices so that all the missing |
| 2310 | * devices have the higher raid_disk numbers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2311 | */ |
| 2312 | mempool_t *newpool, *oldpool; |
| 2313 | struct pool_info *newpoolinfo; |
| 2314 | mirror_info_t *newmirrors; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2315 | conf_t *conf = mddev->private; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2316 | int cnt, raid_disks; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 2317 | unsigned long flags; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2318 | int d, d2, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2320 | /* Cannot change chunk_size, layout, or level */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2321 | if (mddev->chunk_sectors != mddev->new_chunk_sectors || |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2322 | mddev->layout != mddev->new_layout || |
| 2323 | mddev->level != mddev->new_level) { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2324 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2325 | mddev->new_layout = mddev->layout; |
| 2326 | mddev->new_level = mddev->level; |
| 2327 | return -EINVAL; |
| 2328 | } |
| 2329 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2330 | err = md_allow_write(mddev); |
| 2331 | if (err) |
| 2332 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2333 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2334 | raid_disks = mddev->raid_disks + mddev->delta_disks; |
| 2335 | |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2336 | if (raid_disks < conf->raid_disks) { |
| 2337 | cnt=0; |
| 2338 | for (d= 0; d < conf->raid_disks; d++) |
| 2339 | if (conf->mirrors[d].rdev) |
| 2340 | cnt++; |
| 2341 | if (cnt > raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | return -EBUSY; |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
| 2345 | newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); |
| 2346 | if (!newpoolinfo) |
| 2347 | return -ENOMEM; |
| 2348 | newpoolinfo->mddev = mddev; |
| 2349 | newpoolinfo->raid_disks = raid_disks; |
| 2350 | |
| 2351 | newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, |
| 2352 | r1bio_pool_free, newpoolinfo); |
| 2353 | if (!newpool) { |
| 2354 | kfree(newpoolinfo); |
| 2355 | return -ENOMEM; |
| 2356 | } |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2357 | newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (!newmirrors) { |
| 2359 | kfree(newpoolinfo); |
| 2360 | mempool_destroy(newpool); |
| 2361 | return -ENOMEM; |
| 2362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 2364 | raise_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | |
| 2366 | /* ok, everything is stopped */ |
| 2367 | oldpool = conf->r1bio_pool; |
| 2368 | conf->r1bio_pool = newpool; |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2369 | |
NeilBrown | a88aa78 | 2007-08-22 14:01:53 -0700 | [diff] [blame] | 2370 | for (d = d2 = 0; d < conf->raid_disks; d++) { |
| 2371 | mdk_rdev_t *rdev = conf->mirrors[d].rdev; |
| 2372 | if (rdev && rdev->raid_disk != d2) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2373 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | a88aa78 | 2007-08-22 14:01:53 -0700 | [diff] [blame] | 2374 | rdev->raid_disk = d2; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2375 | sysfs_unlink_rdev(mddev, rdev); |
| 2376 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | a88aa78 | 2007-08-22 14:01:53 -0700 | [diff] [blame] | 2377 | printk(KERN_WARNING |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2378 | "md/raid1:%s: cannot register rd%d\n", |
| 2379 | mdname(mddev), rdev->raid_disk); |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2380 | } |
NeilBrown | a88aa78 | 2007-08-22 14:01:53 -0700 | [diff] [blame] | 2381 | if (rdev) |
| 2382 | newmirrors[d2++].rdev = rdev; |
| 2383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | kfree(conf->mirrors); |
| 2385 | conf->mirrors = newmirrors; |
| 2386 | kfree(conf->poolinfo); |
| 2387 | conf->poolinfo = newpoolinfo; |
| 2388 | |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 2389 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | mddev->degraded += (raid_disks - conf->raid_disks); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 2391 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | conf->raid_disks = mddev->raid_disks = raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2393 | mddev->delta_disks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 2395 | conf->last_used = 0; /* just make sure it is in-range */ |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 2396 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
| 2398 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2399 | md_wakeup_thread(mddev->thread); |
| 2400 | |
| 2401 | mempool_destroy(oldpool); |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
NeilBrown | 500af87 | 2005-09-09 16:23:58 -0700 | [diff] [blame] | 2405 | static void raid1_quiesce(mddev_t *mddev, int state) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2406 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2407 | conf_t *conf = mddev->private; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2408 | |
| 2409 | switch(state) { |
NeilBrown | 6eef4b2 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2410 | case 2: /* wake for suspend */ |
| 2411 | wake_up(&conf->wait_barrier); |
| 2412 | break; |
NeilBrown | 9e6603d | 2005-09-09 16:23:48 -0700 | [diff] [blame] | 2413 | case 1: |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 2414 | raise_barrier(conf); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2415 | break; |
NeilBrown | 9e6603d | 2005-09-09 16:23:48 -0700 | [diff] [blame] | 2416 | case 0: |
NeilBrown | 17999be | 2006-01-06 00:20:12 -0800 | [diff] [blame] | 2417 | lower_barrier(conf); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2418 | break; |
| 2419 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2422 | static void *raid1_takeover(mddev_t *mddev) |
| 2423 | { |
| 2424 | /* raid1 can take over: |
| 2425 | * raid5 with 2 devices, any layout or chunk size |
| 2426 | */ |
| 2427 | if (mddev->level == 5 && mddev->raid_disks == 2) { |
| 2428 | conf_t *conf; |
| 2429 | mddev->new_level = 1; |
| 2430 | mddev->new_layout = 0; |
| 2431 | mddev->new_chunk_sectors = 0; |
| 2432 | conf = setup_conf(mddev); |
| 2433 | if (!IS_ERR(conf)) |
| 2434 | conf->barrier = 1; |
| 2435 | return conf; |
| 2436 | } |
| 2437 | return ERR_PTR(-EINVAL); |
| 2438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2440 | static struct mdk_personality raid1_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | { |
| 2442 | .name = "raid1", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2443 | .level = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | .owner = THIS_MODULE, |
| 2445 | .make_request = make_request, |
| 2446 | .run = run, |
| 2447 | .stop = stop, |
| 2448 | .status = status, |
| 2449 | .error_handler = error, |
| 2450 | .hot_add_disk = raid1_add_disk, |
| 2451 | .hot_remove_disk= raid1_remove_disk, |
| 2452 | .spare_active = raid1_spare_active, |
| 2453 | .sync_request = sync_request, |
| 2454 | .resize = raid1_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2455 | .size = raid1_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2456 | .check_reshape = raid1_reshape, |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2457 | .quiesce = raid1_quiesce, |
NeilBrown | 709ae48 | 2009-12-14 12:49:51 +1100 | [diff] [blame] | 2458 | .takeover = raid1_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | }; |
| 2460 | |
| 2461 | static int __init raid_init(void) |
| 2462 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2463 | return register_md_personality(&raid1_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | static void raid_exit(void) |
| 2467 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2468 | unregister_md_personality(&raid1_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | module_init(raid_init); |
| 2472 | module_exit(raid_exit); |
| 2473 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2474 | MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | MODULE_ALIAS("md-personality-3"); /* RAID1 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2476 | MODULE_ALIAS("md-raid1"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2477 | MODULE_ALIAS("md-level-1"); |