xref: /openbmc/linux/block/partitions/core.c (revision dfc53baa)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1991-1998  Linus Torvalds
4  * Re-organised Feb 1998 Russell King
5  */
6 #include <linux/fs.h>
7 #include <linux/slab.h>
8 #include <linux/ctype.h>
9 #include <linux/genhd.h>
10 #include <linux/vmalloc.h>
11 #include <linux/blktrace_api.h>
12 #include <linux/raid/detect.h>
13 #include "check.h"
14 
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 }
174 
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 
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 
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);
220 #ifdef CONFIG_FAIL_MAKE_REQUEST
221 static struct device_attribute dev_attr_fail =
222 	__ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
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);
255 	blk_free_devt(dev->devt);
256 	hd_free_part(p);
257 	kfree(p);
258 }
259 
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 
270 struct device_type part_type = {
271 	.name		= "partition",
272 	.groups		= part_attr_groups,
273 	.release	= part_release,
274 	.uevent		= part_uevent,
275 };
276 
277 static void hd_struct_free_work(struct work_struct *work)
278 {
279 	struct hd_struct *part =
280 		container_of(to_rcu_work(work), struct hd_struct, rcu_work);
281 	struct gendisk *disk = part_to_disk(part);
282 
283 	/*
284 	 * Release the disk reference acquired in delete_partition here.
285 	 * We can't release it in hd_struct_free because the final put_device
286 	 * needs process context and thus can't be run directly from a
287 	 * percpu_ref ->release handler.
288 	 */
289 	put_device(disk_to_dev(disk));
290 
291 	part->start_sect = 0;
292 	part->nr_sects = 0;
293 	part_stat_set_all(part, 0);
294 	put_device(part_to_dev(part));
295 }
296 
297 static void hd_struct_free(struct percpu_ref *ref)
298 {
299 	struct hd_struct *part = container_of(ref, struct hd_struct, ref);
300 	struct gendisk *disk = part_to_disk(part);
301 	struct disk_part_tbl *ptbl =
302 		rcu_dereference_protected(disk->part_tbl, 1);
303 
304 	rcu_assign_pointer(ptbl->last_lookup, NULL);
305 
306 	INIT_RCU_WORK(&part->rcu_work, hd_struct_free_work);
307 	queue_rcu_work(system_wq, &part->rcu_work);
308 }
309 
310 int hd_ref_init(struct hd_struct *part)
311 {
312 	if (percpu_ref_init(&part->ref, hd_struct_free, 0, GFP_KERNEL))
313 		return -ENOMEM;
314 	return 0;
315 }
316 
317 /*
318  * Must be called either with bd_mutex held, before a disk can be opened or
319  * after all disk users are gone.
320  */
321 void delete_partition(struct gendisk *disk, struct hd_struct *part)
322 {
323 	struct disk_part_tbl *ptbl =
324 		rcu_dereference_protected(disk->part_tbl, 1);
325 
326 	/*
327 	 * ->part_tbl is referenced in this part's release handler, so
328 	 *  we have to hold the disk device
329 	 */
330 	get_device(disk_to_dev(part_to_disk(part)));
331 	rcu_assign_pointer(ptbl->part[part->partno], NULL);
332 	kobject_put(part->holder_dir);
333 	device_del(part_to_dev(part));
334 
335 	/*
336 	 * Remove gendisk pointer from idr so that it cannot be looked up
337 	 * while RCU period before freeing gendisk is running to prevent
338 	 * use-after-free issues. Note that the device number stays
339 	 * "in-use" until we really free the gendisk.
340 	 */
341 	blk_invalidate_devt(part_devt(part));
342 	percpu_ref_kill(&part->ref);
343 }
344 
345 static ssize_t whole_disk_show(struct device *dev,
346 			       struct device_attribute *attr, char *buf)
347 {
348 	return 0;
349 }
350 static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
351 
352 /*
353  * Must be called either with bd_mutex held, before a disk can be opened or
354  * after all disk users are gone.
355  */
356 static struct hd_struct *add_partition(struct gendisk *disk, int partno,
357 				sector_t start, sector_t len, int flags,
358 				struct partition_meta_info *info)
359 {
360 	struct hd_struct *p;
361 	dev_t devt = MKDEV(0, 0);
362 	struct device *ddev = disk_to_dev(disk);
363 	struct device *pdev;
364 	struct disk_part_tbl *ptbl;
365 	const char *dname;
366 	int err;
367 
368 	/*
369 	 * Partitions are not supported on zoned block devices that are used as
370 	 * such.
371 	 */
372 	switch (disk->queue->limits.zoned) {
373 	case BLK_ZONED_HM:
374 		pr_warn("%s: partitions not supported on host managed zoned block device\n",
375 			disk->disk_name);
376 		return ERR_PTR(-ENXIO);
377 	case BLK_ZONED_HA:
378 		pr_info("%s: disabling host aware zoned block device support due to partitions\n",
379 			disk->disk_name);
380 		disk->queue->limits.zoned = BLK_ZONED_NONE;
381 		break;
382 	case BLK_ZONED_NONE:
383 		break;
384 	}
385 
386 	err = disk_expand_part_tbl(disk, partno);
387 	if (err)
388 		return ERR_PTR(err);
389 	ptbl = rcu_dereference_protected(disk->part_tbl, 1);
390 
391 	if (ptbl->part[partno])
392 		return ERR_PTR(-EBUSY);
393 
394 	p = kzalloc(sizeof(*p), GFP_KERNEL);
395 	if (!p)
396 		return ERR_PTR(-EBUSY);
397 
398 	p->dkstats = alloc_percpu(struct disk_stats);
399 	if (!p->dkstats) {
400 		err = -ENOMEM;
401 		goto out_free;
402 	}
403 
404 	hd_sects_seq_init(p);
405 	pdev = part_to_dev(p);
406 
407 	p->start_sect = start;
408 	p->alignment_offset =
409 		queue_limit_alignment_offset(&disk->queue->limits, start);
410 	p->discard_alignment =
411 		queue_limit_discard_alignment(&disk->queue->limits, start);
412 	p->nr_sects = len;
413 	p->partno = partno;
414 	p->policy = get_disk_ro(disk);
415 
416 	if (info) {
417 		struct partition_meta_info *pinfo;
418 
419 		pinfo = kzalloc_node(sizeof(*pinfo), GFP_KERNEL, disk->node_id);
420 		if (!pinfo) {
421 			err = -ENOMEM;
422 			goto out_free_stats;
423 		}
424 		memcpy(pinfo, info, sizeof(*info));
425 		p->info = pinfo;
426 	}
427 
428 	dname = dev_name(ddev);
429 	if (isdigit(dname[strlen(dname) - 1]))
430 		dev_set_name(pdev, "%sp%d", dname, partno);
431 	else
432 		dev_set_name(pdev, "%s%d", dname, partno);
433 
434 	device_initialize(pdev);
435 	pdev->class = &block_class;
436 	pdev->type = &part_type;
437 	pdev->parent = ddev;
438 
439 	err = blk_alloc_devt(p, &devt);
440 	if (err)
441 		goto out_free_info;
442 	pdev->devt = devt;
443 
444 	/* delay uevent until 'holders' subdir is created */
445 	dev_set_uevent_suppress(pdev, 1);
446 	err = device_add(pdev);
447 	if (err)
448 		goto out_put;
449 
450 	err = -ENOMEM;
451 	p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
452 	if (!p->holder_dir)
453 		goto out_del;
454 
455 	dev_set_uevent_suppress(pdev, 0);
456 	if (flags & ADDPART_FLAG_WHOLEDISK) {
457 		err = device_create_file(pdev, &dev_attr_whole_disk);
458 		if (err)
459 			goto out_del;
460 	}
461 
462 	err = hd_ref_init(p);
463 	if (err) {
464 		if (flags & ADDPART_FLAG_WHOLEDISK)
465 			goto out_remove_file;
466 		goto out_del;
467 	}
468 
469 	/* everything is up and running, commence */
470 	rcu_assign_pointer(ptbl->part[partno], p);
471 
472 	/* suppress uevent if the disk suppresses it */
473 	if (!dev_get_uevent_suppress(ddev))
474 		kobject_uevent(&pdev->kobj, KOBJ_ADD);
475 	return p;
476 
477 out_free_info:
478 	kfree(p->info);
479 out_free_stats:
480 	free_percpu(p->dkstats);
481 out_free:
482 	kfree(p);
483 	return ERR_PTR(err);
484 out_remove_file:
485 	device_remove_file(pdev, &dev_attr_whole_disk);
486 out_del:
487 	kobject_put(p->holder_dir);
488 	device_del(pdev);
489 out_put:
490 	put_device(pdev);
491 	return ERR_PTR(err);
492 }
493 
494 static bool partition_overlaps(struct gendisk *disk, sector_t start,
495 		sector_t length, int skip_partno)
496 {
497 	struct disk_part_iter piter;
498 	struct hd_struct *part;
499 	bool overlap = false;
500 
501 	disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
502 	while ((part = disk_part_iter_next(&piter))) {
503 		if (part->partno == skip_partno ||
504 		    start >= part->start_sect + part->nr_sects ||
505 		    start + length <= part->start_sect)
506 			continue;
507 		overlap = true;
508 		break;
509 	}
510 
511 	disk_part_iter_exit(&piter);
512 	return overlap;
513 }
514 
515 int bdev_add_partition(struct block_device *bdev, int partno,
516 		sector_t start, sector_t length)
517 {
518 	struct hd_struct *part;
519 
520 	mutex_lock(&bdev->bd_mutex);
521 	if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
522 		mutex_unlock(&bdev->bd_mutex);
523 		return -EBUSY;
524 	}
525 
526 	part = add_partition(bdev->bd_disk, partno, start, length,
527 			ADDPART_FLAG_NONE, NULL);
528 	mutex_unlock(&bdev->bd_mutex);
529 	return PTR_ERR_OR_ZERO(part);
530 }
531 
532 int bdev_del_partition(struct block_device *bdev, int partno)
533 {
534 	struct block_device *bdevp;
535 	struct hd_struct *part = NULL;
536 	int ret;
537 
538 	bdevp = bdget_disk(bdev->bd_disk, partno);
539 	if (!bdevp)
540 		return -ENXIO;
541 
542 	mutex_lock(&bdevp->bd_mutex);
543 	mutex_lock_nested(&bdev->bd_mutex, 1);
544 
545 	ret = -ENXIO;
546 	part = disk_get_part(bdev->bd_disk, partno);
547 	if (!part)
548 		goto out_unlock;
549 
550 	ret = -EBUSY;
551 	if (bdevp->bd_openers)
552 		goto out_unlock;
553 
554 	sync_blockdev(bdevp);
555 	invalidate_bdev(bdevp);
556 
557 	delete_partition(bdev->bd_disk, part);
558 	ret = 0;
559 out_unlock:
560 	mutex_unlock(&bdev->bd_mutex);
561 	mutex_unlock(&bdevp->bd_mutex);
562 	bdput(bdevp);
563 	if (part)
564 		disk_put_part(part);
565 	return ret;
566 }
567 
568 int bdev_resize_partition(struct block_device *bdev, int partno,
569 		sector_t start, sector_t length)
570 {
571 	struct block_device *bdevp;
572 	struct hd_struct *part;
573 	int ret = 0;
574 
575 	part = disk_get_part(bdev->bd_disk, partno);
576 	if (!part)
577 		return -ENXIO;
578 
579 	ret = -ENOMEM;
580 	bdevp = bdget(part_devt(part));
581 	if (!bdevp)
582 		goto out_put_part;
583 
584 	mutex_lock(&bdevp->bd_mutex);
585 	mutex_lock_nested(&bdev->bd_mutex, 1);
586 
587 	ret = -EINVAL;
588 	if (start != part->start_sect)
589 		goto out_unlock;
590 
591 	ret = -EBUSY;
592 	if (partition_overlaps(bdev->bd_disk, start, length, partno))
593 		goto out_unlock;
594 
595 	part_nr_sects_write(part, (sector_t)length);
596 	i_size_write(bdevp->bd_inode, length << SECTOR_SHIFT);
597 
598 	ret = 0;
599 out_unlock:
600 	mutex_unlock(&bdevp->bd_mutex);
601 	mutex_unlock(&bdev->bd_mutex);
602 	bdput(bdevp);
603 out_put_part:
604 	disk_put_part(part);
605 	return ret;
606 }
607 
608 static bool disk_unlock_native_capacity(struct gendisk *disk)
609 {
610 	const struct block_device_operations *bdops = disk->fops;
611 
612 	if (bdops->unlock_native_capacity &&
613 	    !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
614 		printk(KERN_CONT "enabling native capacity\n");
615 		bdops->unlock_native_capacity(disk);
616 		disk->flags |= GENHD_FL_NATIVE_CAPACITY;
617 		return true;
618 	} else {
619 		printk(KERN_CONT "truncated\n");
620 		return false;
621 	}
622 }
623 
624 int blk_drop_partitions(struct block_device *bdev)
625 {
626 	struct disk_part_iter piter;
627 	struct hd_struct *part;
628 
629 	if (bdev->bd_part_count)
630 		return -EBUSY;
631 
632 	sync_blockdev(bdev);
633 	invalidate_bdev(bdev);
634 
635 	disk_part_iter_init(&piter, bdev->bd_disk, DISK_PITER_INCL_EMPTY);
636 	while ((part = disk_part_iter_next(&piter)))
637 		delete_partition(bdev->bd_disk, part);
638 	disk_part_iter_exit(&piter);
639 
640 	return 0;
641 }
642 #ifdef CONFIG_S390
643 /* for historic reasons in the DASD driver */
644 EXPORT_SYMBOL_GPL(blk_drop_partitions);
645 #endif
646 
647 static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
648 		struct parsed_partitions *state, int p)
649 {
650 	sector_t size = state->parts[p].size;
651 	sector_t from = state->parts[p].from;
652 	struct hd_struct *part;
653 
654 	if (!size)
655 		return true;
656 
657 	if (from >= get_capacity(disk)) {
658 		printk(KERN_WARNING
659 		       "%s: p%d start %llu is beyond EOD, ",
660 		       disk->disk_name, p, (unsigned long long) from);
661 		if (disk_unlock_native_capacity(disk))
662 			return false;
663 		return true;
664 	}
665 
666 	if (from + size > get_capacity(disk)) {
667 		printk(KERN_WARNING
668 		       "%s: p%d size %llu extends beyond EOD, ",
669 		       disk->disk_name, p, (unsigned long long) size);
670 
671 		if (disk_unlock_native_capacity(disk))
672 			return false;
673 
674 		/*
675 		 * We can not ignore partitions of broken tables created by for
676 		 * example camera firmware, but we limit them to the end of the
677 		 * disk to avoid creating invalid block devices.
678 		 */
679 		size = get_capacity(disk) - from;
680 	}
681 
682 	part = add_partition(disk, p, from, size, state->parts[p].flags,
683 			     &state->parts[p].info);
684 	if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
685 		printk(KERN_ERR " %s: p%d could not be added: %ld\n",
686 		       disk->disk_name, p, -PTR_ERR(part));
687 		return true;
688 	}
689 
690 	if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
691 	    (state->parts[p].flags & ADDPART_FLAG_RAID))
692 		md_autodetect_dev(part_to_dev(part)->devt);
693 
694 	return true;
695 }
696 
697 int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
698 {
699 	struct parsed_partitions *state;
700 	int ret = -EAGAIN, p, highest;
701 
702 	if (!disk_part_scan_enabled(disk))
703 		return 0;
704 
705 	state = check_partition(disk, bdev);
706 	if (!state)
707 		return 0;
708 	if (IS_ERR(state)) {
709 		/*
710 		 * I/O error reading the partition table.  If we tried to read
711 		 * beyond EOD, retry after unlocking the native capacity.
712 		 */
713 		if (PTR_ERR(state) == -ENOSPC) {
714 			printk(KERN_WARNING "%s: partition table beyond EOD, ",
715 			       disk->disk_name);
716 			if (disk_unlock_native_capacity(disk))
717 				return -EAGAIN;
718 		}
719 		return -EIO;
720 	}
721 
722 	/*
723 	 * Partitions are not supported on host managed zoned block devices.
724 	 */
725 	if (disk->queue->limits.zoned == BLK_ZONED_HM) {
726 		pr_warn("%s: ignoring partition table on host managed zoned block device\n",
727 			disk->disk_name);
728 		ret = 0;
729 		goto out_free_state;
730 	}
731 
732 	/*
733 	 * If we read beyond EOD, try unlocking native capacity even if the
734 	 * partition table was successfully read as we could be missing some
735 	 * partitions.
736 	 */
737 	if (state->access_beyond_eod) {
738 		printk(KERN_WARNING
739 		       "%s: partition table partially beyond EOD, ",
740 		       disk->disk_name);
741 		if (disk_unlock_native_capacity(disk))
742 			goto out_free_state;
743 	}
744 
745 	/* tell userspace that the media / partition table may have changed */
746 	kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
747 
748 	/*
749 	 * Detect the highest partition number and preallocate disk->part_tbl.
750 	 * This is an optimization and not strictly necessary.
751 	 */
752 	for (p = 1, highest = 0; p < state->limit; p++)
753 		if (state->parts[p].size)
754 			highest = p;
755 	disk_expand_part_tbl(disk, highest);
756 
757 	for (p = 1; p < state->limit; p++)
758 		if (!blk_add_partition(disk, bdev, state, p))
759 			goto out_free_state;
760 
761 	ret = 0;
762 out_free_state:
763 	free_partitions(state);
764 	return ret;
765 }
766 
767 void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
768 {
769 	struct address_space *mapping = state->bdev->bd_inode->i_mapping;
770 	struct page *page;
771 
772 	if (n >= get_capacity(state->bdev->bd_disk)) {
773 		state->access_beyond_eod = true;
774 		return NULL;
775 	}
776 
777 	page = read_mapping_page(mapping,
778 			(pgoff_t)(n >> (PAGE_SHIFT - 9)), NULL);
779 	if (IS_ERR(page))
780 		goto out;
781 	if (PageError(page))
782 		goto out_put_page;
783 
784 	p->v = page;
785 	return (unsigned char *)page_address(page) +
786 			((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << SECTOR_SHIFT);
787 out_put_page:
788 	put_page(page);
789 out:
790 	p->v = NULL;
791 	return NULL;
792 }
793