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