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