0001
0002
0003
0004
0005
0006
0007 #include <linux/fs.h>
0008 #include <linux/major.h>
0009 #include <linux/slab.h>
0010 #include <linux/ctype.h>
0011 #include <linux/vmalloc.h>
0012 #include <linux/raid/detect.h>
0013 #include "check.h"
0014
0015 static int (*check_part[])(struct parsed_partitions *) = {
0016
0017
0018
0019
0020 #ifdef CONFIG_ACORN_PARTITION_ICS
0021 adfspart_check_ICS,
0022 #endif
0023 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
0024 adfspart_check_POWERTEC,
0025 #endif
0026 #ifdef CONFIG_ACORN_PARTITION_EESOX
0027 adfspart_check_EESOX,
0028 #endif
0029
0030
0031
0032
0033
0034
0035
0036 #ifdef CONFIG_ACORN_PARTITION_CUMANA
0037 adfspart_check_CUMANA,
0038 #endif
0039 #ifdef CONFIG_ACORN_PARTITION_ADFS
0040 adfspart_check_ADFS,
0041 #endif
0042
0043 #ifdef CONFIG_CMDLINE_PARTITION
0044 cmdline_partition,
0045 #endif
0046 #ifdef CONFIG_EFI_PARTITION
0047 efi_partition,
0048 #endif
0049 #ifdef CONFIG_SGI_PARTITION
0050 sgi_partition,
0051 #endif
0052 #ifdef CONFIG_LDM_PARTITION
0053 ldm_partition,
0054 #endif
0055 #ifdef CONFIG_MSDOS_PARTITION
0056 msdos_partition,
0057 #endif
0058 #ifdef CONFIG_OSF_PARTITION
0059 osf_partition,
0060 #endif
0061 #ifdef CONFIG_SUN_PARTITION
0062 sun_partition,
0063 #endif
0064 #ifdef CONFIG_AMIGA_PARTITION
0065 amiga_partition,
0066 #endif
0067 #ifdef CONFIG_ATARI_PARTITION
0068 atari_partition,
0069 #endif
0070 #ifdef CONFIG_MAC_PARTITION
0071 mac_partition,
0072 #endif
0073 #ifdef CONFIG_ULTRIX_PARTITION
0074 ultrix_partition,
0075 #endif
0076 #ifdef CONFIG_IBM_PARTITION
0077 ibm_partition,
0078 #endif
0079 #ifdef CONFIG_KARMA_PARTITION
0080 karma_partition,
0081 #endif
0082 #ifdef CONFIG_SYSV68_PARTITION
0083 sysv68_partition,
0084 #endif
0085 NULL
0086 };
0087
0088 static void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
0089 {
0090 spin_lock(&bdev->bd_size_lock);
0091 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
0092 bdev->bd_nr_sectors = sectors;
0093 spin_unlock(&bdev->bd_size_lock);
0094 }
0095
0096 static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
0097 {
0098 struct parsed_partitions *state;
0099 int nr = DISK_MAX_PARTS;
0100
0101 state = kzalloc(sizeof(*state), GFP_KERNEL);
0102 if (!state)
0103 return NULL;
0104
0105 state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
0106 if (!state->parts) {
0107 kfree(state);
0108 return NULL;
0109 }
0110
0111 state->limit = nr;
0112
0113 return state;
0114 }
0115
0116 static void free_partitions(struct parsed_partitions *state)
0117 {
0118 vfree(state->parts);
0119 kfree(state);
0120 }
0121
0122 static struct parsed_partitions *check_partition(struct gendisk *hd)
0123 {
0124 struct parsed_partitions *state;
0125 int i, res, err;
0126
0127 state = allocate_partitions(hd);
0128 if (!state)
0129 return NULL;
0130 state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
0131 if (!state->pp_buf) {
0132 free_partitions(state);
0133 return NULL;
0134 }
0135 state->pp_buf[0] = '\0';
0136
0137 state->disk = hd;
0138 snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
0139 snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
0140 if (isdigit(state->name[strlen(state->name)-1]))
0141 sprintf(state->name, "p");
0142
0143 i = res = err = 0;
0144 while (!res && check_part[i]) {
0145 memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
0146 res = check_part[i++](state);
0147 if (res < 0) {
0148
0149
0150
0151
0152 err = res;
0153 res = 0;
0154 }
0155
0156 }
0157 if (res > 0) {
0158 printk(KERN_INFO "%s", state->pp_buf);
0159
0160 free_page((unsigned long)state->pp_buf);
0161 return state;
0162 }
0163 if (state->access_beyond_eod)
0164 err = -ENOSPC;
0165
0166
0167
0168 if (err)
0169 res = err;
0170 if (res) {
0171 strlcat(state->pp_buf,
0172 " unable to read partition table\n", PAGE_SIZE);
0173 printk(KERN_INFO "%s", state->pp_buf);
0174 }
0175
0176 free_page((unsigned long)state->pp_buf);
0177 free_partitions(state);
0178 return ERR_PTR(res);
0179 }
0180
0181 static ssize_t part_partition_show(struct device *dev,
0182 struct device_attribute *attr, char *buf)
0183 {
0184 return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_partno);
0185 }
0186
0187 static ssize_t part_start_show(struct device *dev,
0188 struct device_attribute *attr, char *buf)
0189 {
0190 return sprintf(buf, "%llu\n", dev_to_bdev(dev)->bd_start_sect);
0191 }
0192
0193 static ssize_t part_ro_show(struct device *dev,
0194 struct device_attribute *attr, char *buf)
0195 {
0196 return sprintf(buf, "%d\n", bdev_read_only(dev_to_bdev(dev)));
0197 }
0198
0199 static ssize_t part_alignment_offset_show(struct device *dev,
0200 struct device_attribute *attr, char *buf)
0201 {
0202 return sprintf(buf, "%u\n", bdev_alignment_offset(dev_to_bdev(dev)));
0203 }
0204
0205 static ssize_t part_discard_alignment_show(struct device *dev,
0206 struct device_attribute *attr, char *buf)
0207 {
0208 return sprintf(buf, "%u\n", bdev_discard_alignment(dev_to_bdev(dev)));
0209 }
0210
0211 static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
0212 static DEVICE_ATTR(start, 0444, part_start_show, NULL);
0213 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
0214 static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
0215 static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
0216 static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
0217 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
0218 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
0219 #ifdef CONFIG_FAIL_MAKE_REQUEST
0220 static struct device_attribute dev_attr_fail =
0221 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
0222 #endif
0223
0224 static struct attribute *part_attrs[] = {
0225 &dev_attr_partition.attr,
0226 &dev_attr_start.attr,
0227 &dev_attr_size.attr,
0228 &dev_attr_ro.attr,
0229 &dev_attr_alignment_offset.attr,
0230 &dev_attr_discard_alignment.attr,
0231 &dev_attr_stat.attr,
0232 &dev_attr_inflight.attr,
0233 #ifdef CONFIG_FAIL_MAKE_REQUEST
0234 &dev_attr_fail.attr,
0235 #endif
0236 NULL
0237 };
0238
0239 static struct attribute_group part_attr_group = {
0240 .attrs = part_attrs,
0241 };
0242
0243 static const struct attribute_group *part_attr_groups[] = {
0244 &part_attr_group,
0245 #ifdef CONFIG_BLK_DEV_IO_TRACE
0246 &blk_trace_attr_group,
0247 #endif
0248 NULL
0249 };
0250
0251 static void part_release(struct device *dev)
0252 {
0253 put_disk(dev_to_bdev(dev)->bd_disk);
0254 iput(dev_to_bdev(dev)->bd_inode);
0255 }
0256
0257 static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
0258 {
0259 struct block_device *part = dev_to_bdev(dev);
0260
0261 add_uevent_var(env, "PARTN=%u", part->bd_partno);
0262 if (part->bd_meta_info && part->bd_meta_info->volname[0])
0263 add_uevent_var(env, "PARTNAME=%s", part->bd_meta_info->volname);
0264 return 0;
0265 }
0266
0267 struct device_type part_type = {
0268 .name = "partition",
0269 .groups = part_attr_groups,
0270 .release = part_release,
0271 .uevent = part_uevent,
0272 };
0273
0274 static void delete_partition(struct block_device *part)
0275 {
0276 lockdep_assert_held(&part->bd_disk->open_mutex);
0277
0278 fsync_bdev(part);
0279 __invalidate_device(part, true);
0280
0281 xa_erase(&part->bd_disk->part_tbl, part->bd_partno);
0282 kobject_put(part->bd_holder_dir);
0283 device_del(&part->bd_device);
0284
0285
0286
0287
0288
0289 remove_inode_hash(part->bd_inode);
0290
0291 put_device(&part->bd_device);
0292 }
0293
0294 static ssize_t whole_disk_show(struct device *dev,
0295 struct device_attribute *attr, char *buf)
0296 {
0297 return 0;
0298 }
0299 static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
0300
0301
0302
0303
0304
0305 static struct block_device *add_partition(struct gendisk *disk, int partno,
0306 sector_t start, sector_t len, int flags,
0307 struct partition_meta_info *info)
0308 {
0309 dev_t devt = MKDEV(0, 0);
0310 struct device *ddev = disk_to_dev(disk);
0311 struct device *pdev;
0312 struct block_device *bdev;
0313 const char *dname;
0314 int err;
0315
0316 lockdep_assert_held(&disk->open_mutex);
0317
0318 if (partno >= DISK_MAX_PARTS)
0319 return ERR_PTR(-EINVAL);
0320
0321
0322
0323
0324
0325 switch (disk->queue->limits.zoned) {
0326 case BLK_ZONED_HM:
0327 pr_warn("%s: partitions not supported on host managed zoned block device\n",
0328 disk->disk_name);
0329 return ERR_PTR(-ENXIO);
0330 case BLK_ZONED_HA:
0331 pr_info("%s: disabling host aware zoned block device support due to partitions\n",
0332 disk->disk_name);
0333 disk_set_zoned(disk, BLK_ZONED_NONE);
0334 break;
0335 case BLK_ZONED_NONE:
0336 break;
0337 }
0338
0339 if (xa_load(&disk->part_tbl, partno))
0340 return ERR_PTR(-EBUSY);
0341
0342
0343 get_device(disk_to_dev(disk));
0344
0345 err = -ENOMEM;
0346 bdev = bdev_alloc(disk, partno);
0347 if (!bdev)
0348 goto out_put_disk;
0349
0350 bdev->bd_start_sect = start;
0351 bdev_set_nr_sectors(bdev, len);
0352
0353 pdev = &bdev->bd_device;
0354 dname = dev_name(ddev);
0355 if (isdigit(dname[strlen(dname) - 1]))
0356 dev_set_name(pdev, "%sp%d", dname, partno);
0357 else
0358 dev_set_name(pdev, "%s%d", dname, partno);
0359
0360 device_initialize(pdev);
0361 pdev->class = &block_class;
0362 pdev->type = &part_type;
0363 pdev->parent = ddev;
0364
0365
0366 if (bdev->bd_partno < disk->minors) {
0367 devt = MKDEV(disk->major, disk->first_minor + bdev->bd_partno);
0368 } else {
0369 err = blk_alloc_ext_minor();
0370 if (err < 0)
0371 goto out_put;
0372 devt = MKDEV(BLOCK_EXT_MAJOR, err);
0373 }
0374 pdev->devt = devt;
0375
0376 if (info) {
0377 err = -ENOMEM;
0378 bdev->bd_meta_info = kmemdup(info, sizeof(*info), GFP_KERNEL);
0379 if (!bdev->bd_meta_info)
0380 goto out_put;
0381 }
0382
0383
0384 dev_set_uevent_suppress(pdev, 1);
0385 err = device_add(pdev);
0386 if (err)
0387 goto out_put;
0388
0389 err = -ENOMEM;
0390 bdev->bd_holder_dir = kobject_create_and_add("holders", &pdev->kobj);
0391 if (!bdev->bd_holder_dir)
0392 goto out_del;
0393
0394 dev_set_uevent_suppress(pdev, 0);
0395 if (flags & ADDPART_FLAG_WHOLEDISK) {
0396 err = device_create_file(pdev, &dev_attr_whole_disk);
0397 if (err)
0398 goto out_del;
0399 }
0400
0401
0402 err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL);
0403 if (err)
0404 goto out_del;
0405 bdev_add(bdev, devt);
0406
0407
0408 if (!dev_get_uevent_suppress(ddev))
0409 kobject_uevent(&pdev->kobj, KOBJ_ADD);
0410 return bdev;
0411
0412 out_del:
0413 kobject_put(bdev->bd_holder_dir);
0414 device_del(pdev);
0415 out_put:
0416 put_device(pdev);
0417 return ERR_PTR(err);
0418 out_put_disk:
0419 put_disk(disk);
0420 return ERR_PTR(err);
0421 }
0422
0423 static bool partition_overlaps(struct gendisk *disk, sector_t start,
0424 sector_t length, int skip_partno)
0425 {
0426 struct block_device *part;
0427 bool overlap = false;
0428 unsigned long idx;
0429
0430 rcu_read_lock();
0431 xa_for_each_start(&disk->part_tbl, idx, part, 1) {
0432 if (part->bd_partno != skip_partno &&
0433 start < part->bd_start_sect + bdev_nr_sectors(part) &&
0434 start + length > part->bd_start_sect) {
0435 overlap = true;
0436 break;
0437 }
0438 }
0439 rcu_read_unlock();
0440
0441 return overlap;
0442 }
0443
0444 int bdev_add_partition(struct gendisk *disk, int partno, sector_t start,
0445 sector_t length)
0446 {
0447 struct block_device *part;
0448 int ret;
0449
0450 mutex_lock(&disk->open_mutex);
0451 if (!disk_live(disk)) {
0452 ret = -ENXIO;
0453 goto out;
0454 }
0455
0456 if (partition_overlaps(disk, start, length, -1)) {
0457 ret = -EBUSY;
0458 goto out;
0459 }
0460
0461 part = add_partition(disk, partno, start, length,
0462 ADDPART_FLAG_NONE, NULL);
0463 ret = PTR_ERR_OR_ZERO(part);
0464 out:
0465 mutex_unlock(&disk->open_mutex);
0466 return ret;
0467 }
0468
0469 int bdev_del_partition(struct gendisk *disk, int partno)
0470 {
0471 struct block_device *part = NULL;
0472 int ret = -ENXIO;
0473
0474 mutex_lock(&disk->open_mutex);
0475 part = xa_load(&disk->part_tbl, partno);
0476 if (!part)
0477 goto out_unlock;
0478
0479 ret = -EBUSY;
0480 if (atomic_read(&part->bd_openers))
0481 goto out_unlock;
0482
0483 delete_partition(part);
0484 ret = 0;
0485 out_unlock:
0486 mutex_unlock(&disk->open_mutex);
0487 return ret;
0488 }
0489
0490 int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
0491 sector_t length)
0492 {
0493 struct block_device *part = NULL;
0494 int ret = -ENXIO;
0495
0496 mutex_lock(&disk->open_mutex);
0497 part = xa_load(&disk->part_tbl, partno);
0498 if (!part)
0499 goto out_unlock;
0500
0501 ret = -EINVAL;
0502 if (start != part->bd_start_sect)
0503 goto out_unlock;
0504
0505 ret = -EBUSY;
0506 if (partition_overlaps(disk, start, length, partno))
0507 goto out_unlock;
0508
0509 bdev_set_nr_sectors(part, length);
0510
0511 ret = 0;
0512 out_unlock:
0513 mutex_unlock(&disk->open_mutex);
0514 return ret;
0515 }
0516
0517 static bool disk_unlock_native_capacity(struct gendisk *disk)
0518 {
0519 if (!disk->fops->unlock_native_capacity ||
0520 test_and_set_bit(GD_NATIVE_CAPACITY, &disk->state)) {
0521 printk(KERN_CONT "truncated\n");
0522 return false;
0523 }
0524
0525 printk(KERN_CONT "enabling native capacity\n");
0526 disk->fops->unlock_native_capacity(disk);
0527 return true;
0528 }
0529
0530 void blk_drop_partitions(struct gendisk *disk)
0531 {
0532 struct block_device *part;
0533 unsigned long idx;
0534
0535 lockdep_assert_held(&disk->open_mutex);
0536
0537 xa_for_each_start(&disk->part_tbl, idx, part, 1)
0538 delete_partition(part);
0539 }
0540
0541 static bool blk_add_partition(struct gendisk *disk,
0542 struct parsed_partitions *state, int p)
0543 {
0544 sector_t size = state->parts[p].size;
0545 sector_t from = state->parts[p].from;
0546 struct block_device *part;
0547
0548 if (!size)
0549 return true;
0550
0551 if (from >= get_capacity(disk)) {
0552 printk(KERN_WARNING
0553 "%s: p%d start %llu is beyond EOD, ",
0554 disk->disk_name, p, (unsigned long long) from);
0555 if (disk_unlock_native_capacity(disk))
0556 return false;
0557 return true;
0558 }
0559
0560 if (from + size > get_capacity(disk)) {
0561 printk(KERN_WARNING
0562 "%s: p%d size %llu extends beyond EOD, ",
0563 disk->disk_name, p, (unsigned long long) size);
0564
0565 if (disk_unlock_native_capacity(disk))
0566 return false;
0567
0568
0569
0570
0571
0572
0573 size = get_capacity(disk) - from;
0574 }
0575
0576 part = add_partition(disk, p, from, size, state->parts[p].flags,
0577 &state->parts[p].info);
0578 if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
0579 printk(KERN_ERR " %s: p%d could not be added: %ld\n",
0580 disk->disk_name, p, -PTR_ERR(part));
0581 return true;
0582 }
0583
0584 if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
0585 (state->parts[p].flags & ADDPART_FLAG_RAID))
0586 md_autodetect_dev(part->bd_dev);
0587
0588 return true;
0589 }
0590
0591 static int blk_add_partitions(struct gendisk *disk)
0592 {
0593 struct parsed_partitions *state;
0594 int ret = -EAGAIN, p;
0595
0596 if (disk->flags & GENHD_FL_NO_PART)
0597 return 0;
0598
0599 if (test_bit(GD_SUPPRESS_PART_SCAN, &disk->state))
0600 return 0;
0601
0602 state = check_partition(disk);
0603 if (!state)
0604 return 0;
0605 if (IS_ERR(state)) {
0606
0607
0608
0609
0610 if (PTR_ERR(state) == -ENOSPC) {
0611 printk(KERN_WARNING "%s: partition table beyond EOD, ",
0612 disk->disk_name);
0613 if (disk_unlock_native_capacity(disk))
0614 return -EAGAIN;
0615 }
0616 return -EIO;
0617 }
0618
0619
0620
0621
0622 if (disk->queue->limits.zoned == BLK_ZONED_HM) {
0623 pr_warn("%s: ignoring partition table on host managed zoned block device\n",
0624 disk->disk_name);
0625 ret = 0;
0626 goto out_free_state;
0627 }
0628
0629
0630
0631
0632
0633
0634 if (state->access_beyond_eod) {
0635 printk(KERN_WARNING
0636 "%s: partition table partially beyond EOD, ",
0637 disk->disk_name);
0638 if (disk_unlock_native_capacity(disk))
0639 goto out_free_state;
0640 }
0641
0642
0643 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
0644
0645 for (p = 1; p < state->limit; p++)
0646 if (!blk_add_partition(disk, state, p))
0647 goto out_free_state;
0648
0649 ret = 0;
0650 out_free_state:
0651 free_partitions(state);
0652 return ret;
0653 }
0654
0655 int bdev_disk_changed(struct gendisk *disk, bool invalidate)
0656 {
0657 int ret = 0;
0658
0659 lockdep_assert_held(&disk->open_mutex);
0660
0661 if (!disk_live(disk))
0662 return -ENXIO;
0663
0664 rescan:
0665 if (disk->open_partitions)
0666 return -EBUSY;
0667 sync_blockdev(disk->part0);
0668 invalidate_bdev(disk->part0);
0669 blk_drop_partitions(disk);
0670
0671 clear_bit(GD_NEED_PART_SCAN, &disk->state);
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681 if (invalidate) {
0682 if (!(disk->flags & GENHD_FL_NO_PART) ||
0683 !(disk->flags & GENHD_FL_REMOVABLE))
0684 set_capacity(disk, 0);
0685 }
0686
0687 if (get_capacity(disk)) {
0688 ret = blk_add_partitions(disk);
0689 if (ret == -EAGAIN)
0690 goto rescan;
0691 } else if (invalidate) {
0692
0693
0694
0695
0696 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
0697 }
0698
0699 return ret;
0700 }
0701
0702
0703
0704
0705 EXPORT_SYMBOL_GPL(bdev_disk_changed);
0706
0707 void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
0708 {
0709 struct address_space *mapping = state->disk->part0->bd_inode->i_mapping;
0710 struct folio *folio;
0711
0712 if (n >= get_capacity(state->disk)) {
0713 state->access_beyond_eod = true;
0714 goto out;
0715 }
0716
0717 folio = read_mapping_folio(mapping, n >> PAGE_SECTORS_SHIFT, NULL);
0718 if (IS_ERR(folio))
0719 goto out;
0720
0721 p->v = folio;
0722 return folio_address(folio) + offset_in_folio(folio, n * SECTOR_SIZE);
0723 out:
0724 p->v = NULL;
0725 return NULL;
0726 }