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
110 * disk_name() is used by partition check code and the genhd driver.
111 * It formats the devicename of the indicated disk into
112 * the supplied buffer (of size at least 32), and returns
113 * a pointer to that same buffer (for convenience).
116 char *disk_name(struct gendisk *hd, int part, char *buf)
119 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
120 else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
121 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, part);
123 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, part);
128 const char *bdevname(struct block_device *bdev, char *buf)
130 int part = MINOR(bdev->bd_dev) - bdev->bd_disk->first_minor;
131 return disk_name(bdev->bd_disk, part, buf);
134 EXPORT_SYMBOL(bdevname);
137 * There's very little reason to use this, you should really
138 * have a struct block_device just about everywhere and use
139 * bdevname() instead.
141 const char *__bdevname(dev_t dev, char *buffer)
143 scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
144 MAJOR(dev), MINOR(dev));
148 EXPORT_SYMBOL(__bdevname);
150 static struct parsed_partitions *
151 check_partition(struct gendisk *hd, struct block_device *bdev)
153 struct parsed_partitions *state;
156 state = kmalloc(sizeof(struct parsed_partitions), GFP_KERNEL);
160 #ifdef CONFIG_DEVFS_FS
161 if (hd->devfs_name[0] != '\0') {
162 printk(KERN_INFO " /dev/%s:", hd->devfs_name);
163 sprintf(state->name, "p");
167 disk_name(hd, 0, state->name);
168 printk(KERN_INFO " %s:", state->name);
169 if (isdigit(state->name[strlen(state->name)-1]))
170 sprintf(state->name, "p");
172 state->limit = hd->minors;
174 while (!res && check_part[i]) {
175 memset(&state->parts, 0, sizeof(state->parts));
176 res = check_part[i++](state, bdev);
181 printk(" unknown partition table\n");
182 else if (warn_no_part)
183 printk(" unable to read partition table\n");
189 * sysfs bindings for partitions
192 struct part_attribute {
193 struct attribute attr;
194 ssize_t (*show)(struct hd_struct *,char *);
198 part_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
200 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
201 struct part_attribute * part_attr = container_of(attr,struct part_attribute,attr);
204 ret = part_attr->show(p,page);
208 static struct sysfs_ops part_sysfs_ops = {
209 .show = part_attr_show,
212 static ssize_t part_dev_read(struct hd_struct * p, char *page)
214 struct gendisk *disk = container_of(p->kobj.parent,struct gendisk,kobj);
215 dev_t dev = MKDEV(disk->major, disk->first_minor + p->partno);
216 return print_dev_t(page, dev);
218 static ssize_t part_start_read(struct hd_struct * p, char *page)
220 return sprintf(page, "%llu\n",(unsigned long long)p->start_sect);
222 static ssize_t part_size_read(struct hd_struct * p, char *page)
224 return sprintf(page, "%llu\n",(unsigned long long)p->nr_sects);
226 static ssize_t part_stat_read(struct hd_struct * p, char *page)
228 return sprintf(page, "%8u %8llu %8u %8llu\n",
229 p->reads, (unsigned long long)p->read_sectors,
230 p->writes, (unsigned long long)p->write_sectors);
232 static struct part_attribute part_attr_dev = {
233 .attr = {.name = "dev", .mode = S_IRUGO },
234 .show = part_dev_read
236 static struct part_attribute part_attr_start = {
237 .attr = {.name = "start", .mode = S_IRUGO },
238 .show = part_start_read
240 static struct part_attribute part_attr_size = {
241 .attr = {.name = "size", .mode = S_IRUGO },
242 .show = part_size_read
244 static struct part_attribute part_attr_stat = {
245 .attr = {.name = "stat", .mode = S_IRUGO },
246 .show = part_stat_read
249 static struct attribute * default_attrs[] = {
251 &part_attr_start.attr,
252 &part_attr_size.attr,
253 &part_attr_stat.attr,
257 extern struct subsystem block_subsys;
259 static void part_release(struct kobject *kobj)
261 struct hd_struct * p = container_of(kobj,struct hd_struct,kobj);
265 struct kobj_type ktype_part = {
266 .release = part_release,
267 .default_attrs = default_attrs,
268 .sysfs_ops = &part_sysfs_ops,
271 void delete_partition(struct gendisk *disk, int part)
273 struct hd_struct *p = disk->part[part-1];
278 disk->part[part-1] = NULL;
281 p->reads = p->writes = p->read_sectors = p->write_sectors = 0;
282 devfs_remove("%s/part%d", disk->devfs_name, part);
283 kobject_unregister(&p->kobj);
286 void add_partition(struct gendisk *disk, int part, sector_t start, sector_t len)
290 p = kmalloc(sizeof(*p), GFP_KERNEL);
294 memset(p, 0, sizeof(*p));
295 p->start_sect = start;
299 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor + part),
300 S_IFBLK|S_IRUSR|S_IWUSR,
301 "%s/part%d", disk->devfs_name, part);
303 if (isdigit(disk->kobj.name[strlen(disk->kobj.name)-1]))
304 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%sp%d",disk->kobj.name,part);
306 snprintf(p->kobj.name,KOBJ_NAME_LEN,"%s%d",disk->kobj.name,part);
307 p->kobj.parent = &disk->kobj;
308 p->kobj.ktype = &ktype_part;
309 kobject_register(&p->kobj);
310 disk->part[part-1] = p;
313 static void disk_sysfs_symlinks(struct gendisk *disk)
315 struct device *target = get_device(disk->driverfs_dev);
317 sysfs_create_link(&disk->kobj,&target->kobj,"device");
318 sysfs_create_link(&target->kobj,&disk->kobj,"block");
322 /* Not exported, helper to add_disk(). */
323 void register_disk(struct gendisk *disk)
325 struct block_device *bdev;
329 strlcpy(disk->kobj.name,disk->disk_name,KOBJ_NAME_LEN);
330 /* ewww... some of these buggers have / in name... */
331 s = strchr(disk->kobj.name, '/');
334 if ((err = kobject_add(&disk->kobj)))
336 disk_sysfs_symlinks(disk);
337 kobject_hotplug(&disk->kobj, KOBJ_ADD);
339 /* No minors to use for partitions */
340 if (disk->minors == 1) {
341 if (disk->devfs_name[0] != '\0')
342 devfs_add_disk(disk);
346 /* always add handle for the whole disk */
347 devfs_add_partitioned(disk);
349 /* No such device (e.g., media were just removed) */
350 if (!get_capacity(disk))
353 bdev = bdget_disk(disk, 0);
357 bdev->bd_invalidated = 1;
358 if (blkdev_get(bdev, FMODE_READ, 0) < 0)
363 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
365 struct parsed_partitions *state;
368 if (bdev->bd_part_count)
370 res = invalidate_partition(disk, 0);
373 bdev->bd_invalidated = 0;
374 for (p = 1; p < disk->minors; p++)
375 delete_partition(disk, p);
376 if (disk->fops->revalidate_disk)
377 disk->fops->revalidate_disk(disk);
378 if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
380 for (p = 1; p < state->limit; p++) {
381 sector_t size = state->parts[p].size;
382 sector_t from = state->parts[p].from;
385 add_partition(disk, p, from, size);
386 #ifdef CONFIG_BLK_DEV_MD
387 if (state->parts[p].flags)
388 md_autodetect_dev(bdev->bd_dev+p);
395 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
397 struct address_space *mapping = bdev->bd_inode->i_mapping;
400 page = read_cache_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
401 (filler_t *)mapping->a_ops->readpage, NULL);
403 wait_on_page_locked(page);
404 if (!PageUptodate(page))
409 return (unsigned char *)page_address(page) + ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
411 page_cache_release(page);
417 EXPORT_SYMBOL(read_dev_sector);
419 void del_gendisk(struct gendisk *disk)
423 /* invalidate stuff */
424 for (p = disk->minors - 1; p > 0; p--) {
425 invalidate_partition(disk, p);
426 delete_partition(disk, p);
428 invalidate_partition(disk, 0);
430 disk->flags &= ~GENHD_FL_UP;
431 unlink_gendisk(disk);
432 disk_stat_set_all(disk, 0);
435 devfs_remove_disk(disk);
437 if (disk->driverfs_dev) {
438 sysfs_remove_link(&disk->kobj, "device");
439 sysfs_remove_link(&disk->driverfs_dev->kobj, "block");
440 put_device(disk->driverfs_dev);
442 kobject_hotplug(&disk->kobj, KOBJ_REMOVE);
443 kobject_del(&disk->kobj);