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