2 * fs/partitions/check.c
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
6 * Re-organised Feb 1998 Russell King
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
13 * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl}
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/kmod.h>
20 #include <linux/ctype.h>
39 #ifdef CONFIG_BLK_DEV_MD
40 extern void md_autodetect_dev(dev_t dev);
43 int warn_no_part = 1; /*This is ugly: should make genhd removable media aware*/
45 static int (*check_part[])(struct parsed_partitions *, struct block_device *) = {
47 * Probe partition formats with tables at disk address 0
48 * that also have an ADFS boot block at 0xdc0.
50 #ifdef CONFIG_ACORN_PARTITION_ICS
53 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
54 adfspart_check_POWERTEC,
56 #ifdef CONFIG_ACORN_PARTITION_EESOX
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
66 #ifdef CONFIG_ACORN_PARTITION_CUMANA
67 adfspart_check_CUMANA,
69 #ifdef CONFIG_ACORN_PARTITION_ADFS
73 #ifdef CONFIG_EFI_PARTITION
74 efi_partition, /* this must come before msdos */
76 #ifdef CONFIG_SGI_PARTITION
79 #ifdef CONFIG_LDM_PARTITION
80 ldm_partition, /* this must come before msdos */
82 #ifdef CONFIG_MSDOS_PARTITION
85 #ifdef CONFIG_OSF_PARTITION
88 #ifdef CONFIG_SUN_PARTITION
91 #ifdef CONFIG_AMIGA_PARTITION
94 #ifdef CONFIG_ATARI_PARTITION
97 #ifdef CONFIG_MAC_PARTITION
100 #ifdef CONFIG_ULTRIX_PARTITION
103 #ifdef CONFIG_IBM_PARTITION
106 #ifdef CONFIG_KARMA_PARTITION
109 #ifdef CONFIG_SYSV68_PARTITION
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).
122 char *disk_name(struct gendisk *hd, int part, char *buf)
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);
129 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
134 const char *bdevname(struct block_device *bdev, char *buf)
136 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
137 return disk_name(bdev->bd_disk, part, buf);
140 EXPORT_SYMBOL(bdevname);
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.
147 const char *__bdevname(dev_t dev, char *buffer)
149 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
150 MAJOR(dev), MINOR(dev));
154 EXPORT_SYMBOL(__bdevname);
156 static struct parsed_partitions *
157 check_partition(struct gendisk *hd, struct block_device *bdev)
159 struct parsed_partitions *state;
162 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
166 disk_name(hd, 0, state->name);
167 printk(KERN_INFO " %s:", state->name);
168 if (isdigit(state->name[strlen(state->name)-1]))
169 sprintf(state->name, "p");
171 state->limit = hd->minors;
173 while (!res && check_part[i]) {
174 memset(&state->parts, 0, sizeof(state->parts));
175 res = check_part[i++](state, bdev);
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.
188 /* The partition is unrecognized. So report I/O errors if there were any */
191 printk(" unknown partition table\n");
192 else if (warn_no_part)
193 printk(" unable to read partition table\n");
198 static ssize_t part_start_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
201 struct hd_struct *p = dev_to_part(dev);
203 return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
206 static ssize_t part_size_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 struct hd_struct *p = dev_to_part(dev);
210 return sprintf(buf, "%llu\n",(unsigned long long)p->nr_sects);
213 static ssize_t part_stat_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct hd_struct *p = dev_to_part(dev);
218 return sprintf(buf, "%8u %8llu %8u %8llu\n",
219 p->ios[0], (unsigned long long)p->sectors[0],
220 p->ios[1], (unsigned long long)p->sectors[1]);
223 #ifdef CONFIG_FAIL_MAKE_REQUEST
224 static ssize_t part_fail_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
227 struct hd_struct *p = dev_to_part(dev);
229 return sprintf(buf, "%d\n", p->make_it_fail);
232 static ssize_t part_fail_store(struct device *dev,
233 struct device_attribute *attr,
234 const char *buf, size_t count)
236 struct hd_struct *p = dev_to_part(dev);
239 if (count > 0 && sscanf(buf, "%d", &i) > 0)
240 p->make_it_fail = (i == 0) ? 0 : 1;
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);
249 #ifdef CONFIG_FAIL_MAKE_REQUEST
250 static struct device_attribute dev_attr_fail =
251 __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
254 static struct attribute *part_attrs[] = {
255 &dev_attr_start.attr,
258 #ifdef CONFIG_FAIL_MAKE_REQUEST
264 static struct attribute_group part_attr_group = {
268 static struct attribute_group *part_attr_groups[] = {
273 static void part_release(struct device *dev)
275 struct hd_struct *p = dev_to_part(dev);
279 struct device_type part_type = {
281 .groups = part_attr_groups,
282 .release = part_release,
285 static inline void partition_sysfs_add_subdir(struct hd_struct *p)
289 k = kobject_get(&p->dev.kobj);
290 p->holder_dir = kobject_create_and_add("holders", k);
294 static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
298 k = kobject_get(&disk->dev.kobj);
299 disk->holder_dir = kobject_create_and_add("holders", k);
300 disk->slave_dir = kobject_create_and_add("slaves", k);
304 void delete_partition(struct gendisk *disk, int part)
306 struct hd_struct *p = disk->part[part-1];
312 disk->part[part-1] = NULL;
315 p->ios[0] = p->ios[1] = 0;
316 p->sectors[0] = p->sectors[1] = 0;
317 kobject_put(p->holder_dir);
322 static ssize_t whole_disk_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
327 static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
328 whole_disk_show, NULL);
330 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
335 p = kzalloc(sizeof(*p), GFP_KERNEL);
339 p->start_sect = start;
342 p->policy = disk->policy;
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);
348 snprintf(p->dev.bus_id, BUS_ID_SIZE,
349 "%s%d", disk->dev.bus_id, part);
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;
358 /* delay uevent until 'holders' subdir is created */
359 p->dev.uevent_suppress = 1;
361 partition_sysfs_add_subdir(p);
362 p->dev.uevent_suppress = 0;
363 if (flags & ADDPART_FLAG_WHOLEDISK)
364 err = device_create_file(&p->dev, &dev_attr_whole_disk);
366 /* suppress uevent if the disk supresses it */
367 if (!disk->dev.uevent_suppress)
368 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
371 /* Not exported, helper to add_disk(). */
372 void register_disk(struct gendisk *disk)
374 struct block_device *bdev;
380 disk->dev.parent = disk->driverfs_dev;
381 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
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, '/');
389 /* delay uevents, until we scanned partition table */
390 disk->dev.uevent_suppress = 1;
392 if (device_add(&disk->dev))
394 #ifndef CONFIG_SYSFS_DEPRECATED
395 err = sysfs_create_link(block_depr, &disk->dev.kobj,
396 kobject_name(&disk->dev.kobj));
398 device_del(&disk->dev);
402 disk_sysfs_add_subdirs(disk);
404 /* No minors to use for partitions */
405 if (disk->minors == 1)
408 /* No such device (e.g., media were just removed) */
409 if (!get_capacity(disk))
412 bdev = bdget_disk(disk, 0);
416 bdev->bd_invalidated = 1;
417 err = blkdev_get(bdev, FMODE_READ, 0);
423 /* announce disk after possible partitions are created */
424 disk->dev.uevent_suppress = 0;
425 kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
427 /* announce possible partitions */
428 for (i = 1; i < disk->minors; i++) {
430 if (!p || !p->nr_sects)
432 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
436 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
438 struct parsed_partitions *state;
441 if (bdev->bd_part_count)
443 res = invalidate_partition(disk, 0);
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)))
453 if (IS_ERR(state)) /* I/O error reading the partition table */
455 for (p = 1; p < state->limit; p++) {
456 sector_t size = state->parts[p].size;
457 sector_t from = state->parts[p].from;
460 if (from + size > get_capacity(disk)) {
461 printk(" %s: p%d exceeds device capacity\n",
464 add_partition(disk, p, from, size, state->parts[p].flags);
465 #ifdef CONFIG_BLK_DEV_MD
466 if (state->parts[p].flags & ADDPART_FLAG_RAID)
467 md_autodetect_dev(bdev->bd_dev+p);
474 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
476 struct address_space *mapping = bdev->bd_inode->i_mapping;
479 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
485 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
487 page_cache_release(page);
493 EXPORT_SYMBOL(read_dev_sector);
495 void del_gendisk(struct gendisk *disk)
499 /* invalidate stuff */
500 for (p = disk->minors - 1; p > 0; p--) {
501 invalidate_partition(disk, p);
502 delete_partition(disk, p);
504 invalidate_partition(disk, 0);
506 disk->flags &= ~GENHD_FL_UP;
507 unlink_gendisk(disk);
508 disk_stat_set_all(disk, 0);
511 kobject_put(disk->holder_dir);
512 kobject_put(disk->slave_dir);
513 disk->driverfs_dev = NULL;
514 #ifndef CONFIG_SYSFS_DEPRECATED
515 sysfs_remove_link(block_depr, disk->dev.bus_id);
517 device_del(&disk->dev);