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/genhd.h>
40 #ifdef CONFIG_BLK_DEV_MD
41 extern void md_autodetect_dev(dev_t dev
);
44 int warn_no_part
= 1; /*This is ugly: should make genhd removable media aware*/
46 static int (*check_part
[])(struct parsed_partitions
*, struct block_device
*) = {
48 * Probe partition formats with tables at disk address 0
49 * that also have an ADFS boot block at 0xdc0.
51 #ifdef CONFIG_ACORN_PARTITION_ICS
54 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
55 adfspart_check_POWERTEC
,
57 #ifdef CONFIG_ACORN_PARTITION_EESOX
62 * Now move on to formats that only have partition info at
63 * disk address 0xdc0. Since these may also have stale
64 * PC/BIOS partition tables, they need to come before
67 #ifdef CONFIG_ACORN_PARTITION_CUMANA
68 adfspart_check_CUMANA
,
70 #ifdef CONFIG_ACORN_PARTITION_ADFS
74 #ifdef CONFIG_EFI_PARTITION
75 efi_partition
, /* this must come before msdos */
77 #ifdef CONFIG_SGI_PARTITION
80 #ifdef CONFIG_LDM_PARTITION
81 ldm_partition
, /* this must come before msdos */
83 #ifdef CONFIG_MSDOS_PARTITION
86 #ifdef CONFIG_OSF_PARTITION
89 #ifdef CONFIG_SUN_PARTITION
92 #ifdef CONFIG_AMIGA_PARTITION
95 #ifdef CONFIG_ATARI_PARTITION
98 #ifdef CONFIG_MAC_PARTITION
101 #ifdef CONFIG_ULTRIX_PARTITION
104 #ifdef CONFIG_IBM_PARTITION
107 #ifdef CONFIG_KARMA_PARTITION
110 #ifdef CONFIG_SYSV68_PARTITION
117 * disk_name() is used by partition check code and the genhd driver.
118 * It formats the devicename of the indicated disk into
119 * the supplied buffer (of size at least 32), and returns
120 * a pointer to that same buffer (for convenience).
123 char *disk_name(struct gendisk
*hd
, int part
, char *buf
)
126 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
127 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
128 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, part
);
130 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, part
);
135 const char *bdevname(struct block_device
*bdev
, char *buf
)
137 int part
= MINOR(bdev
->bd_dev
) - bdev
->bd_disk
->first_minor
;
138 return disk_name(bdev
->bd_disk
, part
, buf
);
141 EXPORT_SYMBOL(bdevname
);
144 * There's very little reason to use this, you should really
145 * have a struct block_device just about everywhere and use
146 * bdevname() instead.
148 const char *__bdevname(dev_t dev
, char *buffer
)
150 scnprintf(buffer
, BDEVNAME_SIZE
, "unknown-block(%u,%u)",
151 MAJOR(dev
), MINOR(dev
));
155 EXPORT_SYMBOL(__bdevname
);
157 static struct parsed_partitions
*
158 check_partition(struct gendisk
*hd
, struct block_device
*bdev
)
160 struct parsed_partitions
*state
;
163 state
= kmalloc(sizeof(struct parsed_partitions
), GFP_KERNEL
);
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
);
178 /* We have hit an I/O error which we don't report now.
179 * But record it, and let the others do their job.
189 /* The partition is unrecognized. So report I/O errors if there were any */
192 printk(" unknown partition table\n");
193 else if (warn_no_part
)
194 printk(" unable to read partition table\n");
199 static ssize_t
part_start_show(struct device
*dev
,
200 struct device_attribute
*attr
, char *buf
)
202 struct hd_struct
*p
= dev_to_part(dev
);
204 return sprintf(buf
, "%llu\n",(unsigned long long)p
->start_sect
);
207 static ssize_t
part_size_show(struct device
*dev
,
208 struct device_attribute
*attr
, char *buf
)
210 struct hd_struct
*p
= dev_to_part(dev
);
211 return sprintf(buf
, "%llu\n",(unsigned long long)p
->nr_sects
);
214 static ssize_t
part_stat_show(struct device
*dev
,
215 struct device_attribute
*attr
, char *buf
)
217 struct hd_struct
*p
= dev_to_part(dev
);
223 "%8lu %8lu %8llu %8u "
224 "%8lu %8lu %8llu %8u "
227 part_stat_read(p
, ios
[READ
]),
228 part_stat_read(p
, merges
[READ
]),
229 (unsigned long long)part_stat_read(p
, sectors
[READ
]),
230 jiffies_to_msecs(part_stat_read(p
, ticks
[READ
])),
231 part_stat_read(p
, ios
[WRITE
]),
232 part_stat_read(p
, merges
[WRITE
]),
233 (unsigned long long)part_stat_read(p
, sectors
[WRITE
]),
234 jiffies_to_msecs(part_stat_read(p
, ticks
[WRITE
])),
236 jiffies_to_msecs(part_stat_read(p
, io_ticks
)),
237 jiffies_to_msecs(part_stat_read(p
, time_in_queue
)));
240 #ifdef CONFIG_FAIL_MAKE_REQUEST
241 static ssize_t
part_fail_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
244 struct hd_struct
*p
= dev_to_part(dev
);
246 return sprintf(buf
, "%d\n", p
->make_it_fail
);
249 static ssize_t
part_fail_store(struct device
*dev
,
250 struct device_attribute
*attr
,
251 const char *buf
, size_t count
)
253 struct hd_struct
*p
= dev_to_part(dev
);
256 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0)
257 p
->make_it_fail
= (i
== 0) ? 0 : 1;
263 static DEVICE_ATTR(start
, S_IRUGO
, part_start_show
, NULL
);
264 static DEVICE_ATTR(size
, S_IRUGO
, part_size_show
, NULL
);
265 static DEVICE_ATTR(stat
, S_IRUGO
, part_stat_show
, NULL
);
266 #ifdef CONFIG_FAIL_MAKE_REQUEST
267 static struct device_attribute dev_attr_fail
=
268 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, part_fail_show
, part_fail_store
);
271 static struct attribute
*part_attrs
[] = {
272 &dev_attr_start
.attr
,
275 #ifdef CONFIG_FAIL_MAKE_REQUEST
281 static struct attribute_group part_attr_group
= {
285 static struct attribute_group
*part_attr_groups
[] = {
290 static void part_release(struct device
*dev
)
292 struct hd_struct
*p
= dev_to_part(dev
);
297 struct device_type part_type
= {
299 .groups
= part_attr_groups
,
300 .release
= part_release
,
303 static inline void partition_sysfs_add_subdir(struct hd_struct
*p
)
307 k
= kobject_get(&p
->dev
.kobj
);
308 p
->holder_dir
= kobject_create_and_add("holders", k
);
312 static inline void disk_sysfs_add_subdirs(struct gendisk
*disk
)
316 k
= kobject_get(&disk
->dev
.kobj
);
317 disk
->holder_dir
= kobject_create_and_add("holders", k
);
318 disk
->slave_dir
= kobject_create_and_add("slaves", k
);
322 void delete_partition(struct gendisk
*disk
, int part
)
324 struct hd_struct
*p
= disk
->part
[part
-1];
330 disk
->part
[part
-1] = NULL
;
333 part_stat_set_all(p
, 0);
334 kobject_put(p
->holder_dir
);
339 static ssize_t
whole_disk_show(struct device
*dev
,
340 struct device_attribute
*attr
, char *buf
)
344 static DEVICE_ATTR(whole_disk
, S_IRUSR
| S_IRGRP
| S_IROTH
,
345 whole_disk_show
, NULL
);
347 void add_partition(struct gendisk
*disk
, int part
, sector_t start
, sector_t len
, int flags
)
352 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
356 if (!init_part_stats(p
)) {
360 p
->start_sect
= start
;
363 p
->policy
= disk
->policy
;
365 if (isdigit(disk
->dev
.bus_id
[strlen(disk
->dev
.bus_id
)-1]))
366 snprintf(p
->dev
.bus_id
, BUS_ID_SIZE
,
367 "%sp%d", disk
->dev
.bus_id
, part
);
369 snprintf(p
->dev
.bus_id
, BUS_ID_SIZE
,
370 "%s%d", disk
->dev
.bus_id
, part
);
372 device_initialize(&p
->dev
);
373 p
->dev
.devt
= MKDEV(disk
->major
, disk
->first_minor
+ part
);
374 p
->dev
.class = &block_class
;
375 p
->dev
.type
= &part_type
;
376 p
->dev
.parent
= &disk
->dev
;
377 disk
->part
[part
-1] = p
;
379 /* delay uevent until 'holders' subdir is created */
380 p
->dev
.uevent_suppress
= 1;
382 partition_sysfs_add_subdir(p
);
383 p
->dev
.uevent_suppress
= 0;
384 if (flags
& ADDPART_FLAG_WHOLEDISK
)
385 err
= device_create_file(&p
->dev
, &dev_attr_whole_disk
);
387 /* suppress uevent if the disk supresses it */
388 if (!disk
->dev
.uevent_suppress
)
389 kobject_uevent(&p
->dev
.kobj
, KOBJ_ADD
);
392 /* Not exported, helper to add_disk(). */
393 void register_disk(struct gendisk
*disk
)
395 struct block_device
*bdev
;
401 disk
->dev
.parent
= disk
->driverfs_dev
;
402 disk
->dev
.devt
= MKDEV(disk
->major
, disk
->first_minor
);
404 strlcpy(disk
->dev
.bus_id
, disk
->disk_name
, BUS_ID_SIZE
);
405 /* ewww... some of these buggers have / in the name... */
406 s
= strchr(disk
->dev
.bus_id
, '/');
410 /* delay uevents, until we scanned partition table */
411 disk
->dev
.uevent_suppress
= 1;
413 if (device_add(&disk
->dev
))
415 #ifndef CONFIG_SYSFS_DEPRECATED
416 err
= sysfs_create_link(block_depr
, &disk
->dev
.kobj
,
417 kobject_name(&disk
->dev
.kobj
));
419 device_del(&disk
->dev
);
423 disk_sysfs_add_subdirs(disk
);
425 /* No minors to use for partitions */
426 if (disk
->minors
== 1)
429 /* No such device (e.g., media were just removed) */
430 if (!get_capacity(disk
))
433 bdev
= bdget_disk(disk
, 0);
437 bdev
->bd_invalidated
= 1;
438 err
= blkdev_get(bdev
, FMODE_READ
, 0);
444 /* announce disk after possible partitions are created */
445 disk
->dev
.uevent_suppress
= 0;
446 kobject_uevent(&disk
->dev
.kobj
, KOBJ_ADD
);
448 /* announce possible partitions */
449 for (i
= 1; i
< disk
->minors
; i
++) {
451 if (!p
|| !p
->nr_sects
)
453 kobject_uevent(&p
->dev
.kobj
, KOBJ_ADD
);
457 int rescan_partitions(struct gendisk
*disk
, struct block_device
*bdev
)
459 struct parsed_partitions
*state
;
462 if (bdev
->bd_part_count
)
464 res
= invalidate_partition(disk
, 0);
467 bdev
->bd_invalidated
= 0;
468 for (p
= 1; p
< disk
->minors
; p
++)
469 delete_partition(disk
, p
);
470 if (disk
->fops
->revalidate_disk
)
471 disk
->fops
->revalidate_disk(disk
);
472 if (!get_capacity(disk
) || !(state
= check_partition(disk
, bdev
)))
474 if (IS_ERR(state
)) /* I/O error reading the partition table */
477 /* tell userspace that the media / partition table may have changed */
478 kobject_uevent(&disk
->dev
.kobj
, KOBJ_CHANGE
);
480 for (p
= 1; p
< state
->limit
; p
++) {
481 sector_t size
= state
->parts
[p
].size
;
482 sector_t from
= state
->parts
[p
].from
;
485 if (from
+ size
> get_capacity(disk
)) {
486 printk(" %s: p%d exceeds device capacity\n",
489 add_partition(disk
, p
, from
, size
, state
->parts
[p
].flags
);
490 #ifdef CONFIG_BLK_DEV_MD
491 if (state
->parts
[p
].flags
& ADDPART_FLAG_RAID
)
492 md_autodetect_dev(bdev
->bd_dev
+p
);
499 unsigned char *read_dev_sector(struct block_device
*bdev
, sector_t n
, Sector
*p
)
501 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
504 page
= read_mapping_page(mapping
, (pgoff_t
)(n
>> (PAGE_CACHE_SHIFT
-9)),
510 return (unsigned char *)page_address(page
) + ((n
& ((1 << (PAGE_CACHE_SHIFT
- 9)) - 1)) << 9);
512 page_cache_release(page
);
518 EXPORT_SYMBOL(read_dev_sector
);
520 void del_gendisk(struct gendisk
*disk
)
524 /* invalidate stuff */
525 for (p
= disk
->minors
- 1; p
> 0; p
--) {
526 invalidate_partition(disk
, p
);
527 delete_partition(disk
, p
);
529 invalidate_partition(disk
, 0);
531 disk
->flags
&= ~GENHD_FL_UP
;
532 unlink_gendisk(disk
);
533 disk_stat_set_all(disk
, 0);
536 kobject_put(disk
->holder_dir
);
537 kobject_put(disk
->slave_dir
);
538 disk
->driverfs_dev
= NULL
;
539 #ifndef CONFIG_SYSFS_DEPRECATED
540 sysfs_remove_link(block_depr
, disk
->dev
.bus_id
);
542 device_del(&disk
->dev
);