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>
38 #ifdef CONFIG_BLK_DEV_MD
39 extern void md_autodetect_dev(dev_t dev
);
42 int warn_no_part
= 1; /*This is ugly: should make genhd removable media aware*/
44 static int (*check_part
[])(struct parsed_partitions
*, struct block_device
*) = {
46 * Probe partition formats with tables at disk address 0
47 * that also have an ADFS boot block at 0xdc0.
49 #ifdef CONFIG_ACORN_PARTITION_ICS
52 #ifdef CONFIG_ACORN_PARTITION_POWERTEC
53 adfspart_check_POWERTEC
,
55 #ifdef CONFIG_ACORN_PARTITION_EESOX
60 * Now move on to formats that only have partition info at
61 * disk address 0xdc0. Since these may also have stale
62 * PC/BIOS partition tables, they need to come before
65 #ifdef CONFIG_ACORN_PARTITION_CUMANA
66 adfspart_check_CUMANA
,
68 #ifdef CONFIG_ACORN_PARTITION_ADFS
72 #ifdef CONFIG_EFI_PARTITION
73 efi_partition
, /* this must come before msdos */
75 #ifdef CONFIG_SGI_PARTITION
78 #ifdef CONFIG_LDM_PARTITION
79 ldm_partition
, /* this must come before msdos */
81 #ifdef CONFIG_MSDOS_PARTITION
84 #ifdef CONFIG_OSF_PARTITION
87 #ifdef CONFIG_SUN_PARTITION
90 #ifdef CONFIG_AMIGA_PARTITION
93 #ifdef CONFIG_ATARI_PARTITION
96 #ifdef CONFIG_MAC_PARTITION
99 #ifdef CONFIG_ULTRIX_PARTITION
102 #ifdef CONFIG_IBM_PARTITION
105 #ifdef CONFIG_KARMA_PARTITION
112 * disk_name() is used by partition check code and the genhd driver.
113 * It formats the devicename of the indicated disk into
114 * the supplied buffer (of size at least 32), and returns
115 * a pointer to that same buffer (for convenience).
118 char *disk_name(struct gendisk
*hd
, int part
, char *buf
)
121 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
122 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
123 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, part
);
125 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, part
);
130 const char *bdevname(struct block_device
*bdev
, char *buf
)
132 int part
= MINOR(bdev
->bd_dev
) - bdev
->bd_disk
->first_minor
;
133 return disk_name(bdev
->bd_disk
, part
, buf
);
136 EXPORT_SYMBOL(bdevname
);
139 * There's very little reason to use this, you should really
140 * have a struct block_device just about everywhere and use
141 * bdevname() instead.
143 const char *__bdevname(dev_t dev
, char *buffer
)
145 scnprintf(buffer
, BDEVNAME_SIZE
, "unknown-block(%u,%u)",
146 MAJOR(dev
), MINOR(dev
));
150 EXPORT_SYMBOL(__bdevname
);
152 static struct parsed_partitions
*
153 check_partition(struct gendisk
*hd
, struct block_device
*bdev
)
155 struct parsed_partitions
*state
;
158 state
= kmalloc(sizeof(struct parsed_partitions
), GFP_KERNEL
);
162 disk_name(hd
, 0, state
->name
);
163 printk(KERN_INFO
" %s:", state
->name
);
164 if (isdigit(state
->name
[strlen(state
->name
)-1]))
165 sprintf(state
->name
, "p");
167 state
->limit
= hd
->minors
;
169 while (!res
&& check_part
[i
]) {
170 memset(&state
->parts
, 0, sizeof(state
->parts
));
171 res
= check_part
[i
++](state
, bdev
);
176 printk(" unknown partition table\n");
177 else if (warn_no_part
)
178 printk(" unable to read partition table\n");
184 * sysfs bindings for partitions
187 struct part_attribute
{
188 struct attribute attr
;
189 ssize_t (*show
)(struct hd_struct
*,char *);
190 ssize_t (*store
)(struct hd_struct
*,const char *, size_t);
194 part_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * page
)
196 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
197 struct part_attribute
* part_attr
= container_of(attr
,struct part_attribute
,attr
);
200 ret
= part_attr
->show(p
, page
);
204 part_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
205 const char *page
, size_t count
)
207 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
208 struct part_attribute
* part_attr
= container_of(attr
,struct part_attribute
,attr
);
211 if (part_attr
->store
)
212 ret
= part_attr
->store(p
, page
, count
);
216 static struct sysfs_ops part_sysfs_ops
= {
217 .show
= part_attr_show
,
218 .store
= part_attr_store
,
221 static ssize_t
part_uevent_store(struct hd_struct
* p
,
222 const char *page
, size_t count
)
224 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
227 static ssize_t
part_dev_read(struct hd_struct
* p
, char *page
)
229 struct gendisk
*disk
= container_of(p
->kobj
.parent
,struct gendisk
,kobj
);
230 dev_t dev
= MKDEV(disk
->major
, disk
->first_minor
+ p
->partno
);
231 return print_dev_t(page
, dev
);
233 static ssize_t
part_start_read(struct hd_struct
* p
, char *page
)
235 return sprintf(page
, "%llu\n",(unsigned long long)p
->start_sect
);
237 static ssize_t
part_size_read(struct hd_struct
* p
, char *page
)
239 return sprintf(page
, "%llu\n",(unsigned long long)p
->nr_sects
);
241 static ssize_t
part_stat_read(struct hd_struct
* p
, char *page
)
243 return sprintf(page
, "%8u %8llu %8u %8llu\n",
244 p
->ios
[0], (unsigned long long)p
->sectors
[0],
245 p
->ios
[1], (unsigned long long)p
->sectors
[1]);
247 static struct part_attribute part_attr_uevent
= {
248 .attr
= {.name
= "uevent", .mode
= S_IWUSR
},
249 .store
= part_uevent_store
251 static struct part_attribute part_attr_dev
= {
252 .attr
= {.name
= "dev", .mode
= S_IRUGO
},
253 .show
= part_dev_read
255 static struct part_attribute part_attr_start
= {
256 .attr
= {.name
= "start", .mode
= S_IRUGO
},
257 .show
= part_start_read
259 static struct part_attribute part_attr_size
= {
260 .attr
= {.name
= "size", .mode
= S_IRUGO
},
261 .show
= part_size_read
263 static struct part_attribute part_attr_stat
= {
264 .attr
= {.name
= "stat", .mode
= S_IRUGO
},
265 .show
= part_stat_read
268 static struct attribute
* default_attrs
[] = {
269 &part_attr_uevent
.attr
,
271 &part_attr_start
.attr
,
272 &part_attr_size
.attr
,
273 &part_attr_stat
.attr
,
277 extern struct subsystem block_subsys
;
279 static void part_release(struct kobject
*kobj
)
281 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
285 struct kobj_type ktype_part
= {
286 .release
= part_release
,
287 .default_attrs
= default_attrs
,
288 .sysfs_ops
= &part_sysfs_ops
,
291 static inline void partition_sysfs_add_subdir(struct hd_struct
*p
)
295 k
= kobject_get(&p
->kobj
);
296 p
->holder_dir
= kobject_add_dir(k
, "holders");
300 static inline void disk_sysfs_add_subdirs(struct gendisk
*disk
)
304 k
= kobject_get(&disk
->kobj
);
305 disk
->holder_dir
= kobject_add_dir(k
, "holders");
306 disk
->slave_dir
= kobject_add_dir(k
, "slaves");
310 void delete_partition(struct gendisk
*disk
, int part
)
312 struct hd_struct
*p
= disk
->part
[part
-1];
317 disk
->part
[part
-1] = NULL
;
320 p
->ios
[0] = p
->ios
[1] = 0;
321 p
->sectors
[0] = p
->sectors
[1] = 0;
322 sysfs_remove_link(&p
->kobj
, "subsystem");
324 kobject_unregister(p
->holder_dir
);
325 kobject_uevent(&p
->kobj
, KOBJ_REMOVE
);
326 kobject_del(&p
->kobj
);
327 kobject_put(&p
->kobj
);
330 void add_partition(struct gendisk
*disk
, int part
, sector_t start
, sector_t len
)
334 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
338 memset(p
, 0, sizeof(*p
));
339 p
->start_sect
= start
;
343 if (isdigit(disk
->kobj
.name
[strlen(disk
->kobj
.name
)-1]))
344 snprintf(p
->kobj
.name
,KOBJ_NAME_LEN
,"%sp%d",disk
->kobj
.name
,part
);
346 snprintf(p
->kobj
.name
,KOBJ_NAME_LEN
,"%s%d",disk
->kobj
.name
,part
);
347 p
->kobj
.parent
= &disk
->kobj
;
348 p
->kobj
.ktype
= &ktype_part
;
349 kobject_init(&p
->kobj
);
350 kobject_add(&p
->kobj
);
351 if (!disk
->part_uevent_suppress
)
352 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
353 sysfs_create_link(&p
->kobj
, &block_subsys
.kset
.kobj
, "subsystem");
354 partition_sysfs_add_subdir(p
);
355 disk
->part
[part
-1] = p
;
358 static char *make_block_name(struct gendisk
*disk
)
361 static char *block_str
= "block:";
365 size
= strlen(block_str
) + strlen(disk
->disk_name
) + 1;
366 name
= kmalloc(size
, GFP_KERNEL
);
369 strcpy(name
, block_str
);
370 strcat(name
, disk
->disk_name
);
371 /* ewww... some of these buggers have / in name... */
372 s
= strchr(name
, '/');
378 static void disk_sysfs_symlinks(struct gendisk
*disk
)
380 struct device
*target
= get_device(disk
->driverfs_dev
);
382 char *disk_name
= make_block_name(disk
);
383 sysfs_create_link(&disk
->kobj
,&target
->kobj
,"device");
385 sysfs_create_link(&target
->kobj
,&disk
->kobj
,disk_name
);
389 sysfs_create_link(&disk
->kobj
, &block_subsys
.kset
.kobj
, "subsystem");
392 /* Not exported, helper to add_disk(). */
393 void register_disk(struct gendisk
*disk
)
395 struct block_device
*bdev
;
401 strlcpy(disk
->kobj
.name
,disk
->disk_name
,KOBJ_NAME_LEN
);
402 /* ewww... some of these buggers have / in name... */
403 s
= strchr(disk
->kobj
.name
, '/');
406 if ((err
= kobject_add(&disk
->kobj
)))
408 disk_sysfs_symlinks(disk
);
409 disk_sysfs_add_subdirs(disk
);
411 /* No minors to use for partitions */
412 if (disk
->minors
== 1)
415 /* No such device (e.g., media were just removed) */
416 if (!get_capacity(disk
))
419 bdev
= bdget_disk(disk
, 0);
423 /* scan partition table, but suppress uevents */
424 bdev
->bd_invalidated
= 1;
425 disk
->part_uevent_suppress
= 1;
426 err
= blkdev_get(bdev
, FMODE_READ
, 0);
427 disk
->part_uevent_suppress
= 0;
433 /* announce disk after possible partitions are already created */
434 kobject_uevent(&disk
->kobj
, KOBJ_ADD
);
436 /* announce possible partitions */
437 for (i
= 1; i
< disk
->minors
; i
++) {
439 if (!p
|| !p
->nr_sects
)
441 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
445 int rescan_partitions(struct gendisk
*disk
, struct block_device
*bdev
)
447 struct parsed_partitions
*state
;
450 if (bdev
->bd_part_count
)
452 res
= invalidate_partition(disk
, 0);
455 bdev
->bd_invalidated
= 0;
456 for (p
= 1; p
< disk
->minors
; p
++)
457 delete_partition(disk
, p
);
458 if (disk
->fops
->revalidate_disk
)
459 disk
->fops
->revalidate_disk(disk
);
460 if (!get_capacity(disk
) || !(state
= check_partition(disk
, bdev
)))
462 for (p
= 1; p
< state
->limit
; p
++) {
463 sector_t size
= state
->parts
[p
].size
;
464 sector_t from
= state
->parts
[p
].from
;
467 if (from
+ size
> get_capacity(disk
)) {
468 printk(" %s: p%d exceeds device capacity\n",
471 add_partition(disk
, p
, from
, size
);
472 #ifdef CONFIG_BLK_DEV_MD
473 if (state
->parts
[p
].flags
)
474 md_autodetect_dev(bdev
->bd_dev
+p
);
481 unsigned char *read_dev_sector(struct block_device
*bdev
, sector_t n
, Sector
*p
)
483 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
486 page
= read_mapping_page(mapping
, (pgoff_t
)(n
>> (PAGE_CACHE_SHIFT
-9)),
489 wait_on_page_locked(page
);
490 if (!PageUptodate(page
))
495 return (unsigned char *)page_address(page
) + ((n
& ((1 << (PAGE_CACHE_SHIFT
- 9)) - 1)) << 9);
497 page_cache_release(page
);
503 EXPORT_SYMBOL(read_dev_sector
);
505 void del_gendisk(struct gendisk
*disk
)
509 /* invalidate stuff */
510 for (p
= disk
->minors
- 1; p
> 0; p
--) {
511 invalidate_partition(disk
, p
);
512 delete_partition(disk
, p
);
514 invalidate_partition(disk
, 0);
516 disk
->flags
&= ~GENHD_FL_UP
;
517 unlink_gendisk(disk
);
518 disk_stat_set_all(disk
, 0);
521 kobject_uevent(&disk
->kobj
, KOBJ_REMOVE
);
522 if (disk
->holder_dir
)
523 kobject_unregister(disk
->holder_dir
);
525 kobject_unregister(disk
->slave_dir
);
526 if (disk
->driverfs_dev
) {
527 char *disk_name
= make_block_name(disk
);
528 sysfs_remove_link(&disk
->kobj
, "device");
530 sysfs_remove_link(&disk
->driverfs_dev
->kobj
, disk_name
);
533 put_device(disk
->driverfs_dev
);
534 disk
->driverfs_dev
= NULL
;
536 sysfs_remove_link(&disk
->kobj
, "subsystem");
537 kobject_del(&disk
->kobj
);