NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Neil Brown |
| 3 | * Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/slab.h> |
| 9 | |
| 10 | #include "md.h" |
| 11 | #include "raid5.h" |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 12 | #include "bitmap.h" |
| 13 | |
Alasdair G Kergon | 3e8dbb7 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 14 | #include <linux/device-mapper.h> |
| 15 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 16 | #define DM_MSG_PREFIX "raid" |
| 17 | |
| 18 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 19 | * The following flags are used by dm-raid.c to set up the array state. |
| 20 | * They must be cleared before md_run is called. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 21 | */ |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 22 | #define FirstUse 10 /* rdev flag */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 23 | |
| 24 | struct raid_dev { |
| 25 | /* |
| 26 | * Two DM devices, one to hold metadata and one to hold the |
| 27 | * actual data/parity. The reason for this is to not confuse |
| 28 | * ti->len and give more flexibility in altering size and |
| 29 | * characteristics. |
| 30 | * |
| 31 | * While it is possible for this device to be associated |
| 32 | * with a different physical device than the data_dev, it |
| 33 | * is intended for it to be the same. |
| 34 | * |--------- Physical Device ---------| |
| 35 | * |- meta_dev -|------ data_dev ------| |
| 36 | */ |
| 37 | struct dm_dev *meta_dev; |
| 38 | struct dm_dev *data_dev; |
| 39 | struct mdk_rdev_s rdev; |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Flags for rs->print_flags field. |
| 44 | */ |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 45 | #define DMPF_SYNC 0x1 |
| 46 | #define DMPF_NOSYNC 0x2 |
| 47 | #define DMPF_REBUILD 0x4 |
| 48 | #define DMPF_DAEMON_SLEEP 0x8 |
| 49 | #define DMPF_MIN_RECOVERY_RATE 0x10 |
| 50 | #define DMPF_MAX_RECOVERY_RATE 0x20 |
| 51 | #define DMPF_MAX_WRITE_BEHIND 0x40 |
| 52 | #define DMPF_STRIPE_CACHE 0x80 |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 53 | #define DMPF_REGION_SIZE 0X100 |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 54 | struct raid_set { |
| 55 | struct dm_target *ti; |
| 56 | |
| 57 | uint64_t print_flags; |
| 58 | |
| 59 | struct mddev_s md; |
| 60 | struct raid_type *raid_type; |
| 61 | struct dm_target_callbacks callbacks; |
| 62 | |
| 63 | struct raid_dev dev[0]; |
| 64 | }; |
| 65 | |
| 66 | /* Supported raid types and properties. */ |
| 67 | static struct raid_type { |
| 68 | const char *name; /* RAID algorithm. */ |
| 69 | const char *descr; /* Descriptor text for logging. */ |
| 70 | const unsigned parity_devs; /* # of parity devices. */ |
| 71 | const unsigned minimal_devs; /* minimal # of devices in set. */ |
| 72 | const unsigned level; /* RAID level. */ |
| 73 | const unsigned algorithm; /* RAID algorithm. */ |
| 74 | } raid_types[] = { |
| 75 | {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, |
| 76 | {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC}, |
| 77 | {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC}, |
| 78 | {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC}, |
| 79 | {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC}, |
| 80 | {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART}, |
| 81 | {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART}, |
| 82 | {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE} |
| 83 | }; |
| 84 | |
| 85 | static struct raid_type *get_raid_type(char *name) |
| 86 | { |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < ARRAY_SIZE(raid_types); i++) |
| 90 | if (!strcmp(raid_types[i].name, name)) |
| 91 | return &raid_types[i]; |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs) |
| 97 | { |
| 98 | unsigned i; |
| 99 | struct raid_set *rs; |
| 100 | sector_t sectors_per_dev; |
| 101 | |
| 102 | if (raid_devs <= raid_type->parity_devs) { |
| 103 | ti->error = "Insufficient number of devices"; |
| 104 | return ERR_PTR(-EINVAL); |
| 105 | } |
| 106 | |
| 107 | sectors_per_dev = ti->len; |
| 108 | if (sector_div(sectors_per_dev, (raid_devs - raid_type->parity_devs))) { |
| 109 | ti->error = "Target length not divisible by number of data devices"; |
| 110 | return ERR_PTR(-EINVAL); |
| 111 | } |
| 112 | |
| 113 | rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL); |
| 114 | if (!rs) { |
| 115 | ti->error = "Cannot allocate raid context"; |
| 116 | return ERR_PTR(-ENOMEM); |
| 117 | } |
| 118 | |
| 119 | mddev_init(&rs->md); |
| 120 | |
| 121 | rs->ti = ti; |
| 122 | rs->raid_type = raid_type; |
| 123 | rs->md.raid_disks = raid_devs; |
| 124 | rs->md.level = raid_type->level; |
| 125 | rs->md.new_level = rs->md.level; |
| 126 | rs->md.dev_sectors = sectors_per_dev; |
| 127 | rs->md.layout = raid_type->algorithm; |
| 128 | rs->md.new_layout = rs->md.layout; |
| 129 | rs->md.delta_disks = 0; |
| 130 | rs->md.recovery_cp = 0; |
| 131 | |
| 132 | for (i = 0; i < raid_devs; i++) |
| 133 | md_rdev_init(&rs->dev[i].rdev); |
| 134 | |
| 135 | /* |
| 136 | * Remaining items to be initialized by further RAID params: |
| 137 | * rs->md.persistent |
| 138 | * rs->md.external |
| 139 | * rs->md.chunk_sectors |
| 140 | * rs->md.new_chunk_sectors |
| 141 | */ |
| 142 | |
| 143 | return rs; |
| 144 | } |
| 145 | |
| 146 | static void context_free(struct raid_set *rs) |
| 147 | { |
| 148 | int i; |
| 149 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 150 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 151 | if (rs->dev[i].meta_dev) |
| 152 | dm_put_device(rs->ti, rs->dev[i].meta_dev); |
| 153 | if (rs->dev[i].rdev.sb_page) |
| 154 | put_page(rs->dev[i].rdev.sb_page); |
| 155 | rs->dev[i].rdev.sb_page = NULL; |
| 156 | rs->dev[i].rdev.sb_loaded = 0; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 157 | if (rs->dev[i].data_dev) |
| 158 | dm_put_device(rs->ti, rs->dev[i].data_dev); |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 159 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 160 | |
| 161 | kfree(rs); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * For every device we have two words |
| 166 | * <meta_dev>: meta device name or '-' if missing |
| 167 | * <data_dev>: data device name or '-' if missing |
| 168 | * |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 169 | * The following are permitted: |
| 170 | * - - |
| 171 | * - <data_dev> |
| 172 | * <meta_dev> <data_dev> |
| 173 | * |
| 174 | * The following is not allowed: |
| 175 | * <meta_dev> - |
| 176 | * |
| 177 | * This code parses those words. If there is a failure, |
| 178 | * the caller must use context_free to unwind the operations. |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 179 | */ |
| 180 | static int dev_parms(struct raid_set *rs, char **argv) |
| 181 | { |
| 182 | int i; |
| 183 | int rebuild = 0; |
| 184 | int metadata_available = 0; |
| 185 | int ret = 0; |
| 186 | |
| 187 | for (i = 0; i < rs->md.raid_disks; i++, argv += 2) { |
| 188 | rs->dev[i].rdev.raid_disk = i; |
| 189 | |
| 190 | rs->dev[i].meta_dev = NULL; |
| 191 | rs->dev[i].data_dev = NULL; |
| 192 | |
| 193 | /* |
| 194 | * There are no offsets, since there is a separate device |
| 195 | * for data and metadata. |
| 196 | */ |
| 197 | rs->dev[i].rdev.data_offset = 0; |
| 198 | rs->dev[i].rdev.mddev = &rs->md; |
| 199 | |
| 200 | if (strcmp(argv[0], "-")) { |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 201 | ret = dm_get_device(rs->ti, argv[0], |
| 202 | dm_table_get_mode(rs->ti->table), |
| 203 | &rs->dev[i].meta_dev); |
| 204 | rs->ti->error = "RAID metadata device lookup failure"; |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
| 208 | rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL); |
| 209 | if (!rs->dev[i].rdev.sb_page) |
| 210 | return -ENOMEM; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (!strcmp(argv[1], "-")) { |
| 214 | if (!test_bit(In_sync, &rs->dev[i].rdev.flags) && |
| 215 | (!rs->dev[i].rdev.recovery_offset)) { |
| 216 | rs->ti->error = "Drive designated for rebuild not specified"; |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 220 | rs->ti->error = "No data device supplied with metadata device"; |
| 221 | if (rs->dev[i].meta_dev) |
| 222 | return -EINVAL; |
| 223 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | |
| 227 | ret = dm_get_device(rs->ti, argv[1], |
| 228 | dm_table_get_mode(rs->ti->table), |
| 229 | &rs->dev[i].data_dev); |
| 230 | if (ret) { |
| 231 | rs->ti->error = "RAID device lookup failure"; |
| 232 | return ret; |
| 233 | } |
| 234 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 235 | if (rs->dev[i].meta_dev) { |
| 236 | metadata_available = 1; |
| 237 | rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev; |
| 238 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 239 | rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev; |
| 240 | list_add(&rs->dev[i].rdev.same_set, &rs->md.disks); |
| 241 | if (!test_bit(In_sync, &rs->dev[i].rdev.flags)) |
| 242 | rebuild++; |
| 243 | } |
| 244 | |
| 245 | if (metadata_available) { |
| 246 | rs->md.external = 0; |
| 247 | rs->md.persistent = 1; |
| 248 | rs->md.major_version = 2; |
| 249 | } else if (rebuild && !rs->md.recovery_cp) { |
| 250 | /* |
| 251 | * Without metadata, we will not be able to tell if the array |
| 252 | * is in-sync or not - we must assume it is not. Therefore, |
| 253 | * it is impossible to rebuild a drive. |
| 254 | * |
| 255 | * Even if there is metadata, the on-disk information may |
| 256 | * indicate that the array is not in-sync and it will then |
| 257 | * fail at that time. |
| 258 | * |
| 259 | * User could specify 'nosync' option if desperate. |
| 260 | */ |
| 261 | DMERR("Unable to rebuild drive while array is not in-sync"); |
| 262 | rs->ti->error = "RAID device lookup failure"; |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 270 | * validate_region_size |
| 271 | * @rs |
| 272 | * @region_size: region size in sectors. If 0, pick a size (4MiB default). |
| 273 | * |
| 274 | * Set rs->md.bitmap_info.chunksize (which really refers to 'region size'). |
| 275 | * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap. |
| 276 | * |
| 277 | * Returns: 0 on success, -EINVAL on failure. |
| 278 | */ |
| 279 | static int validate_region_size(struct raid_set *rs, unsigned long region_size) |
| 280 | { |
| 281 | unsigned long min_region_size = rs->ti->len / (1 << 21); |
| 282 | |
| 283 | if (!region_size) { |
| 284 | /* |
| 285 | * Choose a reasonable default. All figures in sectors. |
| 286 | */ |
| 287 | if (min_region_size > (1 << 13)) { |
| 288 | DMINFO("Choosing default region size of %lu sectors", |
| 289 | region_size); |
| 290 | region_size = min_region_size; |
| 291 | } else { |
| 292 | DMINFO("Choosing default region size of 4MiB"); |
| 293 | region_size = 1 << 13; /* sectors */ |
| 294 | } |
| 295 | } else { |
| 296 | /* |
| 297 | * Validate user-supplied value. |
| 298 | */ |
| 299 | if (region_size > rs->ti->len) { |
| 300 | rs->ti->error = "Supplied region size is too large"; |
| 301 | return -EINVAL; |
| 302 | } |
| 303 | |
| 304 | if (region_size < min_region_size) { |
| 305 | DMERR("Supplied region_size (%lu sectors) below minimum (%lu)", |
| 306 | region_size, min_region_size); |
| 307 | rs->ti->error = "Supplied region size is too small"; |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | |
| 311 | if (!is_power_of_2(region_size)) { |
| 312 | rs->ti->error = "Region size is not a power of 2"; |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
| 316 | if (region_size < rs->md.chunk_sectors) { |
| 317 | rs->ti->error = "Region size is smaller than the chunk size"; |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Convert sectors to bytes. |
| 324 | */ |
| 325 | rs->md.bitmap_info.chunksize = (region_size << 9); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 331 | * Possible arguments are... |
| 332 | * RAID456: |
| 333 | * <chunk_size> [optional_args] |
| 334 | * |
| 335 | * Optional args: |
| 336 | * [[no]sync] Force or prevent recovery of the entire array |
| 337 | * [rebuild <idx>] Rebuild the drive indicated by the index |
| 338 | * [daemon_sleep <ms>] Time between bitmap daemon work to clear bits |
| 339 | * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization |
| 340 | * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 341 | * [write_mostly <idx>] Indicate a write mostly drive via index |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 342 | * [max_write_behind <sectors>] See '-write-behind=' (man mdadm) |
| 343 | * [stripe_cache <sectors>] Stripe cache size for higher RAIDs |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 344 | * [region_size <sectors>] Defines granularity of bitmap |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 345 | */ |
| 346 | static int parse_raid_params(struct raid_set *rs, char **argv, |
| 347 | unsigned num_raid_params) |
| 348 | { |
| 349 | unsigned i, rebuild_cnt = 0; |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 350 | unsigned long value, region_size = 0; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 351 | char *key; |
| 352 | |
| 353 | /* |
| 354 | * First, parse the in-order required arguments |
| 355 | */ |
| 356 | if ((strict_strtoul(argv[0], 10, &value) < 0) || |
| 357 | !is_power_of_2(value) || (value < 8)) { |
| 358 | rs->ti->error = "Bad chunk size"; |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | rs->md.new_chunk_sectors = rs->md.chunk_sectors = value; |
| 363 | argv++; |
| 364 | num_raid_params--; |
| 365 | |
| 366 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 367 | * We set each individual device as In_sync with a completed |
| 368 | * 'recovery_offset'. If there has been a device failure or |
| 369 | * replacement then one of the following cases applies: |
| 370 | * |
| 371 | * 1) User specifies 'rebuild'. |
| 372 | * - Device is reset when param is read. |
| 373 | * 2) A new device is supplied. |
| 374 | * - No matching superblock found, resets device. |
| 375 | * 3) Device failure was transient and returns on reload. |
| 376 | * - Failure noticed, resets device for bitmap replay. |
| 377 | * 4) Device hadn't completed recovery after previous failure. |
| 378 | * - Superblock is read and overrides recovery_offset. |
| 379 | * |
| 380 | * What is found in the superblocks of the devices is always |
| 381 | * authoritative, unless 'rebuild' or '[no]sync' was specified. |
| 382 | */ |
| 383 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 384 | set_bit(In_sync, &rs->dev[i].rdev.flags); |
| 385 | rs->dev[i].rdev.recovery_offset = MaxSector; |
| 386 | } |
| 387 | |
| 388 | /* |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 389 | * Second, parse the unordered optional arguments |
| 390 | */ |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 391 | for (i = 0; i < num_raid_params; i++) { |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 392 | if (!strcasecmp(argv[i], "nosync")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 393 | rs->md.recovery_cp = MaxSector; |
| 394 | rs->print_flags |= DMPF_NOSYNC; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 395 | continue; |
| 396 | } |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 397 | if (!strcasecmp(argv[i], "sync")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 398 | rs->md.recovery_cp = 0; |
| 399 | rs->print_flags |= DMPF_SYNC; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | |
| 403 | /* The rest of the optional arguments come in key/value pairs */ |
| 404 | if ((i + 1) >= num_raid_params) { |
| 405 | rs->ti->error = "Wrong number of raid parameters given"; |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | key = argv[i++]; |
| 410 | if (strict_strtoul(argv[i], 10, &value) < 0) { |
| 411 | rs->ti->error = "Bad numerical argument given in raid params"; |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 415 | if (!strcasecmp(key, "rebuild")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 416 | if (++rebuild_cnt > rs->raid_type->parity_devs) { |
| 417 | rs->ti->error = "Too many rebuild drives given"; |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | if (value > rs->md.raid_disks) { |
| 421 | rs->ti->error = "Invalid rebuild index given"; |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | clear_bit(In_sync, &rs->dev[value].rdev.flags); |
| 425 | rs->dev[value].rdev.recovery_offset = 0; |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 426 | rs->print_flags |= DMPF_REBUILD; |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 427 | } else if (!strcasecmp(key, "write_mostly")) { |
| 428 | if (rs->raid_type->level != 1) { |
| 429 | rs->ti->error = "write_mostly option is only valid for RAID1"; |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | if (value > rs->md.raid_disks) { |
| 433 | rs->ti->error = "Invalid write_mostly drive index given"; |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | set_bit(WriteMostly, &rs->dev[value].rdev.flags); |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 437 | } else if (!strcasecmp(key, "max_write_behind")) { |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 438 | if (rs->raid_type->level != 1) { |
| 439 | rs->ti->error = "max_write_behind option is only valid for RAID1"; |
| 440 | return -EINVAL; |
| 441 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 442 | rs->print_flags |= DMPF_MAX_WRITE_BEHIND; |
| 443 | |
| 444 | /* |
| 445 | * In device-mapper, we specify things in sectors, but |
| 446 | * MD records this value in kB |
| 447 | */ |
| 448 | value /= 2; |
| 449 | if (value > COUNTER_MAX) { |
| 450 | rs->ti->error = "Max write-behind limit out of range"; |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | rs->md.bitmap_info.max_write_behind = value; |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 454 | } else if (!strcasecmp(key, "daemon_sleep")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 455 | rs->print_flags |= DMPF_DAEMON_SLEEP; |
| 456 | if (!value || (value > MAX_SCHEDULE_TIMEOUT)) { |
| 457 | rs->ti->error = "daemon sleep period out of range"; |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | rs->md.bitmap_info.daemon_sleep = value; |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 461 | } else if (!strcasecmp(key, "stripe_cache")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 462 | rs->print_flags |= DMPF_STRIPE_CACHE; |
| 463 | |
| 464 | /* |
| 465 | * In device-mapper, we specify things in sectors, but |
| 466 | * MD records this value in kB |
| 467 | */ |
| 468 | value /= 2; |
| 469 | |
| 470 | if (rs->raid_type->level < 5) { |
| 471 | rs->ti->error = "Inappropriate argument: stripe_cache"; |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | if (raid5_set_cache_size(&rs->md, (int)value)) { |
| 475 | rs->ti->error = "Bad stripe_cache size"; |
| 476 | return -EINVAL; |
| 477 | } |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 478 | } else if (!strcasecmp(key, "min_recovery_rate")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 479 | rs->print_flags |= DMPF_MIN_RECOVERY_RATE; |
| 480 | if (value > INT_MAX) { |
| 481 | rs->ti->error = "min_recovery_rate out of range"; |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | rs->md.sync_speed_min = (int)value; |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 485 | } else if (!strcasecmp(key, "max_recovery_rate")) { |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 486 | rs->print_flags |= DMPF_MAX_RECOVERY_RATE; |
| 487 | if (value > INT_MAX) { |
| 488 | rs->ti->error = "max_recovery_rate out of range"; |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | rs->md.sync_speed_max = (int)value; |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 492 | } else if (!strcasecmp(key, "region_size")) { |
| 493 | rs->print_flags |= DMPF_REGION_SIZE; |
| 494 | region_size = value; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 495 | } else { |
| 496 | DMERR("Unable to parse RAID parameter: %s", key); |
| 497 | rs->ti->error = "Unable to parse RAID parameters"; |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | } |
| 501 | |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 502 | if (validate_region_size(rs, region_size)) |
| 503 | return -EINVAL; |
| 504 | |
| 505 | if (rs->md.chunk_sectors) |
| 506 | rs->ti->split_io = rs->md.chunk_sectors; |
| 507 | else |
| 508 | rs->ti->split_io = region_size; |
| 509 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 510 | /* Assume there are no metadata devices until the drives are parsed */ |
| 511 | rs->md.persistent = 0; |
| 512 | rs->md.external = 1; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static void do_table_event(struct work_struct *ws) |
| 518 | { |
| 519 | struct raid_set *rs = container_of(ws, struct raid_set, md.event_work); |
| 520 | |
| 521 | dm_table_event(rs->ti->table); |
| 522 | } |
| 523 | |
| 524 | static int raid_is_congested(struct dm_target_callbacks *cb, int bits) |
| 525 | { |
| 526 | struct raid_set *rs = container_of(cb, struct raid_set, callbacks); |
| 527 | |
| 528 | return md_raid5_congested(&rs->md, bits); |
| 529 | } |
| 530 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 531 | /* |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 532 | * This structure is never routinely used by userspace, unlike md superblocks. |
| 533 | * Devices with this superblock should only ever be accessed via device-mapper. |
| 534 | */ |
| 535 | #define DM_RAID_MAGIC 0x64526D44 |
| 536 | struct dm_raid_superblock { |
| 537 | __le32 magic; /* "DmRd" */ |
| 538 | __le32 features; /* Used to indicate possible future changes */ |
| 539 | |
| 540 | __le32 num_devices; /* Number of devices in this array. (Max 64) */ |
| 541 | __le32 array_position; /* The position of this drive in the array */ |
| 542 | |
| 543 | __le64 events; /* Incremented by md when superblock updated */ |
| 544 | __le64 failed_devices; /* Bit field of devices to indicate failures */ |
| 545 | |
| 546 | /* |
| 547 | * This offset tracks the progress of the repair or replacement of |
| 548 | * an individual drive. |
| 549 | */ |
| 550 | __le64 disk_recovery_offset; |
| 551 | |
| 552 | /* |
| 553 | * This offset tracks the progress of the initial array |
| 554 | * synchronisation/parity calculation. |
| 555 | */ |
| 556 | __le64 array_resync_offset; |
| 557 | |
| 558 | /* |
| 559 | * RAID characteristics |
| 560 | */ |
| 561 | __le32 level; |
| 562 | __le32 layout; |
| 563 | __le32 stripe_sectors; |
| 564 | |
| 565 | __u8 pad[452]; /* Round struct to 512 bytes. */ |
| 566 | /* Always set to 0 when writing. */ |
| 567 | } __packed; |
| 568 | |
| 569 | static int read_disk_sb(mdk_rdev_t *rdev, int size) |
| 570 | { |
| 571 | BUG_ON(!rdev->sb_page); |
| 572 | |
| 573 | if (rdev->sb_loaded) |
| 574 | return 0; |
| 575 | |
| 576 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) { |
| 577 | DMERR("Failed to read device superblock"); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | rdev->sb_loaded = 1; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static void super_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 587 | { |
| 588 | mdk_rdev_t *r, *t; |
| 589 | uint64_t failed_devices; |
| 590 | struct dm_raid_superblock *sb; |
| 591 | |
| 592 | sb = page_address(rdev->sb_page); |
| 593 | failed_devices = le64_to_cpu(sb->failed_devices); |
| 594 | |
| 595 | rdev_for_each(r, t, mddev) |
| 596 | if ((r->raid_disk >= 0) && test_bit(Faulty, &r->flags)) |
| 597 | failed_devices |= (1ULL << r->raid_disk); |
| 598 | |
| 599 | memset(sb, 0, sizeof(*sb)); |
| 600 | |
| 601 | sb->magic = cpu_to_le32(DM_RAID_MAGIC); |
| 602 | sb->features = cpu_to_le32(0); /* No features yet */ |
| 603 | |
| 604 | sb->num_devices = cpu_to_le32(mddev->raid_disks); |
| 605 | sb->array_position = cpu_to_le32(rdev->raid_disk); |
| 606 | |
| 607 | sb->events = cpu_to_le64(mddev->events); |
| 608 | sb->failed_devices = cpu_to_le64(failed_devices); |
| 609 | |
| 610 | sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 611 | sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 612 | |
| 613 | sb->level = cpu_to_le32(mddev->level); |
| 614 | sb->layout = cpu_to_le32(mddev->layout); |
| 615 | sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors); |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * super_load |
| 620 | * |
| 621 | * This function creates a superblock if one is not found on the device |
| 622 | * and will decide which superblock to use if there's a choice. |
| 623 | * |
| 624 | * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise |
| 625 | */ |
| 626 | static int super_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev) |
| 627 | { |
| 628 | int ret; |
| 629 | struct dm_raid_superblock *sb; |
| 630 | struct dm_raid_superblock *refsb; |
| 631 | uint64_t events_sb, events_refsb; |
| 632 | |
| 633 | rdev->sb_start = 0; |
| 634 | rdev->sb_size = sizeof(*sb); |
| 635 | |
| 636 | ret = read_disk_sb(rdev, rdev->sb_size); |
| 637 | if (ret) |
| 638 | return ret; |
| 639 | |
| 640 | sb = page_address(rdev->sb_page); |
| 641 | if (sb->magic != cpu_to_le32(DM_RAID_MAGIC)) { |
| 642 | super_sync(rdev->mddev, rdev); |
| 643 | |
| 644 | set_bit(FirstUse, &rdev->flags); |
| 645 | |
| 646 | /* Force writing of superblocks to disk */ |
| 647 | set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags); |
| 648 | |
| 649 | /* Any superblock is better than none, choose that if given */ |
| 650 | return refdev ? 0 : 1; |
| 651 | } |
| 652 | |
| 653 | if (!refdev) |
| 654 | return 1; |
| 655 | |
| 656 | events_sb = le64_to_cpu(sb->events); |
| 657 | |
| 658 | refsb = page_address(refdev->sb_page); |
| 659 | events_refsb = le64_to_cpu(refsb->events); |
| 660 | |
| 661 | return (events_sb > events_refsb) ? 1 : 0; |
| 662 | } |
| 663 | |
| 664 | static int super_init_validation(mddev_t *mddev, mdk_rdev_t *rdev) |
| 665 | { |
| 666 | int role; |
| 667 | struct raid_set *rs = container_of(mddev, struct raid_set, md); |
| 668 | uint64_t events_sb; |
| 669 | uint64_t failed_devices; |
| 670 | struct dm_raid_superblock *sb; |
| 671 | uint32_t new_devs = 0; |
| 672 | uint32_t rebuilds = 0; |
| 673 | mdk_rdev_t *r, *t; |
| 674 | struct dm_raid_superblock *sb2; |
| 675 | |
| 676 | sb = page_address(rdev->sb_page); |
| 677 | events_sb = le64_to_cpu(sb->events); |
| 678 | failed_devices = le64_to_cpu(sb->failed_devices); |
| 679 | |
| 680 | /* |
| 681 | * Initialise to 1 if this is a new superblock. |
| 682 | */ |
| 683 | mddev->events = events_sb ? : 1; |
| 684 | |
| 685 | /* |
| 686 | * Reshaping is not currently allowed |
| 687 | */ |
| 688 | if ((le32_to_cpu(sb->level) != mddev->level) || |
| 689 | (le32_to_cpu(sb->layout) != mddev->layout) || |
| 690 | (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors)) { |
| 691 | DMERR("Reshaping arrays not yet supported."); |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | /* We can only change the number of devices in RAID1 right now */ |
| 696 | if ((rs->raid_type->level != 1) && |
| 697 | (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) { |
| 698 | DMERR("Reshaping arrays not yet supported."); |
| 699 | return -EINVAL; |
| 700 | } |
| 701 | |
| 702 | if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))) |
| 703 | mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset); |
| 704 | |
| 705 | /* |
| 706 | * During load, we set FirstUse if a new superblock was written. |
| 707 | * There are two reasons we might not have a superblock: |
| 708 | * 1) The array is brand new - in which case, all of the |
| 709 | * devices must have their In_sync bit set. Also, |
| 710 | * recovery_cp must be 0, unless forced. |
| 711 | * 2) This is a new device being added to an old array |
| 712 | * and the new device needs to be rebuilt - in which |
| 713 | * case the In_sync bit will /not/ be set and |
| 714 | * recovery_cp must be MaxSector. |
| 715 | */ |
| 716 | rdev_for_each(r, t, mddev) { |
| 717 | if (!test_bit(In_sync, &r->flags)) { |
| 718 | if (!test_bit(FirstUse, &r->flags)) |
| 719 | DMERR("Superblock area of " |
| 720 | "rebuild device %d should have been " |
| 721 | "cleared.", r->raid_disk); |
| 722 | set_bit(FirstUse, &r->flags); |
| 723 | rebuilds++; |
| 724 | } else if (test_bit(FirstUse, &r->flags)) |
| 725 | new_devs++; |
| 726 | } |
| 727 | |
| 728 | if (!rebuilds) { |
| 729 | if (new_devs == mddev->raid_disks) { |
| 730 | DMINFO("Superblocks created for new array"); |
| 731 | set_bit(MD_ARRAY_FIRST_USE, &mddev->flags); |
| 732 | } else if (new_devs) { |
| 733 | DMERR("New device injected " |
| 734 | "into existing array without 'rebuild' " |
| 735 | "parameter specified"); |
| 736 | return -EINVAL; |
| 737 | } |
| 738 | } else if (new_devs) { |
| 739 | DMERR("'rebuild' devices cannot be " |
| 740 | "injected into an array with other first-time devices"); |
| 741 | return -EINVAL; |
| 742 | } else if (mddev->recovery_cp != MaxSector) { |
| 743 | DMERR("'rebuild' specified while array is not in-sync"); |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Now we set the Faulty bit for those devices that are |
| 749 | * recorded in the superblock as failed. |
| 750 | */ |
| 751 | rdev_for_each(r, t, mddev) { |
| 752 | if (!r->sb_page) |
| 753 | continue; |
| 754 | sb2 = page_address(r->sb_page); |
| 755 | sb2->failed_devices = 0; |
| 756 | |
| 757 | /* |
| 758 | * Check for any device re-ordering. |
| 759 | */ |
| 760 | if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) { |
| 761 | role = le32_to_cpu(sb2->array_position); |
| 762 | if (role != r->raid_disk) { |
| 763 | if (rs->raid_type->level != 1) { |
| 764 | rs->ti->error = "Cannot change device " |
| 765 | "positions in RAID array"; |
| 766 | return -EINVAL; |
| 767 | } |
| 768 | DMINFO("RAID1 device #%d now at position #%d", |
| 769 | role, r->raid_disk); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Partial recovery is performed on |
| 774 | * returning failed devices. |
| 775 | */ |
| 776 | if (failed_devices & (1 << role)) |
| 777 | set_bit(Faulty, &r->flags); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int super_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 785 | { |
| 786 | struct dm_raid_superblock *sb = page_address(rdev->sb_page); |
| 787 | |
| 788 | /* |
| 789 | * If mddev->events is not set, we know we have not yet initialized |
| 790 | * the array. |
| 791 | */ |
| 792 | if (!mddev->events && super_init_validation(mddev, rdev)) |
| 793 | return -EINVAL; |
| 794 | |
| 795 | mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */ |
| 796 | rdev->mddev->bitmap_info.default_offset = 4096 >> 9; |
| 797 | if (!test_bit(FirstUse, &rdev->flags)) { |
| 798 | rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset); |
| 799 | if (rdev->recovery_offset != MaxSector) |
| 800 | clear_bit(In_sync, &rdev->flags); |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * If a device comes back, set it as not In_sync and no longer faulty. |
| 805 | */ |
| 806 | if (test_bit(Faulty, &rdev->flags)) { |
| 807 | clear_bit(Faulty, &rdev->flags); |
| 808 | clear_bit(In_sync, &rdev->flags); |
| 809 | rdev->saved_raid_disk = rdev->raid_disk; |
| 810 | rdev->recovery_offset = 0; |
| 811 | } |
| 812 | |
| 813 | clear_bit(FirstUse, &rdev->flags); |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Analyse superblocks and select the freshest. |
| 820 | */ |
| 821 | static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs) |
| 822 | { |
| 823 | int ret; |
| 824 | mdk_rdev_t *rdev, *freshest, *tmp; |
| 825 | mddev_t *mddev = &rs->md; |
| 826 | |
| 827 | freshest = NULL; |
| 828 | rdev_for_each(rdev, tmp, mddev) { |
| 829 | if (!rdev->meta_bdev) |
| 830 | continue; |
| 831 | |
| 832 | ret = super_load(rdev, freshest); |
| 833 | |
| 834 | switch (ret) { |
| 835 | case 1: |
| 836 | freshest = rdev; |
| 837 | break; |
| 838 | case 0: |
| 839 | break; |
| 840 | default: |
| 841 | ti->error = "Failed to load superblock"; |
| 842 | return ret; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (!freshest) |
| 847 | return 0; |
| 848 | |
| 849 | /* |
| 850 | * Validation of the freshest device provides the source of |
| 851 | * validation for the remaining devices. |
| 852 | */ |
| 853 | ti->error = "Unable to assemble array: Invalid superblocks"; |
| 854 | if (super_validate(mddev, freshest)) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | rdev_for_each(rdev, tmp, mddev) |
| 858 | if ((rdev != freshest) && super_validate(mddev, rdev)) |
| 859 | return -EINVAL; |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 865 | * Construct a RAID4/5/6 mapping: |
| 866 | * Args: |
| 867 | * <raid_type> <#raid_params> <raid_params> \ |
| 868 | * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> } |
| 869 | * |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 870 | * <raid_params> varies by <raid_type>. See 'parse_raid_params' for |
| 871 | * details on possible <raid_params>. |
| 872 | */ |
| 873 | static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv) |
| 874 | { |
| 875 | int ret; |
| 876 | struct raid_type *rt; |
| 877 | unsigned long num_raid_params, num_raid_devs; |
| 878 | struct raid_set *rs = NULL; |
| 879 | |
| 880 | /* Must have at least <raid_type> <#raid_params> */ |
| 881 | if (argc < 2) { |
| 882 | ti->error = "Too few arguments"; |
| 883 | return -EINVAL; |
| 884 | } |
| 885 | |
| 886 | /* raid type */ |
| 887 | rt = get_raid_type(argv[0]); |
| 888 | if (!rt) { |
| 889 | ti->error = "Unrecognised raid_type"; |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | argc--; |
| 893 | argv++; |
| 894 | |
| 895 | /* number of RAID parameters */ |
| 896 | if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) { |
| 897 | ti->error = "Cannot understand number of RAID parameters"; |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | argc--; |
| 901 | argv++; |
| 902 | |
| 903 | /* Skip over RAID params for now and find out # of devices */ |
| 904 | if (num_raid_params + 1 > argc) { |
| 905 | ti->error = "Arguments do not agree with counts given"; |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) || |
| 910 | (num_raid_devs >= INT_MAX)) { |
| 911 | ti->error = "Cannot understand number of raid devices"; |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | |
| 915 | rs = context_alloc(ti, rt, (unsigned)num_raid_devs); |
| 916 | if (IS_ERR(rs)) |
| 917 | return PTR_ERR(rs); |
| 918 | |
| 919 | ret = parse_raid_params(rs, argv, (unsigned)num_raid_params); |
| 920 | if (ret) |
| 921 | goto bad; |
| 922 | |
| 923 | ret = -EINVAL; |
| 924 | |
| 925 | argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */ |
| 926 | argv += num_raid_params + 1; |
| 927 | |
| 928 | if (argc != (num_raid_devs * 2)) { |
| 929 | ti->error = "Supplied RAID devices does not match the count given"; |
| 930 | goto bad; |
| 931 | } |
| 932 | |
| 933 | ret = dev_parms(rs, argv); |
| 934 | if (ret) |
| 935 | goto bad; |
| 936 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 937 | rs->md.sync_super = super_sync; |
| 938 | ret = analyse_superblocks(ti, rs); |
| 939 | if (ret) |
| 940 | goto bad; |
| 941 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 942 | INIT_WORK(&rs->md.event_work, do_table_event); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 943 | ti->private = rs; |
| 944 | |
| 945 | mutex_lock(&rs->md.reconfig_mutex); |
| 946 | ret = md_run(&rs->md); |
| 947 | rs->md.in_sync = 0; /* Assume already marked dirty */ |
| 948 | mutex_unlock(&rs->md.reconfig_mutex); |
| 949 | |
| 950 | if (ret) { |
| 951 | ti->error = "Fail to run raid array"; |
| 952 | goto bad; |
| 953 | } |
| 954 | |
| 955 | rs->callbacks.congested_fn = raid_is_congested; |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 956 | dm_table_add_target_callbacks(ti->table, &rs->callbacks); |
| 957 | |
| 958 | return 0; |
| 959 | |
| 960 | bad: |
| 961 | context_free(rs); |
| 962 | |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | static void raid_dtr(struct dm_target *ti) |
| 967 | { |
| 968 | struct raid_set *rs = ti->private; |
| 969 | |
| 970 | list_del_init(&rs->callbacks.list); |
| 971 | md_stop(&rs->md); |
| 972 | context_free(rs); |
| 973 | } |
| 974 | |
| 975 | static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context) |
| 976 | { |
| 977 | struct raid_set *rs = ti->private; |
| 978 | mddev_t *mddev = &rs->md; |
| 979 | |
| 980 | mddev->pers->make_request(mddev, bio); |
| 981 | |
| 982 | return DM_MAPIO_SUBMITTED; |
| 983 | } |
| 984 | |
| 985 | static int raid_status(struct dm_target *ti, status_type_t type, |
| 986 | char *result, unsigned maxlen) |
| 987 | { |
| 988 | struct raid_set *rs = ti->private; |
| 989 | unsigned raid_param_cnt = 1; /* at least 1 for chunksize */ |
| 990 | unsigned sz = 0; |
| 991 | int i; |
| 992 | sector_t sync; |
| 993 | |
| 994 | switch (type) { |
| 995 | case STATUSTYPE_INFO: |
| 996 | DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks); |
| 997 | |
| 998 | for (i = 0; i < rs->md.raid_disks; i++) { |
| 999 | if (test_bit(Faulty, &rs->dev[i].rdev.flags)) |
| 1000 | DMEMIT("D"); |
| 1001 | else if (test_bit(In_sync, &rs->dev[i].rdev.flags)) |
| 1002 | DMEMIT("A"); |
| 1003 | else |
| 1004 | DMEMIT("a"); |
| 1005 | } |
| 1006 | |
| 1007 | if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery)) |
| 1008 | sync = rs->md.curr_resync_completed; |
| 1009 | else |
| 1010 | sync = rs->md.recovery_cp; |
| 1011 | |
| 1012 | if (sync > rs->md.resync_max_sectors) |
| 1013 | sync = rs->md.resync_max_sectors; |
| 1014 | |
| 1015 | DMEMIT(" %llu/%llu", |
| 1016 | (unsigned long long) sync, |
| 1017 | (unsigned long long) rs->md.resync_max_sectors); |
| 1018 | |
| 1019 | break; |
| 1020 | case STATUSTYPE_TABLE: |
| 1021 | /* The string you would use to construct this array */ |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1022 | for (i = 0; i < rs->md.raid_disks; i++) { |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1023 | if ((rs->print_flags & DMPF_REBUILD) && |
| 1024 | rs->dev[i].data_dev && |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1025 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1026 | raid_param_cnt += 2; /* for rebuilds */ |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1027 | if (rs->dev[i].data_dev && |
| 1028 | test_bit(WriteMostly, &rs->dev[i].rdev.flags)) |
| 1029 | raid_param_cnt += 2; |
| 1030 | } |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1031 | |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1032 | raid_param_cnt += (hweight64(rs->print_flags & ~DMPF_REBUILD) * 2); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1033 | if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)) |
| 1034 | raid_param_cnt--; |
| 1035 | |
| 1036 | DMEMIT("%s %u %u", rs->raid_type->name, |
| 1037 | raid_param_cnt, rs->md.chunk_sectors); |
| 1038 | |
| 1039 | if ((rs->print_flags & DMPF_SYNC) && |
| 1040 | (rs->md.recovery_cp == MaxSector)) |
| 1041 | DMEMIT(" sync"); |
| 1042 | if (rs->print_flags & DMPF_NOSYNC) |
| 1043 | DMEMIT(" nosync"); |
| 1044 | |
| 1045 | for (i = 0; i < rs->md.raid_disks; i++) |
Jonathan Brassow | 13c8758 | 2011-08-02 12:32:03 +0100 | [diff] [blame] | 1046 | if ((rs->print_flags & DMPF_REBUILD) && |
| 1047 | rs->dev[i].data_dev && |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1048 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) |
| 1049 | DMEMIT(" rebuild %u", i); |
| 1050 | |
| 1051 | if (rs->print_flags & DMPF_DAEMON_SLEEP) |
| 1052 | DMEMIT(" daemon_sleep %lu", |
| 1053 | rs->md.bitmap_info.daemon_sleep); |
| 1054 | |
| 1055 | if (rs->print_flags & DMPF_MIN_RECOVERY_RATE) |
| 1056 | DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min); |
| 1057 | |
| 1058 | if (rs->print_flags & DMPF_MAX_RECOVERY_RATE) |
| 1059 | DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max); |
| 1060 | |
Jonathan Brassow | 46bed2b | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1061 | for (i = 0; i < rs->md.raid_disks; i++) |
| 1062 | if (rs->dev[i].data_dev && |
| 1063 | test_bit(WriteMostly, &rs->dev[i].rdev.flags)) |
| 1064 | DMEMIT(" write_mostly %u", i); |
| 1065 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1066 | if (rs->print_flags & DMPF_MAX_WRITE_BEHIND) |
| 1067 | DMEMIT(" max_write_behind %lu", |
| 1068 | rs->md.bitmap_info.max_write_behind); |
| 1069 | |
| 1070 | if (rs->print_flags & DMPF_STRIPE_CACHE) { |
| 1071 | raid5_conf_t *conf = rs->md.private; |
| 1072 | |
| 1073 | /* convert from kiB to sectors */ |
| 1074 | DMEMIT(" stripe_cache %d", |
| 1075 | conf ? conf->max_nr_stripes * 2 : 0); |
| 1076 | } |
| 1077 | |
Jonathan Brassow | c108456 | 2011-08-02 12:32:07 +0100 | [diff] [blame] | 1078 | if (rs->print_flags & DMPF_REGION_SIZE) |
| 1079 | DMEMIT(" region_size %lu", |
| 1080 | rs->md.bitmap_info.chunksize >> 9); |
| 1081 | |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1082 | DMEMIT(" %d", rs->md.raid_disks); |
| 1083 | for (i = 0; i < rs->md.raid_disks; i++) { |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 1084 | if (rs->dev[i].meta_dev) |
| 1085 | DMEMIT(" %s", rs->dev[i].meta_dev->name); |
| 1086 | else |
| 1087 | DMEMIT(" -"); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1088 | |
| 1089 | if (rs->dev[i].data_dev) |
| 1090 | DMEMIT(" %s", rs->dev[i].data_dev->name); |
| 1091 | else |
| 1092 | DMEMIT(" -"); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data) |
| 1100 | { |
| 1101 | struct raid_set *rs = ti->private; |
| 1102 | unsigned i; |
| 1103 | int ret = 0; |
| 1104 | |
| 1105 | for (i = 0; !ret && i < rs->md.raid_disks; i++) |
| 1106 | if (rs->dev[i].data_dev) |
| 1107 | ret = fn(ti, |
| 1108 | rs->dev[i].data_dev, |
| 1109 | 0, /* No offset on data devs */ |
| 1110 | rs->md.dev_sectors, |
| 1111 | data); |
| 1112 | |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 1117 | { |
| 1118 | struct raid_set *rs = ti->private; |
| 1119 | unsigned chunk_size = rs->md.chunk_sectors << 9; |
| 1120 | raid5_conf_t *conf = rs->md.private; |
| 1121 | |
| 1122 | blk_limits_io_min(limits, chunk_size); |
| 1123 | blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded)); |
| 1124 | } |
| 1125 | |
| 1126 | static void raid_presuspend(struct dm_target *ti) |
| 1127 | { |
| 1128 | struct raid_set *rs = ti->private; |
| 1129 | |
| 1130 | md_stop_writes(&rs->md); |
| 1131 | } |
| 1132 | |
| 1133 | static void raid_postsuspend(struct dm_target *ti) |
| 1134 | { |
| 1135 | struct raid_set *rs = ti->private; |
| 1136 | |
| 1137 | mddev_suspend(&rs->md); |
| 1138 | } |
| 1139 | |
| 1140 | static void raid_resume(struct dm_target *ti) |
| 1141 | { |
| 1142 | struct raid_set *rs = ti->private; |
| 1143 | |
Jonathan Brassow | b12d437 | 2011-08-02 12:32:07 +0100 | [diff] [blame^] | 1144 | bitmap_load(&rs->md); |
NeilBrown | 9d09e66 | 2011-01-13 20:00:02 +0000 | [diff] [blame] | 1145 | mddev_resume(&rs->md); |
| 1146 | } |
| 1147 | |
| 1148 | static struct target_type raid_target = { |
| 1149 | .name = "raid", |
| 1150 | .version = {1, 0, 0}, |
| 1151 | .module = THIS_MODULE, |
| 1152 | .ctr = raid_ctr, |
| 1153 | .dtr = raid_dtr, |
| 1154 | .map = raid_map, |
| 1155 | .status = raid_status, |
| 1156 | .iterate_devices = raid_iterate_devices, |
| 1157 | .io_hints = raid_io_hints, |
| 1158 | .presuspend = raid_presuspend, |
| 1159 | .postsuspend = raid_postsuspend, |
| 1160 | .resume = raid_resume, |
| 1161 | }; |
| 1162 | |
| 1163 | static int __init dm_raid_init(void) |
| 1164 | { |
| 1165 | return dm_register_target(&raid_target); |
| 1166 | } |
| 1167 | |
| 1168 | static void __exit dm_raid_exit(void) |
| 1169 | { |
| 1170 | dm_unregister_target(&raid_target); |
| 1171 | } |
| 1172 | |
| 1173 | module_init(dm_raid_init); |
| 1174 | module_exit(dm_raid_exit); |
| 1175 | |
| 1176 | MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target"); |
| 1177 | MODULE_ALIAS("dm-raid4"); |
| 1178 | MODULE_ALIAS("dm-raid5"); |
| 1179 | MODULE_ALIAS("dm-raid6"); |
| 1180 | MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>"); |
| 1181 | MODULE_LICENSE("GPL"); |