1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1991-1998 Linus Torvalds 4 * Re-organised Feb 1998 Russell King 5 * Copyright (C) 2020 Christoph Hellwig 6 */ 7 #include <linux/fs.h> 8 #include <linux/major.h> 9 #include <linux/slab.h> 10 #include <linux/string.h> 11 #include <linux/sysfs.h> 12 #include <linux/ctype.h> 13 #include <linux/vmalloc.h> 14 #include <linux/raid/detect.h> 15 #include "check.h" 16 17 static int (*const check_part[])(struct parsed_partitions *) = { 18 /* 19 * Probe partition formats with tables at disk address 0 20 * that also have an ADFS boot block at 0xdc0. 21 */ 22 #ifdef CONFIG_ACORN_PARTITION_ICS 23 adfspart_check_ICS, 24 #endif 25 #ifdef CONFIG_ACORN_PARTITION_POWERTEC 26 adfspart_check_POWERTEC, 27 #endif 28 #ifdef CONFIG_ACORN_PARTITION_EESOX 29 adfspart_check_EESOX, 30 #endif 31 32 /* 33 * Now move on to formats that only have partition info at 34 * disk address 0xdc0. Since these may also have stale 35 * PC/BIOS partition tables, they need to come before 36 * the msdos entry. 37 */ 38 #ifdef CONFIG_ACORN_PARTITION_CUMANA 39 adfspart_check_CUMANA, 40 #endif 41 #ifdef CONFIG_ACORN_PARTITION_ADFS 42 adfspart_check_ADFS, 43 #endif 44 45 #ifdef CONFIG_CMDLINE_PARTITION 46 cmdline_partition, 47 #endif 48 #ifdef CONFIG_OF_PARTITION 49 of_partition, /* cmdline have priority to OF */ 50 #endif 51 #ifdef CONFIG_EFI_PARTITION 52 efi_partition, /* this must come before msdos */ 53 #endif 54 #ifdef CONFIG_SGI_PARTITION 55 sgi_partition, 56 #endif 57 #ifdef CONFIG_LDM_PARTITION 58 ldm_partition, /* this must come before msdos */ 59 #endif 60 #ifdef CONFIG_MSDOS_PARTITION 61 msdos_partition, 62 #endif 63 #ifdef CONFIG_OSF_PARTITION 64 osf_partition, 65 #endif 66 #ifdef CONFIG_SUN_PARTITION 67 sun_partition, 68 #endif 69 #ifdef CONFIG_AMIGA_PARTITION 70 amiga_partition, 71 #endif 72 #ifdef CONFIG_ATARI_PARTITION 73 atari_partition, 74 #endif 75 #ifdef CONFIG_MAC_PARTITION 76 mac_partition, 77 #endif 78 #ifdef CONFIG_ULTRIX_PARTITION 79 ultrix_partition, 80 #endif 81 #ifdef CONFIG_IBM_PARTITION 82 ibm_partition, 83 #endif 84 #ifdef CONFIG_KARMA_PARTITION 85 karma_partition, 86 #endif 87 #ifdef CONFIG_SYSV68_PARTITION 88 sysv68_partition, 89 #endif 90 NULL 91 }; 92 93 static struct parsed_partitions *allocate_partitions(struct gendisk *hd) 94 { 95 struct parsed_partitions *state; 96 int nr = DISK_MAX_PARTS; 97 98 state = kzalloc_obj(*state); 99 if (!state) 100 return NULL; 101 102 state->parts = vzalloc(array_size(nr, sizeof(state->parts[0]))); 103 if (!state->parts) { 104 kfree(state); 105 return NULL; 106 } 107 108 state->limit = nr; 109 110 return state; 111 } 112 113 static void free_partitions(struct parsed_partitions *state) 114 { 115 vfree(state->parts); 116 kfree(state); 117 } 118 119 static struct parsed_partitions *check_partition(struct gendisk *hd) 120 { 121 struct parsed_partitions *state; 122 int i, res, err; 123 124 state = allocate_partitions(hd); 125 if (!state) 126 return NULL; 127 state->pp_buf.buffer = (char *)__get_free_page(GFP_KERNEL); 128 if (!state->pp_buf.buffer) { 129 free_partitions(state); 130 return NULL; 131 } 132 seq_buf_init(&state->pp_buf, state->pp_buf.buffer, PAGE_SIZE); 133 134 state->disk = hd; 135 strscpy(state->name, hd->disk_name); 136 seq_buf_printf(&state->pp_buf, " %s:", state->name); 137 if (isdigit(state->name[strlen(state->name)-1])) 138 sprintf(state->name, "p"); 139 140 i = res = err = 0; 141 while (!res && check_part[i]) { 142 memset(state->parts, 0, state->limit * sizeof(state->parts[0])); 143 res = check_part[i++](state); 144 if (res < 0) { 145 /* 146 * We have hit an I/O error which we don't report now. 147 * But record it, and let the others do their job. 148 */ 149 err = res; 150 res = 0; 151 } 152 153 } 154 if (res > 0) { 155 printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf)); 156 157 free_page((unsigned long)state->pp_buf.buffer); 158 return state; 159 } 160 if (state->access_beyond_eod) 161 err = -ENOSPC; 162 /* 163 * The partition is unrecognized. So report I/O errors if there were any 164 */ 165 if (err) 166 res = err; 167 if (res) { 168 seq_buf_puts(&state->pp_buf, 169 " unable to read partition table\n"); 170 printk(KERN_INFO "%s", seq_buf_str(&state->pp_buf)); 171 } 172 173 free_page((unsigned long)state->pp_buf.buffer); 174 free_partitions(state); 175 return ERR_PTR(res); 176 } 177 178 static ssize_t part_partition_show(struct device *dev, 179 struct device_attribute *attr, char *buf) 180 { 181 return sysfs_emit(buf, "%d\n", bdev_partno(dev_to_bdev(dev))); 182 } 183 184 static ssize_t part_start_show(struct device *dev, 185 struct device_attribute *attr, char *buf) 186 { 187 return sysfs_emit(buf, "%llu\n", dev_to_bdev(dev)->bd_start_sect); 188 } 189 190 static ssize_t part_ro_show(struct device *dev, 191 struct device_attribute *attr, char *buf) 192 { 193 return sysfs_emit(buf, "%d\n", bdev_read_only(dev_to_bdev(dev))); 194 } 195 196 static ssize_t part_alignment_offset_show(struct device *dev, 197 struct device_attribute *attr, char *buf) 198 { 199 return sysfs_emit(buf, "%u\n", bdev_alignment_offset(dev_to_bdev(dev))); 200 } 201 202 static ssize_t part_discard_alignment_show(struct device *dev, 203 struct device_attribute *attr, char *buf) 204 { 205 return sysfs_emit(buf, "%u\n", bdev_discard_alignment(dev_to_bdev(dev))); 206 } 207 208 static DEVICE_ATTR(partition, 0444, part_partition_show, NULL); 209 static DEVICE_ATTR(start, 0444, part_start_show, NULL); 210 static DEVICE_ATTR(size, 0444, part_size_show, NULL); 211 static DEVICE_ATTR(ro, 0444, part_ro_show, NULL); 212 static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL); 213 static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL); 214 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); 215 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); 216 #ifdef CONFIG_FAIL_MAKE_REQUEST 217 static struct device_attribute dev_attr_fail = 218 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store); 219 #endif 220 221 static struct attribute *part_attrs[] = { 222 &dev_attr_partition.attr, 223 &dev_attr_start.attr, 224 &dev_attr_size.attr, 225 &dev_attr_ro.attr, 226 &dev_attr_alignment_offset.attr, 227 &dev_attr_discard_alignment.attr, 228 &dev_attr_stat.attr, 229 &dev_attr_inflight.attr, 230 #ifdef CONFIG_FAIL_MAKE_REQUEST 231 &dev_attr_fail.attr, 232 #endif 233 NULL 234 }; 235 236 static const struct attribute_group part_attr_group = { 237 .attrs = part_attrs, 238 }; 239 240 static const struct attribute_group *part_attr_groups[] = { 241 &part_attr_group, 242 #ifdef CONFIG_BLK_DEV_IO_TRACE 243 &blk_trace_attr_group, 244 #endif 245 NULL 246 }; 247 248 static void part_release(struct device *dev) 249 { 250 put_disk(dev_to_bdev(dev)->bd_disk); 251 bdev_drop(dev_to_bdev(dev)); 252 } 253 254 static int part_uevent(const struct device *dev, struct kobj_uevent_env *env) 255 { 256 const struct block_device *part = dev_to_bdev(dev); 257 258 add_uevent_var(env, "PARTN=%u", bdev_partno(part)); 259 if (part->bd_meta_info && part->bd_meta_info->volname[0]) 260 add_uevent_var(env, "PARTNAME=%s", part->bd_meta_info->volname); 261 if (part->bd_meta_info && part->bd_meta_info->uuid[0]) 262 add_uevent_var(env, "PARTUUID=%s", part->bd_meta_info->uuid); 263 return 0; 264 } 265 266 const struct device_type part_type = { 267 .name = "partition", 268 .groups = part_attr_groups, 269 .release = part_release, 270 .uevent = part_uevent, 271 }; 272 273 void drop_partition(struct block_device *part) 274 { 275 lockdep_assert_held(&part->bd_disk->open_mutex); 276 277 xa_erase(&part->bd_disk->part_tbl, bdev_partno(part)); 278 kobject_put(part->bd_holder_dir); 279 280 device_del(&part->bd_device); 281 put_device(&part->bd_device); 282 } 283 284 static ssize_t whole_disk_show(struct device *dev, 285 struct device_attribute *attr, char *buf) 286 { 287 return 0; 288 } 289 static const DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL); 290 291 /* 292 * Must be called either with open_mutex held, before a disk can be opened or 293 * after all disk users are gone. 294 */ 295 static struct block_device *add_partition(struct gendisk *disk, int partno, 296 sector_t start, sector_t len, int flags, 297 struct partition_meta_info *info) 298 { 299 dev_t devt = MKDEV(0, 0); 300 struct device *ddev = disk_to_dev(disk); 301 struct device *pdev; 302 struct block_device *bdev; 303 const char *dname; 304 int err; 305 306 lockdep_assert_held(&disk->open_mutex); 307 308 if (partno >= DISK_MAX_PARTS) 309 return ERR_PTR(-EINVAL); 310 311 /* 312 * Partitions are not supported on zoned block devices that are used as 313 * such. 314 */ 315 if (bdev_is_zoned(disk->part0)) { 316 pr_warn("%s: partitions not supported on host managed zoned block device\n", 317 disk->disk_name); 318 return ERR_PTR(-ENXIO); 319 } 320 321 if (xa_load(&disk->part_tbl, partno)) 322 return ERR_PTR(-EBUSY); 323 324 /* ensure we always have a reference to the whole disk */ 325 get_device(disk_to_dev(disk)); 326 327 err = -ENOMEM; 328 bdev = bdev_alloc(disk, partno); 329 if (!bdev) 330 goto out_put_disk; 331 332 bdev->bd_start_sect = start; 333 bdev_set_nr_sectors(bdev, len); 334 335 pdev = &bdev->bd_device; 336 dname = dev_name(ddev); 337 if (isdigit(dname[strlen(dname) - 1])) 338 dev_set_name(pdev, "%sp%d", dname, partno); 339 else 340 dev_set_name(pdev, "%s%d", dname, partno); 341 342 device_initialize(pdev); 343 pdev->class = &block_class; 344 pdev->type = &part_type; 345 pdev->parent = ddev; 346 347 /* in consecutive minor range? */ 348 if (bdev_partno(bdev) < disk->minors) { 349 devt = MKDEV(disk->major, disk->first_minor + bdev_partno(bdev)); 350 } else { 351 err = blk_alloc_ext_minor(); 352 if (err < 0) 353 goto out_put; 354 devt = MKDEV(BLOCK_EXT_MAJOR, err); 355 } 356 pdev->devt = devt; 357 358 if (info) { 359 err = -ENOMEM; 360 bdev->bd_meta_info = kmemdup(info, sizeof(*info), GFP_KERNEL); 361 if (!bdev->bd_meta_info) 362 goto out_put; 363 } 364 365 /* delay uevent until 'holders' subdir is created */ 366 dev_set_uevent_suppress(pdev, 1); 367 err = device_add(pdev); 368 if (err) 369 goto out_put; 370 371 err = -ENOMEM; 372 bdev->bd_holder_dir = kobject_create_and_add("holders", &pdev->kobj); 373 if (!bdev->bd_holder_dir) 374 goto out_del; 375 376 dev_set_uevent_suppress(pdev, 0); 377 if (flags & ADDPART_FLAG_WHOLEDISK) { 378 err = device_create_file(pdev, &dev_attr_whole_disk); 379 if (err) 380 goto out_del; 381 } 382 383 if (flags & ADDPART_FLAG_READONLY) 384 bdev_set_flag(bdev, BD_READ_ONLY); 385 386 /* everything is up and running, commence */ 387 err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL); 388 if (err) 389 goto out_del; 390 bdev_add(bdev, devt); 391 392 /* suppress uevent if the disk suppresses it */ 393 if (!dev_get_uevent_suppress(ddev)) 394 kobject_uevent(&pdev->kobj, KOBJ_ADD); 395 return bdev; 396 397 out_del: 398 kobject_put(bdev->bd_holder_dir); 399 device_del(pdev); 400 out_put: 401 put_device(pdev); 402 return ERR_PTR(err); 403 out_put_disk: 404 put_disk(disk); 405 return ERR_PTR(err); 406 } 407 408 static bool partition_overlaps(struct gendisk *disk, sector_t start, 409 sector_t length, int skip_partno) 410 { 411 struct block_device *part; 412 bool overlap = false; 413 unsigned long idx; 414 415 rcu_read_lock(); 416 xa_for_each_start(&disk->part_tbl, idx, part, 1) { 417 if (bdev_partno(part) != skip_partno && 418 start < part->bd_start_sect + bdev_nr_sectors(part) && 419 start + length > part->bd_start_sect) { 420 overlap = true; 421 break; 422 } 423 } 424 rcu_read_unlock(); 425 426 return overlap; 427 } 428 429 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start, 430 sector_t length) 431 { 432 struct block_device *part; 433 int ret; 434 435 mutex_lock(&disk->open_mutex); 436 if (!disk_live(disk)) { 437 ret = -ENXIO; 438 goto out; 439 } 440 441 if (disk->flags & GENHD_FL_NO_PART) { 442 ret = -EINVAL; 443 goto out; 444 } 445 446 if (partition_overlaps(disk, start, length, -1)) { 447 ret = -EBUSY; 448 goto out; 449 } 450 451 part = add_partition(disk, partno, start, length, 452 ADDPART_FLAG_NONE, NULL); 453 ret = PTR_ERR_OR_ZERO(part); 454 out: 455 mutex_unlock(&disk->open_mutex); 456 return ret; 457 } 458 459 int bdev_del_partition(struct gendisk *disk, int partno) 460 { 461 struct block_device *part = NULL; 462 int ret = -ENXIO; 463 464 mutex_lock(&disk->open_mutex); 465 part = xa_load(&disk->part_tbl, partno); 466 if (!part) 467 goto out_unlock; 468 469 ret = -EBUSY; 470 if (atomic_read(&part->bd_openers)) 471 goto out_unlock; 472 473 /* 474 * We verified that @part->bd_openers is zero above and so 475 * @part->bd_holder{_ops} can't be set. And since we hold 476 * @disk->open_mutex the device can't be claimed by anyone. 477 * 478 * So no need to call @part->bd_holder_ops->mark_dead() here. 479 * Just delete the partition and invalidate it. 480 */ 481 482 bdev_unhash(part); 483 invalidate_bdev(part); 484 drop_partition(part); 485 ret = 0; 486 out_unlock: 487 mutex_unlock(&disk->open_mutex); 488 return ret; 489 } 490 491 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start, 492 sector_t length) 493 { 494 struct block_device *part = NULL; 495 int ret = -ENXIO; 496 497 mutex_lock(&disk->open_mutex); 498 part = xa_load(&disk->part_tbl, partno); 499 if (!part) 500 goto out_unlock; 501 502 ret = -EINVAL; 503 if (start != part->bd_start_sect) 504 goto out_unlock; 505 506 ret = -EBUSY; 507 if (partition_overlaps(disk, start, length, partno)) 508 goto out_unlock; 509 510 bdev_set_nr_sectors(part, length); 511 512 ret = 0; 513 out_unlock: 514 mutex_unlock(&disk->open_mutex); 515 return ret; 516 } 517 518 static bool disk_unlock_native_capacity(struct gendisk *disk) 519 { 520 if (!disk->fops->unlock_native_capacity || 521 test_and_set_bit(GD_NATIVE_CAPACITY, &disk->state)) { 522 printk(KERN_CONT "truncated\n"); 523 return false; 524 } 525 526 printk(KERN_CONT "enabling native capacity\n"); 527 disk->fops->unlock_native_capacity(disk); 528 return true; 529 } 530 531 static bool blk_add_partition(struct gendisk *disk, 532 struct parsed_partitions *state, int p) 533 { 534 sector_t size = state->parts[p].size; 535 sector_t from = state->parts[p].from; 536 struct block_device *part; 537 538 if (!size) 539 return true; 540 541 if (from >= get_capacity(disk)) { 542 printk(KERN_WARNING 543 "%s: p%d start %llu is beyond EOD, ", 544 disk->disk_name, p, (unsigned long long) from); 545 if (disk_unlock_native_capacity(disk)) 546 return false; 547 return true; 548 } 549 550 if (from + size > get_capacity(disk)) { 551 printk(KERN_WARNING 552 "%s: p%d size %llu extends beyond EOD, ", 553 disk->disk_name, p, (unsigned long long) size); 554 555 if (disk_unlock_native_capacity(disk)) 556 return false; 557 558 /* 559 * We can not ignore partitions of broken tables created by for 560 * example camera firmware, but we limit them to the end of the 561 * disk to avoid creating invalid block devices. 562 */ 563 size = get_capacity(disk) - from; 564 } 565 566 part = add_partition(disk, p, from, size, state->parts[p].flags, 567 &state->parts[p].info); 568 if (IS_ERR(part)) { 569 if (PTR_ERR(part) != -ENXIO) { 570 printk(KERN_ERR " %s: p%d could not be added: %pe\n", 571 disk->disk_name, p, part); 572 } 573 return true; 574 } 575 576 if (IS_BUILTIN(CONFIG_BLK_DEV_MD) && 577 (state->parts[p].flags & ADDPART_FLAG_RAID)) 578 md_autodetect_dev(part->bd_dev); 579 580 return true; 581 } 582 583 static int blk_add_partitions(struct gendisk *disk) 584 { 585 struct parsed_partitions *state; 586 int ret = -EAGAIN, p; 587 588 if (!disk_has_partscan(disk)) 589 return 0; 590 591 state = check_partition(disk); 592 if (!state) 593 return 0; 594 if (IS_ERR(state)) { 595 /* 596 * I/O error reading the partition table. If we tried to read 597 * beyond EOD, retry after unlocking the native capacity. 598 */ 599 if (PTR_ERR(state) == -ENOSPC) { 600 printk(KERN_WARNING "%s: partition table beyond EOD, ", 601 disk->disk_name); 602 if (disk_unlock_native_capacity(disk)) 603 return -EAGAIN; 604 } 605 return -EIO; 606 } 607 608 /* 609 * Partitions are not supported on host managed zoned block devices. 610 */ 611 if (bdev_is_zoned(disk->part0)) { 612 pr_warn("%s: ignoring partition table on host managed zoned block device\n", 613 disk->disk_name); 614 ret = 0; 615 goto out_free_state; 616 } 617 618 /* 619 * If we read beyond EOD, try unlocking native capacity even if the 620 * partition table was successfully read as we could be missing some 621 * partitions. 622 */ 623 if (state->access_beyond_eod) { 624 printk(KERN_WARNING 625 "%s: partition table partially beyond EOD, ", 626 disk->disk_name); 627 if (disk_unlock_native_capacity(disk)) 628 goto out_free_state; 629 } 630 631 /* tell userspace that the media / partition table may have changed */ 632 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE); 633 634 for (p = 1; p < state->limit; p++) 635 if (!blk_add_partition(disk, state, p)) 636 goto out_free_state; 637 638 ret = 0; 639 out_free_state: 640 free_partitions(state); 641 return ret; 642 } 643 644 int bdev_disk_changed(struct gendisk *disk, bool invalidate) 645 { 646 struct block_device *part; 647 unsigned long idx; 648 int ret = 0; 649 650 lockdep_assert_held(&disk->open_mutex); 651 652 if (!disk_live(disk)) 653 return -ENXIO; 654 655 rescan: 656 if (disk->open_partitions) 657 return -EBUSY; 658 sync_blockdev(disk->part0); 659 invalidate_bdev(disk->part0); 660 661 xa_for_each_start(&disk->part_tbl, idx, part, 1) { 662 /* 663 * Remove the block device from the inode hash, so that 664 * it cannot be looked up any more even when openers 665 * still hold references. 666 */ 667 bdev_unhash(part); 668 669 /* 670 * If @disk->open_partitions isn't elevated but there's 671 * still an active holder of that block device things 672 * are broken. 673 */ 674 WARN_ON_ONCE(atomic_read(&part->bd_openers)); 675 invalidate_bdev(part); 676 drop_partition(part); 677 } 678 clear_bit(GD_NEED_PART_SCAN, &disk->state); 679 680 /* 681 * Historically we only set the capacity to zero for devices that 682 * support partitions (independ of actually having partitions created). 683 * Doing that is rather inconsistent, but changing it broke legacy 684 * udisks polling for legacy ide-cdrom devices. Use the crude check 685 * below to get the sane behavior for most device while not breaking 686 * userspace for this particular setup. 687 */ 688 if (invalidate) { 689 if (!(disk->flags & GENHD_FL_NO_PART) || 690 !(disk->flags & GENHD_FL_REMOVABLE)) 691 set_capacity(disk, 0); 692 } 693 694 if (get_capacity(disk)) { 695 ret = blk_add_partitions(disk); 696 if (ret == -EAGAIN) 697 goto rescan; 698 } else if (invalidate) { 699 /* 700 * Tell userspace that the media / partition table may have 701 * changed. 702 */ 703 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE); 704 } 705 706 return ret; 707 } 708 /* 709 * Only exported for loop and dasd for historic reasons. Don't use in new 710 * code! 711 */ 712 EXPORT_SYMBOL_GPL(bdev_disk_changed); 713 714 void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p) 715 { 716 struct address_space *mapping = state->disk->part0->bd_mapping; 717 struct folio *folio; 718 719 if (n >= get_capacity(state->disk)) { 720 state->access_beyond_eod = true; 721 goto out; 722 } 723 724 folio = read_mapping_folio(mapping, n >> PAGE_SECTORS_SHIFT, NULL); 725 if (IS_ERR(folio)) 726 goto out; 727 728 p->v = folio; 729 return folio_address(folio) + offset_in_folio(folio, n * SECTOR_SIZE); 730 out: 731 p->v = NULL; 732 return NULL; 733 } 734