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