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