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 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len, int flags)
327 p = kzalloc(sizeof(*p), GFP_KERNEL);
331 p->start_sect = start;
334 p->policy = disk->policy;
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);
340 snprintf(p->dev.bus_id, BUS_ID_SIZE,
341 "%s%d", disk->dev.bus_id, part);
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;
350 /* delay uevent until 'holders' subdir is created */
351 p->dev.uevent_suppress = 1;
353 partition_sysfs_add_subdir(p);
354 p->dev.uevent_suppress = 0;
355 if (flags & ADDPART_FLAG_WHOLEDISK) {
356 static struct attribute addpartattr = {
357 .name = "whole_disk",
358 .mode = S_IRUSR | S_IRGRP | S_IROTH,
360 err = sysfs_create_file(&p->dev.kobj, &addpartattr);
363 /* suppress uevent if the disk supresses it */
364 if (!disk->dev.uevent_suppress)
365 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
368 /* Not exported, helper to add_disk(). */
369 void register_disk(struct gendisk *disk)
371 struct block_device *bdev;
377 disk->dev.parent = disk->driverfs_dev;
378 disk->dev.devt = MKDEV(disk->major, disk->first_minor);
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, '/');
386 /* delay uevents, until we scanned partition table */
387 disk->dev.uevent_suppress = 1;
389 if (device_add(&disk->dev))
391 #ifndef CONFIG_SYSFS_DEPRECATED
392 err = sysfs_create_link(block_depr, &disk->dev.kobj,
393 kobject_name(&disk->dev.kobj));
395 device_del(&disk->dev);
399 disk_sysfs_add_subdirs(disk);
401 /* No minors to use for partitions */
402 if (disk->minors == 1)
405 /* No such device (e.g., media were just removed) */
406 if (!get_capacity(disk))
409 bdev = bdget_disk(disk, 0);
413 bdev->bd_invalidated = 1;
414 err = blkdev_get(bdev, FMODE_READ, 0);
420 /* announce disk after possible partitions are created */
421 disk->dev.uevent_suppress = 0;
422 kobject_uevent(&disk->dev.kobj, KOBJ_ADD);
424 /* announce possible partitions */
425 for (i = 1; i < disk->minors; i++) {
427 if (!p || !p->nr_sects)
429 kobject_uevent(&p->dev.kobj, KOBJ_ADD);
433 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
435 struct parsed_partitions *state;
438 if (bdev->bd_part_count)
440 res = invalidate_partition(disk, 0);
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)))
450 if (IS_ERR(state)) /* I/O error reading the partition table */
452 for (p = 1; p < state->limit; p++) {
453 sector_t size = state->parts[p].size;
454 sector_t from = state->parts[p].from;
457 if (from + size > get_capacity(disk)) {
458 printk(" %s: p%d exceeds device capacity\n",
461 add_partition(disk, p, from, size, state->parts[p].flags);
462 #ifdef CONFIG_BLK_DEV_MD
463 if (state->parts[p].flags & ADDPART_FLAG_RAID)
464 md_autodetect_dev(bdev->bd_dev+p);
471 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
473 struct address_space *mapping = bdev->bd_inode->i_mapping;
476 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
482 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
484 page_cache_release(page);
490 EXPORT_SYMBOL(read_dev_sector);
492 void del_gendisk(struct gendisk *disk)
496 /* invalidate stuff */
497 for (p = disk->minors - 1; p > 0; p--) {
498 invalidate_partition(disk, p);
499 delete_partition(disk, p);
501 invalidate_partition(disk, 0);
503 disk->flags &= ~GENHD_FL_UP;
504 unlink_gendisk(disk);
505 disk_stat_set_all(disk, 0);
508 kobject_put(disk->holder_dir);
509 kobject_put(disk->slave_dir);
510 disk->driverfs_dev = NULL;
511 #ifndef CONFIG_SYSFS_DEPRECATED
512 sysfs_remove_link(block_depr, disk->dev.bus_id);
514 device_del(&disk->dev);