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