block: simplify partition_overlaps
[linux-2.6-microblaze.git] / block / partitions / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1991-1998  Linus Torvalds
4  * Re-organised Feb 1998 Russell King
5  * Copyright (C) 2020 Christoph Hellwig
6  */
7 #include <linux/fs.h>
8 #include <linux/slab.h>
9 #include <linux/ctype.h>
10 #include <linux/genhd.h>
11 #include <linux/vmalloc.h>
12 #include <linux/blktrace_api.h>
13 #include <linux/raid/detect.h>
14 #include "check.h"
15
16 static 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
89 static void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
90 {
91         spin_lock(&bdev->bd_size_lock);
92         i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
93         spin_unlock(&bdev->bd_size_lock);
94 }
95
96 static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
97 {
98         struct parsed_partitions *state;
99         int nr;
100
101         state = kzalloc(sizeof(*state), GFP_KERNEL);
102         if (!state)
103                 return NULL;
104
105         nr = disk_max_parts(hd);
106         state->parts = vzalloc(array_size(nr, sizeof(state->parts[0])));
107         if (!state->parts) {
108                 kfree(state);
109                 return NULL;
110         }
111
112         state->limit = nr;
113
114         return state;
115 }
116
117 static void free_partitions(struct parsed_partitions *state)
118 {
119         vfree(state->parts);
120         kfree(state);
121 }
122
123 static struct parsed_partitions *check_partition(struct gendisk *hd,
124                 struct block_device *bdev)
125 {
126         struct parsed_partitions *state;
127         int i, res, err;
128
129         state = allocate_partitions(hd);
130         if (!state)
131                 return NULL;
132         state->pp_buf = (char *)__get_free_page(GFP_KERNEL);
133         if (!state->pp_buf) {
134                 free_partitions(state);
135                 return NULL;
136         }
137         state->pp_buf[0] = '\0';
138
139         state->bdev = bdev;
140         disk_name(hd, 0, state->name);
141         snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
142         if (isdigit(state->name[strlen(state->name)-1]))
143                 sprintf(state->name, "p");
144
145         i = res = err = 0;
146         while (!res && check_part[i]) {
147                 memset(state->parts, 0, state->limit * sizeof(state->parts[0]));
148                 res = check_part[i++](state);
149                 if (res < 0) {
150                         /*
151                          * We have hit an I/O error which we don't report now.
152                          * But record it, and let the others do their job.
153                          */
154                         err = res;
155                         res = 0;
156                 }
157
158         }
159         if (res > 0) {
160                 printk(KERN_INFO "%s", state->pp_buf);
161
162                 free_page((unsigned long)state->pp_buf);
163                 return state;
164         }
165         if (state->access_beyond_eod)
166                 err = -ENOSPC;
167         /*
168          * The partition is unrecognized. So report I/O errors if there were any
169          */
170         if (err)
171                 res = err;
172         if (res) {
173                 strlcat(state->pp_buf,
174                         " unable to read partition table\n", PAGE_SIZE);
175                 printk(KERN_INFO "%s", state->pp_buf);
176         }
177
178         free_page((unsigned long)state->pp_buf);
179         free_partitions(state);
180         return ERR_PTR(res);
181 }
182
183 static ssize_t part_partition_show(struct device *dev,
184                                    struct device_attribute *attr, char *buf)
185 {
186         return sprintf(buf, "%d\n", dev_to_bdev(dev)->bd_partno);
187 }
188
189 static ssize_t part_start_show(struct device *dev,
190                                struct device_attribute *attr, char *buf)
191 {
192         return sprintf(buf, "%llu\n", dev_to_bdev(dev)->bd_start_sect);
193 }
194
195 static ssize_t part_ro_show(struct device *dev,
196                             struct device_attribute *attr, char *buf)
197 {
198         return sprintf(buf, "%d\n", bdev_read_only(dev_to_bdev(dev)));
199 }
200
201 static ssize_t part_alignment_offset_show(struct device *dev,
202                                           struct device_attribute *attr, char *buf)
203 {
204         struct block_device *bdev = dev_to_bdev(dev);
205
206         return sprintf(buf, "%u\n",
207                 queue_limit_alignment_offset(&bdev->bd_disk->queue->limits,
208                                 bdev->bd_start_sect));
209 }
210
211 static ssize_t part_discard_alignment_show(struct device *dev,
212                                            struct device_attribute *attr, char *buf)
213 {
214         struct block_device *bdev = dev_to_bdev(dev);
215
216         return sprintf(buf, "%u\n",
217                 queue_limit_discard_alignment(&bdev->bd_disk->queue->limits,
218                                 bdev->bd_start_sect));
219 }
220
221 static DEVICE_ATTR(partition, 0444, part_partition_show, NULL);
222 static DEVICE_ATTR(start, 0444, part_start_show, NULL);
223 static DEVICE_ATTR(size, 0444, part_size_show, NULL);
224 static DEVICE_ATTR(ro, 0444, part_ro_show, NULL);
225 static DEVICE_ATTR(alignment_offset, 0444, part_alignment_offset_show, NULL);
226 static DEVICE_ATTR(discard_alignment, 0444, part_discard_alignment_show, NULL);
227 static DEVICE_ATTR(stat, 0444, part_stat_show, NULL);
228 static DEVICE_ATTR(inflight, 0444, part_inflight_show, NULL);
229 #ifdef CONFIG_FAIL_MAKE_REQUEST
230 static struct device_attribute dev_attr_fail =
231         __ATTR(make-it-fail, 0644, part_fail_show, part_fail_store);
232 #endif
233
234 static struct attribute *part_attrs[] = {
235         &dev_attr_partition.attr,
236         &dev_attr_start.attr,
237         &dev_attr_size.attr,
238         &dev_attr_ro.attr,
239         &dev_attr_alignment_offset.attr,
240         &dev_attr_discard_alignment.attr,
241         &dev_attr_stat.attr,
242         &dev_attr_inflight.attr,
243 #ifdef CONFIG_FAIL_MAKE_REQUEST
244         &dev_attr_fail.attr,
245 #endif
246         NULL
247 };
248
249 static struct attribute_group part_attr_group = {
250         .attrs = part_attrs,
251 };
252
253 static const struct attribute_group *part_attr_groups[] = {
254         &part_attr_group,
255 #ifdef CONFIG_BLK_DEV_IO_TRACE
256         &blk_trace_attr_group,
257 #endif
258         NULL
259 };
260
261 static void part_release(struct device *dev)
262 {
263         blk_free_devt(dev->devt);
264         bdput(dev_to_bdev(dev));
265 }
266
267 static int part_uevent(struct device *dev, struct kobj_uevent_env *env)
268 {
269         struct block_device *part = dev_to_bdev(dev);
270
271         add_uevent_var(env, "PARTN=%u", part->bd_partno);
272         if (part->bd_meta_info && part->bd_meta_info->volname[0])
273                 add_uevent_var(env, "PARTNAME=%s", part->bd_meta_info->volname);
274         return 0;
275 }
276
277 struct device_type part_type = {
278         .name           = "partition",
279         .groups         = part_attr_groups,
280         .release        = part_release,
281         .uevent         = part_uevent,
282 };
283
284 /*
285  * Must be called either with bd_mutex held, before a disk can be opened or
286  * after all disk users are gone.
287  */
288 static void delete_partition(struct block_device *part)
289 {
290         fsync_bdev(part);
291         __invalidate_device(part, true);
292
293         xa_erase(&part->bd_disk->part_tbl, part->bd_partno);
294         kobject_put(part->bd_holder_dir);
295         device_del(&part->bd_device);
296
297         /*
298          * Remove the block device from the inode hash, so that it cannot be
299          * looked up any more even when openers still hold references.
300          */
301         remove_inode_hash(part->bd_inode);
302
303         put_device(&part->bd_device);
304 }
305
306 static ssize_t whole_disk_show(struct device *dev,
307                                struct device_attribute *attr, char *buf)
308 {
309         return 0;
310 }
311 static DEVICE_ATTR(whole_disk, 0444, whole_disk_show, NULL);
312
313 /*
314  * Must be called either with bd_mutex held, before a disk can be opened or
315  * after all disk users are gone.
316  */
317 static struct block_device *add_partition(struct gendisk *disk, int partno,
318                                 sector_t start, sector_t len, int flags,
319                                 struct partition_meta_info *info)
320 {
321         dev_t devt = MKDEV(0, 0);
322         struct device *ddev = disk_to_dev(disk);
323         struct device *pdev;
324         struct block_device *bdev;
325         const char *dname;
326         int err;
327
328         /*
329          * Partitions are not supported on zoned block devices that are used as
330          * such.
331          */
332         switch (disk->queue->limits.zoned) {
333         case BLK_ZONED_HM:
334                 pr_warn("%s: partitions not supported on host managed zoned block device\n",
335                         disk->disk_name);
336                 return ERR_PTR(-ENXIO);
337         case BLK_ZONED_HA:
338                 pr_info("%s: disabling host aware zoned block device support due to partitions\n",
339                         disk->disk_name);
340                 blk_queue_set_zoned(disk, BLK_ZONED_NONE);
341                 break;
342         case BLK_ZONED_NONE:
343                 break;
344         }
345
346         if (xa_load(&disk->part_tbl, partno))
347                 return ERR_PTR(-EBUSY);
348
349         bdev = bdev_alloc(disk, partno);
350         if (!bdev)
351                 return ERR_PTR(-ENOMEM);
352
353         bdev->bd_start_sect = start;
354         bdev_set_nr_sectors(bdev, len);
355
356         if (info) {
357                 err = -ENOMEM;
358                 bdev->bd_meta_info = kmemdup(info, sizeof(*info), GFP_KERNEL);
359                 if (!bdev->bd_meta_info)
360                         goto out_bdput;
361         }
362
363         pdev = &bdev->bd_device;
364         dname = dev_name(ddev);
365         if (isdigit(dname[strlen(dname) - 1]))
366                 dev_set_name(pdev, "%sp%d", dname, partno);
367         else
368                 dev_set_name(pdev, "%s%d", dname, partno);
369
370         device_initialize(pdev);
371         pdev->class = &block_class;
372         pdev->type = &part_type;
373         pdev->parent = ddev;
374
375         err = blk_alloc_devt(bdev, &devt);
376         if (err)
377                 goto out_put;
378         pdev->devt = devt;
379
380         /* delay uevent until 'holders' subdir is created */
381         dev_set_uevent_suppress(pdev, 1);
382         err = device_add(pdev);
383         if (err)
384                 goto out_put;
385
386         err = -ENOMEM;
387         bdev->bd_holder_dir = kobject_create_and_add("holders", &pdev->kobj);
388         if (!bdev->bd_holder_dir)
389                 goto out_del;
390
391         dev_set_uevent_suppress(pdev, 0);
392         if (flags & ADDPART_FLAG_WHOLEDISK) {
393                 err = device_create_file(pdev, &dev_attr_whole_disk);
394                 if (err)
395                         goto out_del;
396         }
397
398         /* everything is up and running, commence */
399         err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL);
400         if (err)
401                 goto out_del;
402         bdev_add(bdev, devt);
403
404         /* suppress uevent if the disk suppresses it */
405         if (!dev_get_uevent_suppress(ddev))
406                 kobject_uevent(&pdev->kobj, KOBJ_ADD);
407         return bdev;
408
409 out_bdput:
410         bdput(bdev);
411         return ERR_PTR(err);
412 out_del:
413         kobject_put(bdev->bd_holder_dir);
414         device_del(pdev);
415 out_put:
416         put_device(pdev);
417         return ERR_PTR(err);
418 }
419
420 static bool partition_overlaps(struct gendisk *disk, sector_t start,
421                 sector_t length, int skip_partno)
422 {
423         struct block_device *part;
424         bool overlap = false;
425         unsigned long idx;
426
427         rcu_read_lock();
428         xa_for_each_start(&disk->part_tbl, idx, part, 1) {
429                 if (part->bd_partno != skip_partno &&
430                     start < part->bd_start_sect + bdev_nr_sectors(part) &&
431                     start + length > part->bd_start_sect) {
432                         overlap = true;
433                         break;
434                 }
435         }
436         rcu_read_unlock();
437
438         return overlap;
439 }
440
441 int bdev_add_partition(struct block_device *bdev, int partno,
442                 sector_t start, sector_t length)
443 {
444         struct block_device *part;
445
446         mutex_lock(&bdev->bd_mutex);
447         if (partition_overlaps(bdev->bd_disk, start, length, -1)) {
448                 mutex_unlock(&bdev->bd_mutex);
449                 return -EBUSY;
450         }
451
452         part = add_partition(bdev->bd_disk, partno, start, length,
453                         ADDPART_FLAG_NONE, NULL);
454         mutex_unlock(&bdev->bd_mutex);
455         return PTR_ERR_OR_ZERO(part);
456 }
457
458 int bdev_del_partition(struct block_device *bdev, int partno)
459 {
460         struct block_device *part;
461         int ret;
462
463         part = bdget_disk(bdev->bd_disk, partno);
464         if (!part)
465                 return -ENXIO;
466
467         mutex_lock(&part->bd_mutex);
468         mutex_lock_nested(&bdev->bd_mutex, 1);
469
470         ret = -EBUSY;
471         if (part->bd_openers)
472                 goto out_unlock;
473
474         delete_partition(part);
475         ret = 0;
476 out_unlock:
477         mutex_unlock(&bdev->bd_mutex);
478         mutex_unlock(&part->bd_mutex);
479         bdput(part);
480         return ret;
481 }
482
483 int bdev_resize_partition(struct block_device *bdev, int partno,
484                 sector_t start, sector_t length)
485 {
486         struct block_device *part;
487         int ret = 0;
488
489         part = bdget_disk(bdev->bd_disk, partno);
490         if (!part)
491                 return -ENXIO;
492
493         mutex_lock(&part->bd_mutex);
494         mutex_lock_nested(&bdev->bd_mutex, 1);
495         ret = -EINVAL;
496         if (start != part->bd_start_sect)
497                 goto out_unlock;
498
499         ret = -EBUSY;
500         if (partition_overlaps(bdev->bd_disk, start, length, partno))
501                 goto out_unlock;
502
503         bdev_set_nr_sectors(part, length);
504
505         ret = 0;
506 out_unlock:
507         mutex_unlock(&part->bd_mutex);
508         mutex_unlock(&bdev->bd_mutex);
509         bdput(part);
510         return ret;
511 }
512
513 static bool disk_unlock_native_capacity(struct gendisk *disk)
514 {
515         const struct block_device_operations *bdops = disk->fops;
516
517         if (bdops->unlock_native_capacity &&
518             !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
519                 printk(KERN_CONT "enabling native capacity\n");
520                 bdops->unlock_native_capacity(disk);
521                 disk->flags |= GENHD_FL_NATIVE_CAPACITY;
522                 return true;
523         } else {
524                 printk(KERN_CONT "truncated\n");
525                 return false;
526         }
527 }
528
529 void blk_drop_partitions(struct gendisk *disk)
530 {
531         struct block_device *part;
532         unsigned long idx;
533
534         lockdep_assert_held(&disk->part0->bd_mutex);
535
536         xa_for_each_start(&disk->part_tbl, idx, part, 1) {
537                 if (!bdgrab(part))
538                         continue;
539                 delete_partition(part);
540                 bdput(part);
541         }
542 }
543
544 static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
545                 struct parsed_partitions *state, int p)
546 {
547         sector_t size = state->parts[p].size;
548         sector_t from = state->parts[p].from;
549         struct block_device *part;
550
551         if (!size)
552                 return true;
553
554         if (from >= get_capacity(disk)) {
555                 printk(KERN_WARNING
556                        "%s: p%d start %llu is beyond EOD, ",
557                        disk->disk_name, p, (unsigned long long) from);
558                 if (disk_unlock_native_capacity(disk))
559                         return false;
560                 return true;
561         }
562
563         if (from + size > get_capacity(disk)) {
564                 printk(KERN_WARNING
565                        "%s: p%d size %llu extends beyond EOD, ",
566                        disk->disk_name, p, (unsigned long long) size);
567
568                 if (disk_unlock_native_capacity(disk))
569                         return false;
570
571                 /*
572                  * We can not ignore partitions of broken tables created by for
573                  * example camera firmware, but we limit them to the end of the
574                  * disk to avoid creating invalid block devices.
575                  */
576                 size = get_capacity(disk) - from;
577         }
578
579         part = add_partition(disk, p, from, size, state->parts[p].flags,
580                              &state->parts[p].info);
581         if (IS_ERR(part) && PTR_ERR(part) != -ENXIO) {
582                 printk(KERN_ERR " %s: p%d could not be added: %ld\n",
583                        disk->disk_name, p, -PTR_ERR(part));
584                 return true;
585         }
586
587         if (IS_BUILTIN(CONFIG_BLK_DEV_MD) &&
588             (state->parts[p].flags & ADDPART_FLAG_RAID))
589                 md_autodetect_dev(part->bd_dev);
590
591         return true;
592 }
593
594 int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)
595 {
596         struct parsed_partitions *state;
597         int ret = -EAGAIN, p;
598
599         if (!disk_part_scan_enabled(disk))
600                 return 0;
601
602         state = check_partition(disk, bdev);
603         if (!state)
604                 return 0;
605         if (IS_ERR(state)) {
606                 /*
607                  * I/O error reading the partition table.  If we tried to read
608                  * beyond EOD, retry after unlocking the native capacity.
609                  */
610                 if (PTR_ERR(state) == -ENOSPC) {
611                         printk(KERN_WARNING "%s: partition table beyond EOD, ",
612                                disk->disk_name);
613                         if (disk_unlock_native_capacity(disk))
614                                 return -EAGAIN;
615                 }
616                 return -EIO;
617         }
618
619         /*
620          * Partitions are not supported on host managed zoned block devices.
621          */
622         if (disk->queue->limits.zoned == BLK_ZONED_HM) {
623                 pr_warn("%s: ignoring partition table on host managed zoned block device\n",
624                         disk->disk_name);
625                 ret = 0;
626                 goto out_free_state;
627         }
628
629         /*
630          * If we read beyond EOD, try unlocking native capacity even if the
631          * partition table was successfully read as we could be missing some
632          * partitions.
633          */
634         if (state->access_beyond_eod) {
635                 printk(KERN_WARNING
636                        "%s: partition table partially beyond EOD, ",
637                        disk->disk_name);
638                 if (disk_unlock_native_capacity(disk))
639                         goto out_free_state;
640         }
641
642         /* tell userspace that the media / partition table may have changed */
643         kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
644
645         for (p = 1; p < state->limit; p++)
646                 if (!blk_add_partition(disk, bdev, state, p))
647                         goto out_free_state;
648
649         ret = 0;
650 out_free_state:
651         free_partitions(state);
652         return ret;
653 }
654
655 void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
656 {
657         struct address_space *mapping = state->bdev->bd_inode->i_mapping;
658         struct page *page;
659
660         if (n >= get_capacity(state->bdev->bd_disk)) {
661                 state->access_beyond_eod = true;
662                 return NULL;
663         }
664
665         page = read_mapping_page(mapping,
666                         (pgoff_t)(n >> (PAGE_SHIFT - 9)), NULL);
667         if (IS_ERR(page))
668                 goto out;
669         if (PageError(page))
670                 goto out_put_page;
671
672         p->v = page;
673         return (unsigned char *)page_address(page) +
674                         ((n & ((1 << (PAGE_SHIFT - 9)) - 1)) << SECTOR_SHIFT);
675 out_put_page:
676         put_page(page);
677 out:
678         p->v = NULL;
679         return NULL;
680 }