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>
21 #include <linux/devfs_fs_kernel.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
113 * disk_name() is used by partition check code and the genhd driver.
114 * It formats the devicename of the indicated disk into
115 * the supplied buffer (of size at least 32), and returns
116 * a pointer to that same buffer (for convenience).
119 char *disk_name(struct gendisk *hd, int part, char *buf)
122 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
123 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
124 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
126 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
131 const char *bdevname(struct block_device *bdev, char *buf)
133 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
134 return disk_name(bdev->bd_disk, part, buf);
137 EXPORT_SYMBOL(bdevname);
140 * There's very little reason to use this, you should really
141 * have a struct block_device just about everywhere and use
142 * bdevname() instead.
144 const char *__bdevname(dev_t dev, char *buffer)
146 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
147 MAJOR(dev), MINOR(dev));
151 EXPORT_SYMBOL(__bdevname);
153 static struct parsed_partitions *
154 check_partition(struct gendisk *hd, struct block_device *bdev)
156 struct parsed_partitions *state;
159 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
163 disk_name(hd, 0, state->name);
164 printk(KERN_INFO " %s:", state->name);
165 if (isdigit(state->name[strlen(state->name)-1]))
166 sprintf(state->name, "p");
168 state->limit = hd->minors;
170 while (!res && check_part[i]) {
171 memset(&state->parts, 0, sizeof(state->parts));
172 res = check_part[i++](state, bdev);
177 printk(" unknown partition table\n");
178 else if (warn_no_part)
179 printk(" unable to read partition table\n");
185 * sysfs bindings for partitions
188 struct part_attribute {
189 struct attribute attr;
190 ssize_t (*show)(struct hd_struct *,char *);
191 ssize_t (*store)(struct hd_struct *,const char *, size_t);
195 part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
197 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
198 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
201 ret = part_attr->show(p, page);
205 part_attr_store(struct kobject * kobj, struct attribute * attr,
206 const char *page, size_t count)
208 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
209 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
212 if (part_attr->store)
213 ret = part_attr->store(p, page, count);
217 static struct sysfs_ops part_sysfs_ops = {
218 .show = part_attr_show,
219 .store = part_attr_store,
222 static ssize_t part_uevent_store(struct hd_struct * p,
223 const char *page, size_t count)
225 kobject_uevent(&p->kobj, KOBJ_ADD);
228 static ssize_t part_dev_read(struct hd_struct * p, char *page)
230 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
231 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
232 return print_dev_t(page, dev);
234 static ssize_t part_start_read(struct hd_struct * p, char *page)
236 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
238 static ssize_t part_size_read(struct hd_struct * p, char *page)
240 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
242 static ssize_t part_stat_read(struct hd_struct * p, char *page)
244 return sprintf(page, "%8u %8llu %8u %8llu\n",
245 p->ios[0], (unsigned long long)p->sectors[0],
246 p->ios[1], (unsigned long long)p->sectors[1]);
248 static struct part_attribute part_attr_uevent = {
249 .attr = {.name = "uevent", .mode = S_IWUSR },
250 .store = part_uevent_store
252 static struct part_attribute part_attr_dev = {
253 .attr = {.name = "dev", .mode = S_IRUGO },
254 .show = part_dev_read
256 static struct part_attribute part_attr_start = {
257 .attr = {.name = "start", .mode = S_IRUGO },
258 .show = part_start_read
260 static struct part_attribute part_attr_size = {
261 .attr = {.name = "size", .mode = S_IRUGO },
262 .show = part_size_read
264 static struct part_attribute part_attr_stat = {
265 .attr = {.name = "stat", .mode = S_IRUGO },
266 .show = part_stat_read
269 static struct attribute * default_attrs[] = {
270 &part_attr_uevent.attr,
272 &part_attr_start.attr,
273 &part_attr_size.attr,
274 &part_attr_stat.attr,
278 extern struct subsystem block_subsys;
280 static void part_release(struct kobject *kobj)
282 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
286 struct kobj_type ktype_part = {
287 .release = part_release,
288 .default_attrs = default_attrs,
289 .sysfs_ops = &part_sysfs_ops,
292 static inline void partition_sysfs_add_subdir(struct hd_struct *p)
296 k = kobject_get(&p->kobj);
297 p->holder_dir = kobject_add_dir(k, "holders");
301 static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
305 k = kobject_get(&disk->kobj);
306 disk->holder_dir = kobject_add_dir(k, "holders");
307 disk->slave_dir = kobject_add_dir(k, "slaves");
311 void delete_partition(struct gendisk *disk, int part)
313 struct hd_struct *p = disk->part[part-1];
318 disk->part[part-1] = NULL;
321 p->ios[0] = p->ios[1] = 0;
322 p->sectors[0] = p->sectors[1] = 0;
323 devfs_remove("%s/part%d", disk->devfs_name, part);
324 sysfs_remove_link(&p->kobj, "subsystem");
326 kobject_unregister(p->holder_dir);
327 kobject_uevent(&p->kobj, KOBJ_REMOVE);
328 kobject_del(&p->kobj);
329 kobject_put(&p->kobj);
332 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len)
336 p = kmalloc(sizeof(*p), GFP_KERNEL);
340 memset(p, 0, sizeof(*p));
341 p->start_sect = start;
345 if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
346 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
348 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
349 p->kobj.parent = &disk->kobj;
350 p->kobj.ktype = &ktype_part;
351 kobject_init(&p->kobj);
352 kobject_add(&p->kobj);
353 if (!disk->part_uevent_suppress)
354 kobject_uevent(&p->kobj, KOBJ_ADD);
355 sysfs_create_link(&p->kobj, &block_subsys.kset.kobj, "subsystem");
356 partition_sysfs_add_subdir(p);
357 disk->part[part-1] = p;
360 static char *make_block_name(struct gendisk *disk)
363 static char *block_str = "block:";
367 size = strlen(block_str) + strlen(disk->disk_name) + 1;
368 name = kmalloc(size, GFP_KERNEL);
371 strcpy(name, block_str);
372 strcat(name, disk->disk_name);
373 /* ewww... some of these buggers have / in name... */
374 s = strchr(name, '/');
380 static void disk_sysfs_symlinks(struct gendisk *disk)
382 struct device *target = get_device(disk->driverfs_dev);
384 char *disk_name = make_block_name(disk);
385 sysfs_create_link(&disk->kobj,&target->kobj,"device");
387 sysfs_create_link(&target->kobj,&disk->kobj,disk_name);
391 sysfs_create_link(&disk->kobj, &block_subsys.kset.kobj, "subsystem");
394 /* Not exported, helper to add_disk(). */
395 void register_disk(struct gendisk *disk)
397 struct block_device *bdev;
403 strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
404 /* ewww... some of these buggers have / in name... */
405 s = strchr(disk->kobj.name, '/');
408 if ((err = kobject_add(&disk->kobj)))
410 disk_sysfs_symlinks(disk);
411 disk_sysfs_add_subdirs(disk);
413 /* No minors to use for partitions */
414 if (disk->minors == 1)
417 /* No such device (e.g., media were just removed) */
418 if (!get_capacity(disk))
421 bdev = bdget_disk(disk, 0);
425 /* scan partition table, but suppress uevents */
426 bdev->bd_invalidated = 1;
427 disk->part_uevent_suppress = 1;
428 err = blkdev_get(bdev, FMODE_READ, 0);
429 disk->part_uevent_suppress = 0;
435 /* announce disk after possible partitions are already created */
436 kobject_uevent(&disk->kobj, KOBJ_ADD);
438 /* announce possible partitions */
439 for (i = 1; i < disk->minors; i++) {
441 if (!p || !p->nr_sects)
443 kobject_uevent(&p->kobj, KOBJ_ADD);
447 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
449 struct parsed_partitions *state;
452 if (bdev->bd_part_count)
454 res = invalidate_partition(disk, 0);
457 bdev->bd_invalidated = 0;
458 for (p = 1; p < disk->minors; p++)
459 delete_partition(disk, p);
460 if (disk->fops->revalidate_disk)
461 disk->fops->revalidate_disk(disk);
462 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
464 for (p = 1; p < state->limit; p++) {
465 sector_t size = state->parts[p].size;
466 sector_t from = state->parts[p].from;
469 if (from + size > get_capacity(disk)) {
470 printk(" %s: p%d exceeds device capacity\n",
473 add_partition(disk, p, from, size);
474 #ifdef CONFIG_BLK_DEV_MD
475 if (state->parts[p].flags)
476 md_autodetect_dev(bdev->bd_dev+p);
483 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
485 struct address_space *mapping = bdev->bd_inode->i_mapping;
488 page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
491 wait_on_page_locked(page);
492 if (!PageUptodate(page))
497 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
499 page_cache_release(page);
505 EXPORT_SYMBOL(read_dev_sector);
507 void del_gendisk(struct gendisk *disk)
511 /* invalidate stuff */
512 for (p = disk->minors - 1; p > 0; p--) {
513 invalidate_partition(disk, p);
514 delete_partition(disk, p);
516 invalidate_partition(disk, 0);
518 disk->flags &= ~GENHD_FL_UP;
519 unlink_gendisk(disk);
520 disk_stat_set_all(disk, 0);
523 kobject_uevent(&disk->kobj, KOBJ_REMOVE);
524 if (disk->holder_dir)
525 kobject_unregister(disk->holder_dir);
527 kobject_unregister(disk->slave_dir);
528 if (disk->driverfs_dev) {
529 char *disk_name = make_block_name(disk);
530 sysfs_remove_link(&disk->kobj, "device");
532 sysfs_remove_link(&disk->driverfs_dev->kobj, disk_name);
535 put_device(disk->driverfs_dev);
536 disk->driverfs_dev = NULL;
538 sysfs_remove_link(&disk->kobj, "subsystem");
539 kobject_del(&disk->kobj);