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