Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 2 | /* |
Christoph Hellwig | 387048b | 2020-03-24 08:25:30 +0100 | [diff] [blame] | 3 | * Copyright (C) 1991-1998 Linus Torvalds |
| 4 | * Re-organised Feb 1998 Russell King |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 5 | */ |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 6 | #include <linux/fs.h> |
| 7 | #include <linux/slab.h> |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 8 | #include <linux/ctype.h> |
| 9 | #include <linux/genhd.h> |
Christoph Hellwig | 387048b | 2020-03-24 08:25:30 +0100 | [diff] [blame] | 10 | #include <linux/vmalloc.h> |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 11 | #include <linux/blktrace_api.h> |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 12 | #include <linux/raid/detect.h> |
Christoph Hellwig | 387048b | 2020-03-24 08:25:30 +0100 | [diff] [blame] | 13 | #include "check.h" |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 14 | |
Christoph Hellwig | 387048b | 2020-03-24 08:25:30 +0100 | [diff] [blame] | 15 | static int (*check_part[])(struct parsed_partitions *) = { |
| 16 | /* |
| 17 | * Probe partition formats with tables at disk address 0 |
| 18 | * that also have an ADFS boot block at 0xdc0. |
| 19 | */ |
| 20 | #ifdef CONFIG_ACORN_PARTITION_ICS |
| 21 | adfspart_check_ICS, |
| 22 | #endif |
| 23 | #ifdef CONFIG_ACORN_PARTITION_POWERTEC |
| 24 | adfspart_check_POWERTEC, |
| 25 | #endif |
| 26 | #ifdef CONFIG_ACORN_PARTITION_EESOX |
| 27 | adfspart_check_EESOX, |
| 28 | #endif |
| 29 | |
| 30 | /* |
| 31 | * Now move on to formats that only have partition info at |
| 32 | * disk address 0xdc0. Since these may also have stale |
| 33 | * PC/BIOS partition tables, they need to come before |
| 34 | * the msdos entry. |
| 35 | */ |
| 36 | #ifdef CONFIG_ACORN_PARTITION_CUMANA |
| 37 | adfspart_check_CUMANA, |
| 38 | #endif |
| 39 | #ifdef CONFIG_ACORN_PARTITION_ADFS |
| 40 | adfspart_check_ADFS, |
| 41 | #endif |
| 42 | |
| 43 | #ifdef CONFIG_CMDLINE_PARTITION |
| 44 | cmdline_partition, |
| 45 | #endif |
| 46 | #ifdef CONFIG_EFI_PARTITION |
| 47 | efi_partition, /* this must come before msdos */ |
| 48 | #endif |
| 49 | #ifdef CONFIG_SGI_PARTITION |
| 50 | sgi_partition, |
| 51 | #endif |
| 52 | #ifdef CONFIG_LDM_PARTITION |
| 53 | ldm_partition, /* this must come before msdos */ |
| 54 | #endif |
| 55 | #ifdef CONFIG_MSDOS_PARTITION |
| 56 | msdos_partition, |
| 57 | #endif |
| 58 | #ifdef CONFIG_OSF_PARTITION |
| 59 | osf_partition, |
| 60 | #endif |
| 61 | #ifdef CONFIG_SUN_PARTITION |
| 62 | sun_partition, |
| 63 | #endif |
| 64 | #ifdef CONFIG_AMIGA_PARTITION |
| 65 | amiga_partition, |
| 66 | #endif |
| 67 | #ifdef CONFIG_ATARI_PARTITION |
| 68 | atari_partition, |
| 69 | #endif |
| 70 | #ifdef CONFIG_MAC_PARTITION |
| 71 | mac_partition, |
| 72 | #endif |
| 73 | #ifdef CONFIG_ULTRIX_PARTITION |
| 74 | ultrix_partition, |
| 75 | #endif |
| 76 | #ifdef CONFIG_IBM_PARTITION |
| 77 | ibm_partition, |
| 78 | #endif |
| 79 | #ifdef CONFIG_KARMA_PARTITION |
| 80 | karma_partition, |
| 81 | #endif |
| 82 | #ifdef CONFIG_SYSV68_PARTITION |
| 83 | sysv68_partition, |
| 84 | #endif |
| 85 | NULL |
| 86 | }; |
| 87 | |
| 88 | static struct parsed_partitions *allocate_partitions(struct gendisk *hd) |
| 89 | { |
| 90 | struct parsed_partitions *state; |
| 91 | int nr; |
| 92 | |
| 93 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 94 | if (!state) |
| 95 | return NULL; |
| 96 | |
| 97 | nr = disk_max_parts(hd); |
| 98 | state->parts = vzalloc(array_size(nr, sizeof(state->parts[0]))); |
| 99 | if (!state->parts) { |
| 100 | kfree(state); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | state->limit = nr; |
| 105 | |
| 106 | return state; |
| 107 | } |
| 108 | |
| 109 | static void free_partitions(struct parsed_partitions *state) |
| 110 | { |
| 111 | vfree(state->parts); |
| 112 | kfree(state); |
| 113 | } |
| 114 | |
| 115 | static struct parsed_partitions *check_partition(struct gendisk *hd, |
| 116 | struct block_device *bdev) |
| 117 | { |
| 118 | struct parsed_partitions *state; |
| 119 | int i, res, err; |
| 120 | |
| 121 | state = allocate_partitions(hd); |
| 122 | if (!state) |
| 123 | return NULL; |
| 124 | state->pp_buf = (char *)__get_free_page(GFP_KERNEL); |
| 125 | if (!state->pp_buf) { |
| 126 | free_partitions(state); |
| 127 | return NULL; |
| 128 | } |
| 129 | state->pp_buf[0] = '\0'; |
| 130 | |
| 131 | state->bdev = bdev; |
| 132 | disk_name(hd, 0, state->name); |
| 133 | snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name); |
| 134 | if (isdigit(state->name[strlen(state->name)-1])) |
| 135 | sprintf(state->name, "p"); |
| 136 | |
| 137 | i = res = err = 0; |
| 138 | while (!res && check_part[i]) { |
| 139 | memset(state->parts, 0, state->limit * sizeof(state->parts[0])); |
| 140 | res = check_part[i++](state); |
| 141 | if (res < 0) { |
| 142 | /* |
| 143 | * We have hit an I/O error which we don't report now. |
| 144 | * But record it, and let the others do their job. |
| 145 | */ |
| 146 | err = res; |
| 147 | res = 0; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | if (res > 0) { |
| 152 | printk(KERN_INFO "%s", state->pp_buf); |
| 153 | |
| 154 | free_page((unsigned long)state->pp_buf); |
| 155 | return state; |
| 156 | } |
| 157 | if (state->access_beyond_eod) |
| 158 | err = -ENOSPC; |
| 159 | /* |
| 160 | * The partition is unrecognized. So report I/O errors if there were any |
| 161 | */ |
| 162 | if (err) |
| 163 | res = err; |
| 164 | if (res) { |
| 165 | strlcat(state->pp_buf, |
| 166 | " unable to read partition table\n", PAGE_SIZE); |
| 167 | printk(KERN_INFO "%s", state->pp_buf); |
| 168 | } |
| 169 | |
| 170 | free_page((unsigned long)state->pp_buf); |
| 171 | free_partitions(state); |
| 172 | return ERR_PTR(res); |
| 173 | } |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 174 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 175 | static ssize_t part_partition_show(struct device *dev, |
| 176 | struct device_attribute *attr, char *buf) |
| 177 | { |
| 178 | struct hd_struct *p = dev_to_part(dev); |
| 179 | |
| 180 | return sprintf(buf, "%d\n", p->partno); |
| 181 | } |
| 182 | |
| 183 | static ssize_t part_start_show(struct device *dev, |
| 184 | struct device_attribute *attr, char *buf) |
| 185 | { |
| 186 | struct hd_struct *p = dev_to_part(dev); |
| 187 | |
| 188 | return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect); |
| 189 | } |
| 190 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 191 | static ssize_t part_ro_show(struct device *dev, |
| 192 | struct device_attribute *attr, char *buf) |
| 193 | { |
| 194 | struct hd_struct *p = dev_to_part(dev); |
| 195 | return sprintf(buf, "%d\n", p->policy ? 1 : 0); |
| 196 | } |
| 197 | |
| 198 | static ssize_t part_alignment_offset_show(struct device *dev, |
| 199 | struct device_attribute *attr, char *buf) |
| 200 | { |
| 201 | struct hd_struct *p = dev_to_part(dev); |
| 202 | return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset); |
| 203 | } |
| 204 | |
| 205 | static ssize_t part_discard_alignment_show(struct device *dev, |
| 206 | struct device_attribute *attr, char *buf) |
| 207 | { |
| 208 | struct hd_struct *p = dev_to_part(dev); |
| 209 | return sprintf(buf, "%u\n", p->discard_alignment); |
| 210 | } |
| 211 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 212 | static DEVICE_ATTR(partition, 0444, part_partition_show, NULL); |
| 213 | static DEVICE_ATTR(start, 0444, part_start_show, NULL); |
| 214 | static DEVICE_ATTR(size, 0444, part_size_show, NULL); |
| 215 | static DEVICE_ATTR(ro, 0444, part_ro_show, NULL); |
| 216 | static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL); |
| 217 | static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL); |
| 218 | static DEVICE_ATTR(stat, 0444, part_stat_show, NULL); |
| 219 | static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 220 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 221 | static struct device_attribute dev_attr_fail = |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 222 | __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 223 | #endif |
| 224 | |
| 225 | static struct attribute *part_attrs[] = { |
| 226 | &dev_attr_partition.attr, |
| 227 | &dev_attr_start.attr, |
| 228 | &dev_attr_size.attr, |
| 229 | &dev_attr_ro.attr, |
| 230 | &dev_attr_alignment_offset.attr, |
| 231 | &dev_attr_discard_alignment.attr, |
| 232 | &dev_attr_stat.attr, |
| 233 | &dev_attr_inflight.attr, |
| 234 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 235 | &dev_attr_fail.attr, |
| 236 | #endif |
| 237 | NULL |
| 238 | }; |
| 239 | |
| 240 | static struct attribute_group part_attr_group = { |
| 241 | .attrs = part_attrs, |
| 242 | }; |
| 243 | |
| 244 | static const struct attribute_group *part_attr_groups[] = { |
| 245 | &part_attr_group, |
| 246 | #ifdef CONFIG_BLK_DEV_IO_TRACE |
| 247 | &blk_trace_attr_group, |
| 248 | #endif |
| 249 | NULL |
| 250 | }; |
| 251 | |
| 252 | static void part_release(struct device *dev) |
| 253 | { |
| 254 | struct hd_struct *p = dev_to_part(dev); |
Keith Busch | 2da7809 | 2014-08-26 09:05:36 -0600 | [diff] [blame] | 255 | blk_free_devt(dev->devt); |
Ming Lei | b54e5ed | 2015-07-16 11:16:44 +0800 | [diff] [blame] | 256 | hd_free_part(p); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 257 | kfree(p); |
| 258 | } |
| 259 | |
San Mehat | 0d9c51a | 2016-03-15 14:53:26 -0700 | [diff] [blame] | 260 | static int part_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 261 | { |
| 262 | struct hd_struct *part = dev_to_part(dev); |
| 263 | |
| 264 | add_uevent_var(env, "PARTN=%u", part->partno); |
| 265 | if (part->info && part->info->volname[0]) |
| 266 | add_uevent_var(env, "PARTNAME=%s", part->info->volname); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 270 | struct device_type part_type = { |
| 271 | .name = "partition", |
| 272 | .groups = part_attr_groups, |
| 273 | .release = part_release, |
San Mehat | 0d9c51a | 2016-03-15 14:53:26 -0700 | [diff] [blame] | 274 | .uevent = part_uevent, |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 275 | }; |
| 276 | |
Yufen Yu | 94a2c3a | 2018-11-28 16:42:01 +0800 | [diff] [blame] | 277 | static void delete_partition_work_fn(struct work_struct *work) |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 278 | { |
Yufen Yu | 94a2c3a | 2018-11-28 16:42:01 +0800 | [diff] [blame] | 279 | struct hd_struct *part = container_of(to_rcu_work(work), struct hd_struct, |
| 280 | rcu_work); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 281 | |
| 282 | part->start_sect = 0; |
| 283 | part->nr_sects = 0; |
| 284 | part_stat_set_all(part, 0); |
| 285 | put_device(part_to_dev(part)); |
| 286 | } |
| 287 | |
Ming Lei | 6c71013 | 2015-07-16 11:16:45 +0800 | [diff] [blame] | 288 | void __delete_partition(struct percpu_ref *ref) |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 289 | { |
Ming Lei | 6c71013 | 2015-07-16 11:16:45 +0800 | [diff] [blame] | 290 | struct hd_struct *part = container_of(ref, struct hd_struct, ref); |
Yufen Yu | 94a2c3a | 2018-11-28 16:42:01 +0800 | [diff] [blame] | 291 | INIT_RCU_WORK(&part->rcu_work, delete_partition_work_fn); |
| 292 | queue_rcu_work(system_wq, &part->rcu_work); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Bart Van Assche | 6d2cf6f | 2017-08-17 16:23:06 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Must be called either with bd_mutex held, before a disk can be opened or |
| 297 | * after all disk users are gone. |
| 298 | */ |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 299 | void delete_partition(struct gendisk *disk, int partno) |
| 300 | { |
Bart Van Assche | 6d2cf6f | 2017-08-17 16:23:06 -0700 | [diff] [blame] | 301 | struct disk_part_tbl *ptbl = |
| 302 | rcu_dereference_protected(disk->part_tbl, 1); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 303 | struct hd_struct *part; |
| 304 | |
| 305 | if (partno >= ptbl->len) |
| 306 | return; |
| 307 | |
Bart Van Assche | 6d2cf6f | 2017-08-17 16:23:06 -0700 | [diff] [blame] | 308 | part = rcu_dereference_protected(ptbl->part[partno], 1); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 309 | if (!part) |
| 310 | return; |
| 311 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 312 | rcu_assign_pointer(ptbl->part[partno], NULL); |
| 313 | rcu_assign_pointer(ptbl->last_lookup, NULL); |
| 314 | kobject_put(part->holder_dir); |
| 315 | device_del(part_to_dev(part)); |
| 316 | |
Yufen Yu | 6fcc44d | 2019-04-02 20:06:34 +0800 | [diff] [blame] | 317 | /* |
| 318 | * Remove gendisk pointer from idr so that it cannot be looked up |
| 319 | * while RCU period before freeing gendisk is running to prevent |
| 320 | * use-after-free issues. Note that the device number stays |
| 321 | * "in-use" until we really free the gendisk. |
| 322 | */ |
| 323 | blk_invalidate_devt(part_devt(part)); |
Ming Lei | 6c71013 | 2015-07-16 11:16:45 +0800 | [diff] [blame] | 324 | hd_struct_kill(part); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static ssize_t whole_disk_show(struct device *dev, |
| 328 | struct device_attribute *attr, char *buf) |
| 329 | { |
| 330 | return 0; |
| 331 | } |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 332 | static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 333 | |
Bart Van Assche | 6d2cf6f | 2017-08-17 16:23:06 -0700 | [diff] [blame] | 334 | /* |
| 335 | * Must be called either with bd_mutex held, before a disk can be opened or |
| 336 | * after all disk users are gone. |
| 337 | */ |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 338 | struct hd_struct *add_partition(struct gendisk *disk, int partno, |
| 339 | sector_t start, sector_t len, int flags, |
| 340 | struct partition_meta_info *info) |
| 341 | { |
| 342 | struct hd_struct *p; |
| 343 | dev_t devt = MKDEV(0, 0); |
| 344 | struct device *ddev = disk_to_dev(disk); |
| 345 | struct device *pdev; |
| 346 | struct disk_part_tbl *ptbl; |
| 347 | const char *dname; |
| 348 | int err; |
| 349 | |
Christoph Hellwig | b720530 | 2020-01-26 14:05:43 +0100 | [diff] [blame] | 350 | /* |
| 351 | * Partitions are not supported on zoned block devices that are used as |
| 352 | * such. |
| 353 | */ |
| 354 | switch (disk->queue->limits.zoned) { |
| 355 | case BLK_ZONED_HM: |
| 356 | pr_warn("%s: partitions not supported on host managed zoned block device\n", |
| 357 | disk->disk_name); |
| 358 | return ERR_PTR(-ENXIO); |
| 359 | case BLK_ZONED_HA: |
| 360 | pr_info("%s: disabling host aware zoned block device support due to partitions\n", |
| 361 | disk->disk_name); |
| 362 | disk->queue->limits.zoned = BLK_ZONED_NONE; |
| 363 | break; |
| 364 | case BLK_ZONED_NONE: |
| 365 | break; |
| 366 | } |
| 367 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 368 | err = disk_expand_part_tbl(disk, partno); |
| 369 | if (err) |
| 370 | return ERR_PTR(err); |
Bart Van Assche | 6d2cf6f | 2017-08-17 16:23:06 -0700 | [diff] [blame] | 371 | ptbl = rcu_dereference_protected(disk->part_tbl, 1); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 372 | |
| 373 | if (ptbl->part[partno]) |
| 374 | return ERR_PTR(-EBUSY); |
| 375 | |
| 376 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 377 | if (!p) |
| 378 | return ERR_PTR(-EBUSY); |
| 379 | |
| 380 | if (!init_part_stats(p)) { |
| 381 | err = -ENOMEM; |
| 382 | goto out_free; |
| 383 | } |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 384 | |
| 385 | seqcount_init(&p->nr_sects_seq); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 386 | pdev = part_to_dev(p); |
| 387 | |
| 388 | p->start_sect = start; |
| 389 | p->alignment_offset = |
| 390 | queue_limit_alignment_offset(&disk->queue->limits, start); |
| 391 | p->discard_alignment = |
| 392 | queue_limit_discard_alignment(&disk->queue->limits, start); |
| 393 | p->nr_sects = len; |
| 394 | p->partno = partno; |
| 395 | p->policy = get_disk_ro(disk); |
| 396 | |
| 397 | if (info) { |
Christoph Hellwig | f17c21c | 2020-03-24 08:25:14 +0100 | [diff] [blame] | 398 | struct partition_meta_info *pinfo; |
| 399 | |
| 400 | pinfo = kzalloc_node(sizeof(*pinfo), GFP_KERNEL, disk->node_id); |
Dan Carpenter | 7bd897c | 2017-05-23 17:28:36 +0300 | [diff] [blame] | 401 | if (!pinfo) { |
| 402 | err = -ENOMEM; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 403 | goto out_free_stats; |
Dan Carpenter | 7bd897c | 2017-05-23 17:28:36 +0300 | [diff] [blame] | 404 | } |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 405 | memcpy(pinfo, info, sizeof(*info)); |
| 406 | p->info = pinfo; |
| 407 | } |
| 408 | |
| 409 | dname = dev_name(ddev); |
| 410 | if (isdigit(dname[strlen(dname) - 1])) |
| 411 | dev_set_name(pdev, "%sp%d", dname, partno); |
| 412 | else |
| 413 | dev_set_name(pdev, "%s%d", dname, partno); |
| 414 | |
| 415 | device_initialize(pdev); |
| 416 | pdev->class = &block_class; |
| 417 | pdev->type = &part_type; |
| 418 | pdev->parent = ddev; |
| 419 | |
| 420 | err = blk_alloc_devt(p, &devt); |
| 421 | if (err) |
| 422 | goto out_free_info; |
| 423 | pdev->devt = devt; |
| 424 | |
| 425 | /* delay uevent until 'holders' subdir is created */ |
| 426 | dev_set_uevent_suppress(pdev, 1); |
| 427 | err = device_add(pdev); |
| 428 | if (err) |
| 429 | goto out_put; |
| 430 | |
| 431 | err = -ENOMEM; |
| 432 | p->holder_dir = kobject_create_and_add("holders", &pdev->kobj); |
| 433 | if (!p->holder_dir) |
| 434 | goto out_del; |
| 435 | |
| 436 | dev_set_uevent_suppress(pdev, 0); |
| 437 | if (flags & ADDPART_FLAG_WHOLEDISK) { |
| 438 | err = device_create_file(pdev, &dev_attr_whole_disk); |
| 439 | if (err) |
| 440 | goto out_del; |
| 441 | } |
| 442 | |
Ming Lei | b30a337ca | 2016-03-30 08:46:31 +0800 | [diff] [blame] | 443 | err = hd_ref_init(p); |
| 444 | if (err) { |
| 445 | if (flags & ADDPART_FLAG_WHOLEDISK) |
| 446 | goto out_remove_file; |
| 447 | goto out_del; |
| 448 | } |
| 449 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 450 | /* everything is up and running, commence */ |
| 451 | rcu_assign_pointer(ptbl->part[partno], p); |
| 452 | |
| 453 | /* suppress uevent if the disk suppresses it */ |
| 454 | if (!dev_get_uevent_suppress(ddev)) |
| 455 | kobject_uevent(&pdev->kobj, KOBJ_ADD); |
Ming Lei | b30a337ca | 2016-03-30 08:46:31 +0800 | [diff] [blame] | 456 | return p; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 457 | |
| 458 | out_free_info: |
Christoph Hellwig | f17c21c | 2020-03-24 08:25:14 +0100 | [diff] [blame] | 459 | kfree(p->info); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 460 | out_free_stats: |
| 461 | free_part_stats(p); |
| 462 | out_free: |
| 463 | kfree(p); |
| 464 | return ERR_PTR(err); |
Ming Lei | b30a337ca | 2016-03-30 08:46:31 +0800 | [diff] [blame] | 465 | out_remove_file: |
| 466 | device_remove_file(pdev, &dev_attr_whole_disk); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 467 | out_del: |
| 468 | kobject_put(p->holder_dir); |
| 469 | device_del(pdev); |
| 470 | out_put: |
| 471 | put_device(pdev); |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 472 | return ERR_PTR(err); |
| 473 | } |
| 474 | |
| 475 | static bool disk_unlock_native_capacity(struct gendisk *disk) |
| 476 | { |
| 477 | const struct block_device_operations *bdops = disk->fops; |
| 478 | |
| 479 | if (bdops->unlock_native_capacity && |
| 480 | !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) { |
| 481 | printk(KERN_CONT "enabling native capacity\n"); |
| 482 | bdops->unlock_native_capacity(disk); |
| 483 | disk->flags |= GENHD_FL_NATIVE_CAPACITY; |
| 484 | return true; |
| 485 | } else { |
| 486 | printk(KERN_CONT "truncated\n"); |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 491 | int blk_drop_partitions(struct gendisk *disk, struct block_device *bdev) |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 492 | { |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 493 | struct disk_part_iter piter; |
| 494 | struct hd_struct *part; |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 495 | int res; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 496 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 497 | if (!disk_part_scan_enabled(disk)) |
| 498 | return 0; |
Christoph Hellwig | cb6b771 | 2020-04-10 14:31:47 +0200 | [diff] [blame] | 499 | if (bdev->bd_part_count || bdev->bd_openers > 1) |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 500 | return -EBUSY; |
| 501 | res = invalidate_partition(disk, 0); |
| 502 | if (res) |
| 503 | return res; |
| 504 | |
| 505 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY); |
| 506 | while ((part = disk_part_iter_next(&piter))) |
| 507 | delete_partition(disk, part->partno); |
| 508 | disk_part_iter_exit(&piter); |
| 509 | |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 513 | static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev, |
| 514 | struct parsed_partitions *state, int p) |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 515 | { |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 516 | sector_t size = state->parts[p].size; |
| 517 | sector_t from = state->parts[p].from; |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 518 | struct hd_struct *part; |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 519 | |
| 520 | if (!size) |
| 521 | return true; |
| 522 | |
| 523 | if (from >= get_capacity(disk)) { |
| 524 | printk(KERN_WARNING |
| 525 | "%s: p%d start %llu is beyond EOD, ", |
| 526 | disk->disk_name, p, (unsigned long long) from); |
| 527 | if (disk_unlock_native_capacity(disk)) |
| 528 | return false; |
| 529 | return true; |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 532 | if (from + size > get_capacity(disk)) { |
| 533 | printk(KERN_WARNING |
| 534 | "%s: p%d size %llu extends beyond EOD, ", |
| 535 | disk->disk_name, p, (unsigned long long) size); |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 536 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 537 | if (disk_unlock_native_capacity(disk)) |
| 538 | return false; |
| 539 | |
| 540 | /* |
| 541 | * We can not ignore partitions of broken tables created by for |
| 542 | * example camera firmware, but we limit them to the end of the |
| 543 | * disk to avoid creating invalid block devices. |
| 544 | */ |
| 545 | size = get_capacity(disk) - from; |
| 546 | } |
| 547 | |
| 548 | part = add_partition(disk, p, from, size, state->parts[p].flags, |
| 549 | &state->parts[p].info); |
Christoph Hellwig | b720530 | 2020-01-26 14:05:43 +0100 | [diff] [blame] | 550 | if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) { |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 551 | printk(KERN_ERR " %s: p%d could not be added: %ld\n", |
| 552 | disk->disk_name, p, -PTR_ERR(part)); |
| 553 | return true; |
| 554 | } |
| 555 | |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 556 | if (IS_BUILTIN(CONFIG_BLK_DEV_MD) && |
| 557 | (state->parts[p].flags & ADDPART_FLAG_RAID)) |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 558 | md_autodetect_dev(part_to_dev(part)->devt); |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 559 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 560 | return true; |
| 561 | } |
| 562 | |
Christoph Hellwig | a1548b6 | 2019-11-14 15:34:34 +0100 | [diff] [blame] | 563 | int blk_add_partitions(struct gendisk *disk, struct block_device *bdev) |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 564 | { |
| 565 | struct parsed_partitions *state; |
| 566 | int ret = -EAGAIN, p, highest; |
| 567 | |
Christoph Hellwig | 142fe8f | 2019-11-14 15:34:35 +0100 | [diff] [blame] | 568 | if (!disk_part_scan_enabled(disk)) |
| 569 | return 0; |
| 570 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 571 | state = check_partition(disk, bdev); |
| 572 | if (!state) |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 573 | return 0; |
| 574 | if (IS_ERR(state)) { |
| 575 | /* |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 576 | * I/O error reading the partition table. If we tried to read |
| 577 | * beyond EOD, retry after unlocking the native capacity. |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 578 | */ |
| 579 | if (PTR_ERR(state) == -ENOSPC) { |
| 580 | printk(KERN_WARNING "%s: partition table beyond EOD, ", |
| 581 | disk->disk_name); |
| 582 | if (disk_unlock_native_capacity(disk)) |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 583 | return -EAGAIN; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 584 | } |
| 585 | return -EIO; |
| 586 | } |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 587 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 588 | /* |
Christoph Hellwig | b720530 | 2020-01-26 14:05:43 +0100 | [diff] [blame] | 589 | * Partitions are not supported on host managed zoned block devices. |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 590 | */ |
Christoph Hellwig | b720530 | 2020-01-26 14:05:43 +0100 | [diff] [blame] | 591 | if (disk->queue->limits.zoned == BLK_ZONED_HM) { |
| 592 | pr_warn("%s: ignoring partition table on host managed zoned block device\n", |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 593 | disk->disk_name); |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 594 | ret = 0; |
| 595 | goto out_free_state; |
Damien Le Moal | 5eac3eb | 2019-11-11 11:39:25 +0900 | [diff] [blame] | 596 | } |
| 597 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 598 | /* |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 599 | * If we read beyond EOD, try unlocking native capacity even if the |
| 600 | * partition table was successfully read as we could be missing some |
| 601 | * partitions. |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 602 | */ |
| 603 | if (state->access_beyond_eod) { |
| 604 | printk(KERN_WARNING |
| 605 | "%s: partition table partially beyond EOD, ", |
| 606 | disk->disk_name); |
| 607 | if (disk_unlock_native_capacity(disk)) |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 608 | goto out_free_state; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* tell userspace that the media / partition table may have changed */ |
| 612 | kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE); |
| 613 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 614 | /* |
| 615 | * Detect the highest partition number and preallocate disk->part_tbl. |
| 616 | * This is an optimization and not strictly necessary. |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 617 | */ |
| 618 | for (p = 1, highest = 0; p < state->limit; p++) |
| 619 | if (state->parts[p].size) |
| 620 | highest = p; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 621 | disk_expand_part_tbl(disk, highest); |
| 622 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 623 | for (p = 1; p < state->limit; p++) |
| 624 | if (!blk_add_partition(disk, bdev, state, p)) |
| 625 | goto out_free_state; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 626 | |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 627 | ret = 0; |
| 628 | out_free_state: |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 629 | free_partitions(state); |
Christoph Hellwig | f902b02 | 2019-11-14 15:34:32 +0100 | [diff] [blame] | 630 | return ret; |
Jun'ichi Nomura | fe316bf | 2012-03-02 10:38:33 +0100 | [diff] [blame] | 631 | } |
| 632 | |
Christoph Hellwig | 1a9fba3 | 2020-03-24 08:25:18 +0100 | [diff] [blame] | 633 | void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p) |
Dan Williams | d1a5f2b4 | 2016-01-28 20:25:31 -0800 | [diff] [blame] | 634 | { |
Christoph Hellwig | 1a9fba3 | 2020-03-24 08:25:18 +0100 | [diff] [blame] | 635 | struct address_space *mapping = state->bdev->bd_inode->i_mapping; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 636 | struct page *page; |
| 637 | |
Christoph Hellwig | 1a9fba3 | 2020-03-24 08:25:18 +0100 | [diff] [blame] | 638 | if (n >= get_capacity(state->bdev->bd_disk)) { |
| 639 | state->access_beyond_eod = true; |
| 640 | return NULL; |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 641 | } |
Christoph Hellwig | 1a9fba3 | 2020-03-24 08:25:18 +0100 | [diff] [blame] | 642 | |
| 643 | page = read_mapping_page(mapping, |
| 644 | (pgoff_t)(n >> (PAGE_SHIFT - 9)), NULL); |
| 645 | if (IS_ERR(page)) |
| 646 | goto out; |
| 647 | if (PageError(page)) |
| 648 | goto out_put_page; |
| 649 | |
| 650 | p->v = page; |
| 651 | return (unsigned char *)page_address(page) + |
| 652 | ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << SECTOR_SHIFT); |
| 653 | out_put_page: |
| 654 | put_page(page); |
| 655 | out: |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 656 | p->v = NULL; |
| 657 | return NULL; |
| 658 | } |