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