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