| 1 | |
| 2 | |
| 3 | raid0.c : Multiple Devices driver for Linux
|
| 4 | Copyright (C) 1994-96 Marc ZYNGIER
|
| 5 | <[email protected]> or
|
| 6 | <[email protected]>
|
| 7 | Copyright (C) 1999, 2000 Ingo Molnar, Red Hat
|
| 8 |
|
| 9 | RAID-0 management functions.
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 | #include <linux/blkdev.h>
|
| 14 | #include <linux/seq_file.h>
|
| 15 | #include <linux/module.h>
|
| 16 | #include <linux/slab.h>
|
| 17 | #include <linux/string_choices.h>
|
| 18 | #include <trace/events/block.h>
|
| 19 | #include "md.h"
|
| 20 | #include "raid0.h"
|
| 21 | #include "raid5.h"
|
| 22 |
|
| 23 | static int default_layout = 0;
|
| 24 | module_param(default_layout, int, 0644);
|
| 25 |
|
| 26 | #define UNSUPPORTED_MDDEV_FLAGS \
|
| 27 | ((1L << MD_HAS_JOURNAL) | \
|
| 28 | (1L << MD_JOURNAL_CLEAN) | \
|
| 29 | (1L << MD_FAILFAST_SUPPORTED) |\
|
| 30 | (1L << MD_HAS_PPL) | \
|
| 31 | (1L << MD_HAS_MULTIPLE_PPLS) | \
|
| 32 | (1L << MD_FAILLAST_DEV) | \
|
| 33 | (1L << MD_SERIALIZE_POLICY))
|
| 34 |
|
| 35 | |
| 36 | * inform the user of the raid configuration
|
| 37 |
|
| 38 | static void dump_zones(struct mddev *mddev)
|
| 39 | {
|
| 40 | int j, k;
|
| 41 | sector_t zone_size = 0;
|
| 42 | sector_t zone_start = 0;
|
| 43 | struct r0conf *conf = mddev->private;
|
| 44 | int raid_disks = conf->strip_zone[0].nb_dev;
|
| 45 | pr_debug("md: RAID0 configuration for %s - %d zone%s\n",
|
| 46 | mdname(mddev),
|
| 47 | conf->nr_strip_zones, str_plural(conf->nr_strip_zones));
|
| 48 | for (j = 0; j < conf->nr_strip_zones; j++) {
|
| 49 | char line[200];
|
| 50 | int len = 0;
|
| 51 |
|
| 52 | for (k = 0; k < conf->strip_zone[j].nb_dev; k++)
|
| 53 | len += scnprintf(line+len, 200-len, "%s%pg", k?"/":"",
|
| 54 | conf->devlist[j * raid_disks + k]->bdev);
|
| 55 | pr_debug("md: zone%d=[%s]\n", j, line);
|
| 56 |
|
| 57 | zone_size = conf->strip_zone[j].zone_end - zone_start;
|
| 58 | pr_debug(" zone-offset=%10lluKB, device-offset=%10lluKB, size=%10lluKB\n",
|
| 59 | (unsigned long long)zone_start>>1,
|
| 60 | (unsigned long long)conf->strip_zone[j].dev_start>>1,
|
| 61 | (unsigned long long)zone_size>>1);
|
| 62 | zone_start = conf->strip_zone[j].zone_end;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | static int create_strip_zones(struct mddev *mddev, struct r0conf **private_conf)
|
| 67 | {
|
| 68 | int i, c, err;
|
| 69 | sector_t curr_zone_end, sectors;
|
| 70 | struct md_rdev *smallest, *rdev1, *rdev2, *rdev, **dev;
|
| 71 | struct strip_zone *zone;
|
| 72 | int cnt;
|
| 73 | struct r0conf *conf = kzalloc_obj(*conf);
|
| 74 | unsigned int blksize = 512;
|
| 75 |
|
| 76 | if (!mddev_is_dm(mddev))
|
| 77 | blksize = queue_logical_block_size(mddev->gendisk->queue);
|
| 78 |
|
| 79 | *private_conf = ERR_PTR(-ENOMEM);
|
| 80 | if (!conf)
|
| 81 | return -ENOMEM;
|
| 82 | rdev_for_each(rdev1, mddev) {
|
| 83 | pr_debug("md/raid0:%s: looking at %pg\n",
|
| 84 | mdname(mddev),
|
| 85 | rdev1->bdev);
|
| 86 | c = 0;
|
| 87 |
|
| 88 |
|
| 89 | sectors = rdev1->sectors;
|
| 90 | sector_div(sectors, mddev->chunk_sectors);
|
| 91 | rdev1->sectors = sectors * mddev->chunk_sectors;
|
| 92 |
|
| 93 | if (mddev_is_dm(mddev))
|
| 94 | blksize = max(blksize, queue_logical_block_size(
|
| 95 | rdev1->bdev->bd_disk->queue));
|
| 96 |
|
| 97 | rdev_for_each(rdev2, mddev) {
|
| 98 | pr_debug("md/raid0:%s: comparing %pg(%llu)"
|
| 99 | " with %pg(%llu)\n",
|
| 100 | mdname(mddev),
|
| 101 | rdev1->bdev,
|
| 102 | (unsigned long long)rdev1->sectors,
|
| 103 | rdev2->bdev,
|
| 104 | (unsigned long long)rdev2->sectors);
|
| 105 | if (rdev2 == rdev1) {
|
| 106 | pr_debug("md/raid0:%s: END\n",
|
| 107 | mdname(mddev));
|
| 108 | break;
|
| 109 | }
|
| 110 | if (rdev2->sectors == rdev1->sectors) {
|
| 111 | |
| 112 | * Not unique, don't count it as a new
|
| 113 | * group
|
| 114 |
|
| 115 | pr_debug("md/raid0:%s: EQUAL\n",
|
| 116 | mdname(mddev));
|
| 117 | c = 1;
|
| 118 | break;
|
| 119 | }
|
| 120 | pr_debug("md/raid0:%s: NOT EQUAL\n",
|
| 121 | mdname(mddev));
|
| 122 | }
|
| 123 | if (!c) {
|
| 124 | pr_debug("md/raid0:%s: ==> UNIQUE\n",
|
| 125 | mdname(mddev));
|
| 126 | conf->nr_strip_zones++;
|
| 127 | pr_debug("md/raid0:%s: %d zones\n",
|
| 128 | mdname(mddev), conf->nr_strip_zones);
|
| 129 | }
|
| 130 | }
|
| 131 | pr_debug("md/raid0:%s: FINAL %d zones\n",
|
| 132 | mdname(mddev), conf->nr_strip_zones);
|
| 133 |
|
| 134 | |
| 135 | * now since we have the hard sector sizes, we can make sure
|
| 136 | * chunk size is a multiple of that sector size
|
| 137 |
|
| 138 | if ((mddev->chunk_sectors << 9) % blksize) {
|
| 139 | pr_warn("md/raid0:%s: chunk_size of %d not multiple of block size %d\n",
|
| 140 | mdname(mddev),
|
| 141 | mddev->chunk_sectors << 9, blksize);
|
| 142 | err = -EINVAL;
|
| 143 | goto abort;
|
| 144 | }
|
| 145 |
|
| 146 | err = -ENOMEM;
|
| 147 | conf->strip_zone = kvzalloc_objs(struct strip_zone, conf->nr_strip_zones);
|
| 148 | if (!conf->strip_zone)
|
| 149 | goto abort;
|
| 150 | conf->devlist = kvzalloc(array3_size(sizeof(struct md_rdev *),
|
| 151 | conf->nr_strip_zones,
|
| 152 | mddev->raid_disks),
|
| 153 | GFP_KERNEL);
|
| 154 | if (!conf->devlist)
|
| 155 | goto abort;
|
| 156 |
|
| 157 | |
| 158 | * there is a proper alignment of slots to devices and find them all
|
| 159 |
|
| 160 | zone = &conf->strip_zone[0];
|
| 161 | cnt = 0;
|
| 162 | smallest = NULL;
|
| 163 | dev = conf->devlist;
|
| 164 | err = -EINVAL;
|
| 165 | rdev_for_each(rdev1, mddev) {
|
| 166 | int j = rdev1->raid_disk;
|
| 167 |
|
| 168 | if (mddev->level == 10) {
|
| 169 |
|
| 170 | j /= 2;
|
| 171 | rdev1->new_raid_disk = j;
|
| 172 | }
|
| 173 |
|
| 174 | if (mddev->level == 1) {
|
| 175 | |
| 176 | * we have only one active disk
|
| 177 |
|
| 178 | j = 0;
|
| 179 | rdev1->new_raid_disk = j;
|
| 180 | }
|
| 181 |
|
| 182 | if (j < 0) {
|
| 183 | pr_warn("md/raid0:%s: remove inactive devices before converting to RAID0\n",
|
| 184 | mdname(mddev));
|
| 185 | goto abort;
|
| 186 | }
|
| 187 | if (j >= mddev->raid_disks) {
|
| 188 | pr_warn("md/raid0:%s: bad disk number %d - aborting!\n",
|
| 189 | mdname(mddev), j);
|
| 190 | goto abort;
|
| 191 | }
|
| 192 | if (dev[j]) {
|
| 193 | pr_warn("md/raid0:%s: multiple devices for %d - aborting!\n",
|
| 194 | mdname(mddev), j);
|
| 195 | goto abort;
|
| 196 | }
|
| 197 | dev[j] = rdev1;
|
| 198 |
|
| 199 | if (!smallest || (rdev1->sectors < smallest->sectors))
|
| 200 | smallest = rdev1;
|
| 201 | cnt++;
|
| 202 | }
|
| 203 | if (cnt != mddev->raid_disks) {
|
| 204 | pr_warn("md/raid0:%s: too few disks (%d of %d) - aborting!\n",
|
| 205 | mdname(mddev), cnt, mddev->raid_disks);
|
| 206 | goto abort;
|
| 207 | }
|
| 208 | zone->nb_dev = cnt;
|
| 209 | zone->zone_end = smallest->sectors * cnt;
|
| 210 |
|
| 211 | curr_zone_end = zone->zone_end;
|
| 212 |
|
| 213 |
|
| 214 | for (i = 1; i < conf->nr_strip_zones; i++)
|
| 215 | {
|
| 216 | int j;
|
| 217 |
|
| 218 | zone = conf->strip_zone + i;
|
| 219 | dev = conf->devlist + i * mddev->raid_disks;
|
| 220 |
|
| 221 | pr_debug("md/raid0:%s: zone %d\n", mdname(mddev), i);
|
| 222 | zone->dev_start = smallest->sectors;
|
| 223 | smallest = NULL;
|
| 224 | c = 0;
|
| 225 |
|
| 226 | for (j=0; j<cnt; j++) {
|
| 227 | rdev = conf->devlist[j];
|
| 228 | if (rdev->sectors <= zone->dev_start) {
|
| 229 | pr_debug("md/raid0:%s: checking %pg ... nope\n",
|
| 230 | mdname(mddev),
|
| 231 | rdev->bdev);
|
| 232 | continue;
|
| 233 | }
|
| 234 | pr_debug("md/raid0:%s: checking %pg ..."
|
| 235 | " contained as device %d\n",
|
| 236 | mdname(mddev),
|
| 237 | rdev->bdev, c);
|
| 238 | dev[c] = rdev;
|
| 239 | c++;
|
| 240 | if (!smallest || rdev->sectors < smallest->sectors) {
|
| 241 | smallest = rdev;
|
| 242 | pr_debug("md/raid0:%s: (%llu) is smallest!.\n",
|
| 243 | mdname(mddev),
|
| 244 | (unsigned long long)rdev->sectors);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | zone->nb_dev = c;
|
| 249 | sectors = (smallest->sectors - zone->dev_start) * c;
|
| 250 | pr_debug("md/raid0:%s: zone->nb_dev: %d, sectors: %llu\n",
|
| 251 | mdname(mddev),
|
| 252 | zone->nb_dev, (unsigned long long)sectors);
|
| 253 |
|
| 254 | curr_zone_end += sectors;
|
| 255 | zone->zone_end = curr_zone_end;
|
| 256 |
|
| 257 | pr_debug("md/raid0:%s: current zone start: %llu\n",
|
| 258 | mdname(mddev),
|
| 259 | (unsigned long long)smallest->sectors);
|
| 260 | }
|
| 261 |
|
| 262 | if (conf->nr_strip_zones == 1 || conf->strip_zone[1].nb_dev == 1) {
|
| 263 | conf->layout = RAID0_ORIG_LAYOUT;
|
| 264 | } else if (mddev->layout == RAID0_ORIG_LAYOUT ||
|
| 265 | mddev->layout == RAID0_ALT_MULTIZONE_LAYOUT) {
|
| 266 | conf->layout = mddev->layout;
|
| 267 | } else if (default_layout == RAID0_ORIG_LAYOUT ||
|
| 268 | default_layout == RAID0_ALT_MULTIZONE_LAYOUT) {
|
| 269 | conf->layout = default_layout;
|
| 270 | } else {
|
| 271 | pr_err("md/raid0:%s: cannot assemble multi-zone RAID0 with default_layout setting\n",
|
| 272 | mdname(mddev));
|
| 273 | pr_err("md/raid0: please set raid0.default_layout to 1 or 2\n");
|
| 274 | err = -EOPNOTSUPP;
|
| 275 | goto abort;
|
| 276 | }
|
| 277 |
|
| 278 | if (conf->layout == RAID0_ORIG_LAYOUT) {
|
| 279 | for (i = 1; i < conf->nr_strip_zones; i++) {
|
| 280 | sector_t first_sector = conf->strip_zone[i-1].zone_end;
|
| 281 |
|
| 282 | sector_div(first_sector, mddev->chunk_sectors);
|
| 283 | zone = conf->strip_zone + i;
|
| 284 |
|
| 285 | zone->disk_shift = sector_div(first_sector,
|
| 286 | zone->nb_dev);
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | pr_debug("md/raid0:%s: done.\n", mdname(mddev));
|
| 291 | *private_conf = conf;
|
| 292 |
|
| 293 | return 0;
|
| 294 | abort:
|
| 295 | kvfree(conf->strip_zone);
|
| 296 | kvfree(conf->devlist);
|
| 297 | kfree(conf);
|
| 298 | *private_conf = ERR_PTR(err);
|
| 299 | return err;
|
| 300 | }
|
| 301 |
|
| 302 | |
| 303 | * Update *sectorp to be an offset in that zone
|
| 304 |
|
| 305 | static struct strip_zone *find_zone(struct r0conf *conf,
|
| 306 | sector_t *sectorp)
|
| 307 | {
|
| 308 | int i;
|
| 309 | struct strip_zone *z = conf->strip_zone;
|
| 310 | sector_t sector = *sectorp;
|
| 311 |
|
| 312 | for (i = 0; i < conf->nr_strip_zones; i++)
|
| 313 | if (sector < z[i].zone_end) {
|
| 314 | if (i)
|
| 315 | *sectorp = sector - z[i-1].zone_end;
|
| 316 | return z + i;
|
| 317 | }
|
| 318 | BUG();
|
| 319 | }
|
| 320 |
|
| 321 | |
| 322 | * remaps the bio to the target device. we separate two flows.
|
| 323 | * power 2 flow and a general flow for the sake of performance
|
| 324 |
|
| 325 | static struct md_rdev *map_sector(struct mddev *mddev, struct strip_zone *zone,
|
| 326 | sector_t sector, sector_t *sector_offset)
|
| 327 | {
|
| 328 | unsigned int sect_in_chunk;
|
| 329 | sector_t chunk;
|
| 330 | struct r0conf *conf = mddev->private;
|
| 331 | int raid_disks = conf->strip_zone[0].nb_dev;
|
| 332 | unsigned int chunk_sects = mddev->chunk_sectors;
|
| 333 |
|
| 334 | if (is_power_of_2(chunk_sects)) {
|
| 335 | int chunksect_bits = ffz(~chunk_sects);
|
| 336 |
|
| 337 | sect_in_chunk = sector & (chunk_sects - 1);
|
| 338 | sector >>= chunksect_bits;
|
| 339 |
|
| 340 | chunk = *sector_offset;
|
| 341 |
|
| 342 | sector_div(chunk, zone->nb_dev << chunksect_bits);
|
| 343 | } else{
|
| 344 | sect_in_chunk = sector_div(sector, chunk_sects);
|
| 345 | chunk = *sector_offset;
|
| 346 | sector_div(chunk, chunk_sects * zone->nb_dev);
|
| 347 | }
|
| 348 | |
| 349 | * position the bio over the real device
|
| 350 | * real sector = chunk in device + starting of zone
|
| 351 | * + the position in the chunk
|
| 352 |
|
| 353 | *sector_offset = (chunk * chunk_sects) + sect_in_chunk;
|
| 354 | return conf->devlist[(zone - conf->strip_zone)*raid_disks
|
| 355 | + sector_div(sector, zone->nb_dev)];
|
| 356 | }
|
| 357 |
|
| 358 | static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks)
|
| 359 | {
|
| 360 | sector_t array_sectors = 0;
|
| 361 | struct md_rdev *rdev;
|
| 362 |
|
| 363 | WARN_ONCE(sectors || raid_disks,
|
| 364 | "%s does not support generic reshape\n", __func__);
|
| 365 |
|
| 366 | rdev_for_each(rdev, mddev)
|
| 367 | array_sectors += (rdev->sectors &
|
| 368 | ~(sector_t)(mddev->chunk_sectors-1));
|
| 369 |
|
| 370 | return array_sectors;
|
| 371 | }
|
| 372 |
|
| 373 | static void raid0_free(struct mddev *mddev, void *priv)
|
| 374 | {
|
| 375 | struct r0conf *conf = priv;
|
| 376 |
|
| 377 | kvfree(conf->strip_zone);
|
| 378 | kvfree(conf->devlist);
|
| 379 | kfree(conf);
|
| 380 | }
|
| 381 |
|
| 382 | static int raid0_set_limits(struct mddev *mddev)
|
| 383 | {
|
| 384 | struct queue_limits lim;
|
| 385 | int err;
|
| 386 |
|
| 387 | md_init_stacking_limits(&lim);
|
| 388 | lim.features |= BLK_FEAT_NOWAIT;
|
| 389 | lim.max_hw_sectors = mddev->chunk_sectors;
|
| 390 | lim.max_write_zeroes_sectors = mddev->chunk_sectors;
|
| 391 | lim.max_hw_wzeroes_unmap_sectors = mddev->chunk_sectors;
|
| 392 | lim.logical_block_size = mddev->logical_block_size;
|
| 393 | lim.io_min = mddev->chunk_sectors << 9;
|
| 394 | lim.io_opt = lim.io_min * mddev->raid_disks;
|
| 395 | lim.chunk_sectors = mddev->chunk_sectors;
|
| 396 | lim.features |= BLK_FEAT_ATOMIC_WRITES;
|
| 397 | lim.features |= BLK_FEAT_PCI_P2PDMA;
|
| 398 | err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY);
|
| 399 | if (err)
|
| 400 | return err;
|
| 401 | return queue_limits_set(mddev->gendisk->queue, &lim);
|
| 402 | }
|
| 403 |
|
| 404 | static int raid0_run(struct mddev *mddev)
|
| 405 | {
|
| 406 | struct r0conf *conf;
|
| 407 | int ret;
|
| 408 |
|
| 409 | if (mddev->chunk_sectors == 0) {
|
| 410 | pr_warn("md/raid0:%s: chunk size must be set.\n", mdname(mddev));
|
| 411 | return -EINVAL;
|
| 412 | }
|
| 413 | if (md_check_no_bitmap(mddev))
|
| 414 | return -EINVAL;
|
| 415 |
|
| 416 | if (!mddev_is_dm(mddev)) {
|
| 417 | ret = raid0_set_limits(mddev);
|
| 418 | if (ret)
|
| 419 | return ret;
|
| 420 | }
|
| 421 |
|
| 422 |
|
| 423 | if (mddev->private == NULL) {
|
| 424 | ret = create_strip_zones(mddev, &conf);
|
| 425 | if (ret < 0)
|
| 426 | return ret;
|
| 427 | mddev->private = conf;
|
| 428 | }
|
| 429 | conf = mddev->private;
|
| 430 |
|
| 431 |
|
| 432 | md_set_array_sectors(mddev, raid0_size(mddev, 0, 0));
|
| 433 |
|
| 434 | pr_debug("md/raid0:%s: md_size is %llu sectors.\n",
|
| 435 | mdname(mddev),
|
| 436 | (unsigned long long)mddev->array_sectors);
|
| 437 |
|
| 438 | dump_zones(mddev);
|
| 439 |
|
| 440 | return md_integrity_register(mddev);
|
| 441 | }
|
| 442 |
|
| 443 | |
| 444 | * Convert disk_index to the disk order in which it is read/written.
|
| 445 | * For example, if we have 4 disks, they are numbered 0,1,2,3. If we
|
| 446 | * write the disks starting at disk 3, then the read/write order would
|
| 447 | * be disk 3, then 0, then 1, and then disk 2 and we want map_disk_shift()
|
| 448 | * to map the disks as follows 0,1,2,3 => 1,2,3,0. So disk 0 would map
|
| 449 | * to 1, 1 to 2, 2 to 3, and 3 to 0. That way we can compare disks in
|
| 450 | * that 'output' space to understand the read/write disk ordering.
|
| 451 |
|
| 452 | static int map_disk_shift(int disk_index, int num_disks, int disk_shift)
|
| 453 | {
|
| 454 | return ((disk_index + num_disks - disk_shift) % num_disks);
|
| 455 | }
|
| 456 |
|
| 457 | static void raid0_handle_discard(struct mddev *mddev, struct bio *bio)
|
| 458 | {
|
| 459 | struct r0conf *conf = mddev->private;
|
| 460 | struct strip_zone *zone;
|
| 461 | sector_t start = bio->bi_iter.bi_sector;
|
| 462 | sector_t end;
|
| 463 | unsigned int stripe_size;
|
| 464 | sector_t first_stripe_index, last_stripe_index;
|
| 465 | sector_t start_disk_offset;
|
| 466 | unsigned int start_disk_index;
|
| 467 | sector_t end_disk_offset;
|
| 468 | unsigned int end_disk_index;
|
| 469 | unsigned int disk;
|
| 470 | sector_t orig_start, orig_end;
|
| 471 |
|
| 472 | orig_start = start;
|
| 473 | zone = find_zone(conf, &start);
|
| 474 |
|
| 475 | if (bio_end_sector(bio) > zone->zone_end) {
|
| 476 | bio = bio_submit_split_bioset(bio,
|
| 477 | zone->zone_end - bio->bi_iter.bi_sector,
|
| 478 | &mddev->bio_set);
|
| 479 | if (!bio)
|
| 480 | return;
|
| 481 |
|
| 482 | end = zone->zone_end;
|
| 483 | } else {
|
| 484 | end = bio_end_sector(bio);
|
| 485 | }
|
| 486 |
|
| 487 | orig_end = end;
|
| 488 | if (zone != conf->strip_zone)
|
| 489 | end = end - zone[-1].zone_end;
|
| 490 |
|
| 491 |
|
| 492 | stripe_size = zone->nb_dev * mddev->chunk_sectors;
|
| 493 |
|
| 494 | first_stripe_index = start;
|
| 495 | sector_div(first_stripe_index, stripe_size);
|
| 496 | last_stripe_index = end;
|
| 497 | sector_div(last_stripe_index, stripe_size);
|
| 498 |
|
| 499 |
|
| 500 | if ((conf->layout == RAID0_ORIG_LAYOUT) && (zone != conf->strip_zone)) {
|
| 501 | sector_div(orig_start, mddev->chunk_sectors);
|
| 502 | start_disk_index = sector_div(orig_start, zone->nb_dev);
|
| 503 | start_disk_index = map_disk_shift(start_disk_index,
|
| 504 | zone->nb_dev,
|
| 505 | zone->disk_shift);
|
| 506 | sector_div(orig_end, mddev->chunk_sectors);
|
| 507 | end_disk_index = sector_div(orig_end, zone->nb_dev);
|
| 508 | end_disk_index = map_disk_shift(end_disk_index,
|
| 509 | zone->nb_dev, zone->disk_shift);
|
| 510 | } else {
|
| 511 | start_disk_index = (int)(start - first_stripe_index * stripe_size) /
|
| 512 | mddev->chunk_sectors;
|
| 513 | end_disk_index = (int)(end - last_stripe_index * stripe_size) /
|
| 514 | mddev->chunk_sectors;
|
| 515 | }
|
| 516 | start_disk_offset = ((int)(start - first_stripe_index * stripe_size) %
|
| 517 | mddev->chunk_sectors) +
|
| 518 | first_stripe_index * mddev->chunk_sectors;
|
| 519 | end_disk_offset = ((int)(end - last_stripe_index * stripe_size) %
|
| 520 | mddev->chunk_sectors) +
|
| 521 | last_stripe_index * mddev->chunk_sectors;
|
| 522 |
|
| 523 | for (disk = 0; disk < zone->nb_dev; disk++) {
|
| 524 | sector_t dev_start, dev_end;
|
| 525 | struct md_rdev *rdev;
|
| 526 | int compare_disk;
|
| 527 |
|
| 528 | compare_disk = map_disk_shift(disk, zone->nb_dev,
|
| 529 | zone->disk_shift);
|
| 530 |
|
| 531 | if (compare_disk < start_disk_index)
|
| 532 | dev_start = (first_stripe_index + 1) *
|
| 533 | mddev->chunk_sectors;
|
| 534 | else if (compare_disk > start_disk_index)
|
| 535 | dev_start = first_stripe_index * mddev->chunk_sectors;
|
| 536 | else
|
| 537 | dev_start = start_disk_offset;
|
| 538 |
|
| 539 | if (compare_disk < end_disk_index)
|
| 540 | dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
|
| 541 | else if (compare_disk > end_disk_index)
|
| 542 | dev_end = last_stripe_index * mddev->chunk_sectors;
|
| 543 | else
|
| 544 | dev_end = end_disk_offset;
|
| 545 |
|
| 546 | if (dev_end <= dev_start)
|
| 547 | continue;
|
| 548 |
|
| 549 | rdev = conf->devlist[(zone - conf->strip_zone) *
|
| 550 | conf->strip_zone[0].nb_dev + disk];
|
| 551 | md_submit_discard_bio(mddev, rdev, bio,
|
| 552 | dev_start + zone->dev_start + rdev->data_offset,
|
| 553 | dev_end - dev_start);
|
| 554 | }
|
| 555 | bio_endio(bio);
|
| 556 | }
|
| 557 |
|
| 558 | static void raid0_map_submit_bio(struct mddev *mddev, struct bio *bio)
|
| 559 | {
|
| 560 | struct r0conf *conf = mddev->private;
|
| 561 | struct strip_zone *zone;
|
| 562 | struct md_rdev *tmp_dev;
|
| 563 | sector_t bio_sector = bio->bi_iter.bi_sector;
|
| 564 | sector_t sector = bio_sector;
|
| 565 |
|
| 566 | md_account_bio(mddev, &bio);
|
| 567 |
|
| 568 | zone = find_zone(mddev->private, §or);
|
| 569 | switch (conf->layout) {
|
| 570 | case RAID0_ORIG_LAYOUT:
|
| 571 | tmp_dev = map_sector(mddev, zone, bio_sector, §or);
|
| 572 | break;
|
| 573 | case RAID0_ALT_MULTIZONE_LAYOUT:
|
| 574 | tmp_dev = map_sector(mddev, zone, sector, §or);
|
| 575 | break;
|
| 576 | default:
|
| 577 | WARN(1, "md/raid0:%s: Invalid layout\n", mdname(mddev));
|
| 578 | bio_io_error(bio);
|
| 579 | return;
|
| 580 | }
|
| 581 |
|
| 582 | if (unlikely(is_rdev_broken(tmp_dev))) {
|
| 583 | bio_io_error(bio);
|
| 584 | md_error(mddev, tmp_dev);
|
| 585 | return;
|
| 586 | }
|
| 587 |
|
| 588 | bio_set_dev(bio, tmp_dev->bdev);
|
| 589 | bio->bi_iter.bi_sector = sector + zone->dev_start +
|
| 590 | tmp_dev->data_offset;
|
| 591 | mddev_trace_remap(mddev, bio, bio_sector);
|
| 592 | mddev_check_write_zeroes(mddev, bio);
|
| 593 | submit_bio_noacct(bio);
|
| 594 | }
|
| 595 |
|
| 596 | static bool raid0_make_request(struct mddev *mddev, struct bio *bio)
|
| 597 | {
|
| 598 | sector_t sector;
|
| 599 | unsigned chunk_sects;
|
| 600 | unsigned sectors;
|
| 601 |
|
| 602 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)
|
| 603 | && md_flush_request(mddev, bio))
|
| 604 | return true;
|
| 605 |
|
| 606 | if (unlikely((bio_op(bio) == REQ_OP_DISCARD))) {
|
| 607 | raid0_handle_discard(mddev, bio);
|
| 608 | return true;
|
| 609 | }
|
| 610 |
|
| 611 | sector = bio->bi_iter.bi_sector;
|
| 612 | chunk_sects = mddev->chunk_sectors;
|
| 613 |
|
| 614 | sectors = chunk_sects -
|
| 615 | (likely(is_power_of_2(chunk_sects))
|
| 616 | ? (sector & (chunk_sects-1))
|
| 617 | : sector_div(sector, chunk_sects));
|
| 618 |
|
| 619 | if (sectors < bio_sectors(bio)) {
|
| 620 | bio = bio_submit_split_bioset(bio, sectors,
|
| 621 | &mddev->bio_set);
|
| 622 | if (!bio)
|
| 623 | return true;
|
| 624 | }
|
| 625 |
|
| 626 | raid0_map_submit_bio(mddev, bio);
|
| 627 | return true;
|
| 628 | }
|
| 629 |
|
| 630 | static void raid0_status(struct seq_file *seq, struct mddev *mddev)
|
| 631 | {
|
| 632 | seq_printf(seq, " %dk chunks", mddev->chunk_sectors / 2);
|
| 633 | return;
|
| 634 | }
|
| 635 |
|
| 636 | static void raid0_error(struct mddev *mddev, struct md_rdev *rdev)
|
| 637 | {
|
| 638 | if (!test_and_set_bit(MD_BROKEN, &mddev->flags)) {
|
| 639 | char *md_name = mdname(mddev);
|
| 640 |
|
| 641 | pr_crit("md/raid0%s: Disk failure on %pg detected, failing array.\n",
|
| 642 | md_name, rdev->bdev);
|
| 643 | }
|
| 644 | }
|
| 645 |
|
| 646 | static void *raid0_takeover_raid45(struct mddev *mddev)
|
| 647 | {
|
| 648 | struct md_rdev *rdev;
|
| 649 | struct r0conf *priv_conf;
|
| 650 |
|
| 651 | if (mddev->degraded != 1) {
|
| 652 | pr_warn("md/raid0:%s: raid5 must be degraded! Degraded disks: %d\n",
|
| 653 | mdname(mddev),
|
| 654 | mddev->degraded);
|
| 655 | return ERR_PTR(-EINVAL);
|
| 656 | }
|
| 657 |
|
| 658 | rdev_for_each(rdev, mddev) {
|
| 659 |
|
| 660 | if (rdev->raid_disk == mddev->raid_disks-1) {
|
| 661 | pr_warn("md/raid0:%s: raid5 must have missing parity disk!\n",
|
| 662 | mdname(mddev));
|
| 663 | return ERR_PTR(-EINVAL);
|
| 664 | }
|
| 665 | rdev->sectors = mddev->dev_sectors;
|
| 666 | }
|
| 667 |
|
| 668 |
|
| 669 | mddev->new_level = 0;
|
| 670 | mddev->new_layout = 0;
|
| 671 | mddev->new_chunk_sectors = mddev->chunk_sectors;
|
| 672 | mddev->raid_disks--;
|
| 673 | mddev->delta_disks = -1;
|
| 674 |
|
| 675 | mddev->resync_offset = MaxSector;
|
| 676 | mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
|
| 677 |
|
| 678 | create_strip_zones(mddev, &priv_conf);
|
| 679 |
|
| 680 | return priv_conf;
|
| 681 | }
|
| 682 |
|
| 683 | static void *raid0_takeover_raid10(struct mddev *mddev)
|
| 684 | {
|
| 685 | struct r0conf *priv_conf;
|
| 686 |
|
| 687 | |
| 688 | * - far_copies must be 1
|
| 689 | * - near_copies must be 2
|
| 690 | * - disks number must be even
|
| 691 | * - all mirrors must be already degraded
|
| 692 |
|
| 693 | if (mddev->layout != ((1 << 8) + 2)) {
|
| 694 | pr_warn("md/raid0:%s:: Raid0 cannot takeover layout: 0x%x\n",
|
| 695 | mdname(mddev),
|
| 696 | mddev->layout);
|
| 697 | return ERR_PTR(-EINVAL);
|
| 698 | }
|
| 699 | if (mddev->raid_disks & 1) {
|
| 700 | pr_warn("md/raid0:%s: Raid0 cannot takeover Raid10 with odd disk number.\n",
|
| 701 | mdname(mddev));
|
| 702 | return ERR_PTR(-EINVAL);
|
| 703 | }
|
| 704 | if (mddev->degraded != (mddev->raid_disks>>1)) {
|
| 705 | pr_warn("md/raid0:%s: All mirrors must be already degraded!\n",
|
| 706 | mdname(mddev));
|
| 707 | return ERR_PTR(-EINVAL);
|
| 708 | }
|
| 709 |
|
| 710 |
|
| 711 | mddev->new_level = 0;
|
| 712 | mddev->new_layout = 0;
|
| 713 | mddev->new_chunk_sectors = mddev->chunk_sectors;
|
| 714 | mddev->delta_disks = - mddev->raid_disks / 2;
|
| 715 | mddev->raid_disks += mddev->delta_disks;
|
| 716 | mddev->degraded = 0;
|
| 717 |
|
| 718 | mddev->resync_offset = MaxSector;
|
| 719 | mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
|
| 720 |
|
| 721 | create_strip_zones(mddev, &priv_conf);
|
| 722 | return priv_conf;
|
| 723 | }
|
| 724 |
|
| 725 | static void *raid0_takeover_raid1(struct mddev *mddev)
|
| 726 | {
|
| 727 | struct r0conf *priv_conf;
|
| 728 | int chunksect;
|
| 729 |
|
| 730 | |
| 731 | * - (N - 1) mirror drives must be already faulty
|
| 732 |
|
| 733 | if ((mddev->raid_disks - 1) != mddev->degraded) {
|
| 734 | pr_err("md/raid0:%s: (N - 1) mirrors drives must be already faulty!\n",
|
| 735 | mdname(mddev));
|
| 736 | return ERR_PTR(-EINVAL);
|
| 737 | }
|
| 738 |
|
| 739 | |
| 740 | * a raid1 doesn't have the notion of chunk size, so
|
| 741 | * figure out the largest suitable size we can use.
|
| 742 |
|
| 743 | chunksect = 64 * 2;
|
| 744 |
|
| 745 |
|
| 746 | while (chunksect && (mddev->array_sectors & (chunksect - 1)))
|
| 747 | chunksect >>= 1;
|
| 748 |
|
| 749 | if ((chunksect << 9) < PAGE_SIZE)
|
| 750 |
|
| 751 | return ERR_PTR(-EINVAL);
|
| 752 |
|
| 753 |
|
| 754 | mddev->new_level = 0;
|
| 755 | mddev->new_layout = 0;
|
| 756 | mddev->new_chunk_sectors = chunksect;
|
| 757 | mddev->chunk_sectors = chunksect;
|
| 758 | mddev->delta_disks = 1 - mddev->raid_disks;
|
| 759 | mddev->raid_disks = 1;
|
| 760 |
|
| 761 | mddev->resync_offset = MaxSector;
|
| 762 | mddev_clear_unsupported_flags(mddev, UNSUPPORTED_MDDEV_FLAGS);
|
| 763 |
|
| 764 | create_strip_zones(mddev, &priv_conf);
|
| 765 | return priv_conf;
|
| 766 | }
|
| 767 |
|
| 768 | static void *raid0_takeover(struct mddev *mddev)
|
| 769 | {
|
| 770 | |
| 771 | * raid4 - if all data disks are active.
|
| 772 | * raid5 - providing it is Raid4 layout and one disk is faulty
|
| 773 | * raid10 - assuming we have all necessary active disks
|
| 774 | * raid1 - with (N -1) mirror drives faulty
|
| 775 |
|
| 776 |
|
| 777 | if (mddev->bitmap) {
|
| 778 | pr_warn("md/raid0: %s: cannot takeover array with bitmap\n",
|
| 779 | mdname(mddev));
|
| 780 | return ERR_PTR(-EBUSY);
|
| 781 | }
|
| 782 | if (mddev->level == 4)
|
| 783 | return raid0_takeover_raid45(mddev);
|
| 784 |
|
| 785 | if (mddev->level == 5) {
|
| 786 | if (mddev->layout == ALGORITHM_PARITY_N)
|
| 787 | return raid0_takeover_raid45(mddev);
|
| 788 |
|
| 789 | pr_warn("md/raid0:%s: Raid can only takeover Raid5 with layout: %d\n",
|
| 790 | mdname(mddev), ALGORITHM_PARITY_N);
|
| 791 | }
|
| 792 |
|
| 793 | if (mddev->level == 10)
|
| 794 | return raid0_takeover_raid10(mddev);
|
| 795 |
|
| 796 | if (mddev->level == 1)
|
| 797 | return raid0_takeover_raid1(mddev);
|
| 798 |
|
| 799 | pr_warn("Takeover from raid%i to raid0 not supported\n",
|
| 800 | mddev->level);
|
| 801 |
|
| 802 | return ERR_PTR(-EINVAL);
|
| 803 | }
|
| 804 |
|
| 805 | static void raid0_quiesce(struct mddev *mddev, int quiesce)
|
| 806 | {
|
| 807 | }
|
| 808 |
|
| 809 | static struct md_personality raid0_personality=
|
| 810 | {
|
| 811 | .head = {
|
| 812 | .type = MD_PERSONALITY,
|
| 813 | .id = ID_RAID0,
|
| 814 | .name = "raid0",
|
| 815 | .owner = THIS_MODULE,
|
| 816 | },
|
| 817 |
|
| 818 | .make_request = raid0_make_request,
|
| 819 | .run = raid0_run,
|
| 820 | .free = raid0_free,
|
| 821 | .status = raid0_status,
|
| 822 | .size = raid0_size,
|
| 823 | .takeover = raid0_takeover,
|
| 824 | .quiesce = raid0_quiesce,
|
| 825 | .error_handler = raid0_error,
|
| 826 | };
|
| 827 |
|
| 828 | static int __init raid0_init(void)
|
| 829 | {
|
| 830 | return register_md_submodule(&raid0_personality.head);
|
| 831 | }
|
| 832 |
|
| 833 | static void __exit raid0_exit(void)
|
| 834 | {
|
| 835 | unregister_md_submodule(&raid0_personality.head);
|
| 836 | }
|
| 837 |
|
| 838 | module_init(raid0_init);
|
| 839 | module_exit(raid0_exit);
|
| 840 | MODULE_LICENSE("GPL");
|
| 841 | MODULE_DESCRIPTION("RAID0 (striping) personality for MD");
|
| 842 | MODULE_ALIAS("md-personality-2");
|
| 843 | MODULE_ALIAS("md-raid0");
|
| 844 | MODULE_ALIAS("md-level-0");
|