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