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