blob: b442bc209b86181761325589f44ba7014bdba593 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Viro94ea4152011-09-16 00:45:36 -04002/*
Christoph Hellwig387048b2020-03-24 08:25:30 +01003 * Copyright (C) 1991-1998 Linus Torvalds
4 * Re-organised Feb 1998 Russell King
Al Viro94ea4152011-09-16 00:45:36 -04005 */
Al Viro94ea4152011-09-16 00:45:36 -04006#include <linux/fs.h>
7#include <linux/slab.h>
Al Viro94ea4152011-09-16 00:45:36 -04008#include <linux/ctype.h>
9#include <linux/genhd.h>
Christoph Hellwig387048b2020-03-24 08:25:30 +010010#include <linux/vmalloc.h>
Al Viro94ea4152011-09-16 00:45:36 -040011#include <linux/blktrace_api.h>
Christoph Hellwig74cc979c2020-03-24 08:25:19 +010012#include <linux/raid/detect.h>
Christoph Hellwig387048b2020-03-24 08:25:30 +010013#include "../blk.h"
14#include "check.h"
Al Viro94ea4152011-09-16 00:45:36 -040015
Christoph Hellwig387048b2020-03-24 08:25:30 +010016static int (*check_part[])(struct parsed_partitions *) = {
17 /*
18 * Probe partition formats with tables at disk address 0
19 * that also have an ADFS boot block at 0xdc0.
20 */
21#ifdef CONFIG_ACORN_PARTITION_ICS
22 adfspart_check_ICS,
23#endif
24#ifdef CONFIG_ACORN_PARTITION_POWERTEC
25 adfspart_check_POWERTEC,
26#endif
27#ifdef CONFIG_ACORN_PARTITION_EESOX
28 adfspart_check_EESOX,
29#endif
30
31 /*
32 * Now move on to formats that only have partition info at
33 * disk address 0xdc0. Since these may also have stale
34 * PC/BIOS partition tables, they need to come before
35 * the msdos entry.
36 */
37#ifdef CONFIG_ACORN_PARTITION_CUMANA
38 adfspart_check_CUMANA,
39#endif
40#ifdef CONFIG_ACORN_PARTITION_ADFS
41 adfspart_check_ADFS,
42#endif
43
44#ifdef CONFIG_CMDLINE_PARTITION
45 cmdline_partition,
46#endif
47#ifdef CONFIG_EFI_PARTITION
48 efi_partition, /* this must come before msdos */
49#endif
50#ifdef CONFIG_SGI_PARTITION
51 sgi_partition,
52#endif
53#ifdef CONFIG_LDM_PARTITION
54 ldm_partition, /* this must come before msdos */
55#endif
56#ifdef CONFIG_MSDOS_PARTITION
57 msdos_partition,
58#endif
59#ifdef CONFIG_OSF_PARTITION
60 osf_partition,
61#endif
62#ifdef CONFIG_SUN_PARTITION
63 sun_partition,
64#endif
65#ifdef CONFIG_AMIGA_PARTITION
66 amiga_partition,
67#endif
68#ifdef CONFIG_ATARI_PARTITION
69 atari_partition,
70#endif
71#ifdef CONFIG_MAC_PARTITION
72 mac_partition,
73#endif
74#ifdef CONFIG_ULTRIX_PARTITION
75 ultrix_partition,
76#endif
77#ifdef CONFIG_IBM_PARTITION
78 ibm_partition,
79#endif
80#ifdef CONFIG_KARMA_PARTITION
81 karma_partition,
82#endif
83#ifdef CONFIG_SYSV68_PARTITION
84 sysv68_partition,
85#endif
86 NULL
87};
88
89static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
90{
91 struct parsed_partitions *state;
92 int nr;
93
94 state = kzalloc(sizeof(*state), GFP_KERNEL);
95 if (!state)
96 return NULL;
97
98 nr = disk_max_parts(hd);
99 state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
100 if (!state->parts) {
101 kfree(state);
102 return NULL;
103 }
104
105 state->limit = nr;
106
107 return state;
108}
109
110static void free_partitions(struct parsed_partitions *state)
111{
112 vfree(state->parts);
113 kfree(state);
114}
115
116static struct parsed_partitions *check_partition(struct gendisk *hd,
117 struct block_device *bdev)
118{
119 struct parsed_partitions *state;
120 int i, res, err;
121
122 state = allocate_partitions(hd);
123 if (!state)
124 return NULL;
125 state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
126 if (!state->pp_buf) {
127 free_partitions(state);
128 return NULL;
129 }
130 state->pp_buf[0] = '\0';
131
132 state->bdev = bdev;
133 disk_name(hd, 0, state->name);
134 snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
135 if (isdigit(state->name[strlen(state->name)-1]))
136 sprintf(state->name, "p");
137
138 i = res = err = 0;
139 while (!res && check_part[i]) {
140 memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
141 res = check_part[i++](state);
142 if (res < 0) {
143 /*
144 * We have hit an I/O error which we don't report now.
145 * But record it, and let the others do their job.
146 */
147 err = res;
148 res = 0;
149 }
150
151 }
152 if (res > 0) {
153 printk(KERN_INFO "%s", state->pp_buf);
154
155 free_page((unsigned long)state->pp_buf);
156 return state;
157 }
158 if (state->access_beyond_eod)
159 err = -ENOSPC;
160 /*
161 * The partition is unrecognized. So report I/O errors if there were any
162 */
163 if (err)
164 res = err;
165 if (res) {
166 strlcat(state->pp_buf,
167 " unable to read partition table\n", PAGE_SIZE);
168 printk(KERN_INFO "%s", state->pp_buf);
169 }
170
171 free_page((unsigned long)state->pp_buf);
172 free_partitions(state);
173 return ERR_PTR(res);
174}
Al Viro94ea4152011-09-16 00:45:36 -0400175
Al Viro94ea4152011-09-16 00:45:36 -0400176static ssize_t part_partition_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
179 struct hd_struct *p = dev_to_part(dev);
180
181 return sprintf(buf, "%d\n", p->partno);
182}
183
184static ssize_t part_start_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
186{
187 struct hd_struct *p = dev_to_part(dev);
188
189 return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
190}
191
Al Viro94ea4152011-09-16 00:45:36 -0400192static ssize_t part_ro_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 struct hd_struct *p = dev_to_part(dev);
196 return sprintf(buf, "%d\n", p->policy ? 1 : 0);
197}
198
199static ssize_t part_alignment_offset_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 struct hd_struct *p = dev_to_part(dev);
203 return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
204}
205
206static ssize_t part_discard_alignment_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
209 struct hd_struct *p = dev_to_part(dev);
210 return sprintf(buf, "%u\n", p->discard_alignment);
211}
212
Joe Perches5657a812018-05-24 13:38:59 -0600213static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
214static DEVICE_ATTR(start, 0444, part_start_show, NULL);
215static DEVICE_ATTR(size, 0444, part_size_show, NULL);
216static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
217static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
218static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
219static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
220static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
Al Viro94ea4152011-09-16 00:45:36 -0400221#ifdef CONFIG_FAIL_MAKE_REQUEST
222static struct device_attribute dev_attr_fail =
Joe Perches5657a812018-05-24 13:38:59 -0600223 __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
Al Viro94ea4152011-09-16 00:45:36 -0400224#endif
225
226static struct attribute *part_attrs[] = {
227 &dev_attr_partition.attr,
228 &dev_attr_start.attr,
229 &dev_attr_size.attr,
230 &dev_attr_ro.attr,
231 &dev_attr_alignment_offset.attr,
232 &dev_attr_discard_alignment.attr,
233 &dev_attr_stat.attr,
234 &dev_attr_inflight.attr,
235#ifdef CONFIG_FAIL_MAKE_REQUEST
236 &dev_attr_fail.attr,
237#endif
238 NULL
239};
240
241static struct attribute_group part_attr_group = {
242 .attrs = part_attrs,
243};
244
245static const struct attribute_group *part_attr_groups[] = {
246 &part_attr_group,
247#ifdef CONFIG_BLK_DEV_IO_TRACE
248 &blk_trace_attr_group,
249#endif
250 NULL
251};
252
253static void part_release(struct device *dev)
254{
255 struct hd_struct *p = dev_to_part(dev);
Keith Busch2da78092014-08-26 09:05:36 -0600256 blk_free_devt(dev->devt);
Ming Leib54e5ed2015-07-16 11:16:44 +0800257 hd_free_part(p);
Al Viro94ea4152011-09-16 00:45:36 -0400258 kfree(p);
259}
260
San Mehat0d9c51a2016-03-15 14:53:26 -0700261static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
262{
263 struct hd_struct *part = dev_to_part(dev);
264
265 add_uevent_var(env, "PARTN=%u", part->partno);
266 if (part->info && part->info->volname[0])
267 add_uevent_var(env, "PARTNAME=%s", part->info->volname);
268 return 0;
269}
270
Al Viro94ea4152011-09-16 00:45:36 -0400271struct device_type part_type = {
272 .name = "partition",
273 .groups = part_attr_groups,
274 .release = part_release,
San Mehat0d9c51a2016-03-15 14:53:26 -0700275 .uevent = part_uevent,
Al Viro94ea4152011-09-16 00:45:36 -0400276};
277
Yufen Yu94a2c3a2018-11-28 16:42:01 +0800278static void delete_partition_work_fn(struct work_struct *work)
Al Viro94ea4152011-09-16 00:45:36 -0400279{
Yufen Yu94a2c3a2018-11-28 16:42:01 +0800280 struct hd_struct *part = container_of(to_rcu_work(work), struct hd_struct,
281 rcu_work);
Al Viro94ea4152011-09-16 00:45:36 -0400282
283 part->start_sect = 0;
284 part->nr_sects = 0;
285 part_stat_set_all(part, 0);
286 put_device(part_to_dev(part));
287}
288
Ming Lei6c710132015-07-16 11:16:45 +0800289void __delete_partition(struct percpu_ref *ref)
Al Viro94ea4152011-09-16 00:45:36 -0400290{
Ming Lei6c710132015-07-16 11:16:45 +0800291 struct hd_struct *part = container_of(ref, struct hd_struct, ref);
Yufen Yu94a2c3a2018-11-28 16:42:01 +0800292 INIT_RCU_WORK(&part->rcu_work, delete_partition_work_fn);
293 queue_rcu_work(system_wq, &part->rcu_work);
Al Viro94ea4152011-09-16 00:45:36 -0400294}
295
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -0700296/*
297 * Must be called either with bd_mutex held, before a disk can be opened or
298 * after all disk users are gone.
299 */
Al Viro94ea4152011-09-16 00:45:36 -0400300void delete_partition(struct gendisk *disk, int partno)
301{
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -0700302 struct disk_part_tbl *ptbl =
303 rcu_dereference_protected(disk->part_tbl, 1);
Al Viro94ea4152011-09-16 00:45:36 -0400304 struct hd_struct *part;
305
306 if (partno >= ptbl->len)
307 return;
308
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -0700309 part = rcu_dereference_protected(ptbl->part[partno], 1);
Al Viro94ea4152011-09-16 00:45:36 -0400310 if (!part)
311 return;
312
Al Viro94ea4152011-09-16 00:45:36 -0400313 rcu_assign_pointer(ptbl->part[partno], NULL);
314 rcu_assign_pointer(ptbl->last_lookup, NULL);
315 kobject_put(part->holder_dir);
316 device_del(part_to_dev(part));
317
Yufen Yu6fcc44d2019-04-02 20:06:34 +0800318 /*
319 * Remove gendisk pointer from idr so that it cannot be looked up
320 * while RCU period before freeing gendisk is running to prevent
321 * use-after-free issues. Note that the device number stays
322 * "in-use" until we really free the gendisk.
323 */
324 blk_invalidate_devt(part_devt(part));
Ming Lei6c710132015-07-16 11:16:45 +0800325 hd_struct_kill(part);
Al Viro94ea4152011-09-16 00:45:36 -0400326}
327
328static ssize_t whole_disk_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
330{
331 return 0;
332}
Joe Perches5657a812018-05-24 13:38:59 -0600333static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
Al Viro94ea4152011-09-16 00:45:36 -0400334
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -0700335/*
336 * Must be called either with bd_mutex held, before a disk can be opened or
337 * after all disk users are gone.
338 */
Al Viro94ea4152011-09-16 00:45:36 -0400339struct hd_struct *add_partition(struct gendisk *disk, int partno,
340 sector_t start, sector_t len, int flags,
341 struct partition_meta_info *info)
342{
343 struct hd_struct *p;
344 dev_t devt = MKDEV(0, 0);
345 struct device *ddev = disk_to_dev(disk);
346 struct device *pdev;
347 struct disk_part_tbl *ptbl;
348 const char *dname;
349 int err;
350
Christoph Hellwigb7205302020-01-26 14:05:43 +0100351 /*
352 * Partitions are not supported on zoned block devices that are used as
353 * such.
354 */
355 switch (disk->queue->limits.zoned) {
356 case BLK_ZONED_HM:
357 pr_warn("%s: partitions not supported on host managed zoned block device\n",
358 disk->disk_name);
359 return ERR_PTR(-ENXIO);
360 case BLK_ZONED_HA:
361 pr_info("%s: disabling host aware zoned block device support due to partitions\n",
362 disk->disk_name);
363 disk->queue->limits.zoned = BLK_ZONED_NONE;
364 break;
365 case BLK_ZONED_NONE:
366 break;
367 }
368
Al Viro94ea4152011-09-16 00:45:36 -0400369 err = disk_expand_part_tbl(disk, partno);
370 if (err)
371 return ERR_PTR(err);
Bart Van Assche6d2cf6f2017-08-17 16:23:06 -0700372 ptbl = rcu_dereference_protected(disk->part_tbl, 1);
Al Viro94ea4152011-09-16 00:45:36 -0400373
374 if (ptbl->part[partno])
375 return ERR_PTR(-EBUSY);
376
377 p = kzalloc(sizeof(*p), GFP_KERNEL);
378 if (!p)
379 return ERR_PTR(-EBUSY);
380
381 if (!init_part_stats(p)) {
382 err = -ENOMEM;
383 goto out_free;
384 }
Vivek Goyalc83f6bf2012-08-01 12:24:18 +0200385
386 seqcount_init(&p->nr_sects_seq);
Al Viro94ea4152011-09-16 00:45:36 -0400387 pdev = part_to_dev(p);
388
389 p->start_sect = start;
390 p->alignment_offset =
391 queue_limit_alignment_offset(&disk->queue->limits, start);
392 p->discard_alignment =
393 queue_limit_discard_alignment(&disk->queue->limits, start);
394 p->nr_sects = len;
395 p->partno = partno;
396 p->policy = get_disk_ro(disk);
397
398 if (info) {
Christoph Hellwigf17c21c2020-03-24 08:25:14 +0100399 struct partition_meta_info *pinfo;
400
401 pinfo = kzalloc_node(sizeof(*pinfo), GFP_KERNEL, disk->node_id);
Dan Carpenter7bd897c2017-05-23 17:28:36 +0300402 if (!pinfo) {
403 err = -ENOMEM;
Al Viro94ea4152011-09-16 00:45:36 -0400404 goto out_free_stats;
Dan Carpenter7bd897c2017-05-23 17:28:36 +0300405 }
Al Viro94ea4152011-09-16 00:45:36 -0400406 memcpy(pinfo, info, sizeof(*info));
407 p->info = pinfo;
408 }
409
410 dname = dev_name(ddev);
411 if (isdigit(dname[strlen(dname) - 1]))
412 dev_set_name(pdev, "%sp%d", dname, partno);
413 else
414 dev_set_name(pdev, "%s%d", dname, partno);
415
416 device_initialize(pdev);
417 pdev->class = &block_class;
418 pdev->type = &part_type;
419 pdev->parent = ddev;
420
421 err = blk_alloc_devt(p, &devt);
422 if (err)
423 goto out_free_info;
424 pdev->devt = devt;
425
426 /* delay uevent until 'holders' subdir is created */
427 dev_set_uevent_suppress(pdev, 1);
428 err = device_add(pdev);
429 if (err)
430 goto out_put;
431
432 err = -ENOMEM;
433 p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
434 if (!p->holder_dir)
435 goto out_del;
436
437 dev_set_uevent_suppress(pdev, 0);
438 if (flags & ADDPART_FLAG_WHOLEDISK) {
439 err = device_create_file(pdev, &dev_attr_whole_disk);
440 if (err)
441 goto out_del;
442 }
443
Ming Leib30a337ca2016-03-30 08:46:31 +0800444 err = hd_ref_init(p);
445 if (err) {
446 if (flags & ADDPART_FLAG_WHOLEDISK)
447 goto out_remove_file;
448 goto out_del;
449 }
450
Al Viro94ea4152011-09-16 00:45:36 -0400451 /* everything is up and running, commence */
452 rcu_assign_pointer(ptbl->part[partno], p);
453
454 /* suppress uevent if the disk suppresses it */
455 if (!dev_get_uevent_suppress(ddev))
456 kobject_uevent(&pdev->kobj, KOBJ_ADD);
Ming Leib30a337ca2016-03-30 08:46:31 +0800457 return p;
Al Viro94ea4152011-09-16 00:45:36 -0400458
459out_free_info:
Christoph Hellwigf17c21c2020-03-24 08:25:14 +0100460 kfree(p->info);
Al Viro94ea4152011-09-16 00:45:36 -0400461out_free_stats:
462 free_part_stats(p);
463out_free:
464 kfree(p);
465 return ERR_PTR(err);
Ming Leib30a337ca2016-03-30 08:46:31 +0800466out_remove_file:
467 device_remove_file(pdev, &dev_attr_whole_disk);
Al Viro94ea4152011-09-16 00:45:36 -0400468out_del:
469 kobject_put(p->holder_dir);
470 device_del(pdev);
471out_put:
472 put_device(pdev);
Al Viro94ea4152011-09-16 00:45:36 -0400473 return ERR_PTR(err);
474}
475
476static bool disk_unlock_native_capacity(struct gendisk *disk)
477{
478 const struct block_device_operations *bdops = disk->fops;
479
480 if (bdops->unlock_native_capacity &&
481 !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
482 printk(KERN_CONT "enabling native capacity\n");
483 bdops->unlock_native_capacity(disk);
484 disk->flags |= GENHD_FL_NATIVE_CAPACITY;
485 return true;
486 } else {
487 printk(KERN_CONT "truncated\n");
488 return false;
489 }
490}
491
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100492int blk_drop_partitions(struct gendisk *disk, struct block_device *bdev)
Al Viro94ea4152011-09-16 00:45:36 -0400493{
Al Viro94ea4152011-09-16 00:45:36 -0400494 struct disk_part_iter piter;
495 struct hd_struct *part;
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100496 int res;
Al Viro94ea4152011-09-16 00:45:36 -0400497
Christoph Hellwig142fe8f2019-11-14 15:34:35 +0100498 if (!disk_part_scan_enabled(disk))
499 return 0;
Eric Sandeen77032ca2015-11-24 17:30:34 -0600500 if (bdev->bd_part_count || bdev->bd_super)
Al Viro94ea4152011-09-16 00:45:36 -0400501 return -EBUSY;
502 res = invalidate_partition(disk, 0);
503 if (res)
504 return res;
505
506 disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
507 while ((part = disk_part_iter_next(&piter)))
508 delete_partition(disk, part->partno);
509 disk_part_iter_exit(&piter);
510
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100511 return 0;
512}
513
Christoph Hellwigf902b022019-11-14 15:34:32 +0100514static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
515 struct parsed_partitions *state, int p)
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100516{
Christoph Hellwigf902b022019-11-14 15:34:32 +0100517 sector_t size = state->parts[p].size;
518 sector_t from = state->parts[p].from;
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100519 struct hd_struct *part;
Christoph Hellwigf902b022019-11-14 15:34:32 +0100520
521 if (!size)
522 return true;
523
524 if (from >= get_capacity(disk)) {
525 printk(KERN_WARNING
526 "%s: p%d start %llu is beyond EOD, ",
527 disk->disk_name, p, (unsigned long long) from);
528 if (disk_unlock_native_capacity(disk))
529 return false;
530 return true;
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100531 }
532
Christoph Hellwigf902b022019-11-14 15:34:32 +0100533 if (from + size > get_capacity(disk)) {
534 printk(KERN_WARNING
535 "%s: p%d size %llu extends beyond EOD, ",
536 disk->disk_name, p, (unsigned long long) size);
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100537
Christoph Hellwigf902b022019-11-14 15:34:32 +0100538 if (disk_unlock_native_capacity(disk))
539 return false;
540
541 /*
542 * We can not ignore partitions of broken tables created by for
543 * example camera firmware, but we limit them to the end of the
544 * disk to avoid creating invalid block devices.
545 */
546 size = get_capacity(disk) - from;
547 }
548
549 part = add_partition(disk, p, from, size, state->parts[p].flags,
550 &state->parts[p].info);
Christoph Hellwigb7205302020-01-26 14:05:43 +0100551 if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
Christoph Hellwigf902b022019-11-14 15:34:32 +0100552 printk(KERN_ERR " %s: p%d could not be added: %ld\n",
553 disk->disk_name, p, -PTR_ERR(part));
554 return true;
555 }
556
Christoph Hellwig74cc979c2020-03-24 08:25:19 +0100557 if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
558 (state->parts[p].flags & ADDPART_FLAG_RAID))
Christoph Hellwigf902b022019-11-14 15:34:32 +0100559 md_autodetect_dev(part_to_dev(part)->devt);
Christoph Hellwig74cc979c2020-03-24 08:25:19 +0100560
Christoph Hellwigf902b022019-11-14 15:34:32 +0100561 return true;
562}
563
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100564int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
Christoph Hellwigf902b022019-11-14 15:34:32 +0100565{
566 struct parsed_partitions *state;
567 int ret = -EAGAIN, p, highest;
568
Christoph Hellwig142fe8f2019-11-14 15:34:35 +0100569 if (!disk_part_scan_enabled(disk))
570 return 0;
571
Christoph Hellwigf902b022019-11-14 15:34:32 +0100572 state = check_partition(disk, bdev);
573 if (!state)
Al Viro94ea4152011-09-16 00:45:36 -0400574 return 0;
575 if (IS_ERR(state)) {
576 /*
Christoph Hellwigf902b022019-11-14 15:34:32 +0100577 * I/O error reading the partition table. If we tried to read
578 * beyond EOD, retry after unlocking the native capacity.
Al Viro94ea4152011-09-16 00:45:36 -0400579 */
580 if (PTR_ERR(state) == -ENOSPC) {
581 printk(KERN_WARNING "%s: partition table beyond EOD, ",
582 disk->disk_name);
583 if (disk_unlock_native_capacity(disk))
Christoph Hellwigf902b022019-11-14 15:34:32 +0100584 return -EAGAIN;
Al Viro94ea4152011-09-16 00:45:36 -0400585 }
586 return -EIO;
587 }
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900588
Christoph Hellwigf902b022019-11-14 15:34:32 +0100589 /*
Christoph Hellwigb7205302020-01-26 14:05:43 +0100590 * Partitions are not supported on host managed zoned block devices.
Christoph Hellwigf902b022019-11-14 15:34:32 +0100591 */
Christoph Hellwigb7205302020-01-26 14:05:43 +0100592 if (disk->queue->limits.zoned == BLK_ZONED_HM) {
593 pr_warn("%s: ignoring partition table on host managed zoned block device\n",
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900594 disk->disk_name);
Christoph Hellwigf902b022019-11-14 15:34:32 +0100595 ret = 0;
596 goto out_free_state;
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900597 }
598
Al Viro94ea4152011-09-16 00:45:36 -0400599 /*
Christoph Hellwigf902b022019-11-14 15:34:32 +0100600 * If we read beyond EOD, try unlocking native capacity even if the
601 * partition table was successfully read as we could be missing some
602 * partitions.
Al Viro94ea4152011-09-16 00:45:36 -0400603 */
604 if (state->access_beyond_eod) {
605 printk(KERN_WARNING
606 "%s: partition table partially beyond EOD, ",
607 disk->disk_name);
608 if (disk_unlock_native_capacity(disk))
Christoph Hellwigf902b022019-11-14 15:34:32 +0100609 goto out_free_state;
Al Viro94ea4152011-09-16 00:45:36 -0400610 }
611
612 /* tell userspace that the media / partition table may have changed */
613 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
614
Christoph Hellwigf902b022019-11-14 15:34:32 +0100615 /*
616 * Detect the highest partition number and preallocate disk->part_tbl.
617 * This is an optimization and not strictly necessary.
Al Viro94ea4152011-09-16 00:45:36 -0400618 */
619 for (p = 1, highest = 0; p < state->limit; p++)
620 if (state->parts[p].size)
621 highest = p;
Al Viro94ea4152011-09-16 00:45:36 -0400622 disk_expand_part_tbl(disk, highest);
623
Christoph Hellwigf902b022019-11-14 15:34:32 +0100624 for (p = 1; p < state->limit; p++)
625 if (!blk_add_partition(disk, bdev, state, p))
626 goto out_free_state;
Al Viro94ea4152011-09-16 00:45:36 -0400627
Christoph Hellwigf902b022019-11-14 15:34:32 +0100628 ret = 0;
629out_free_state:
Ming Leiac2e5322013-02-27 17:05:19 -0800630 free_partitions(state);
Christoph Hellwigf902b022019-11-14 15:34:32 +0100631 return ret;
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +0100632}
633
Christoph Hellwig1a9fba32020-03-24 08:25:18 +0100634void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
Dan Williamsd1a5f2b42016-01-28 20:25:31 -0800635{
Christoph Hellwig1a9fba32020-03-24 08:25:18 +0100636 struct address_space *mapping = state->bdev->bd_inode->i_mapping;
Al Viro94ea4152011-09-16 00:45:36 -0400637 struct page *page;
638
Christoph Hellwig1a9fba32020-03-24 08:25:18 +0100639 if (n >= get_capacity(state->bdev->bd_disk)) {
640 state->access_beyond_eod = true;
641 return NULL;
Al Viro94ea4152011-09-16 00:45:36 -0400642 }
Christoph Hellwig1a9fba32020-03-24 08:25:18 +0100643
644 page = read_mapping_page(mapping,
645 (pgoff_t)(n >> (PAGE_SHIFT - 9)), NULL);
646 if (IS_ERR(page))
647 goto out;
648 if (PageError(page))
649 goto out_put_page;
650
651 p->v = page;
652 return (unsigned char *)page_address(page) +
653 ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << SECTOR_SHIFT);
654out_put_page:
655 put_page(page);
656out:
Al Viro94ea4152011-09-16 00:45:36 -0400657 p->v = NULL;
658 return NULL;
659}