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