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