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 | |
Greg Kroah-Hartman | f4a00a2 | 2008-02-06 23:33:39 -0800 | [diff] [blame^] | 322 | static ssize_t whole_disk_show(struct device *dev, |
| 323 | struct device_attribute *attr, char *buf) |
| 324 | { |
| 325 | return 0; |
| 326 | } |
| 327 | static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH, |
| 328 | whole_disk_show, NULL); |
| 329 | |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 330 | 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] | 331 | { |
| 332 | struct hd_struct *p; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 333 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 335 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (!p) |
| 337 | return; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 344 | if (isdigit(disk->dev.bus_id[strlen(disk->dev.bus_id)-1])) |
| 345 | snprintf(p->dev.bus_id, BUS_ID_SIZE, |
| 346 | "%sp%d", disk->dev.bus_id, part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 348 | snprintf(p->dev.bus_id, BUS_ID_SIZE, |
| 349 | "%s%d", disk->dev.bus_id, part); |
| 350 | |
| 351 | device_initialize(&p->dev); |
| 352 | p->dev.devt = MKDEV(disk->major, disk->first_minor + part); |
| 353 | p->dev.class = &block_class; |
| 354 | p->dev.type = &part_type; |
| 355 | p->dev.parent = &disk->dev; |
| 356 | disk->part[part-1] = p; |
| 357 | |
| 358 | /* delay uevent until 'holders' subdir is created */ |
| 359 | p->dev.uevent_suppress = 1; |
| 360 | device_add(&p->dev); |
| 361 | partition_sysfs_add_subdir(p); |
| 362 | p->dev.uevent_suppress = 0; |
Greg Kroah-Hartman | f4a00a2 | 2008-02-06 23:33:39 -0800 | [diff] [blame^] | 363 | if (flags & ADDPART_FLAG_WHOLEDISK) |
| 364 | err = device_create_file(&p->dev, &dev_attr_whole_disk); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 365 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 366 | /* suppress uevent if the disk supresses it */ |
| 367 | if (!disk->dev.uevent_suppress) |
| 368 | kobject_uevent(&p->dev.kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* Not exported, helper to add_disk(). */ |
| 372 | void register_disk(struct gendisk *disk) |
| 373 | { |
| 374 | struct block_device *bdev; |
| 375 | char *s; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 376 | int i; |
| 377 | struct hd_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | int err; |
| 379 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 380 | disk->dev.parent = disk->driverfs_dev; |
| 381 | disk->dev.devt = MKDEV(disk->major, disk->first_minor); |
| 382 | |
| 383 | strlcpy(disk->dev.bus_id, disk->disk_name, KOBJ_NAME_LEN); |
| 384 | /* ewww... some of these buggers have / in the name... */ |
| 385 | s = strchr(disk->dev.bus_id, '/'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | if (s) |
| 387 | *s = '!'; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 388 | |
| 389 | /* delay uevents, until we scanned partition table */ |
| 390 | disk->dev.uevent_suppress = 1; |
| 391 | |
| 392 | if (device_add(&disk->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 394 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 395 | err = sysfs_create_link(block_depr, &disk->dev.kobj, |
| 396 | kobject_name(&disk->dev.kobj)); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 397 | if (err) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 398 | device_del(&disk->dev); |
Jeff Garzik | eee44cc | 2006-10-17 00:10:23 -0700 | [diff] [blame] | 399 | return; |
| 400 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 401 | #endif |
| 402 | disk_sysfs_add_subdirs(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | /* No minors to use for partitions */ |
Greg Kroah-Hartman | a296418 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 405 | if (disk->minors == 1) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 406 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | /* No such device (e.g., media were just removed) */ |
| 409 | if (!get_capacity(disk)) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 410 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | bdev = bdget_disk(disk, 0); |
| 413 | if (!bdev) |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 414 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | bdev->bd_invalidated = 1; |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 417 | err = blkdev_get(bdev, FMODE_READ, 0); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 418 | if (err < 0) |
| 419 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | blkdev_put(bdev); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 421 | |
| 422 | exit: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 423 | /* announce disk after possible partitions are created */ |
| 424 | disk->dev.uevent_suppress = 0; |
| 425 | kobject_uevent(&disk->dev.kobj, KOBJ_ADD); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 426 | |
| 427 | /* announce possible partitions */ |
| 428 | for (i = 1; i < disk->minors; i++) { |
| 429 | p = disk->part[i-1]; |
| 430 | if (!p || !p->nr_sects) |
| 431 | continue; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 432 | kobject_uevent(&p->dev.kobj, KOBJ_ADD); |
Kay Sievers | d4d7e5d | 2006-03-24 20:45:35 +0100 | [diff] [blame] | 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | int rescan_partitions(struct gendisk *disk, struct block_device *bdev) |
| 437 | { |
| 438 | struct parsed_partitions *state; |
| 439 | int p, res; |
| 440 | |
| 441 | if (bdev->bd_part_count) |
| 442 | return -EBUSY; |
| 443 | res = invalidate_partition(disk, 0); |
| 444 | if (res) |
| 445 | return res; |
| 446 | bdev->bd_invalidated = 0; |
| 447 | for (p = 1; p < disk->minors; p++) |
| 448 | delete_partition(disk, p); |
| 449 | if (disk->fops->revalidate_disk) |
| 450 | disk->fops->revalidate_disk(disk); |
| 451 | if (!get_capacity(disk) || !(state = check_partition(disk, bdev))) |
| 452 | return 0; |
Suzuki Kp | 5127d00 | 2006-12-06 20:35:14 -0800 | [diff] [blame] | 453 | if (IS_ERR(state)) /* I/O error reading the partition table */ |
suzuki | b74a2f0 | 2007-03-16 13:38:25 -0800 | [diff] [blame] | 454 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | for (p = 1; p < state->limit; p++) { |
| 456 | sector_t size = state->parts[p].size; |
| 457 | sector_t from = state->parts[p].from; |
| 458 | if (!size) |
| 459 | continue; |
Mike Miller | 98bd34e | 2006-06-23 02:06:07 -0700 | [diff] [blame] | 460 | if (from + size > get_capacity(disk)) { |
| 461 | printk(" %s: p%d exceeds device capacity\n", |
| 462 | disk->disk_name, p); |
| 463 | } |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 464 | add_partition(disk, p, from, size, state->parts[p].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | #ifdef CONFIG_BLK_DEV_MD |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 466 | if (state->parts[p].flags & ADDPART_FLAG_RAID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | md_autodetect_dev(bdev->bd_dev+p); |
| 468 | #endif |
| 469 | } |
| 470 | kfree(state); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p) |
| 475 | { |
| 476 | struct address_space *mapping = bdev->bd_inode->i_mapping; |
| 477 | struct page *page; |
| 478 | |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 479 | page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)), |
| 480 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (!IS_ERR(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (PageError(page)) |
| 483 | goto fail; |
| 484 | p->v = page; |
| 485 | return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9); |
| 486 | fail: |
| 487 | page_cache_release(page); |
| 488 | } |
| 489 | p->v = NULL; |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | EXPORT_SYMBOL(read_dev_sector); |
| 494 | |
| 495 | void del_gendisk(struct gendisk *disk) |
| 496 | { |
| 497 | int p; |
| 498 | |
| 499 | /* invalidate stuff */ |
| 500 | for (p = disk->minors - 1; p > 0; p--) { |
| 501 | invalidate_partition(disk, p); |
| 502 | delete_partition(disk, p); |
| 503 | } |
| 504 | invalidate_partition(disk, 0); |
| 505 | disk->capacity = 0; |
| 506 | disk->flags &= ~GENHD_FL_UP; |
| 507 | unlink_gendisk(disk); |
| 508 | disk_stat_set_all(disk, 0); |
Chen, Kenneth W | 20e5c81 | 2005-10-13 21:48:42 +0200 | [diff] [blame] | 509 | disk->stamp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 511 | kobject_put(disk->holder_dir); |
| 512 | kobject_put(disk->slave_dir); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 513 | disk->driverfs_dev = NULL; |
| 514 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 515 | sysfs_remove_link(block_depr, disk->dev.bus_id); |
| 516 | #endif |
| 517 | device_del(&disk->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |