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 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/kmod.h> |
| 20 | #include <linux/ctype.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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #ifdef CONFIG_BLK_DEV_MD |
| 40 | extern void md_autodetect_dev(dev_t dev); |
| 41 | #endif |
| 42 | |
| 43 | int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/ |
| 44 | |
| 45 | static int (*check_part[])(struct parsed_partitions *, struct block_device *) = { |
| 46 | /* |
| 47 | * Probe partition formats with tables at disk address 0 |
| 48 | * that also have an ADFS boot block at 0xdc0. |
| 49 | */ |
| 50 | #ifdef CONFIG_ACORN_PARTITION_ICS |
| 51 | adfspart_check_ICS, |
| 52 | #endif |
| 53 | #ifdef CONFIG_ACORN_PARTITION_POWERTEC |
| 54 | adfspart_check_POWERTEC, |
| 55 | #endif |
| 56 | #ifdef CONFIG_ACORN_PARTITION_EESOX |
| 57 | adfspart_check_EESOX, |
| 58 | #endif |
| 59 | |
| 60 | /* |
| 61 | * Now move on to formats that only have partition info at |
| 62 | * disk address 0xdc0. Since these may also have stale |
| 63 | * PC/BIOS partition tables, they need to come before |
| 64 | * the msdos entry. |
| 65 | */ |
| 66 | #ifdef CONFIG_ACORN_PARTITION_CUMANA |
| 67 | adfspart_check_CUMANA, |
| 68 | #endif |
| 69 | #ifdef CONFIG_ACORN_PARTITION_ADFS |
| 70 | adfspart_check_ADFS, |
| 71 | #endif |
| 72 | |
| 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 | }; |
| 114 | |
| 115 | /* |
| 116 | * disk_name() is used by partition check code and the genhd driver. |
| 117 | * It formats the devicename of the indicated disk into |
| 118 | * the supplied buffer (of size at least 32), and returns |
| 119 | * a pointer to that same buffer (for convenience). |
| 120 | */ |
| 121 | |
| 122 | char *disk_name(struct gendisk *hd, int part, char *buf) |
| 123 | { |
| 124 | if (!part) |
| 125 | snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name); |
| 126 | else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) |
| 127 | snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part); |
| 128 | else |
| 129 | snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part); |
| 130 | |
| 131 | return buf; |
| 132 | } |
| 133 | |
| 134 | const char *bdevname(struct block_device *bdev, char *buf) |
| 135 | { |
| 136 | int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor; |
| 137 | return disk_name(bdev->bd_disk, part, buf); |
| 138 | } |
| 139 | |
| 140 | EXPORT_SYMBOL(bdevname); |
| 141 | |
| 142 | /* |
| 143 | * There's very little reason to use this, you should really |
| 144 | * have a struct block_device just about everywhere and use |
| 145 | * bdevname() instead. |
| 146 | */ |
| 147 | const char *__bdevname(dev_t dev, char *buffer) |
| 148 | { |
| 149 | scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)", |
| 150 | MAJOR(dev), MINOR(dev)); |
| 151 | return buffer; |
| 152 | } |
| 153 | |
| 154 | EXPORT_SYMBOL(__bdevname); |
| 155 | |
| 156 | static struct parsed_partitions * |
| 157 | check_partition(struct gendisk *hd, struct block_device *bdev) |
| 158 | { |
| 159 | struct parsed_partitions *state; |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 160 | int i, res, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL); |
| 163 | if (!state) |
| 164 | return NULL; |
| 165 | |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 166 | disk_name(hd, 0, state->name); |
| 167 | printk(KERN_INFO " %s:", state->name); |
| 168 | if (isdigit(state->name[strlen(state->name)-1])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | sprintf(state->name, "p"); |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | state->limit = hd->minors; |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 172 | i = res = err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | while (!res && check_part[i]) { |
| 174 | memset(&state->parts, 0, sizeof(state->parts)); |
| 175 | res = check_part[i++](state, bdev); |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 176 | if (res < 0) { |
| 177 | /* We have hit an I/O error which we don't report now. |
| 178 | * But record it, and let the others do their job. |
| 179 | */ |
| 180 | err = res; |
| 181 | res = 0; |
| 182 | } |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | if (res > 0) |
| 186 | return state; |
suzuki | 9bebff6 | 2007-03-07 20:41:24 -0800 | [diff] [blame] | 187 | if (err) |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 188 | /* The partition is unrecognized. So report I/O errors if there were any */ |
| 189 | res = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (!res) |
| 191 | printk(" unknown partition table\n"); |
| 192 | else if (warn_no_part) |
| 193 | printk(" unable to read partition table\n"); |
| 194 | kfree(state); |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 195 | return ERR_PTR(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 198 | static ssize_t part_start_show(struct device *dev, |
| 199 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 201 | struct hd_struct *p = dev_to_part(dev); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 202 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 203 | return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 206 | static ssize_t part_size_show(struct device *dev, |
| 207 | struct device_attribute *attr, char *buf) |
| 208 | { |
| 209 | struct hd_struct *p = dev_to_part(dev); |
| 210 | return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects); |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 213 | static ssize_t part_stat_show(struct device *dev, |
| 214 | struct device_attribute *attr, char *buf) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 215 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 216 | struct hd_struct *p = dev_to_part(dev); |
| 217 | |
| 218 | return sprintf(buf, "%8u %8llu %8u %8llu\n", |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 219 | p->ios[0], (unsigned long long)p->sectors[0], |
| 220 | p->ios[1], (unsigned long long)p->sectors[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 223 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 224 | static ssize_t part_fail_show(struct device *dev, |
| 225 | struct device_attribute *attr, char *buf) |
| 226 | { |
| 227 | struct hd_struct *p = dev_to_part(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 228 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 229 | return sprintf(buf, "%d\n", p->make_it_fail); |
| 230 | } |
| 231 | |
| 232 | static ssize_t part_fail_store(struct device *dev, |
| 233 | struct device_attribute *attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 234 | const char *buf, size_t count) |
| 235 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 236 | struct hd_struct *p = dev_to_part(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 237 | int i; |
| 238 | |
| 239 | if (count > 0 && sscanf(buf, "%d", &i) > 0) |
| 240 | p->make_it_fail = (i == 0) ? 0 : 1; |
| 241 | |
| 242 | return count; |
| 243 | } |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 244 | #endif |
| 245 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 246 | static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL); |
| 247 | static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL); |
| 248 | static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 249 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 250 | static struct device_attribute dev_attr_fail = |
| 251 | __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 252 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 253 | |
| 254 | static struct attribute *part_attrs[] = { |
| 255 | &dev_attr_start.attr, |
| 256 | &dev_attr_size.attr, |
| 257 | &dev_attr_stat.attr, |
| 258 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 259 | &dev_attr_fail.attr, |
| 260 | #endif |
| 261 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 264 | static struct attribute_group part_attr_group = { |
| 265 | .attrs = part_attrs, |
| 266 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 268 | static struct attribute_group *part_attr_groups[] = { |
| 269 | &part_attr_group, |
| 270 | NULL |
| 271 | }; |
| 272 | |
| 273 | static void part_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 275 | struct hd_struct *p = dev_to_part(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | kfree(p); |
| 277 | } |
| 278 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 279 | struct device_type part_type = { |
| 280 | .name = "partition", |
| 281 | .groups = part_attr_groups, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | .release = part_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 285 | static inline void partition_sysfs_add_subdir(struct hd_struct *p) |
| 286 | { |
| 287 | struct kobject *k; |
| 288 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 289 | k = kobject_get(&p->dev.kobj); |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 290 | p->holder_dir = kobject_create_and_add("holders", k); |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 291 | kobject_put(k); |
| 292 | } |
| 293 | |
| 294 | static inline void disk_sysfs_add_subdirs(struct gendisk *disk) |
| 295 | { |
| 296 | struct kobject *k; |
| 297 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 298 | k = kobject_get(&disk->dev.kobj); |
Greg Kroah-Hartman | 4ff6abf | 2007-11-05 22:24:43 -0800 | [diff] [blame] | 299 | disk->holder_dir = kobject_create_and_add("holders", k); |
| 300 | disk->slave_dir = kobject_create_and_add("slaves", k); |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 301 | kobject_put(k); |
| 302 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | void delete_partition(struct gendisk *disk, int part) |
| 305 | { |
| 306 | struct hd_struct *p = disk->part[part-1]; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (!p) |
| 309 | return; |
| 310 | if (!p->nr_sects) |
| 311 | return; |
| 312 | disk->part[part-1] = NULL; |
| 313 | p->start_sect = 0; |
| 314 | p->nr_sects = 0; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 315 | p->ios[0] = p->ios[1] = 0; |
| 316 | p->sectors[0] = p->sectors[1] = 0; |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 317 | kobject_put(p->holder_dir); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 318 | device_del(&p->dev); |
| 319 | put_device(&p->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 322 | void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | struct hd_struct *p; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 325 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 327 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | if (!p) |
| 329 | return; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | p->start_sect = start; |
| 332 | p->nr_sects = len; |
| 333 | p->partno = part; |
Peter Oberparleiter | 25e206b | 2006-07-10 04:44:00 -0700 | [diff] [blame] | 334 | p->policy = disk->policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 336 | if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1])) |
| 337 | snprintf(p->dev.bus_id, BUS_ID_SIZE, |
| 338 | "%sp%d", disk->dev.bus_id, part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 340 | snprintf(p->dev.bus_id, BUS_ID_SIZE, |
| 341 | "%s%d", disk->dev.bus_id, part); |
| 342 | |
| 343 | device_initialize(&p->dev); |
| 344 | p->dev.devt = MKDEV(disk->major, disk->first_minor + part); |
| 345 | p->dev.class = &block_class; |
| 346 | p->dev.type = &part_type; |
| 347 | p->dev.parent = &disk->dev; |
| 348 | disk->part[part-1] = p; |
| 349 | |
| 350 | /* delay uevent until 'holders' subdir is created */ |
| 351 | p->dev.uevent_suppress = 1; |
| 352 | device_add(&p->dev); |
| 353 | partition_sysfs_add_subdir(p); |
| 354 | p->dev.uevent_suppress = 0; |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 355 | if (flags & ADDPART_FLAG_WHOLEDISK) { |
| 356 | static struct attribute addpartattr = { |
| 357 | .name = "whole_disk", |
| 358 | .mode = S_IRUSR | S_IRGRP | S_IROTH, |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 359 | }; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 360 | err = sysfs_create_file(&p->dev.kobj, &addpartattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 362 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 363 | /* suppress uevent if the disk supresses it */ |
| 364 | if (!disk->dev.uevent_suppress) |
| 365 | kobject_uevent(&p->dev.kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* Not exported, helper to add_disk(). */ |
| 369 | void register_disk(struct gendisk *disk) |
| 370 | { |
| 371 | struct block_device *bdev; |
| 372 | char *s; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 373 | int i; |
| 374 | struct hd_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | int err; |
| 376 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 377 | disk->dev.parent = disk->driverfs_dev; |
| 378 | disk->dev.devt = MKDEV(disk->major, disk->first_minor); |
| 379 | |
| 380 | strlcpy(disk->dev.bus_id, disk->disk_name, KOBJ_NAME_LEN); |
| 381 | /* ewww... some of these buggers have / in the name... */ |
| 382 | s = strchr(disk->dev.bus_id, '/'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (s) |
| 384 | *s = '!'; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 385 | |
| 386 | /* delay uevents, until we scanned partition table */ |
| 387 | disk->dev.uevent_suppress = 1; |
| 388 | |
| 389 | if (device_add(&disk->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 391 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 392 | err = sysfs_create_link(block_depr, &disk->dev.kobj, |
| 393 | kobject_name(&disk->dev.kobj)); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 394 | if (err) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 395 | device_del(&disk->dev); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 398 | #endif |
| 399 | disk_sysfs_add_subdirs(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | /* No minors to use for partitions */ |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 402 | if (disk->minors == 1) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 403 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | /* No such device (e.g., media were just removed) */ |
| 406 | if (!get_capacity(disk)) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 407 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | bdev = bdget_disk(disk, 0); |
| 410 | if (!bdev) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 411 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | bdev->bd_invalidated = 1; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 414 | err = blkdev_get(bdev, FMODE_READ, 0); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 415 | if (err < 0) |
| 416 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | blkdev_put(bdev); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 418 | |
| 419 | exit: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 420 | /* announce disk after possible partitions are created */ |
| 421 | disk->dev.uevent_suppress = 0; |
| 422 | kobject_uevent(&disk->dev.kobj, KOBJ_ADD); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 423 | |
| 424 | /* announce possible partitions */ |
| 425 | for (i = 1; i < disk->minors; i++) { |
| 426 | p = disk->part[i-1]; |
| 427 | if (!p || !p->nr_sects) |
| 428 | continue; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 429 | kobject_uevent(&p->dev.kobj, KOBJ_ADD); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | int rescan_partitions(struct gendisk *disk, struct block_device *bdev) |
| 434 | { |
| 435 | struct parsed_partitions *state; |
| 436 | int p, res; |
| 437 | |
| 438 | if (bdev->bd_part_count) |
| 439 | return -EBUSY; |
| 440 | res = invalidate_partition(disk, 0); |
| 441 | if (res) |
| 442 | return res; |
| 443 | bdev->bd_invalidated = 0; |
| 444 | for (p = 1; p < disk->minors; p++) |
| 445 | delete_partition(disk, p); |
| 446 | if (disk->fops->revalidate_disk) |
| 447 | disk->fops->revalidate_disk(disk); |
| 448 | if (!get_capacity(disk) || !(state = check_partition(disk, bdev))) |
| 449 | return 0; |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 450 | if (IS_ERR(state)) /* I/O error reading the partition table */ |
suzuki | b74a2f0 | 2007-03-16 13:38:25 -0800 | [diff] [blame] | 451 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | for (p = 1; p < state->limit; p++) { |
| 453 | sector_t size = state->parts[p].size; |
| 454 | sector_t from = state->parts[p].from; |
| 455 | if (!size) |
| 456 | continue; |
Mike Miller | 98bd34e | 2006-06-23 02:06:07 -0700 | [diff] [blame] | 457 | if (from + size > get_capacity(disk)) { |
| 458 | printk(" %s: p%d exceeds device capacity\n", |
| 459 | disk->disk_name, p); |
| 460 | } |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 461 | add_partition(disk, p, from, size, state->parts[p].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | #ifdef CONFIG_BLK_DEV_MD |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 463 | if (state->parts[p].flags & ADDPART_FLAG_RAID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | md_autodetect_dev(bdev->bd_dev+p); |
| 465 | #endif |
| 466 | } |
| 467 | kfree(state); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p) |
| 472 | { |
| 473 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 474 | struct page *page; |
| 475 | |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 476 | page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)), |
| 477 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (!IS_ERR(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (PageError(page)) |
| 480 | goto fail; |
| 481 | p->v = page; |
| 482 | return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9); |
| 483 | fail: |
| 484 | page_cache_release(page); |
| 485 | } |
| 486 | p->v = NULL; |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | EXPORT_SYMBOL(read_dev_sector); |
| 491 | |
| 492 | void del_gendisk(struct gendisk *disk) |
| 493 | { |
| 494 | int p; |
| 495 | |
| 496 | /* invalidate stuff */ |
| 497 | for (p = disk->minors - 1; p > 0; p--) { |
| 498 | invalidate_partition(disk, p); |
| 499 | delete_partition(disk, p); |
| 500 | } |
| 501 | invalidate_partition(disk, 0); |
| 502 | disk->capacity = 0; |
| 503 | disk->flags &= ~GENHD_FL_UP; |
| 504 | unlink_gendisk(disk); |
| 505 | disk_stat_set_all(disk, 0); |
Chen, Kenneth W | 20e5c81 | 2005-10-13 21:48:42 +0200 | [diff] [blame] | 506 | disk->stamp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 508 | kobject_put(disk->holder_dir); |
| 509 | kobject_put(disk->slave_dir); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 510 | disk->driverfs_dev = NULL; |
| 511 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 512 | sysfs_remove_link(block_depr, disk->dev.bus_id); |
| 513 | #endif |
| 514 | device_del(&disk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |