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