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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #ifdef CONFIG_BLK_DEV_MD |
| 39 | extern void md_autodetect_dev(dev_t dev); |
| 40 | #endif |
| 41 | |
| 42 | int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/ |
| 43 | |
| 44 | static int (*check_part[])(struct parsed_partitions *, struct block_device *) = { |
| 45 | /* |
| 46 | * Probe partition formats with tables at disk address 0 |
| 47 | * that also have an ADFS boot block at 0xdc0. |
| 48 | */ |
| 49 | #ifdef CONFIG_ACORN_PARTITION_ICS |
| 50 | adfspart_check_ICS, |
| 51 | #endif |
| 52 | #ifdef CONFIG_ACORN_PARTITION_POWERTEC |
| 53 | adfspart_check_POWERTEC, |
| 54 | #endif |
| 55 | #ifdef CONFIG_ACORN_PARTITION_EESOX |
| 56 | adfspart_check_EESOX, |
| 57 | #endif |
| 58 | |
| 59 | /* |
| 60 | * Now move on to formats that only have partition info at |
| 61 | * disk address 0xdc0. Since these may also have stale |
| 62 | * PC/BIOS partition tables, they need to come before |
| 63 | * the msdos entry. |
| 64 | */ |
| 65 | #ifdef CONFIG_ACORN_PARTITION_CUMANA |
| 66 | adfspart_check_CUMANA, |
| 67 | #endif |
| 68 | #ifdef CONFIG_ACORN_PARTITION_ADFS |
| 69 | adfspart_check_ADFS, |
| 70 | #endif |
| 71 | |
| 72 | #ifdef CONFIG_EFI_PARTITION |
| 73 | efi_partition, /* this must come before msdos */ |
| 74 | #endif |
| 75 | #ifdef CONFIG_SGI_PARTITION |
| 76 | sgi_partition, |
| 77 | #endif |
| 78 | #ifdef CONFIG_LDM_PARTITION |
| 79 | ldm_partition, /* this must come before msdos */ |
| 80 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #ifdef CONFIG_MSDOS_PARTITION |
| 82 | msdos_partition, |
| 83 | #endif |
| 84 | #ifdef CONFIG_OSF_PARTITION |
| 85 | osf_partition, |
| 86 | #endif |
| 87 | #ifdef CONFIG_SUN_PARTITION |
| 88 | sun_partition, |
| 89 | #endif |
| 90 | #ifdef CONFIG_AMIGA_PARTITION |
| 91 | amiga_partition, |
| 92 | #endif |
| 93 | #ifdef CONFIG_ATARI_PARTITION |
| 94 | atari_partition, |
| 95 | #endif |
| 96 | #ifdef CONFIG_MAC_PARTITION |
| 97 | mac_partition, |
| 98 | #endif |
| 99 | #ifdef CONFIG_ULTRIX_PARTITION |
| 100 | ultrix_partition, |
| 101 | #endif |
| 102 | #ifdef CONFIG_IBM_PARTITION |
| 103 | ibm_partition, |
| 104 | #endif |
Bob Copeland | 0e6e1db | 2006-01-16 22:14:20 -0800 | [diff] [blame] | 105 | #ifdef CONFIG_KARMA_PARTITION |
| 106 | karma_partition, |
| 107 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | NULL |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * disk_name() is used by partition check code and the genhd driver. |
| 113 | * It formats the devicename of the indicated disk into |
| 114 | * the supplied buffer (of size at least 32), and returns |
| 115 | * a pointer to that same buffer (for convenience). |
| 116 | */ |
| 117 | |
| 118 | char *disk_name(struct gendisk *hd, int part, char *buf) |
| 119 | { |
| 120 | if (!part) |
| 121 | snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name); |
| 122 | else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) |
| 123 | snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part); |
| 124 | else |
| 125 | snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part); |
| 126 | |
| 127 | return buf; |
| 128 | } |
| 129 | |
| 130 | const char *bdevname(struct block_device *bdev, char *buf) |
| 131 | { |
| 132 | int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor; |
| 133 | return disk_name(bdev->bd_disk, part, buf); |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL(bdevname); |
| 137 | |
| 138 | /* |
| 139 | * There's very little reason to use this, you should really |
| 140 | * have a struct block_device just about everywhere and use |
| 141 | * bdevname() instead. |
| 142 | */ |
| 143 | const char *__bdevname(dev_t dev, char *buffer) |
| 144 | { |
| 145 | scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)", |
| 146 | MAJOR(dev), MINOR(dev)); |
| 147 | return buffer; |
| 148 | } |
| 149 | |
| 150 | EXPORT_SYMBOL(__bdevname); |
| 151 | |
| 152 | static struct parsed_partitions * |
| 153 | check_partition(struct gendisk *hd, struct block_device *bdev) |
| 154 | { |
| 155 | struct parsed_partitions *state; |
| 156 | int i, res; |
| 157 | |
| 158 | state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL); |
| 159 | if (!state) |
| 160 | return NULL; |
| 161 | |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 162 | disk_name(hd, 0, state->name); |
| 163 | printk(KERN_INFO " %s:", state->name); |
| 164 | if (isdigit(state->name[strlen(state->name)-1])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | sprintf(state->name, "p"); |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | state->limit = hd->minors; |
| 168 | i = res = 0; |
| 169 | while (!res && check_part[i]) { |
| 170 | memset(&state->parts, 0, sizeof(state->parts)); |
| 171 | res = check_part[i++](state, bdev); |
| 172 | } |
| 173 | if (res > 0) |
| 174 | return state; |
| 175 | if (!res) |
| 176 | printk(" unknown partition table\n"); |
| 177 | else if (warn_no_part) |
| 178 | printk(" unable to read partition table\n"); |
| 179 | kfree(state); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * sysfs bindings for partitions |
| 185 | */ |
| 186 | |
| 187 | struct part_attribute { |
| 188 | struct attribute attr; |
| 189 | ssize_t (*show)(struct hd_struct *,char *); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 190 | ssize_t (*store)(struct hd_struct *,const char *, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static ssize_t |
| 194 | part_attr_show(struct kobject * kobj, struct attribute * attr, char * page) |
| 195 | { |
| 196 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); |
| 197 | struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr); |
| 198 | ssize_t ret = 0; |
| 199 | if (part_attr->show) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 200 | ret = part_attr->show(p, page); |
| 201 | return ret; |
| 202 | } |
| 203 | static ssize_t |
| 204 | part_attr_store(struct kobject * kobj, struct attribute * attr, |
| 205 | const char *page, size_t count) |
| 206 | { |
| 207 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); |
| 208 | struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr); |
| 209 | ssize_t ret = 0; |
| 210 | |
| 211 | if (part_attr->store) |
| 212 | ret = part_attr->store(p, page, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static struct sysfs_ops part_sysfs_ops = { |
| 217 | .show = part_attr_show, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 218 | .store = part_attr_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 221 | static ssize_t part_uevent_store(struct hd_struct * p, |
| 222 | const char *page, size_t count) |
| 223 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 224 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 225 | return count; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | static ssize_t part_dev_read(struct hd_struct * p, char *page) |
| 228 | { |
| 229 | struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj); |
| 230 | dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno); |
| 231 | return print_dev_t(page, dev); |
| 232 | } |
| 233 | static ssize_t part_start_read(struct hd_struct * p, char *page) |
| 234 | { |
| 235 | return sprintf(page, "%llu\n",(unsigned long long)p->start_sect); |
| 236 | } |
| 237 | static ssize_t part_size_read(struct hd_struct * p, char *page) |
| 238 | { |
| 239 | return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects); |
| 240 | } |
| 241 | static ssize_t part_stat_read(struct hd_struct * p, char *page) |
| 242 | { |
| 243 | return sprintf(page, "%8u %8llu %8u %8llu\n", |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 244 | p->ios[0], (unsigned long long)p->sectors[0], |
| 245 | p->ios[1], (unsigned long long)p->sectors[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 247 | static struct part_attribute part_attr_uevent = { |
| 248 | .attr = {.name = "uevent", .mode = S_IWUSR }, |
| 249 | .store = part_uevent_store |
| 250 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static struct part_attribute part_attr_dev = { |
| 252 | .attr = {.name = "dev", .mode = S_IRUGO }, |
| 253 | .show = part_dev_read |
| 254 | }; |
| 255 | static struct part_attribute part_attr_start = { |
| 256 | .attr = {.name = "start", .mode = S_IRUGO }, |
| 257 | .show = part_start_read |
| 258 | }; |
| 259 | static struct part_attribute part_attr_size = { |
| 260 | .attr = {.name = "size", .mode = S_IRUGO }, |
| 261 | .show = part_size_read |
| 262 | }; |
| 263 | static struct part_attribute part_attr_stat = { |
| 264 | .attr = {.name = "stat", .mode = S_IRUGO }, |
| 265 | .show = part_stat_read |
| 266 | }; |
| 267 | |
| 268 | static struct attribute * default_attrs[] = { |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 269 | &part_attr_uevent.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | &part_attr_dev.attr, |
| 271 | &part_attr_start.attr, |
| 272 | &part_attr_size.attr, |
| 273 | &part_attr_stat.attr, |
| 274 | NULL, |
| 275 | }; |
| 276 | |
| 277 | extern struct subsystem block_subsys; |
| 278 | |
| 279 | static void part_release(struct kobject *kobj) |
| 280 | { |
| 281 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); |
| 282 | kfree(p); |
| 283 | } |
| 284 | |
| 285 | struct kobj_type ktype_part = { |
| 286 | .release = part_release, |
| 287 | .default_attrs = default_attrs, |
| 288 | .sysfs_ops = &part_sysfs_ops, |
| 289 | }; |
| 290 | |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 291 | static inline void partition_sysfs_add_subdir(struct hd_struct *p) |
| 292 | { |
| 293 | struct kobject *k; |
| 294 | |
| 295 | k = kobject_get(&p->kobj); |
| 296 | p->holder_dir = kobject_add_dir(k, "holders"); |
| 297 | kobject_put(k); |
| 298 | } |
| 299 | |
| 300 | static inline void disk_sysfs_add_subdirs(struct gendisk *disk) |
| 301 | { |
| 302 | struct kobject *k; |
| 303 | |
| 304 | k = kobject_get(&disk->kobj); |
| 305 | disk->holder_dir = kobject_add_dir(k, "holders"); |
| 306 | disk->slave_dir = kobject_add_dir(k, "slaves"); |
| 307 | kobject_put(k); |
| 308 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | void delete_partition(struct gendisk *disk, int part) |
| 311 | { |
| 312 | struct hd_struct *p = disk->part[part-1]; |
| 313 | if (!p) |
| 314 | return; |
| 315 | if (!p->nr_sects) |
| 316 | return; |
| 317 | disk->part[part-1] = NULL; |
| 318 | p->start_sect = 0; |
| 319 | p->nr_sects = 0; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 320 | p->ios[0] = p->ios[1] = 0; |
| 321 | p->sectors[0] = p->sectors[1] = 0; |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 322 | sysfs_remove_link(&p->kobj, "subsystem"); |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 323 | if (p->holder_dir) |
| 324 | kobject_unregister(p->holder_dir); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 325 | kobject_uevent(&p->kobj, KOBJ_REMOVE); |
| 326 | kobject_del(&p->kobj); |
| 327 | kobject_put(&p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len) |
| 331 | { |
| 332 | struct hd_struct *p; |
| 333 | |
| 334 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 335 | if (!p) |
| 336 | return; |
| 337 | |
| 338 | memset(p, 0, sizeof(*p)); |
| 339 | p->start_sect = start; |
| 340 | p->nr_sects = len; |
| 341 | p->partno = part; |
Peter Oberparleiter | 25e206b | 2006-07-10 04:44:00 -0700 | [diff] [blame] | 342 | p->policy = disk->policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1])) |
| 345 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part); |
| 346 | else |
| 347 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part); |
| 348 | p->kobj.parent = &disk->kobj; |
| 349 | p->kobj.ktype = &ktype_part; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 350 | kobject_init(&p->kobj); |
| 351 | kobject_add(&p->kobj); |
| 352 | if (!disk->part_uevent_suppress) |
| 353 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 354 | sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem"); |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 355 | partition_sysfs_add_subdir(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | disk->part[part-1] = p; |
| 357 | } |
| 358 | |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 359 | static char *make_block_name(struct gendisk *disk) |
| 360 | { |
| 361 | char *name; |
| 362 | static char *block_str = "block:"; |
| 363 | int size; |
Stephen Rothwell | 2436f03 | 2006-04-10 00:17:20 -0700 | [diff] [blame] | 364 | char *s; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 365 | |
| 366 | size = strlen(block_str) + strlen(disk->disk_name) + 1; |
| 367 | name = kmalloc(size, GFP_KERNEL); |
| 368 | if (!name) |
| 369 | return NULL; |
| 370 | strcpy(name, block_str); |
| 371 | strcat(name, disk->disk_name); |
Stephen Rothwell | 2436f03 | 2006-04-10 00:17:20 -0700 | [diff] [blame] | 372 | /* ewww... some of these buggers have / in name... */ |
| 373 | s = strchr(name, '/'); |
| 374 | if (s) |
| 375 | *s = '!'; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 376 | return name; |
| 377 | } |
| 378 | |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 379 | static int disk_sysfs_symlinks(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
| 381 | struct device *target = get_device(disk->driverfs_dev); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 382 | int err; |
| 383 | char *disk_name = NULL; |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (target) { |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 386 | disk_name = make_block_name(disk); |
| 387 | if (!disk_name) { |
| 388 | err = -ENOMEM; |
| 389 | goto err_out; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 390 | } |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 391 | |
| 392 | err = sysfs_create_link(&disk->kobj, &target->kobj, "device"); |
| 393 | if (err) |
| 394 | goto err_out_disk_name; |
| 395 | |
| 396 | err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name); |
| 397 | if (err) |
| 398 | goto err_out_dev_link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 400 | |
| 401 | err = sysfs_create_link(&disk->kobj, &block_subsys.kset.kobj, |
| 402 | "subsystem"); |
| 403 | if (err) |
| 404 | goto err_out_disk_name_lnk; |
| 405 | |
| 406 | kfree(disk_name); |
| 407 | |
| 408 | return 0; |
| 409 | |
| 410 | err_out_disk_name_lnk: |
| 411 | if (target) { |
| 412 | sysfs_remove_link(&target->kobj, disk_name); |
| 413 | err_out_dev_link: |
| 414 | sysfs_remove_link(&disk->kobj, "device"); |
| 415 | err_out_disk_name: |
| 416 | kfree(disk_name); |
| 417 | err_out: |
| 418 | put_device(target); |
| 419 | } |
| 420 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Not exported, helper to add_disk(). */ |
| 424 | void register_disk(struct gendisk *disk) |
| 425 | { |
| 426 | struct block_device *bdev; |
| 427 | char *s; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 428 | int i; |
| 429 | struct hd_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | int err; |
| 431 | |
| 432 | strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN); |
| 433 | /* ewww... some of these buggers have / in name... */ |
| 434 | s = strchr(disk->kobj.name, '/'); |
| 435 | if (s) |
| 436 | *s = '!'; |
| 437 | if ((err = kobject_add(&disk->kobj))) |
| 438 | return; |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 439 | err = disk_sysfs_symlinks(disk); |
| 440 | if (err) { |
| 441 | kobject_del(&disk->kobj); |
| 442 | return; |
| 443 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 444 | disk_sysfs_add_subdirs(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* No minors to use for partitions */ |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 447 | if (disk->minors == 1) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 448 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | /* No such device (e.g., media were just removed) */ |
| 451 | if (!get_capacity(disk)) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 452 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | bdev = bdget_disk(disk, 0); |
| 455 | if (!bdev) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 456 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 458 | /* scan partition table, but suppress uevents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | bdev->bd_invalidated = 1; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 460 | disk->part_uevent_suppress = 1; |
| 461 | err = blkdev_get(bdev, FMODE_READ, 0); |
| 462 | disk->part_uevent_suppress = 0; |
| 463 | if (err < 0) |
| 464 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | blkdev_put(bdev); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 466 | |
| 467 | exit: |
| 468 | /* announce disk after possible partitions are already created */ |
| 469 | kobject_uevent(&disk->kobj, KOBJ_ADD); |
| 470 | |
| 471 | /* announce possible partitions */ |
| 472 | for (i = 1; i < disk->minors; i++) { |
| 473 | p = disk->part[i-1]; |
| 474 | if (!p || !p->nr_sects) |
| 475 | continue; |
| 476 | kobject_uevent(&p->kobj, KOBJ_ADD); |
| 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | int rescan_partitions(struct gendisk *disk, struct block_device *bdev) |
| 481 | { |
| 482 | struct parsed_partitions *state; |
| 483 | int p, res; |
| 484 | |
| 485 | if (bdev->bd_part_count) |
| 486 | return -EBUSY; |
| 487 | res = invalidate_partition(disk, 0); |
| 488 | if (res) |
| 489 | return res; |
| 490 | bdev->bd_invalidated = 0; |
| 491 | for (p = 1; p < disk->minors; p++) |
| 492 | delete_partition(disk, p); |
| 493 | if (disk->fops->revalidate_disk) |
| 494 | disk->fops->revalidate_disk(disk); |
| 495 | if (!get_capacity(disk) || !(state = check_partition(disk, bdev))) |
| 496 | return 0; |
| 497 | for (p = 1; p < state->limit; p++) { |
| 498 | sector_t size = state->parts[p].size; |
| 499 | sector_t from = state->parts[p].from; |
| 500 | if (!size) |
| 501 | continue; |
Mike Miller | 98bd34e | 2006-06-23 02:06:07 -0700 | [diff] [blame] | 502 | if (from + size > get_capacity(disk)) { |
| 503 | printk(" %s: p%d exceeds device capacity\n", |
| 504 | disk->disk_name, p); |
| 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | add_partition(disk, p, from, size); |
| 507 | #ifdef CONFIG_BLK_DEV_MD |
| 508 | if (state->parts[p].flags) |
| 509 | md_autodetect_dev(bdev->bd_dev+p); |
| 510 | #endif |
| 511 | } |
| 512 | kfree(state); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p) |
| 517 | { |
| 518 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 519 | struct page *page; |
| 520 | |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 521 | page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)), |
| 522 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (!IS_ERR(page)) { |
| 524 | wait_on_page_locked(page); |
| 525 | if (!PageUptodate(page)) |
| 526 | goto fail; |
| 527 | if (PageError(page)) |
| 528 | goto fail; |
| 529 | p->v = page; |
| 530 | return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9); |
| 531 | fail: |
| 532 | page_cache_release(page); |
| 533 | } |
| 534 | p->v = NULL; |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | EXPORT_SYMBOL(read_dev_sector); |
| 539 | |
| 540 | void del_gendisk(struct gendisk *disk) |
| 541 | { |
| 542 | int p; |
| 543 | |
| 544 | /* invalidate stuff */ |
| 545 | for (p = disk->minors - 1; p > 0; p--) { |
| 546 | invalidate_partition(disk, p); |
| 547 | delete_partition(disk, p); |
| 548 | } |
| 549 | invalidate_partition(disk, 0); |
| 550 | disk->capacity = 0; |
| 551 | disk->flags &= ~GENHD_FL_UP; |
| 552 | unlink_gendisk(disk); |
| 553 | disk_stat_set_all(disk, 0); |
Chen, Kenneth W | 20e5c81 | 2005-10-13 21:48:42 +0200 | [diff] [blame] | 554 | disk->stamp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Linus Torvalds | 032ebf2 | 2006-05-12 18:42:09 -0700 | [diff] [blame] | 556 | kobject_uevent(&disk->kobj, KOBJ_REMOVE); |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 557 | if (disk->holder_dir) |
| 558 | kobject_unregister(disk->holder_dir); |
| 559 | if (disk->slave_dir) |
| 560 | kobject_unregister(disk->slave_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | if (disk->driverfs_dev) { |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 562 | char *disk_name = make_block_name(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | sysfs_remove_link(&disk->kobj, "device"); |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 564 | if (disk_name) { |
| 565 | sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name); |
| 566 | kfree(disk_name); |
| 567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | put_device(disk->driverfs_dev); |
Linus Torvalds | 032ebf2 | 2006-05-12 18:42:09 -0700 | [diff] [blame] | 569 | disk->driverfs_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 571 | sysfs_remove_link(&disk->kobj, "subsystem"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | kobject_del(&disk->kobj); |
| 573 | } |