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