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; |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 156 | int i, res, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 168 | i = res = err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | while (!res && check_part[i]) { |
| 170 | memset(&state->parts, 0, sizeof(state->parts)); |
| 171 | res = check_part[i++](state, bdev); |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 172 | if (res < 0) { |
| 173 | /* We have hit an I/O error which we don't report now. |
| 174 | * But record it, and let the others do their job. |
| 175 | */ |
| 176 | err = res; |
| 177 | res = 0; |
| 178 | } |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | if (res > 0) |
| 182 | return state; |
suzuki | 9bebff6 | 2007-03-07 20:41:24 -0800 | [diff] [blame] | 183 | if (err) |
Suzuki K P | 57881dd | 2006-12-06 20:35:16 -0800 | [diff] [blame] | 184 | /* The partition is unrecognized. So report I/O errors if there were any */ |
| 185 | res = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | if (!res) |
| 187 | printk(" unknown partition table\n"); |
| 188 | else if (warn_no_part) |
| 189 | printk(" unable to read partition table\n"); |
| 190 | kfree(state); |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 191 | return ERR_PTR(res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
| 195 | * sysfs bindings for partitions |
| 196 | */ |
| 197 | |
| 198 | struct part_attribute { |
| 199 | struct attribute attr; |
| 200 | ssize_t (*show)(struct hd_struct *,char *); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 201 | ssize_t (*store)(struct hd_struct *,const char *, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static ssize_t |
| 205 | part_attr_show(struct kobject * kobj, struct attribute * attr, char * page) |
| 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 | if (part_attr->show) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 211 | ret = part_attr->show(p, page); |
| 212 | return ret; |
| 213 | } |
| 214 | static ssize_t |
| 215 | part_attr_store(struct kobject * kobj, struct attribute * attr, |
| 216 | const char *page, size_t count) |
| 217 | { |
| 218 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); |
| 219 | struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr); |
| 220 | ssize_t ret = 0; |
| 221 | |
| 222 | if (part_attr->store) |
| 223 | ret = part_attr->store(p, page, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static struct sysfs_ops part_sysfs_ops = { |
| 228 | .show = part_attr_show, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 229 | .store = part_attr_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 232 | static ssize_t part_uevent_store(struct hd_struct * p, |
| 233 | const char *page, size_t count) |
| 234 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 235 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 236 | return count; |
| 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | static ssize_t part_dev_read(struct hd_struct * p, char *page) |
| 239 | { |
| 240 | struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj); |
| 241 | dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno); |
| 242 | return print_dev_t(page, dev); |
| 243 | } |
| 244 | static ssize_t part_start_read(struct hd_struct * p, char *page) |
| 245 | { |
| 246 | return sprintf(page, "%llu\n",(unsigned long long)p->start_sect); |
| 247 | } |
| 248 | static ssize_t part_size_read(struct hd_struct * p, char *page) |
| 249 | { |
| 250 | return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects); |
| 251 | } |
| 252 | static ssize_t part_stat_read(struct hd_struct * p, char *page) |
| 253 | { |
| 254 | return sprintf(page, "%8u %8llu %8u %8llu\n", |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 255 | p->ios[0], (unsigned long long)p->sectors[0], |
| 256 | p->ios[1], (unsigned long long)p->sectors[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 258 | static struct part_attribute part_attr_uevent = { |
| 259 | .attr = {.name = "uevent", .mode = S_IWUSR }, |
| 260 | .store = part_uevent_store |
| 261 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | static struct part_attribute part_attr_dev = { |
| 263 | .attr = {.name = "dev", .mode = S_IRUGO }, |
| 264 | .show = part_dev_read |
| 265 | }; |
| 266 | static struct part_attribute part_attr_start = { |
| 267 | .attr = {.name = "start", .mode = S_IRUGO }, |
| 268 | .show = part_start_read |
| 269 | }; |
| 270 | static struct part_attribute part_attr_size = { |
| 271 | .attr = {.name = "size", .mode = S_IRUGO }, |
| 272 | .show = part_size_read |
| 273 | }; |
| 274 | static struct part_attribute part_attr_stat = { |
| 275 | .attr = {.name = "stat", .mode = S_IRUGO }, |
| 276 | .show = part_stat_read |
| 277 | }; |
| 278 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 279 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 280 | |
| 281 | static ssize_t part_fail_store(struct hd_struct * p, |
| 282 | const char *buf, size_t count) |
| 283 | { |
| 284 | int i; |
| 285 | |
| 286 | if (count > 0 && sscanf(buf, "%d", &i) > 0) |
| 287 | p->make_it_fail = (i == 0) ? 0 : 1; |
| 288 | |
| 289 | return count; |
| 290 | } |
| 291 | static ssize_t part_fail_read(struct hd_struct * p, char *page) |
| 292 | { |
| 293 | return sprintf(page, "%d\n", p->make_it_fail); |
| 294 | } |
| 295 | static struct part_attribute part_attr_fail = { |
| 296 | .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR }, |
| 297 | .store = part_fail_store, |
| 298 | .show = part_fail_read |
| 299 | }; |
| 300 | |
| 301 | #endif |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static struct attribute * default_attrs[] = { |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 304 | &part_attr_uevent.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | &part_attr_dev.attr, |
| 306 | &part_attr_start.attr, |
| 307 | &part_attr_size.attr, |
| 308 | &part_attr_stat.attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 309 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 310 | &part_attr_fail.attr, |
| 311 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | NULL, |
| 313 | }; |
| 314 | |
| 315 | extern struct subsystem block_subsys; |
| 316 | |
| 317 | static void part_release(struct kobject *kobj) |
| 318 | { |
| 319 | struct hd_struct * p = container_of(kobj,struct hd_struct,kobj); |
| 320 | kfree(p); |
| 321 | } |
| 322 | |
| 323 | struct kobj_type ktype_part = { |
| 324 | .release = part_release, |
| 325 | .default_attrs = default_attrs, |
| 326 | .sysfs_ops = &part_sysfs_ops, |
| 327 | }; |
| 328 | |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 329 | static inline void partition_sysfs_add_subdir(struct hd_struct *p) |
| 330 | { |
| 331 | struct kobject *k; |
| 332 | |
| 333 | k = kobject_get(&p->kobj); |
| 334 | p->holder_dir = kobject_add_dir(k, "holders"); |
| 335 | kobject_put(k); |
| 336 | } |
| 337 | |
| 338 | static inline void disk_sysfs_add_subdirs(struct gendisk *disk) |
| 339 | { |
| 340 | struct kobject *k; |
| 341 | |
| 342 | k = kobject_get(&disk->kobj); |
| 343 | disk->holder_dir = kobject_add_dir(k, "holders"); |
| 344 | disk->slave_dir = kobject_add_dir(k, "slaves"); |
| 345 | kobject_put(k); |
| 346 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | void delete_partition(struct gendisk *disk, int part) |
| 349 | { |
| 350 | struct hd_struct *p = disk->part[part-1]; |
| 351 | if (!p) |
| 352 | return; |
| 353 | if (!p->nr_sects) |
| 354 | return; |
| 355 | disk->part[part-1] = NULL; |
| 356 | p->start_sect = 0; |
| 357 | p->nr_sects = 0; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 358 | p->ios[0] = p->ios[1] = 0; |
| 359 | p->sectors[0] = p->sectors[1] = 0; |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 360 | sysfs_remove_link(&p->kobj, "subsystem"); |
Mariusz Kozlowski | b92be9f | 2007-02-14 21:03:39 +0100 | [diff] [blame] | 361 | kobject_unregister(p->holder_dir); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 362 | kobject_uevent(&p->kobj, KOBJ_REMOVE); |
| 363 | kobject_del(&p->kobj); |
| 364 | kobject_put(&p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 367 | 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] | 368 | { |
| 369 | struct hd_struct *p; |
| 370 | |
| 371 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 372 | if (!p) |
| 373 | return; |
| 374 | |
| 375 | memset(p, 0, sizeof(*p)); |
| 376 | p->start_sect = start; |
| 377 | p->nr_sects = len; |
| 378 | p->partno = part; |
Peter Oberparleiter | 25e206b | 2006-07-10 04:44:00 -0700 | [diff] [blame] | 379 | p->policy = disk->policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1])) |
| 382 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part); |
| 383 | else |
| 384 | snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part); |
| 385 | p->kobj.parent = &disk->kobj; |
| 386 | p->kobj.ktype = &ktype_part; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 387 | kobject_init(&p->kobj); |
| 388 | kobject_add(&p->kobj); |
| 389 | if (!disk->part_uevent_suppress) |
| 390 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 391 | sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem"); |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 392 | if (flags & ADDPART_FLAG_WHOLEDISK) { |
| 393 | static struct attribute addpartattr = { |
| 394 | .name = "whole_disk", |
| 395 | .mode = S_IRUSR | S_IRGRP | S_IROTH, |
| 396 | .owner = THIS_MODULE, |
| 397 | }; |
| 398 | |
| 399 | sysfs_create_file(&p->kobj, &addpartattr); |
| 400 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 401 | partition_sysfs_add_subdir(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | disk->part[part-1] = p; |
| 403 | } |
| 404 | |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 405 | static char *make_block_name(struct gendisk *disk) |
| 406 | { |
| 407 | char *name; |
| 408 | static char *block_str = "block:"; |
| 409 | int size; |
Stephen Rothwell | 2436f03 | 2006-04-10 00:17:20 -0700 | [diff] [blame] | 410 | char *s; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 411 | |
| 412 | size = strlen(block_str) + strlen(disk->disk_name) + 1; |
| 413 | name = kmalloc(size, GFP_KERNEL); |
| 414 | if (!name) |
| 415 | return NULL; |
| 416 | strcpy(name, block_str); |
| 417 | strcat(name, disk->disk_name); |
Stephen Rothwell | 2436f03 | 2006-04-10 00:17:20 -0700 | [diff] [blame] | 418 | /* ewww... some of these buggers have / in name... */ |
| 419 | s = strchr(name, '/'); |
| 420 | if (s) |
| 421 | *s = '!'; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 422 | return name; |
| 423 | } |
| 424 | |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 425 | static int disk_sysfs_symlinks(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct device *target = get_device(disk->driverfs_dev); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 428 | int err; |
| 429 | char *disk_name = NULL; |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (target) { |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 432 | disk_name = make_block_name(disk); |
| 433 | if (!disk_name) { |
| 434 | err = -ENOMEM; |
| 435 | goto err_out; |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 436 | } |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 437 | |
| 438 | err = sysfs_create_link(&disk->kobj, &target->kobj, "device"); |
| 439 | if (err) |
| 440 | goto err_out_disk_name; |
| 441 | |
| 442 | err = sysfs_create_link(&target->kobj, &disk->kobj, disk_name); |
| 443 | if (err) |
| 444 | goto err_out_dev_link; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 446 | |
| 447 | err = sysfs_create_link(&disk->kobj, &block_subsys.kset.kobj, |
| 448 | "subsystem"); |
| 449 | if (err) |
| 450 | goto err_out_disk_name_lnk; |
| 451 | |
| 452 | kfree(disk_name); |
| 453 | |
| 454 | return 0; |
| 455 | |
| 456 | err_out_disk_name_lnk: |
| 457 | if (target) { |
| 458 | sysfs_remove_link(&target->kobj, disk_name); |
| 459 | err_out_dev_link: |
| 460 | sysfs_remove_link(&disk->kobj, "device"); |
| 461 | err_out_disk_name: |
| 462 | kfree(disk_name); |
| 463 | err_out: |
| 464 | put_device(target); |
| 465 | } |
| 466 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Not exported, helper to add_disk(). */ |
| 470 | void register_disk(struct gendisk *disk) |
| 471 | { |
| 472 | struct block_device *bdev; |
| 473 | char *s; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 474 | int i; |
| 475 | struct hd_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | int err; |
| 477 | |
| 478 | strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN); |
| 479 | /* ewww... some of these buggers have / in name... */ |
| 480 | s = strchr(disk->kobj.name, '/'); |
| 481 | if (s) |
| 482 | *s = '!'; |
| 483 | if ((err = kobject_add(&disk->kobj))) |
| 484 | return; |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 485 | err = disk_sysfs_symlinks(disk); |
| 486 | if (err) { |
| 487 | kobject_del(&disk->kobj); |
| 488 | return; |
| 489 | } |
Jun'ichi Nomura | 6a4d44c | 2006-03-27 01:17:55 -0800 | [diff] [blame] | 490 | disk_sysfs_add_subdirs(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* No minors to use for partitions */ |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 493 | if (disk->minors == 1) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 494 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | /* No such device (e.g., media were just removed) */ |
| 497 | if (!get_capacity(disk)) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 498 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | bdev = bdget_disk(disk, 0); |
| 501 | if (!bdev) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 502 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 504 | /* scan partition table, but suppress uevents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | bdev->bd_invalidated = 1; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 506 | disk->part_uevent_suppress = 1; |
| 507 | err = blkdev_get(bdev, FMODE_READ, 0); |
| 508 | disk->part_uevent_suppress = 0; |
| 509 | if (err < 0) |
| 510 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | blkdev_put(bdev); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 512 | |
| 513 | exit: |
| 514 | /* announce disk after possible partitions are already created */ |
| 515 | kobject_uevent(&disk->kobj, KOBJ_ADD); |
| 516 | |
| 517 | /* announce possible partitions */ |
| 518 | for (i = 1; i < disk->minors; i++) { |
| 519 | p = disk->part[i-1]; |
| 520 | if (!p || !p->nr_sects) |
| 521 | continue; |
| 522 | kobject_uevent(&p->kobj, KOBJ_ADD); |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | int rescan_partitions(struct gendisk *disk, struct block_device *bdev) |
| 527 | { |
| 528 | struct parsed_partitions *state; |
| 529 | int p, res; |
| 530 | |
| 531 | if (bdev->bd_part_count) |
| 532 | return -EBUSY; |
| 533 | res = invalidate_partition(disk, 0); |
| 534 | if (res) |
| 535 | return res; |
| 536 | bdev->bd_invalidated = 0; |
| 537 | for (p = 1; p < disk->minors; p++) |
| 538 | delete_partition(disk, p); |
| 539 | if (disk->fops->revalidate_disk) |
| 540 | disk->fops->revalidate_disk(disk); |
| 541 | if (!get_capacity(disk) || !(state = check_partition(disk, bdev))) |
| 542 | return 0; |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 543 | if (IS_ERR(state)) /* I/O error reading the partition table */ |
suzuki | b74a2f0 | 2007-03-16 13:38:25 -0800 | [diff] [blame] | 544 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | for (p = 1; p < state->limit; p++) { |
| 546 | sector_t size = state->parts[p].size; |
| 547 | sector_t from = state->parts[p].from; |
| 548 | if (!size) |
| 549 | continue; |
Mike Miller | 98bd34e | 2006-06-23 02:06:07 -0700 | [diff] [blame] | 550 | if (from + size > get_capacity(disk)) { |
| 551 | printk(" %s: p%d exceeds device capacity\n", |
| 552 | disk->disk_name, p); |
| 553 | } |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 554 | add_partition(disk, p, from, size, state->parts[p].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | #ifdef CONFIG_BLK_DEV_MD |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 556 | if (state->parts[p].flags & ADDPART_FLAG_RAID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | md_autodetect_dev(bdev->bd_dev+p); |
| 558 | #endif |
| 559 | } |
| 560 | kfree(state); |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p) |
| 565 | { |
| 566 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 567 | struct page *page; |
| 568 | |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 569 | page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)), |
| 570 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (!IS_ERR(page)) { |
| 572 | wait_on_page_locked(page); |
| 573 | if (!PageUptodate(page)) |
| 574 | goto fail; |
| 575 | if (PageError(page)) |
| 576 | goto fail; |
| 577 | p->v = page; |
| 578 | return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9); |
| 579 | fail: |
| 580 | page_cache_release(page); |
| 581 | } |
| 582 | p->v = NULL; |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | EXPORT_SYMBOL(read_dev_sector); |
| 587 | |
| 588 | void del_gendisk(struct gendisk *disk) |
| 589 | { |
| 590 | int p; |
| 591 | |
| 592 | /* invalidate stuff */ |
| 593 | for (p = disk->minors - 1; p > 0; p--) { |
| 594 | invalidate_partition(disk, p); |
| 595 | delete_partition(disk, p); |
| 596 | } |
| 597 | invalidate_partition(disk, 0); |
| 598 | disk->capacity = 0; |
| 599 | disk->flags &= ~GENHD_FL_UP; |
| 600 | unlink_gendisk(disk); |
| 601 | disk_stat_set_all(disk, 0); |
Chen, Kenneth W | 20e5c81 | 2005-10-13 21:48:42 +0200 | [diff] [blame] | 602 | disk->stamp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Linus Torvalds | 032ebf2 | 2006-05-12 18:42:09 -0700 | [diff] [blame] | 604 | kobject_uevent(&disk->kobj, KOBJ_REMOVE); |
Mariusz Kozlowski | b92be9f | 2007-02-14 21:03:39 +0100 | [diff] [blame] | 605 | kobject_unregister(disk->holder_dir); |
| 606 | kobject_unregister(disk->slave_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (disk->driverfs_dev) { |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 608 | char *disk_name = make_block_name(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | sysfs_remove_link(&disk->kobj, "device"); |
Greg Kroah-Hartman | 8218ef8 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 610 | if (disk_name) { |
| 611 | sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name); |
| 612 | kfree(disk_name); |
| 613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | put_device(disk->driverfs_dev); |
Linus Torvalds | 032ebf2 | 2006-05-12 18:42:09 -0700 | [diff] [blame] | 615 | disk->driverfs_dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 617 | sysfs_remove_link(&disk->kobj, "subsystem"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | kobject_del(&disk->kobj); |
| 619 | } |