Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * fs/partitions/check.c |
| 4 | * |
| 5 | * Code extracted from drivers/block/genhd.c |
| 6 | * Copyright (C) 1991-1998 Linus Torvalds |
| 7 | * Re-organised Feb 1998 Russell King |
| 8 | * |
| 9 | * We now have independent partition support from the |
| 10 | * block drivers, which allows all the partition code to |
| 11 | * be grouped in one location, and it to be mostly self |
| 12 | * contained. |
| 13 | * |
| 14 | * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl} |
| 15 | */ |
| 16 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/ctype.h> |
Jerome Marchand | 6f2576af | 2008-02-08 11:04:35 +0100 | [diff] [blame] | 20 | #include <linux/genhd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include "check.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "acorn.h" |
| 25 | #include "amiga.h" |
| 26 | #include "atari.h" |
| 27 | #include "ldm.h" |
| 28 | #include "mac.h" |
| 29 | #include "msdos.h" |
| 30 | #include "osf.h" |
| 31 | #include "sgi.h" |
| 32 | #include "sun.h" |
| 33 | #include "ibm.h" |
| 34 | #include "ultrix.h" |
| 35 | #include "efi.h" |
Bob Copeland | 0e6e1db | 2006-01-16 22:14:20 -0800 | [diff] [blame] | 36 | #include "karma.h" |
Philippe De Muyter | 19d0e8c | 2007-05-08 00:29:15 -0700 | [diff] [blame] | 37 | #include "sysv68.h" |
Cai Zhiyong | bab5541 | 2013-09-11 14:20:09 -0700 | [diff] [blame] | 38 | #include "cmdline.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/ |
| 41 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 42 | static int (*check_part[])(struct parsed_partitions *) = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Probe partition formats with tables at disk address 0 |
| 45 | * that also have an ADFS boot block at 0xdc0. |
| 46 | */ |
| 47 | #ifdef CONFIG_ACORN_PARTITION_ICS |
| 48 | adfspart_check_ICS, |
| 49 | #endif |
| 50 | #ifdef CONFIG_ACORN_PARTITION_POWERTEC |
| 51 | adfspart_check_POWERTEC, |
| 52 | #endif |
| 53 | #ifdef CONFIG_ACORN_PARTITION_EESOX |
| 54 | adfspart_check_EESOX, |
| 55 | #endif |
| 56 | |
| 57 | /* |
| 58 | * Now move on to formats that only have partition info at |
| 59 | * disk address 0xdc0. Since these may also have stale |
| 60 | * PC/BIOS partition tables, they need to come before |
| 61 | * the msdos entry. |
| 62 | */ |
| 63 | #ifdef CONFIG_ACORN_PARTITION_CUMANA |
| 64 | adfspart_check_CUMANA, |
| 65 | #endif |
| 66 | #ifdef CONFIG_ACORN_PARTITION_ADFS |
| 67 | adfspart_check_ADFS, |
| 68 | #endif |
| 69 | |
Cai Zhiyong | bab5541 | 2013-09-11 14:20:09 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_CMDLINE_PARTITION |
| 71 | cmdline_partition, |
| 72 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #ifdef CONFIG_EFI_PARTITION |
| 74 | efi_partition, /* this must come before msdos */ |
| 75 | #endif |
| 76 | #ifdef CONFIG_SGI_PARTITION |
| 77 | sgi_partition, |
| 78 | #endif |
| 79 | #ifdef CONFIG_LDM_PARTITION |
| 80 | ldm_partition, /* this must come before msdos */ |
| 81 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_MSDOS_PARTITION |
| 83 | msdos_partition, |
| 84 | #endif |
| 85 | #ifdef CONFIG_OSF_PARTITION |
| 86 | osf_partition, |
| 87 | #endif |
| 88 | #ifdef CONFIG_SUN_PARTITION |
| 89 | sun_partition, |
| 90 | #endif |
| 91 | #ifdef CONFIG_AMIGA_PARTITION |
| 92 | amiga_partition, |
| 93 | #endif |
| 94 | #ifdef CONFIG_ATARI_PARTITION |
| 95 | atari_partition, |
| 96 | #endif |
| 97 | #ifdef CONFIG_MAC_PARTITION |
| 98 | mac_partition, |
| 99 | #endif |
| 100 | #ifdef CONFIG_ULTRIX_PARTITION |
| 101 | ultrix_partition, |
| 102 | #endif |
| 103 | #ifdef CONFIG_IBM_PARTITION |
| 104 | ibm_partition, |
| 105 | #endif |
Bob Copeland | 0e6e1db | 2006-01-16 22:14:20 -0800 | [diff] [blame] | 106 | #ifdef CONFIG_KARMA_PARTITION |
| 107 | karma_partition, |
| 108 | #endif |
Philippe De Muyter | 19d0e8c | 2007-05-08 00:29:15 -0700 | [diff] [blame] | 109 | #ifdef CONFIG_SYSV68_PARTITION |
| 110 | sysv68_partition, |
| 111 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | NULL |
| 113 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 115 | static struct parsed_partitions *allocate_partitions(struct gendisk *hd) |
| 116 | { |
| 117 | struct parsed_partitions *state; |
| 118 | int nr; |
| 119 | |
| 120 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
| 121 | if (!state) |
| 122 | return NULL; |
| 123 | |
| 124 | nr = disk_max_parts(hd); |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 125 | state->parts = vzalloc(array_size(nr, sizeof(state->parts[0]))); |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 126 | if (!state->parts) { |
| 127 | kfree(state); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | state->limit = nr; |
| 132 | |
| 133 | return state; |
| 134 | } |
| 135 | |
| 136 | void free_partitions(struct parsed_partitions *state) |
| 137 | { |
| 138 | vfree(state->parts); |
| 139 | kfree(state); |
| 140 | } |
| 141 | |
Al Viro | 94ea415 | 2011-09-16 00:45:36 -0400 | [diff] [blame] | 142 | struct parsed_partitions * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | check_partition(struct gendisk *hd, struct block_device *bdev) |
| 144 | { |
| 145 | struct parsed_partitions *state; |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 146 | int i, res, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 148 | state = allocate_partitions(hd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (!state) |
| 150 | return NULL; |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 151 | state->pp_buf = (char *)__get_free_page(GFP_KERNEL); |
| 152 | if (!state->pp_buf) { |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 153 | free_partitions(state); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | state->pp_buf[0] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 158 | state->bdev = bdev; |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 159 | disk_name(hd, 0, state->name); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 160 | snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name); |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 161 | if (isdigit(state->name[strlen(state->name)-1])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | sprintf(state->name, "p"); |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 163 | |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 164 | i = res = err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | while (!res && check_part[i]) { |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 166 | memset(state->parts, 0, state->limit * sizeof(state->parts[0])); |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 167 | res = check_part[i++](state); |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 168 | if (res < 0) { |
| 169 | /* We have hit an I/O error which we don't report now. |
| 170 | * But record it, and let the others do their job. |
| 171 | */ |
| 172 | err = res; |
| 173 | res = 0; |
| 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 177 | if (res > 0) { |
| 178 | printk(KERN_INFO "%s", state->pp_buf); |
| 179 | |
| 180 | free_page((unsigned long)state->pp_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return state; |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 182 | } |
Tejun Heo | b403a98 | 2010-05-15 20:09:31 +0200 | [diff] [blame] | 183 | if (state->access_beyond_eod) |
| 184 | err = -ENOSPC; |
suzuki | 9bebff6 | 2007-03-07 20:41:24 -0800 | [diff] [blame] | 185 | if (err) |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 186 | /* The partition is unrecognized. So report I/O errors if there were any */ |
| 187 | res = err; |
Boaz Harrosh | bb5c3cd | 2015-01-22 14:39:20 +0200 | [diff] [blame] | 188 | if (res) { |
| 189 | if (warn_no_part) |
| 190 | strlcat(state->pp_buf, |
| 191 | " unable to read partition table\n", PAGE_SIZE); |
| 192 | printk(KERN_INFO "%s", state->pp_buf); |
| 193 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 194 | |
| 195 | free_page((unsigned long)state->pp_buf); |
Ming Lei | ac2e532 | 2013-02-27 17:05:19 -0800 | [diff] [blame] | 196 | free_partitions(state); |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 197 | return ERR_PTR(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |