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