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