Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid10.c : Multiple Devices driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 2000-2004 Neil Brown |
| 5 | * |
| 6 | * RAID-10 support for md. |
| 7 | * |
| 8 | * Base on code in raid1.c. See raid1.c for futher copyright information. |
| 9 | * |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * (for example /usr/src/linux/COPYING); if not, write to the Free |
| 18 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 21 | #include "dm-bio-list.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/raid/raid10.h> |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 23 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * RAID10 provides a combination of RAID0 and RAID1 functionality. |
| 27 | * The layout of data is defined by |
| 28 | * chunk_size |
| 29 | * raid_disks |
| 30 | * near_copies (stored in low byte of layout) |
| 31 | * far_copies (stored in second byte of layout) |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 32 | * far_offset (stored in bit 16 of layout ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | * The data to be stored is divided into chunks using chunksize. |
| 35 | * Each device is divided into far_copies sections. |
| 36 | * In each section, chunks are laid out in a style similar to raid0, but |
| 37 | * near_copies copies of each chunk is stored (each on a different drive). |
| 38 | * The starting device for each section is offset near_copies from the starting |
| 39 | * device of the previous section. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 40 | * Thus they are (near_copies*far_copies) of each chunk, and each is on a different |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * drive. |
| 42 | * near_copies and far_copies must be at least one, and their product is at most |
| 43 | * raid_disks. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 44 | * |
| 45 | * If far_offset is true, then the far_copies are handled a bit differently. |
| 46 | * The copies are still in different stripes, but instead of be very far apart |
| 47 | * on disk, there are adjacent stripes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * Number of guaranteed r10bios in case of extreme VM load: |
| 52 | */ |
| 53 | #define NR_RAID10_BIOS 256 |
| 54 | |
| 55 | static void unplug_slaves(mddev_t *mddev); |
| 56 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 57 | static void allow_barrier(conf_t *conf); |
| 58 | static void lower_barrier(conf_t *conf); |
| 59 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 60 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | conf_t *conf = data; |
| 63 | r10bio_t *r10_bio; |
| 64 | int size = offsetof(struct r10bio_s, devs[conf->copies]); |
| 65 | |
| 66 | /* allocate a r10bio with room for raid_disks entries in the bios array */ |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 67 | r10_bio = kzalloc(size, gfp_flags); |
| 68 | if (!r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | unplug_slaves(conf->mddev); |
| 70 | |
| 71 | return r10_bio; |
| 72 | } |
| 73 | |
| 74 | static void r10bio_pool_free(void *r10_bio, void *data) |
| 75 | { |
| 76 | kfree(r10_bio); |
| 77 | } |
| 78 | |
| 79 | #define RESYNC_BLOCK_SIZE (64*1024) |
| 80 | //#define RESYNC_BLOCK_SIZE PAGE_SIZE |
| 81 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) |
| 82 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
| 83 | #define RESYNC_WINDOW (2048*1024) |
| 84 | |
| 85 | /* |
| 86 | * When performing a resync, we need to read and compare, so |
| 87 | * we need as many pages are there are copies. |
| 88 | * When performing a recovery, we need 2 bios, one for read, |
| 89 | * one for write (we recover only one drive per r10buf) |
| 90 | * |
| 91 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 92 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | conf_t *conf = data; |
| 95 | struct page *page; |
| 96 | r10bio_t *r10_bio; |
| 97 | struct bio *bio; |
| 98 | int i, j; |
| 99 | int nalloc; |
| 100 | |
| 101 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); |
| 102 | if (!r10_bio) { |
| 103 | unplug_slaves(conf->mddev); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) |
| 108 | nalloc = conf->copies; /* resync */ |
| 109 | else |
| 110 | nalloc = 2; /* recovery */ |
| 111 | |
| 112 | /* |
| 113 | * Allocate bios. |
| 114 | */ |
| 115 | for (j = nalloc ; j-- ; ) { |
| 116 | bio = bio_alloc(gfp_flags, RESYNC_PAGES); |
| 117 | if (!bio) |
| 118 | goto out_free_bio; |
| 119 | r10_bio->devs[j].bio = bio; |
| 120 | } |
| 121 | /* |
| 122 | * Allocate RESYNC_PAGES data pages and attach them |
| 123 | * where needed. |
| 124 | */ |
| 125 | for (j = 0 ; j < nalloc; j++) { |
| 126 | bio = r10_bio->devs[j].bio; |
| 127 | for (i = 0; i < RESYNC_PAGES; i++) { |
| 128 | page = alloc_page(gfp_flags); |
| 129 | if (unlikely(!page)) |
| 130 | goto out_free_pages; |
| 131 | |
| 132 | bio->bi_io_vec[i].bv_page = page; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return r10_bio; |
| 137 | |
| 138 | out_free_pages: |
| 139 | for ( ; i > 0 ; i--) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 140 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | while (j--) |
| 142 | for (i = 0; i < RESYNC_PAGES ; i++) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 143 | safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | j = -1; |
| 145 | out_free_bio: |
| 146 | while ( ++j < nalloc ) |
| 147 | bio_put(r10_bio->devs[j].bio); |
| 148 | r10bio_pool_free(r10_bio, conf); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | static void r10buf_pool_free(void *__r10_bio, void *data) |
| 153 | { |
| 154 | int i; |
| 155 | conf_t *conf = data; |
| 156 | r10bio_t *r10bio = __r10_bio; |
| 157 | int j; |
| 158 | |
| 159 | for (j=0; j < conf->copies; j++) { |
| 160 | struct bio *bio = r10bio->devs[j].bio; |
| 161 | if (bio) { |
| 162 | for (i = 0; i < RESYNC_PAGES; i++) { |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 163 | safe_put_page(bio->bi_io_vec[i].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | bio->bi_io_vec[i].bv_page = NULL; |
| 165 | } |
| 166 | bio_put(bio); |
| 167 | } |
| 168 | } |
| 169 | r10bio_pool_free(r10bio, conf); |
| 170 | } |
| 171 | |
| 172 | static void put_all_bios(conf_t *conf, r10bio_t *r10_bio) |
| 173 | { |
| 174 | int i; |
| 175 | |
| 176 | for (i = 0; i < conf->copies; i++) { |
| 177 | struct bio **bio = & r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 178 | if (*bio && *bio != IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | bio_put(*bio); |
| 180 | *bio = NULL; |
| 181 | } |
| 182 | } |
| 183 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 184 | static void free_r10bio(r10bio_t *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
| 187 | |
| 188 | /* |
| 189 | * Wake up any possible resync thread that waits for the device |
| 190 | * to go idle. |
| 191 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 192 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | put_all_bios(conf, r10_bio); |
| 195 | mempool_free(r10_bio, conf->r10bio_pool); |
| 196 | } |
| 197 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 198 | static void put_buf(r10bio_t *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | mempool_free(r10_bio, conf->r10buf_pool); |
| 203 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 204 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void reschedule_retry(r10bio_t *r10_bio) |
| 208 | { |
| 209 | unsigned long flags; |
| 210 | mddev_t *mddev = r10_bio->mddev; |
| 211 | conf_t *conf = mddev_to_conf(mddev); |
| 212 | |
| 213 | spin_lock_irqsave(&conf->device_lock, flags); |
| 214 | list_add(&r10_bio->retry_list, &conf->retry_list); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 215 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 217 | |
| 218 | md_wakeup_thread(mddev->thread); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 223 | * operation and are ready to return a success/failure code to the buffer |
| 224 | * cache layer. |
| 225 | */ |
| 226 | static void raid_end_bio_io(r10bio_t *r10_bio) |
| 227 | { |
| 228 | struct bio *bio = r10_bio->master_bio; |
| 229 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 230 | bio_endio(bio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO); |
| 232 | free_r10bio(r10_bio); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Update disk head position estimator based on IRQ completion info. |
| 237 | */ |
| 238 | static inline void update_head_pos(int slot, r10bio_t *r10_bio) |
| 239 | { |
| 240 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
| 241 | |
| 242 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = |
| 243 | r10_bio->devs[slot].addr + (r10_bio->sectors); |
| 244 | } |
| 245 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 246 | static void raid10_end_read_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 249 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); |
| 250 | int slot, dev; |
| 251 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | slot = r10_bio->read_slot; |
| 255 | dev = r10_bio->devs[slot].devnum; |
| 256 | /* |
| 257 | * this branch is our 'one mirror IO has finished' event handler: |
| 258 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 259 | update_head_pos(slot, r10_bio); |
| 260 | |
| 261 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* |
| 263 | * Set R10BIO_Uptodate in our master bio, so that |
| 264 | * we will return a good error code to the higher |
| 265 | * levels even if IO on some other mirrored buffer fails. |
| 266 | * |
| 267 | * The 'master' represents the composite IO operation to |
| 268 | * user-side. So if something waits for IO, then it will |
| 269 | * wait for the 'master' bio. |
| 270 | */ |
| 271 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | raid_end_bio_io(r10_bio); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 273 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | /* |
| 275 | * oops, read error: |
| 276 | */ |
| 277 | char b[BDEVNAME_SIZE]; |
| 278 | if (printk_ratelimit()) |
| 279 | printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n", |
| 280 | bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector); |
| 281 | reschedule_retry(r10_bio); |
| 282 | } |
| 283 | |
| 284 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 287 | static void raid10_end_write_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 290 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); |
| 291 | int slot, dev; |
| 292 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | for (slot = 0; slot < conf->copies; slot++) |
| 295 | if (r10_bio->devs[slot].bio == bio) |
| 296 | break; |
| 297 | dev = r10_bio->devs[slot].devnum; |
| 298 | |
| 299 | /* |
| 300 | * this branch is our 'one mirror IO has finished' event handler: |
| 301 | */ |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 302 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | md_error(r10_bio->mddev, conf->mirrors[dev].rdev); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 304 | /* an I/O failed, we can't clear the bitmap */ |
| 305 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 306 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* |
| 308 | * Set R10BIO_Uptodate in our master bio, so that |
| 309 | * we will return a good error code for to the higher |
| 310 | * levels even if IO on some other mirrored buffer fails. |
| 311 | * |
| 312 | * The 'master' represents the composite IO operation to |
| 313 | * user-side. So if something waits for IO, then it will |
| 314 | * wait for the 'master' bio. |
| 315 | */ |
| 316 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
| 317 | |
| 318 | update_head_pos(slot, r10_bio); |
| 319 | |
| 320 | /* |
| 321 | * |
| 322 | * Let's see if all mirrored write operations have finished |
| 323 | * already. |
| 324 | */ |
| 325 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 326 | /* clear the bitmap if all writes complete successfully */ |
| 327 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 328 | r10_bio->sectors, |
| 329 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 330 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | md_write_end(r10_bio->mddev); |
| 332 | raid_end_bio_io(r10_bio); |
| 333 | } |
| 334 | |
| 335 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
| 339 | /* |
| 340 | * RAID10 layout manager |
| 341 | * Aswell as the chunksize and raid_disks count, there are two |
| 342 | * parameters: near_copies and far_copies. |
| 343 | * near_copies * far_copies must be <= raid_disks. |
| 344 | * Normally one of these will be 1. |
| 345 | * If both are 1, we get raid0. |
| 346 | * If near_copies == raid_disks, we get raid1. |
| 347 | * |
| 348 | * Chunks are layed out in raid0 style with near_copies copies of the |
| 349 | * first chunk, followed by near_copies copies of the next chunk and |
| 350 | * so on. |
| 351 | * If far_copies > 1, then after 1/far_copies of the array has been assigned |
| 352 | * as described above, we start again with a device offset of near_copies. |
| 353 | * So we effectively have another copy of the whole array further down all |
| 354 | * the drives, but with blocks on different drives. |
| 355 | * With this layout, and block is never stored twice on the one device. |
| 356 | * |
| 357 | * raid10_find_phys finds the sector offset of a given virtual sector |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 358 | * on each device that it is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | * |
| 360 | * raid10_find_virt does the reverse mapping, from a device and a |
| 361 | * sector offset to a virtual address |
| 362 | */ |
| 363 | |
| 364 | static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio) |
| 365 | { |
| 366 | int n,f; |
| 367 | sector_t sector; |
| 368 | sector_t chunk; |
| 369 | sector_t stripe; |
| 370 | int dev; |
| 371 | |
| 372 | int slot = 0; |
| 373 | |
| 374 | /* now calculate first sector/dev */ |
| 375 | chunk = r10bio->sector >> conf->chunk_shift; |
| 376 | sector = r10bio->sector & conf->chunk_mask; |
| 377 | |
| 378 | chunk *= conf->near_copies; |
| 379 | stripe = chunk; |
| 380 | dev = sector_div(stripe, conf->raid_disks); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 381 | if (conf->far_offset) |
| 382 | stripe *= conf->far_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | sector += stripe << conf->chunk_shift; |
| 385 | |
| 386 | /* and calculate all the others */ |
| 387 | for (n=0; n < conf->near_copies; n++) { |
| 388 | int d = dev; |
| 389 | sector_t s = sector; |
| 390 | r10bio->devs[slot].addr = sector; |
| 391 | r10bio->devs[slot].devnum = d; |
| 392 | slot++; |
| 393 | |
| 394 | for (f = 1; f < conf->far_copies; f++) { |
| 395 | d += conf->near_copies; |
| 396 | if (d >= conf->raid_disks) |
| 397 | d -= conf->raid_disks; |
| 398 | s += conf->stride; |
| 399 | r10bio->devs[slot].devnum = d; |
| 400 | r10bio->devs[slot].addr = s; |
| 401 | slot++; |
| 402 | } |
| 403 | dev++; |
| 404 | if (dev >= conf->raid_disks) { |
| 405 | dev = 0; |
| 406 | sector += (conf->chunk_mask + 1); |
| 407 | } |
| 408 | } |
| 409 | BUG_ON(slot != conf->copies); |
| 410 | } |
| 411 | |
| 412 | static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev) |
| 413 | { |
| 414 | sector_t offset, chunk, vchunk; |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | offset = sector & conf->chunk_mask; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 417 | if (conf->far_offset) { |
| 418 | int fc; |
| 419 | chunk = sector >> conf->chunk_shift; |
| 420 | fc = sector_div(chunk, conf->far_copies); |
| 421 | dev -= fc * conf->near_copies; |
| 422 | if (dev < 0) |
| 423 | dev += conf->raid_disks; |
| 424 | } else { |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 425 | while (sector >= conf->stride) { |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 426 | sector -= conf->stride; |
| 427 | if (dev < conf->near_copies) |
| 428 | dev += conf->raid_disks - conf->near_copies; |
| 429 | else |
| 430 | dev -= conf->near_copies; |
| 431 | } |
| 432 | chunk = sector >> conf->chunk_shift; |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | vchunk = chunk * conf->raid_disks + dev; |
| 435 | sector_div(vchunk, conf->near_copies); |
| 436 | return (vchunk << conf->chunk_shift) + offset; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged |
| 441 | * @q: request queue |
| 442 | * @bio: the buffer head that's been built up so far |
| 443 | * @biovec: the request that could be merged to it. |
| 444 | * |
| 445 | * Return amount of bytes we can accept at this offset |
| 446 | * If near_copies == raid_disk, there are no striping issues, |
| 447 | * but in that case, the function isn't called at all. |
| 448 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 449 | static int raid10_mergeable_bvec(struct request_queue *q, struct bio *bio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | struct bio_vec *bio_vec) |
| 451 | { |
| 452 | mddev_t *mddev = q->queuedata; |
| 453 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
| 454 | int max; |
| 455 | unsigned int chunk_sectors = mddev->chunk_size >> 9; |
| 456 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 457 | |
| 458 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 459 | if (max < 0) max = 0; /* bio_add cannot handle a negative return */ |
| 460 | if (max <= bio_vec->bv_len && bio_sectors == 0) |
| 461 | return bio_vec->bv_len; |
| 462 | else |
| 463 | return max; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * This routine returns the disk from which the requested read should |
| 468 | * be done. There is a per-array 'next expected sequential IO' sector |
| 469 | * number - if this matches on the next IO then we use the last disk. |
| 470 | * There is also a per-disk 'last know head position' sector that is |
| 471 | * maintained from IRQ contexts, both the normal and the resync IO |
| 472 | * completion handlers update this position correctly. If there is no |
| 473 | * perfect sequential match then we pick the disk whose head is closest. |
| 474 | * |
| 475 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 476 | * because position is mirror, not device based. |
| 477 | * |
| 478 | * The rdev for the device selected will have nr_pending incremented. |
| 479 | */ |
| 480 | |
| 481 | /* |
| 482 | * FIXME: possibly should rethink readbalancing and do it differently |
| 483 | * depending on near_copies / far_copies geometry. |
| 484 | */ |
| 485 | static int read_balance(conf_t *conf, r10bio_t *r10_bio) |
| 486 | { |
| 487 | const unsigned long this_sector = r10_bio->sector; |
| 488 | int disk, slot, nslot; |
| 489 | const int sectors = r10_bio->sectors; |
| 490 | sector_t new_distance, current_distance; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 491 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | raid10_find_phys(conf, r10_bio); |
| 494 | rcu_read_lock(); |
| 495 | /* |
| 496 | * Check if we can balance. We can balance on the whole |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 497 | * device if no resync is going on (recovery is ok), or below |
| 498 | * the resync window. We take the first readable disk when |
| 499 | * above the resync window. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | */ |
| 501 | if (conf->mddev->recovery_cp < MaxSector |
| 502 | && (this_sector + sectors >= conf->next_resync)) { |
| 503 | /* make sure that disk is operational */ |
| 504 | slot = 0; |
| 505 | disk = r10_bio->devs[slot].devnum; |
| 506 | |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 507 | while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL || |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 508 | r10_bio->devs[slot].bio == IO_BLOCKED || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 509 | !test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | slot++; |
| 511 | if (slot == conf->copies) { |
| 512 | slot = 0; |
| 513 | disk = -1; |
| 514 | break; |
| 515 | } |
| 516 | disk = r10_bio->devs[slot].devnum; |
| 517 | } |
| 518 | goto rb_out; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /* make sure the disk is operational */ |
| 523 | slot = 0; |
| 524 | disk = r10_bio->devs[slot].devnum; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 525 | while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL || |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 526 | r10_bio->devs[slot].bio == IO_BLOCKED || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 527 | !test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | slot ++; |
| 529 | if (slot == conf->copies) { |
| 530 | disk = -1; |
| 531 | goto rb_out; |
| 532 | } |
| 533 | disk = r10_bio->devs[slot].devnum; |
| 534 | } |
| 535 | |
| 536 | |
NeilBrown | 3ec67ac | 2005-09-09 16:23:40 -0700 | [diff] [blame] | 537 | current_distance = abs(r10_bio->devs[slot].addr - |
| 538 | conf->mirrors[disk].head_position); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | /* Find the disk whose head is closest */ |
| 541 | |
| 542 | for (nslot = slot; nslot < conf->copies; nslot++) { |
| 543 | int ndisk = r10_bio->devs[nslot].devnum; |
| 544 | |
| 545 | |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 546 | if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL || |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 547 | r10_bio->devs[nslot].bio == IO_BLOCKED || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 548 | !test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | continue; |
| 550 | |
NeilBrown | 22dfdf5 | 2005-11-28 13:44:09 -0800 | [diff] [blame] | 551 | /* This optimisation is debatable, and completely destroys |
| 552 | * sequential read speed for 'far copies' arrays. So only |
| 553 | * keep it for 'near' arrays, and review those later. |
| 554 | */ |
| 555 | if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | disk = ndisk; |
| 557 | slot = nslot; |
| 558 | break; |
| 559 | } |
| 560 | new_distance = abs(r10_bio->devs[nslot].addr - |
| 561 | conf->mirrors[ndisk].head_position); |
| 562 | if (new_distance < current_distance) { |
| 563 | current_distance = new_distance; |
| 564 | disk = ndisk; |
| 565 | slot = nslot; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | rb_out: |
| 570 | r10_bio->read_slot = slot; |
| 571 | /* conf->next_seq_sect = this_sector + sectors;*/ |
| 572 | |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 573 | if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | atomic_inc(&conf->mirrors[disk].rdev->nr_pending); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 575 | else |
| 576 | disk = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | rcu_read_unlock(); |
| 578 | |
| 579 | return disk; |
| 580 | } |
| 581 | |
| 582 | static void unplug_slaves(mddev_t *mddev) |
| 583 | { |
| 584 | conf_t *conf = mddev_to_conf(mddev); |
| 585 | int i; |
| 586 | |
| 587 | rcu_read_lock(); |
| 588 | for (i=0; i<mddev->raid_disks; i++) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 589 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 590 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 591 | struct request_queue *r_queue = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | atomic_inc(&rdev->nr_pending); |
| 594 | rcu_read_unlock(); |
| 595 | |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 596 | blk_unplug(r_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | rdev_dec_pending(rdev, mddev); |
| 599 | rcu_read_lock(); |
| 600 | } |
| 601 | } |
| 602 | rcu_read_unlock(); |
| 603 | } |
| 604 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 605 | static void raid10_unplug(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 607 | mddev_t *mddev = q->queuedata; |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | unplug_slaves(q->queuedata); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 610 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 613 | static int raid10_congested(void *data, int bits) |
| 614 | { |
| 615 | mddev_t *mddev = data; |
| 616 | conf_t *conf = mddev_to_conf(mddev); |
| 617 | int i, ret = 0; |
| 618 | |
| 619 | rcu_read_lock(); |
| 620 | for (i = 0; i < mddev->raid_disks && ret == 0; i++) { |
| 621 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 622 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 623 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 624 | |
| 625 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 626 | } |
| 627 | } |
| 628 | rcu_read_unlock(); |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 633 | /* Barriers.... |
| 634 | * Sometimes we need to suspend IO while we do something else, |
| 635 | * either some resync/recovery, or reconfigure the array. |
| 636 | * To do this we raise a 'barrier'. |
| 637 | * The 'barrier' is a counter that can be raised multiple times |
| 638 | * to count how many activities are happening which preclude |
| 639 | * normal IO. |
| 640 | * We can only raise the barrier if there is no pending IO. |
| 641 | * i.e. if nr_pending == 0. |
| 642 | * We choose only to raise the barrier if no-one is waiting for the |
| 643 | * barrier to go down. This means that as soon as an IO request |
| 644 | * is ready, no other operations which require a barrier will start |
| 645 | * until the IO request has had a chance. |
| 646 | * |
| 647 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 648 | * is no backgroup IO happening, It must arrange to call |
| 649 | * allow_barrier when it has finished its IO. |
| 650 | * backgroup IO calls must call raise_barrier. Once that returns |
| 651 | * there is no normal IO happeing. It must arrange to call |
| 652 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | */ |
| 654 | #define RESYNC_DEPTH 32 |
| 655 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 656 | static void raise_barrier(conf_t *conf, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 658 | BUG_ON(force && !conf->barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | spin_lock_irq(&conf->resync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 661 | /* Wait until no block IO is waiting (unless 'force') */ |
| 662 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 663 | conf->resync_lock, |
| 664 | raid10_unplug(conf->mddev->queue)); |
| 665 | |
| 666 | /* block any new IO from starting */ |
| 667 | conf->barrier++; |
| 668 | |
| 669 | /* No wait for all pending IO to complete */ |
| 670 | wait_event_lock_irq(conf->wait_barrier, |
| 671 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, |
| 672 | conf->resync_lock, |
| 673 | raid10_unplug(conf->mddev->queue)); |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | spin_unlock_irq(&conf->resync_lock); |
| 676 | } |
| 677 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 678 | static void lower_barrier(conf_t *conf) |
| 679 | { |
| 680 | unsigned long flags; |
| 681 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 682 | conf->barrier--; |
| 683 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 684 | wake_up(&conf->wait_barrier); |
| 685 | } |
| 686 | |
| 687 | static void wait_barrier(conf_t *conf) |
| 688 | { |
| 689 | spin_lock_irq(&conf->resync_lock); |
| 690 | if (conf->barrier) { |
| 691 | conf->nr_waiting++; |
| 692 | wait_event_lock_irq(conf->wait_barrier, !conf->barrier, |
| 693 | conf->resync_lock, |
| 694 | raid10_unplug(conf->mddev->queue)); |
| 695 | conf->nr_waiting--; |
| 696 | } |
| 697 | conf->nr_pending++; |
| 698 | spin_unlock_irq(&conf->resync_lock); |
| 699 | } |
| 700 | |
| 701 | static void allow_barrier(conf_t *conf) |
| 702 | { |
| 703 | unsigned long flags; |
| 704 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 705 | conf->nr_pending--; |
| 706 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 707 | wake_up(&conf->wait_barrier); |
| 708 | } |
| 709 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 710 | static void freeze_array(conf_t *conf) |
| 711 | { |
| 712 | /* stop syncio and normal IO and wait for everything to |
NeilBrown | f188593 | 2006-01-06 00:20:42 -0800 | [diff] [blame] | 713 | * go quiet. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 714 | * We increment barrier and nr_waiting, and then |
| 715 | * wait until barrier+nr_pending match nr_queued+2 |
| 716 | */ |
| 717 | spin_lock_irq(&conf->resync_lock); |
| 718 | conf->barrier++; |
| 719 | conf->nr_waiting++; |
| 720 | wait_event_lock_irq(conf->wait_barrier, |
| 721 | conf->barrier+conf->nr_pending == conf->nr_queued+2, |
| 722 | conf->resync_lock, |
| 723 | raid10_unplug(conf->mddev->queue)); |
| 724 | spin_unlock_irq(&conf->resync_lock); |
| 725 | } |
| 726 | |
| 727 | static void unfreeze_array(conf_t *conf) |
| 728 | { |
| 729 | /* reverse the effect of the freeze */ |
| 730 | spin_lock_irq(&conf->resync_lock); |
| 731 | conf->barrier--; |
| 732 | conf->nr_waiting--; |
| 733 | wake_up(&conf->wait_barrier); |
| 734 | spin_unlock_irq(&conf->resync_lock); |
| 735 | } |
| 736 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 737 | static int make_request(struct request_queue *q, struct bio * bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
| 739 | mddev_t *mddev = q->queuedata; |
| 740 | conf_t *conf = mddev_to_conf(mddev); |
| 741 | mirror_info_t *mirror; |
| 742 | r10bio_t *r10_bio; |
| 743 | struct bio *read_bio; |
| 744 | int i; |
| 745 | int chunk_sects = conf->chunk_mask + 1; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 746 | const int rw = bio_data_dir(bio); |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 747 | const int do_sync = bio_sync(bio); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 748 | struct bio_list bl; |
| 749 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 751 | if (unlikely(bio_barrier(bio))) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 752 | bio_endio(bio, -EOPNOTSUPP); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | /* If this request crosses a chunk boundary, we need to |
| 757 | * split it. This will only happen for 1 PAGE (or less) requests. |
| 758 | */ |
| 759 | if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) |
| 760 | > chunk_sects && |
| 761 | conf->near_copies < conf->raid_disks)) { |
| 762 | struct bio_pair *bp; |
| 763 | /* Sanity check -- queue functions should prevent this happening */ |
| 764 | if (bio->bi_vcnt != 1 || |
| 765 | bio->bi_idx != 0) |
| 766 | goto bad_map; |
| 767 | /* This is a one page bio that upper layers |
| 768 | * refuse to split for us, so we need to split it. |
| 769 | */ |
| 770 | bp = bio_split(bio, bio_split_pool, |
| 771 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); |
| 772 | if (make_request(q, &bp->bio1)) |
| 773 | generic_make_request(&bp->bio1); |
| 774 | if (make_request(q, &bp->bio2)) |
| 775 | generic_make_request(&bp->bio2); |
| 776 | |
| 777 | bio_pair_release(bp); |
| 778 | return 0; |
| 779 | bad_map: |
| 780 | printk("raid10_make_request bug: can't convert block across chunks" |
| 781 | " or bigger than %dk %llu %d\n", chunk_sects/2, |
| 782 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
| 783 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 784 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 788 | md_write_start(mddev, bio); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | /* |
| 791 | * Register the new request and wait if the reconstruction |
| 792 | * thread has put up a bar for new requests. |
| 793 | * Continue immediately if no resync is active currently. |
| 794 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 795 | wait_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 797 | disk_stat_inc(mddev->gendisk, ios[rw]); |
| 798 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
| 800 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 801 | |
| 802 | r10_bio->master_bio = bio; |
| 803 | r10_bio->sectors = bio->bi_size >> 9; |
| 804 | |
| 805 | r10_bio->mddev = mddev; |
| 806 | r10_bio->sector = bio->bi_sector; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 807 | r10_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 809 | if (rw == READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | /* |
| 811 | * read balancing logic: |
| 812 | */ |
| 813 | int disk = read_balance(conf, r10_bio); |
| 814 | int slot = r10_bio->read_slot; |
| 815 | if (disk < 0) { |
| 816 | raid_end_bio_io(r10_bio); |
| 817 | return 0; |
| 818 | } |
| 819 | mirror = conf->mirrors + disk; |
| 820 | |
| 821 | read_bio = bio_clone(bio, GFP_NOIO); |
| 822 | |
| 823 | r10_bio->devs[slot].bio = read_bio; |
| 824 | |
| 825 | read_bio->bi_sector = r10_bio->devs[slot].addr + |
| 826 | mirror->rdev->data_offset; |
| 827 | read_bio->bi_bdev = mirror->rdev->bdev; |
| 828 | read_bio->bi_end_io = raid10_end_read_request; |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 829 | read_bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | read_bio->bi_private = r10_bio; |
| 831 | |
| 832 | generic_make_request(read_bio); |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * WRITE: |
| 838 | */ |
| 839 | /* first select target devices under spinlock and |
| 840 | * inc refcount on their rdev. Record them by setting |
| 841 | * bios[x] to bio |
| 842 | */ |
| 843 | raid10_find_phys(conf, r10_bio); |
| 844 | rcu_read_lock(); |
| 845 | for (i = 0; i < conf->copies; i++) { |
| 846 | int d = r10_bio->devs[i].devnum; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 847 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 848 | if (rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 849 | !test_bit(Faulty, &rdev->flags)) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 850 | atomic_inc(&rdev->nr_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | r10_bio->devs[i].bio = bio; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 852 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | r10_bio->devs[i].bio = NULL; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 854 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | rcu_read_unlock(); |
| 858 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 859 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 860 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 861 | bio_list_init(&bl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | for (i = 0; i < conf->copies; i++) { |
| 863 | struct bio *mbio; |
| 864 | int d = r10_bio->devs[i].devnum; |
| 865 | if (!r10_bio->devs[i].bio) |
| 866 | continue; |
| 867 | |
| 868 | mbio = bio_clone(bio, GFP_NOIO); |
| 869 | r10_bio->devs[i].bio = mbio; |
| 870 | |
| 871 | mbio->bi_sector = r10_bio->devs[i].addr+ |
| 872 | conf->mirrors[d].rdev->data_offset; |
| 873 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 874 | mbio->bi_end_io = raid10_end_write_request; |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 875 | mbio->bi_rw = WRITE | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | mbio->bi_private = r10_bio; |
| 877 | |
| 878 | atomic_inc(&r10_bio->remaining); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 879 | bio_list_add(&bl, mbio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Arne Redlich | f6f953a | 2007-07-31 00:37:57 -0700 | [diff] [blame] | 882 | if (unlikely(!atomic_read(&r10_bio->remaining))) { |
| 883 | /* the array is dead */ |
| 884 | md_write_end(mddev); |
| 885 | raid_end_bio_io(r10_bio); |
| 886 | return 0; |
| 887 | } |
| 888 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 889 | bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0); |
| 890 | spin_lock_irqsave(&conf->device_lock, flags); |
| 891 | bio_list_merge(&conf->pending_bio_list, &bl); |
| 892 | blk_plug_device(mddev->queue); |
| 893 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 895 | if (do_sync) |
| 896 | md_wakeup_thread(mddev->thread); |
| 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | static void status(struct seq_file *seq, mddev_t *mddev) |
| 902 | { |
| 903 | conf_t *conf = mddev_to_conf(mddev); |
| 904 | int i; |
| 905 | |
| 906 | if (conf->near_copies < conf->raid_disks) |
| 907 | seq_printf(seq, " %dK chunks", mddev->chunk_size/1024); |
| 908 | if (conf->near_copies > 1) |
| 909 | seq_printf(seq, " %d near-copies", conf->near_copies); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 910 | if (conf->far_copies > 1) { |
| 911 | if (conf->far_offset) |
| 912 | seq_printf(seq, " %d offset-copies", conf->far_copies); |
| 913 | else |
| 914 | seq_printf(seq, " %d far-copies", conf->far_copies); |
| 915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 917 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | for (i = 0; i < conf->raid_disks; i++) |
| 919 | seq_printf(seq, "%s", |
| 920 | conf->mirrors[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 921 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | seq_printf(seq, "]"); |
| 923 | } |
| 924 | |
| 925 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 926 | { |
| 927 | char b[BDEVNAME_SIZE]; |
| 928 | conf_t *conf = mddev_to_conf(mddev); |
| 929 | |
| 930 | /* |
| 931 | * If it is not operational, then we have already marked it as dead |
| 932 | * else if it is the last working disks, ignore the error, let the |
| 933 | * next level up know. |
| 934 | * else mark the drive as failed |
| 935 | */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 936 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 937 | && conf->raid_disks-mddev->degraded == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * Don't fail the drive, just return an IO error. |
| 940 | * The test should really be more sophisticated than |
| 941 | * "working_disks == 1", but it isn't critical, and |
| 942 | * can wait until we do more sophisticated "is the drive |
| 943 | * really dead" tests... |
| 944 | */ |
| 945 | return; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 946 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 947 | unsigned long flags; |
| 948 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 950 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | /* |
| 952 | * if recovery is running, make sure it aborts. |
| 953 | */ |
| 954 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 955 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 956 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 957 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n" |
| 959 | " Operation continuing on %d devices\n", |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 960 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static void print_conf(conf_t *conf) |
| 964 | { |
| 965 | int i; |
| 966 | mirror_info_t *tmp; |
| 967 | |
| 968 | printk("RAID10 conf printout:\n"); |
| 969 | if (!conf) { |
| 970 | printk("(!conf)\n"); |
| 971 | return; |
| 972 | } |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 973 | printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | conf->raid_disks); |
| 975 | |
| 976 | for (i = 0; i < conf->raid_disks; i++) { |
| 977 | char b[BDEVNAME_SIZE]; |
| 978 | tmp = conf->mirrors + i; |
| 979 | if (tmp->rdev) |
| 980 | printk(" disk %d, wo:%d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 981 | i, !test_bit(In_sync, &tmp->rdev->flags), |
| 982 | !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | bdevname(tmp->rdev->bdev,b)); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | static void close_sync(conf_t *conf) |
| 988 | { |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 989 | wait_barrier(conf); |
| 990 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | mempool_destroy(conf->r10buf_pool); |
| 993 | conf->r10buf_pool = NULL; |
| 994 | } |
| 995 | |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 996 | /* check if there are enough drives for |
| 997 | * every block to appear on atleast one |
| 998 | */ |
| 999 | static int enough(conf_t *conf) |
| 1000 | { |
| 1001 | int first = 0; |
| 1002 | |
| 1003 | do { |
| 1004 | int n = conf->copies; |
| 1005 | int cnt = 0; |
| 1006 | while (n--) { |
| 1007 | if (conf->mirrors[first].rdev) |
| 1008 | cnt++; |
| 1009 | first = (first+1) % conf->raid_disks; |
| 1010 | } |
| 1011 | if (cnt == 0) |
| 1012 | return 0; |
| 1013 | } while (first != 0); |
| 1014 | return 1; |
| 1015 | } |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | static int raid10_spare_active(mddev_t *mddev) |
| 1018 | { |
| 1019 | int i; |
| 1020 | conf_t *conf = mddev->private; |
| 1021 | mirror_info_t *tmp; |
| 1022 | |
| 1023 | /* |
| 1024 | * Find all non-in_sync disks within the RAID10 configuration |
| 1025 | * and mark them in_sync |
| 1026 | */ |
| 1027 | for (i = 0; i < conf->raid_disks; i++) { |
| 1028 | tmp = conf->mirrors + i; |
| 1029 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1030 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1031 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
| 1032 | unsigned long flags; |
| 1033 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | mddev->degraded--; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1035 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | print_conf(conf); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1045 | { |
| 1046 | conf_t *conf = mddev->private; |
| 1047 | int found = 0; |
| 1048 | int mirror; |
| 1049 | mirror_info_t *p; |
| 1050 | |
| 1051 | if (mddev->recovery_cp < MaxSector) |
| 1052 | /* only hot-add to in-sync arrays, as recovery is |
| 1053 | * very different from resync |
| 1054 | */ |
| 1055 | return 0; |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 1056 | if (!enough(conf)) |
| 1057 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1059 | if (rdev->saved_raid_disk >= 0 && |
| 1060 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
| 1061 | mirror = rdev->saved_raid_disk; |
| 1062 | else |
| 1063 | mirror = 0; |
| 1064 | for ( ; mirror < mddev->raid_disks; mirror++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | if ( !(p=conf->mirrors+mirror)->rdev) { |
| 1066 | |
| 1067 | blk_queue_stack_limits(mddev->queue, |
| 1068 | rdev->bdev->bd_disk->queue); |
| 1069 | /* as we don't honour merge_bvec_fn, we must never risk |
| 1070 | * violating it, so limit ->max_sector to one PAGE, as |
| 1071 | * a one page request is never in violation. |
| 1072 | */ |
| 1073 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && |
| 1074 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) |
| 1075 | mddev->queue->max_sectors = (PAGE_SIZE>>9); |
| 1076 | |
| 1077 | p->head_position = 0; |
| 1078 | rdev->raid_disk = mirror; |
| 1079 | found = 1; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1080 | if (rdev->saved_raid_disk != mirror) |
| 1081 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 1082 | rcu_assign_pointer(p->rdev, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | print_conf(conf); |
| 1087 | return found; |
| 1088 | } |
| 1089 | |
| 1090 | static int raid10_remove_disk(mddev_t *mddev, int number) |
| 1091 | { |
| 1092 | conf_t *conf = mddev->private; |
| 1093 | int err = 0; |
| 1094 | mdk_rdev_t *rdev; |
| 1095 | mirror_info_t *p = conf->mirrors+ number; |
| 1096 | |
| 1097 | print_conf(conf); |
| 1098 | rdev = p->rdev; |
| 1099 | if (rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1100 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | atomic_read(&rdev->nr_pending)) { |
| 1102 | err = -EBUSY; |
| 1103 | goto abort; |
| 1104 | } |
| 1105 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 1106 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (atomic_read(&rdev->nr_pending)) { |
| 1108 | /* lost the race, try later */ |
| 1109 | err = -EBUSY; |
| 1110 | p->rdev = rdev; |
| 1111 | } |
| 1112 | } |
| 1113 | abort: |
| 1114 | |
| 1115 | print_conf(conf); |
| 1116 | return err; |
| 1117 | } |
| 1118 | |
| 1119 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1120 | static void end_sync_read(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); |
| 1123 | conf_t *conf = mddev_to_conf(r10_bio->mddev); |
| 1124 | int i,d; |
| 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | for (i=0; i<conf->copies; i++) |
| 1127 | if (r10_bio->devs[i].bio == bio) |
| 1128 | break; |
Eric Sesterhenn | b638548 | 2006-04-02 13:34:29 +0200 | [diff] [blame] | 1129 | BUG_ON(i == conf->copies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | update_head_pos(i, r10_bio); |
| 1131 | d = r10_bio->devs[i].devnum; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1132 | |
| 1133 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 1134 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1135 | else { |
| 1136 | atomic_add(r10_bio->sectors, |
| 1137 | &conf->mirrors[d].rdev->corrected_errors); |
| 1138 | if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) |
| 1139 | md_error(r10_bio->mddev, |
| 1140 | conf->mirrors[d].rdev); |
| 1141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
| 1143 | /* for reconstruct, we always reschedule after a read. |
| 1144 | * for resync, only after all reads |
| 1145 | */ |
| 1146 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
| 1147 | atomic_dec_and_test(&r10_bio->remaining)) { |
| 1148 | /* we have read all the blocks, |
| 1149 | * do the comparison in process context in raid10d |
| 1150 | */ |
| 1151 | reschedule_retry(r10_bio); |
| 1152 | } |
| 1153 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1156 | static void end_sync_write(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | { |
| 1158 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 1159 | r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private); |
| 1160 | mddev_t *mddev = r10_bio->mddev; |
| 1161 | conf_t *conf = mddev_to_conf(mddev); |
| 1162 | int i,d; |
| 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | for (i = 0; i < conf->copies; i++) |
| 1165 | if (r10_bio->devs[i].bio == bio) |
| 1166 | break; |
| 1167 | d = r10_bio->devs[i].devnum; |
| 1168 | |
| 1169 | if (!uptodate) |
| 1170 | md_error(mddev, conf->mirrors[d].rdev); |
| 1171 | update_head_pos(i, r10_bio); |
| 1172 | |
| 1173 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1174 | if (r10_bio->master_bio == NULL) { |
| 1175 | /* the primary of several recovery bios */ |
| 1176 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 1177 | put_buf(r10_bio); |
| 1178 | break; |
| 1179 | } else { |
| 1180 | r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio; |
| 1181 | put_buf(r10_bio); |
| 1182 | r10_bio = r10_bio2; |
| 1183 | } |
| 1184 | } |
| 1185 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Note: sync and recover and handled very differently for raid10 |
| 1190 | * This code is for resync. |
| 1191 | * For resync, we read through virtual addresses and read all blocks. |
| 1192 | * If there is any error, we schedule a write. The lowest numbered |
| 1193 | * drive is authoritative. |
| 1194 | * However requests come for physical address, so we need to map. |
| 1195 | * For every physical address there are raid_disks/copies virtual addresses, |
| 1196 | * which is always are least one, but is not necessarly an integer. |
| 1197 | * This means that a physical address can span multiple chunks, so we may |
| 1198 | * have to submit multiple io requests for a single sync request. |
| 1199 | */ |
| 1200 | /* |
| 1201 | * We check if all blocks are in-sync and only write to blocks that |
| 1202 | * aren't in sync |
| 1203 | */ |
| 1204 | static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio) |
| 1205 | { |
| 1206 | conf_t *conf = mddev_to_conf(mddev); |
| 1207 | int i, first; |
| 1208 | struct bio *tbio, *fbio; |
| 1209 | |
| 1210 | atomic_set(&r10_bio->remaining, 1); |
| 1211 | |
| 1212 | /* find the first device with a block */ |
| 1213 | for (i=0; i<conf->copies; i++) |
| 1214 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) |
| 1215 | break; |
| 1216 | |
| 1217 | if (i == conf->copies) |
| 1218 | goto done; |
| 1219 | |
| 1220 | first = i; |
| 1221 | fbio = r10_bio->devs[i].bio; |
| 1222 | |
| 1223 | /* now find blocks with errors */ |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1224 | for (i=0 ; i < conf->copies ; i++) { |
| 1225 | int j, d; |
| 1226 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | tbio = r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1229 | |
| 1230 | if (tbio->bi_end_io != end_sync_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1232 | if (i == first) |
| 1233 | continue; |
| 1234 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
| 1235 | /* We know that the bi_io_vec layout is the same for |
| 1236 | * both 'first' and 'i', so we just compare them. |
| 1237 | * All vec entries are PAGE_SIZE; |
| 1238 | */ |
| 1239 | for (j = 0; j < vcnt; j++) |
| 1240 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), |
| 1241 | page_address(tbio->bi_io_vec[j].bv_page), |
| 1242 | PAGE_SIZE)) |
| 1243 | break; |
| 1244 | if (j == vcnt) |
| 1245 | continue; |
| 1246 | mddev->resync_mismatches += r10_bio->sectors; |
| 1247 | } |
NeilBrown | 18f0881 | 2006-01-06 00:20:25 -0800 | [diff] [blame] | 1248 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 1249 | /* Don't fix anything. */ |
| 1250 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* Ok, we need to write this bio |
| 1252 | * First we need to fixup bv_offset, bv_len and |
| 1253 | * bi_vecs, as the read request might have corrupted these |
| 1254 | */ |
| 1255 | tbio->bi_vcnt = vcnt; |
| 1256 | tbio->bi_size = r10_bio->sectors << 9; |
| 1257 | tbio->bi_idx = 0; |
| 1258 | tbio->bi_phys_segments = 0; |
| 1259 | tbio->bi_hw_segments = 0; |
| 1260 | tbio->bi_hw_front_size = 0; |
| 1261 | tbio->bi_hw_back_size = 0; |
| 1262 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 1263 | tbio->bi_flags |= 1 << BIO_UPTODATE; |
| 1264 | tbio->bi_next = NULL; |
| 1265 | tbio->bi_rw = WRITE; |
| 1266 | tbio->bi_private = r10_bio; |
| 1267 | tbio->bi_sector = r10_bio->devs[i].addr; |
| 1268 | |
| 1269 | for (j=0; j < vcnt ; j++) { |
| 1270 | tbio->bi_io_vec[j].bv_offset = 0; |
| 1271 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; |
| 1272 | |
| 1273 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), |
| 1274 | page_address(fbio->bi_io_vec[j].bv_page), |
| 1275 | PAGE_SIZE); |
| 1276 | } |
| 1277 | tbio->bi_end_io = end_sync_write; |
| 1278 | |
| 1279 | d = r10_bio->devs[i].devnum; |
| 1280 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1281 | atomic_inc(&r10_bio->remaining); |
| 1282 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); |
| 1283 | |
| 1284 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; |
| 1285 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1286 | generic_make_request(tbio); |
| 1287 | } |
| 1288 | |
| 1289 | done: |
| 1290 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1291 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 1292 | put_buf(r10_bio); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Now for the recovery code. |
| 1298 | * Recovery happens across physical sectors. |
| 1299 | * We recover all non-is_sync drives by finding the virtual address of |
| 1300 | * each, and then choose a working drive that also has that virt address. |
| 1301 | * There is a separate r10_bio for each non-in_sync drive. |
| 1302 | * Only the first two slots are in use. The first for reading, |
| 1303 | * The second for writing. |
| 1304 | * |
| 1305 | */ |
| 1306 | |
| 1307 | static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio) |
| 1308 | { |
| 1309 | conf_t *conf = mddev_to_conf(mddev); |
| 1310 | int i, d; |
| 1311 | struct bio *bio, *wbio; |
| 1312 | |
| 1313 | |
| 1314 | /* move the pages across to the second bio |
| 1315 | * and submit the write request |
| 1316 | */ |
| 1317 | bio = r10_bio->devs[0].bio; |
| 1318 | wbio = r10_bio->devs[1].bio; |
| 1319 | for (i=0; i < wbio->bi_vcnt; i++) { |
| 1320 | struct page *p = bio->bi_io_vec[i].bv_page; |
| 1321 | bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page; |
| 1322 | wbio->bi_io_vec[i].bv_page = p; |
| 1323 | } |
| 1324 | d = r10_bio->devs[1].devnum; |
| 1325 | |
| 1326 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1327 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1328 | if (test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 1329 | generic_make_request(wbio); |
| 1330 | else |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1331 | bio_endio(wbio, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | /* |
| 1336 | * This is a kernel thread which: |
| 1337 | * |
| 1338 | * 1. Retries failed read operations on working mirrors. |
| 1339 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1340 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | */ |
| 1342 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1343 | static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio) |
| 1344 | { |
| 1345 | int sect = 0; /* Offset from r10_bio->sector */ |
| 1346 | int sectors = r10_bio->sectors; |
| 1347 | mdk_rdev_t*rdev; |
| 1348 | while(sectors) { |
| 1349 | int s = sectors; |
| 1350 | int sl = r10_bio->read_slot; |
| 1351 | int success = 0; |
| 1352 | int start; |
| 1353 | |
| 1354 | if (s > (PAGE_SIZE>>9)) |
| 1355 | s = PAGE_SIZE >> 9; |
| 1356 | |
| 1357 | rcu_read_lock(); |
| 1358 | do { |
| 1359 | int d = r10_bio->devs[sl].devnum; |
| 1360 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 1361 | if (rdev && |
| 1362 | test_bit(In_sync, &rdev->flags)) { |
| 1363 | atomic_inc(&rdev->nr_pending); |
| 1364 | rcu_read_unlock(); |
| 1365 | success = sync_page_io(rdev->bdev, |
| 1366 | r10_bio->devs[sl].addr + |
| 1367 | sect + rdev->data_offset, |
| 1368 | s<<9, |
| 1369 | conf->tmppage, READ); |
| 1370 | rdev_dec_pending(rdev, mddev); |
| 1371 | rcu_read_lock(); |
| 1372 | if (success) |
| 1373 | break; |
| 1374 | } |
| 1375 | sl++; |
| 1376 | if (sl == conf->copies) |
| 1377 | sl = 0; |
| 1378 | } while (!success && sl != r10_bio->read_slot); |
| 1379 | rcu_read_unlock(); |
| 1380 | |
| 1381 | if (!success) { |
| 1382 | /* Cannot read from anywhere -- bye bye array */ |
| 1383 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
| 1384 | md_error(mddev, conf->mirrors[dn].rdev); |
| 1385 | break; |
| 1386 | } |
| 1387 | |
| 1388 | start = sl; |
| 1389 | /* write it back and re-read */ |
| 1390 | rcu_read_lock(); |
| 1391 | while (sl != r10_bio->read_slot) { |
| 1392 | int d; |
| 1393 | if (sl==0) |
| 1394 | sl = conf->copies; |
| 1395 | sl--; |
| 1396 | d = r10_bio->devs[sl].devnum; |
| 1397 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 1398 | if (rdev && |
| 1399 | test_bit(In_sync, &rdev->flags)) { |
| 1400 | atomic_inc(&rdev->nr_pending); |
| 1401 | rcu_read_unlock(); |
| 1402 | atomic_add(s, &rdev->corrected_errors); |
| 1403 | if (sync_page_io(rdev->bdev, |
| 1404 | r10_bio->devs[sl].addr + |
| 1405 | sect + rdev->data_offset, |
| 1406 | s<<9, conf->tmppage, WRITE) |
| 1407 | == 0) |
| 1408 | /* Well, this device is dead */ |
| 1409 | md_error(mddev, rdev); |
| 1410 | rdev_dec_pending(rdev, mddev); |
| 1411 | rcu_read_lock(); |
| 1412 | } |
| 1413 | } |
| 1414 | sl = start; |
| 1415 | while (sl != r10_bio->read_slot) { |
| 1416 | int d; |
| 1417 | if (sl==0) |
| 1418 | sl = conf->copies; |
| 1419 | sl--; |
| 1420 | d = r10_bio->devs[sl].devnum; |
| 1421 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 1422 | if (rdev && |
| 1423 | test_bit(In_sync, &rdev->flags)) { |
| 1424 | char b[BDEVNAME_SIZE]; |
| 1425 | atomic_inc(&rdev->nr_pending); |
| 1426 | rcu_read_unlock(); |
| 1427 | if (sync_page_io(rdev->bdev, |
| 1428 | r10_bio->devs[sl].addr + |
| 1429 | sect + rdev->data_offset, |
| 1430 | s<<9, conf->tmppage, READ) == 0) |
| 1431 | /* Well, this device is dead */ |
| 1432 | md_error(mddev, rdev); |
| 1433 | else |
| 1434 | printk(KERN_INFO |
| 1435 | "raid10:%s: read error corrected" |
| 1436 | " (%d sectors at %llu on %s)\n", |
| 1437 | mdname(mddev), s, |
Randy Dunlap | 969b755 | 2006-10-28 10:38:32 -0700 | [diff] [blame] | 1438 | (unsigned long long)(sect+ |
| 1439 | rdev->data_offset), |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1440 | bdevname(rdev->bdev, b)); |
| 1441 | |
| 1442 | rdev_dec_pending(rdev, mddev); |
| 1443 | rcu_read_lock(); |
| 1444 | } |
| 1445 | } |
| 1446 | rcu_read_unlock(); |
| 1447 | |
| 1448 | sectors -= s; |
| 1449 | sect += s; |
| 1450 | } |
| 1451 | } |
| 1452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | static void raid10d(mddev_t *mddev) |
| 1454 | { |
| 1455 | r10bio_t *r10_bio; |
| 1456 | struct bio *bio; |
| 1457 | unsigned long flags; |
| 1458 | conf_t *conf = mddev_to_conf(mddev); |
| 1459 | struct list_head *head = &conf->retry_list; |
| 1460 | int unplug=0; |
| 1461 | mdk_rdev_t *rdev; |
| 1462 | |
| 1463 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
| 1465 | for (;;) { |
| 1466 | char b[BDEVNAME_SIZE]; |
| 1467 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1468 | |
| 1469 | if (conf->pending_bio_list.head) { |
| 1470 | bio = bio_list_get(&conf->pending_bio_list); |
| 1471 | blk_remove_plug(mddev->queue); |
| 1472 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1473 | /* flush any pending bitmap writes to disk before proceeding w/ I/O */ |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1474 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1475 | |
| 1476 | while (bio) { /* submit pending writes */ |
| 1477 | struct bio *next = bio->bi_next; |
| 1478 | bio->bi_next = NULL; |
| 1479 | generic_make_request(bio); |
| 1480 | bio = next; |
| 1481 | } |
| 1482 | unplug = 1; |
| 1483 | |
| 1484 | continue; |
| 1485 | } |
| 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if (list_empty(head)) |
| 1488 | break; |
| 1489 | r10_bio = list_entry(head->prev, r10bio_t, retry_list); |
| 1490 | list_del(head->prev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1491 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1493 | |
| 1494 | mddev = r10_bio->mddev; |
| 1495 | conf = mddev_to_conf(mddev); |
| 1496 | if (test_bit(R10BIO_IsSync, &r10_bio->state)) { |
| 1497 | sync_request_write(mddev, r10_bio); |
| 1498 | unplug = 1; |
| 1499 | } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) { |
| 1500 | recovery_request_write(mddev, r10_bio); |
| 1501 | unplug = 1; |
| 1502 | } else { |
| 1503 | int mirror; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1504 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 1505 | * the block and we can fix it. |
| 1506 | * We freeze all other IO, and try reading the block from |
| 1507 | * other devices. When we find one, we re-write |
| 1508 | * and check it that fixes the read error. |
| 1509 | * This is all done synchronously while the array is |
| 1510 | * frozen. |
| 1511 | */ |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1512 | if (mddev->ro == 0) { |
| 1513 | freeze_array(conf); |
| 1514 | fix_read_error(conf, mddev, r10_bio); |
| 1515 | unfreeze_array(conf); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | bio = r10_bio->devs[r10_bio->read_slot].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1519 | r10_bio->devs[r10_bio->read_slot].bio = |
| 1520 | mddev->ro ? IO_BLOCKED : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | mirror = read_balance(conf, r10_bio); |
| 1522 | if (mirror == -1) { |
| 1523 | printk(KERN_ALERT "raid10: %s: unrecoverable I/O" |
| 1524 | " read error for block %llu\n", |
| 1525 | bdevname(bio->bi_bdev,b), |
| 1526 | (unsigned long long)r10_bio->sector); |
| 1527 | raid_end_bio_io(r10_bio); |
Maik Hampel | 14e7134 | 2007-07-31 00:37:57 -0700 | [diff] [blame] | 1528 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } else { |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 1530 | const int do_sync = bio_sync(r10_bio->master_bio); |
Maik Hampel | 14e7134 | 2007-07-31 00:37:57 -0700 | [diff] [blame] | 1531 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | rdev = conf->mirrors[mirror].rdev; |
| 1533 | if (printk_ratelimit()) |
| 1534 | printk(KERN_ERR "raid10: %s: redirecting sector %llu to" |
| 1535 | " another mirror\n", |
| 1536 | bdevname(rdev->bdev,b), |
| 1537 | (unsigned long long)r10_bio->sector); |
| 1538 | bio = bio_clone(r10_bio->master_bio, GFP_NOIO); |
| 1539 | r10_bio->devs[r10_bio->read_slot].bio = bio; |
| 1540 | bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr |
| 1541 | + rdev->data_offset; |
| 1542 | bio->bi_bdev = rdev->bdev; |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 1543 | bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | bio->bi_private = r10_bio; |
| 1545 | bio->bi_end_io = raid10_end_read_request; |
| 1546 | unplug = 1; |
| 1547 | generic_make_request(bio); |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1552 | if (unplug) |
| 1553 | unplug_slaves(mddev); |
| 1554 | } |
| 1555 | |
| 1556 | |
| 1557 | static int init_resync(conf_t *conf) |
| 1558 | { |
| 1559 | int buffs; |
| 1560 | |
| 1561 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Eric Sesterhenn | b638548 | 2006-04-02 13:34:29 +0200 | [diff] [blame] | 1562 | BUG_ON(conf->r10buf_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
| 1564 | if (!conf->r10buf_pool) |
| 1565 | return -ENOMEM; |
| 1566 | conf->next_resync = 0; |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * perform a "sync" on one "block" |
| 1572 | * |
| 1573 | * We need to make sure that no normal I/O request - particularly write |
| 1574 | * requests - conflict with active sync requests. |
| 1575 | * |
| 1576 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 1577 | * that can be installed to exclude normal IO requests. |
| 1578 | * |
| 1579 | * Resync and recovery are handled very differently. |
| 1580 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. |
| 1581 | * |
| 1582 | * For resync, we iterate over virtual addresses, read all copies, |
| 1583 | * and update if there are differences. If only one copy is live, |
| 1584 | * skip it. |
| 1585 | * For recovery, we iterate over physical addresses, read a good |
| 1586 | * value for each non-in_sync drive, and over-write. |
| 1587 | * |
| 1588 | * So, for recovery we may have several outstanding complex requests for a |
| 1589 | * given address, one for each out-of-sync device. We model this by allocating |
| 1590 | * a number of r10_bio structures, one for each out-of-sync device. |
| 1591 | * As we setup these structures, we collect all bio's together into a list |
| 1592 | * which we then process collectively to add pages, and then process again |
| 1593 | * to pass to generic_make_request. |
| 1594 | * |
| 1595 | * The r10_bio structures are linked using a borrowed master_bio pointer. |
| 1596 | * This link is counted in ->remaining. When the r10_bio that points to NULL |
| 1597 | * has its remaining count decremented to 0, the whole complex operation |
| 1598 | * is complete. |
| 1599 | * |
| 1600 | */ |
| 1601 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1602 | 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] | 1603 | { |
| 1604 | conf_t *conf = mddev_to_conf(mddev); |
| 1605 | r10bio_t *r10_bio; |
| 1606 | struct bio *biolist = NULL, *bio; |
| 1607 | sector_t max_sector, nr_sectors; |
| 1608 | int disk; |
| 1609 | int i; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1610 | int max_sync; |
| 1611 | int sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
| 1613 | sector_t sectors_skipped = 0; |
| 1614 | int chunks_skipped = 0; |
| 1615 | |
| 1616 | if (!conf->r10buf_pool) |
| 1617 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1618 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
| 1620 | skipped: |
| 1621 | max_sector = mddev->size << 1; |
| 1622 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 1623 | max_sector = mddev->resync_max_sectors; |
| 1624 | if (sector_nr >= max_sector) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1625 | /* If we aborted, we need to abort the |
| 1626 | * sync on the 'current' bitmap chucks (there can |
| 1627 | * be several when recovering multiple devices). |
| 1628 | * as we may have started syncing it but not finished. |
| 1629 | * We can find the current address in |
| 1630 | * mddev->curr_resync, but for recovery, |
| 1631 | * we need to convert that to several |
| 1632 | * virtual addresses. |
| 1633 | */ |
| 1634 | if (mddev->curr_resync < max_sector) { /* aborted */ |
| 1635 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 1636 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 1637 | &sync_blocks, 1); |
| 1638 | else for (i=0; i<conf->raid_disks; i++) { |
| 1639 | sector_t sect = |
| 1640 | raid10_find_virt(conf, mddev->curr_resync, i); |
| 1641 | bitmap_end_sync(mddev->bitmap, sect, |
| 1642 | &sync_blocks, 1); |
| 1643 | } |
| 1644 | } else /* completed sync */ |
| 1645 | conf->fullsync = 0; |
| 1646 | |
| 1647 | bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | close_sync(conf); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1649 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | return sectors_skipped; |
| 1651 | } |
| 1652 | if (chunks_skipped >= conf->raid_disks) { |
| 1653 | /* if there has been nothing to do on any drive, |
| 1654 | * then there is nothing to do at all.. |
| 1655 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1656 | *skipped = 1; |
| 1657 | return (max_sector - sector_nr) + sectors_skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | /* make sure whole request will fit in a chunk - if chunks |
| 1661 | * are meaningful |
| 1662 | */ |
| 1663 | if (conf->near_copies < conf->raid_disks && |
| 1664 | max_sector > (sector_nr | conf->chunk_mask)) |
| 1665 | max_sector = (sector_nr | conf->chunk_mask) + 1; |
| 1666 | /* |
| 1667 | * If there is non-resync activity waiting for us then |
| 1668 | * put in a delay to throttle resync. |
| 1669 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1670 | if (!go_faster && conf->nr_waiting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | msleep_interruptible(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame^] | 1673 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 1674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | /* Again, very different code for resync and recovery. |
| 1676 | * Both must result in an r10bio with a list of bios that |
| 1677 | * have bi_end_io, bi_sector, bi_bdev set, |
| 1678 | * and bi_private set to the r10bio. |
| 1679 | * For recovery, we may actually create several r10bios |
| 1680 | * with 2 bios in each, that correspond to the bios in the main one. |
| 1681 | * In this case, the subordinate r10bios link back through a |
| 1682 | * borrowed master_bio pointer, and the counter in the master |
| 1683 | * includes a ref from each subordinate. |
| 1684 | */ |
| 1685 | /* First, we decide what to do and set ->bi_end_io |
| 1686 | * To end_sync_read if we want to read, and |
| 1687 | * end_sync_write if we will want to write. |
| 1688 | */ |
| 1689 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1690 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 1692 | /* recovery... the complicated one */ |
| 1693 | int i, j, k; |
| 1694 | r10_bio = NULL; |
| 1695 | |
| 1696 | for (i=0 ; i<conf->raid_disks; i++) |
| 1697 | if (conf->mirrors[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1698 | !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1699 | int still_degraded = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | /* want to reconstruct this device */ |
| 1701 | r10bio_t *rb2 = r10_bio; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1702 | sector_t sect = raid10_find_virt(conf, sector_nr, i); |
| 1703 | int must_sync; |
| 1704 | /* Unless we are doing a full sync, we only need |
| 1705 | * to recover the block if it is set in the bitmap |
| 1706 | */ |
| 1707 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 1708 | &sync_blocks, 1); |
| 1709 | if (sync_blocks < max_sync) |
| 1710 | max_sync = sync_blocks; |
| 1711 | if (!must_sync && |
| 1712 | !conf->fullsync) { |
| 1713 | /* yep, skip the sync_blocks here, but don't assume |
| 1714 | * that there will never be anything to do here |
| 1715 | */ |
| 1716 | chunks_skipped = -1; |
| 1717 | continue; |
| 1718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
| 1720 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1721 | raise_barrier(conf, rb2 != NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | atomic_set(&r10_bio->remaining, 0); |
| 1723 | |
| 1724 | r10_bio->master_bio = (struct bio*)rb2; |
| 1725 | if (rb2) |
| 1726 | atomic_inc(&rb2->remaining); |
| 1727 | r10_bio->mddev = mddev; |
| 1728 | set_bit(R10BIO_IsRecover, &r10_bio->state); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1729 | r10_bio->sector = sect; |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1732 | /* Need to check if this section will still be |
| 1733 | * degraded |
| 1734 | */ |
| 1735 | for (j=0; j<conf->copies;j++) { |
| 1736 | int d = r10_bio->devs[j].devnum; |
| 1737 | if (conf->mirrors[d].rdev == NULL || |
NeilBrown | a24a8dd | 2006-01-06 00:20:35 -0800 | [diff] [blame] | 1738 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1739 | still_degraded = 1; |
NeilBrown | a24a8dd | 2006-01-06 00:20:35 -0800 | [diff] [blame] | 1740 | break; |
| 1741 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1742 | } |
| 1743 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 1744 | &sync_blocks, still_degraded); |
| 1745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | for (j=0; j<conf->copies;j++) { |
| 1747 | int d = r10_bio->devs[j].devnum; |
| 1748 | if (conf->mirrors[d].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1749 | test_bit(In_sync, &conf->mirrors[d].rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | /* This is where we read from */ |
| 1751 | bio = r10_bio->devs[0].bio; |
| 1752 | bio->bi_next = biolist; |
| 1753 | biolist = bio; |
| 1754 | bio->bi_private = r10_bio; |
| 1755 | bio->bi_end_io = end_sync_read; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 1756 | bio->bi_rw = READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | bio->bi_sector = r10_bio->devs[j].addr + |
| 1758 | conf->mirrors[d].rdev->data_offset; |
| 1759 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1760 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1761 | atomic_inc(&r10_bio->remaining); |
| 1762 | /* and we write to 'i' */ |
| 1763 | |
| 1764 | for (k=0; k<conf->copies; k++) |
| 1765 | if (r10_bio->devs[k].devnum == i) |
| 1766 | break; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 1767 | BUG_ON(k == conf->copies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | bio = r10_bio->devs[1].bio; |
| 1769 | bio->bi_next = biolist; |
| 1770 | biolist = bio; |
| 1771 | bio->bi_private = r10_bio; |
| 1772 | bio->bi_end_io = end_sync_write; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 1773 | bio->bi_rw = WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | bio->bi_sector = r10_bio->devs[k].addr + |
| 1775 | conf->mirrors[i].rdev->data_offset; |
| 1776 | bio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 1777 | |
| 1778 | r10_bio->devs[0].devnum = d; |
| 1779 | r10_bio->devs[1].devnum = i; |
| 1780 | |
| 1781 | break; |
| 1782 | } |
| 1783 | } |
| 1784 | if (j == conf->copies) { |
NeilBrown | 87fc767 | 2005-09-09 16:24:04 -0700 | [diff] [blame] | 1785 | /* Cannot recover, so abort the recovery */ |
| 1786 | put_buf(r10_bio); |
| 1787 | r10_bio = rb2; |
| 1788 | if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery)) |
| 1789 | printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n", |
| 1790 | mdname(mddev)); |
| 1791 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | if (biolist == NULL) { |
| 1795 | while (r10_bio) { |
| 1796 | r10bio_t *rb2 = r10_bio; |
| 1797 | r10_bio = (r10bio_t*) rb2->master_bio; |
| 1798 | rb2->master_bio = NULL; |
| 1799 | put_buf(rb2); |
| 1800 | } |
| 1801 | goto giveup; |
| 1802 | } |
| 1803 | } else { |
| 1804 | /* resync. Schedule a read for every block at this virt offset */ |
| 1805 | int count = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1806 | |
| 1807 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
| 1808 | &sync_blocks, mddev->degraded) && |
| 1809 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 1810 | /* We can skip this block */ |
| 1811 | *skipped = 1; |
| 1812 | return sync_blocks + sectors_skipped; |
| 1813 | } |
| 1814 | if (sync_blocks < max_sync) |
| 1815 | max_sync = sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | r10_bio->mddev = mddev; |
| 1819 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1820 | raise_barrier(conf, 0); |
| 1821 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
| 1823 | r10_bio->master_bio = NULL; |
| 1824 | r10_bio->sector = sector_nr; |
| 1825 | set_bit(R10BIO_IsSync, &r10_bio->state); |
| 1826 | raid10_find_phys(conf, r10_bio); |
| 1827 | r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; |
| 1828 | |
| 1829 | for (i=0; i<conf->copies; i++) { |
| 1830 | int d = r10_bio->devs[i].devnum; |
| 1831 | bio = r10_bio->devs[i].bio; |
| 1832 | bio->bi_end_io = NULL; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 1833 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | if (conf->mirrors[d].rdev == NULL || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1835 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | continue; |
| 1837 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1838 | atomic_inc(&r10_bio->remaining); |
| 1839 | bio->bi_next = biolist; |
| 1840 | biolist = bio; |
| 1841 | bio->bi_private = r10_bio; |
| 1842 | bio->bi_end_io = end_sync_read; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 1843 | bio->bi_rw = READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | bio->bi_sector = r10_bio->devs[i].addr + |
| 1845 | conf->mirrors[d].rdev->data_offset; |
| 1846 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1847 | count++; |
| 1848 | } |
| 1849 | |
| 1850 | if (count < 2) { |
| 1851 | for (i=0; i<conf->copies; i++) { |
| 1852 | int d = r10_bio->devs[i].devnum; |
| 1853 | if (r10_bio->devs[i].bio->bi_end_io) |
| 1854 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
| 1855 | } |
| 1856 | put_buf(r10_bio); |
| 1857 | biolist = NULL; |
| 1858 | goto giveup; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | for (bio = biolist; bio ; bio=bio->bi_next) { |
| 1863 | |
| 1864 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 1865 | if (bio->bi_end_io) |
| 1866 | bio->bi_flags |= 1 << BIO_UPTODATE; |
| 1867 | bio->bi_vcnt = 0; |
| 1868 | bio->bi_idx = 0; |
| 1869 | bio->bi_phys_segments = 0; |
| 1870 | bio->bi_hw_segments = 0; |
| 1871 | bio->bi_size = 0; |
| 1872 | } |
| 1873 | |
| 1874 | nr_sectors = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1875 | if (sector_nr + max_sync < max_sector) |
| 1876 | max_sector = sector_nr + max_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | do { |
| 1878 | struct page *page; |
| 1879 | int len = PAGE_SIZE; |
| 1880 | disk = 0; |
| 1881 | if (sector_nr + (len>>9) > max_sector) |
| 1882 | len = (max_sector - sector_nr) << 9; |
| 1883 | if (len == 0) |
| 1884 | break; |
| 1885 | for (bio= biolist ; bio ; bio=bio->bi_next) { |
| 1886 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
| 1887 | if (bio_add_page(bio, page, len, 0) == 0) { |
| 1888 | /* stop here */ |
| 1889 | struct bio *bio2; |
| 1890 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; |
| 1891 | for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) { |
| 1892 | /* remove last page from this bio */ |
| 1893 | bio2->bi_vcnt--; |
| 1894 | bio2->bi_size -= len; |
| 1895 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); |
| 1896 | } |
| 1897 | goto bio_full; |
| 1898 | } |
| 1899 | disk = i; |
| 1900 | } |
| 1901 | nr_sectors += len>>9; |
| 1902 | sector_nr += len>>9; |
| 1903 | } while (biolist->bi_vcnt < RESYNC_PAGES); |
| 1904 | bio_full: |
| 1905 | r10_bio->sectors = nr_sectors; |
| 1906 | |
| 1907 | while (biolist) { |
| 1908 | bio = biolist; |
| 1909 | biolist = biolist->bi_next; |
| 1910 | |
| 1911 | bio->bi_next = NULL; |
| 1912 | r10_bio = bio->bi_private; |
| 1913 | r10_bio->sectors = nr_sectors; |
| 1914 | |
| 1915 | if (bio->bi_end_io == end_sync_read) { |
| 1916 | md_sync_acct(bio->bi_bdev, nr_sectors); |
| 1917 | generic_make_request(bio); |
| 1918 | } |
| 1919 | } |
| 1920 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1921 | if (sectors_skipped) |
| 1922 | /* pretend they weren't skipped, it makes |
| 1923 | * no important difference in this case |
| 1924 | */ |
| 1925 | md_done_sync(mddev, sectors_skipped, 1); |
| 1926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | return sectors_skipped + nr_sectors; |
| 1928 | giveup: |
| 1929 | /* There is nowhere to write, so all non-sync |
| 1930 | * drives must be failed, so try the next chunk... |
| 1931 | */ |
| 1932 | { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1933 | sector_t sec = max_sector - sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | sectors_skipped += sec; |
| 1935 | chunks_skipped ++; |
| 1936 | sector_nr = max_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | goto skipped; |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | static int run(mddev_t *mddev) |
| 1942 | { |
| 1943 | conf_t *conf; |
| 1944 | int i, disk_idx; |
| 1945 | mirror_info_t *disk; |
| 1946 | mdk_rdev_t *rdev; |
| 1947 | struct list_head *tmp; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1948 | int nc, fc, fo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | sector_t stride, size; |
| 1950 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 1951 | if (mddev->chunk_size == 0) { |
| 1952 | printk(KERN_ERR "md/raid10: non-zero chunk size required.\n"); |
| 1953 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 1955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | nc = mddev->layout & 255; |
| 1957 | fc = (mddev->layout >> 8) & 255; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1958 | fo = mddev->layout & (1<<16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1960 | (mddev->layout >> 17)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n", |
| 1962 | mdname(mddev), mddev->layout); |
| 1963 | goto out; |
| 1964 | } |
| 1965 | /* |
| 1966 | * copy the already verified devices into our private RAID10 |
| 1967 | * bookkeeping area. [whatever we allocate in run(), |
| 1968 | * should be freed in stop()] |
| 1969 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1970 | conf = kzalloc(sizeof(conf_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | mddev->private = conf; |
| 1972 | if (!conf) { |
| 1973 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", |
| 1974 | mdname(mddev)); |
| 1975 | goto out; |
| 1976 | } |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1977 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | GFP_KERNEL); |
| 1979 | if (!conf->mirrors) { |
| 1980 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", |
| 1981 | mdname(mddev)); |
| 1982 | goto out_free_conf; |
| 1983 | } |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1984 | |
| 1985 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 1986 | if (!conf->tmppage) |
| 1987 | goto out_free_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 1989 | conf->mddev = mddev; |
| 1990 | conf->raid_disks = mddev->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | conf->near_copies = nc; |
| 1992 | conf->far_copies = fc; |
| 1993 | conf->copies = nc*fc; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1994 | conf->far_offset = fo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1; |
| 1996 | conf->chunk_shift = ffz(~mddev->chunk_size) - 9; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 1997 | size = mddev->size >> (conf->chunk_shift-1); |
| 1998 | sector_div(size, fc); |
| 1999 | size = size * conf->raid_disks; |
| 2000 | sector_div(size, nc); |
| 2001 | /* 'size' is now the number of chunks in the array */ |
| 2002 | /* calculate "used chunks per device" in 'stride' */ |
| 2003 | stride = size * conf->copies; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 2004 | |
| 2005 | /* We need to round up when dividing by raid_disks to |
| 2006 | * get the stride size. |
| 2007 | */ |
| 2008 | stride += conf->raid_disks - 1; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2009 | sector_div(stride, conf->raid_disks); |
| 2010 | mddev->size = stride << (conf->chunk_shift-1); |
| 2011 | |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2012 | if (fo) |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2013 | stride = 1; |
| 2014 | else |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2015 | sector_div(stride, fc); |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2016 | conf->stride = stride << conf->chunk_shift; |
| 2017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
| 2019 | r10bio_pool_free, conf); |
| 2020 | if (!conf->r10bio_pool) { |
| 2021 | printk(KERN_ERR "raid10: couldn't allocate memory for %s\n", |
| 2022 | mdname(mddev)); |
| 2023 | goto out_free_conf; |
| 2024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
| 2026 | ITERATE_RDEV(mddev, rdev, tmp) { |
| 2027 | disk_idx = rdev->raid_disk; |
| 2028 | if (disk_idx >= mddev->raid_disks |
| 2029 | || disk_idx < 0) |
| 2030 | continue; |
| 2031 | disk = conf->mirrors + disk_idx; |
| 2032 | |
| 2033 | disk->rdev = rdev; |
| 2034 | |
| 2035 | blk_queue_stack_limits(mddev->queue, |
| 2036 | rdev->bdev->bd_disk->queue); |
| 2037 | /* as we don't honour merge_bvec_fn, we must never risk |
| 2038 | * violating it, so limit ->max_sector to one PAGE, as |
| 2039 | * a one page request is never in violation. |
| 2040 | */ |
| 2041 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && |
| 2042 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) |
| 2043 | mddev->queue->max_sectors = (PAGE_SIZE>>9); |
| 2044 | |
| 2045 | disk->head_position = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | spin_lock_init(&conf->device_lock); |
| 2048 | INIT_LIST_HEAD(&conf->retry_list); |
| 2049 | |
| 2050 | spin_lock_init(&conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 2051 | init_waitqueue_head(&conf->wait_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 2053 | /* need to check that every block has at least one working mirror */ |
| 2054 | if (!enough(conf)) { |
| 2055 | printk(KERN_ERR "raid10: not enough operational mirrors for %s\n", |
| 2056 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | goto out_free_conf; |
| 2058 | } |
| 2059 | |
| 2060 | mddev->degraded = 0; |
| 2061 | for (i = 0; i < conf->raid_disks; i++) { |
| 2062 | |
| 2063 | disk = conf->mirrors + i; |
| 2064 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2065 | if (!disk->rdev || |
NeilBrown | 2e333e8 | 2006-10-21 10:24:07 -0700 | [diff] [blame] | 2066 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | disk->head_position = 0; |
| 2068 | mddev->degraded++; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | |
| 2073 | mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10"); |
| 2074 | if (!mddev->thread) { |
| 2075 | printk(KERN_ERR |
| 2076 | "raid10: couldn't allocate thread for %s\n", |
| 2077 | mdname(mddev)); |
| 2078 | goto out_free_conf; |
| 2079 | } |
| 2080 | |
| 2081 | printk(KERN_INFO |
| 2082 | "raid10: raid set %s active with %d out of %d devices\n", |
| 2083 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 2084 | mddev->raid_disks); |
| 2085 | /* |
| 2086 | * Ok, everything is just fine now |
| 2087 | */ |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2088 | mddev->array_size = size << (conf->chunk_shift-1); |
| 2089 | mddev->resync_max_sectors = size << conf->chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 2091 | mddev->queue->unplug_fn = raid10_unplug; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 2092 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
| 2093 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 2094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | /* Calculate max read-ahead size. |
| 2096 | * We need to readahead at least twice a whole stripe.... |
| 2097 | * maybe... |
| 2098 | */ |
| 2099 | { |
NeilBrown | 8932c2e | 2006-06-26 00:27:36 -0700 | [diff] [blame] | 2100 | int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | stripe /= conf->near_copies; |
| 2102 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) |
| 2103 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; |
| 2104 | } |
| 2105 | |
| 2106 | if (conf->near_copies < mddev->raid_disks) |
| 2107 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); |
| 2108 | return 0; |
| 2109 | |
| 2110 | out_free_conf: |
| 2111 | if (conf->r10bio_pool) |
| 2112 | mempool_destroy(conf->r10bio_pool); |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 2113 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 2114 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | kfree(conf); |
| 2116 | mddev->private = NULL; |
| 2117 | out: |
| 2118 | return -EIO; |
| 2119 | } |
| 2120 | |
| 2121 | static int stop(mddev_t *mddev) |
| 2122 | { |
| 2123 | conf_t *conf = mddev_to_conf(mddev); |
| 2124 | |
| 2125 | md_unregister_thread(mddev->thread); |
| 2126 | mddev->thread = NULL; |
| 2127 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 2128 | if (conf->r10bio_pool) |
| 2129 | mempool_destroy(conf->r10bio_pool); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 2130 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | kfree(conf); |
| 2132 | mddev->private = NULL; |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2136 | static void raid10_quiesce(mddev_t *mddev, int state) |
| 2137 | { |
| 2138 | conf_t *conf = mddev_to_conf(mddev); |
| 2139 | |
| 2140 | switch(state) { |
| 2141 | case 1: |
| 2142 | raise_barrier(conf, 0); |
| 2143 | break; |
| 2144 | case 0: |
| 2145 | lower_barrier(conf); |
| 2146 | break; |
| 2147 | } |
| 2148 | if (mddev->thread) { |
| 2149 | if (mddev->bitmap) |
| 2150 | mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ; |
| 2151 | else |
| 2152 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
| 2153 | md_wakeup_thread(mddev->thread); |
| 2154 | } |
| 2155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2157 | static struct mdk_personality raid10_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | { |
| 2159 | .name = "raid10", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2160 | .level = 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | .owner = THIS_MODULE, |
| 2162 | .make_request = make_request, |
| 2163 | .run = run, |
| 2164 | .stop = stop, |
| 2165 | .status = status, |
| 2166 | .error_handler = error, |
| 2167 | .hot_add_disk = raid10_add_disk, |
| 2168 | .hot_remove_disk= raid10_remove_disk, |
| 2169 | .spare_active = raid10_spare_active, |
| 2170 | .sync_request = sync_request, |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2171 | .quiesce = raid10_quiesce, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | }; |
| 2173 | |
| 2174 | static int __init raid_init(void) |
| 2175 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2176 | return register_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | static void raid_exit(void) |
| 2180 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2181 | unregister_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | module_init(raid_init); |
| 2185 | module_exit(raid_exit); |
| 2186 | MODULE_LICENSE("GPL"); |
| 2187 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2188 | MODULE_ALIAS("md-raid10"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2189 | MODULE_ALIAS("md-level-10"); |