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>
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
114 * disk_name() is used by partition check code and the genhd driver.
115 * It formats the devicename of the indicated disk into
116 * the supplied buffer (of size at least 32), and returns
117 * a pointer to that same buffer (for convenience).
120 char *disk_name(struct gendisk
*hd
, int part
, char *buf
)
123 snprintf(buf
, BDEVNAME_SIZE
, "%s", hd
->disk_name
);
124 else if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1]))
125 snprintf(buf
, BDEVNAME_SIZE
, "%sp%d", hd
->disk_name
, part
);
127 snprintf(buf
, BDEVNAME_SIZE
, "%s%d", hd
->disk_name
, part
);
132 const char *bdevname(struct block_device
*bdev
, char *buf
)
134 int part
= MINOR(bdev
->bd_dev
) - bdev
->bd_disk
->first_minor
;
135 return disk_name(bdev
->bd_disk
, part
, buf
);
138 EXPORT_SYMBOL(bdevname
);
141 * There's very little reason to use this, you should really
142 * have a struct block_device just about everywhere and use
143 * bdevname() instead.
145 const char *__bdevname(dev_t dev
, char *buffer
)
147 scnprintf(buffer
, BDEVNAME_SIZE
, "unknown-block(%u,%u)",
148 MAJOR(dev
), MINOR(dev
));
152 EXPORT_SYMBOL(__bdevname
);
154 static struct parsed_partitions
*
155 check_partition(struct gendisk
*hd
, struct block_device
*bdev
)
157 struct parsed_partitions
*state
;
160 state
= kmalloc(sizeof(struct parsed_partitions
), GFP_KERNEL
);
164 #ifdef CONFIG_DEVFS_FS
165 if (hd
->devfs_name
[0] != '\0') {
166 printk(KERN_INFO
" /dev/%s:", hd
->devfs_name
);
167 sprintf(state
->name
, "p");
171 disk_name(hd
, 0, state
->name
);
172 printk(KERN_INFO
" %s:", state
->name
);
173 if (isdigit(state
->name
[strlen(state
->name
)-1]))
174 sprintf(state
->name
, "p");
176 state
->limit
= hd
->minors
;
178 while (!res
&& check_part
[i
]) {
179 memset(&state
->parts
, 0, sizeof(state
->parts
));
180 res
= check_part
[i
++](state
, bdev
);
185 printk(" unknown partition table\n");
186 else if (warn_no_part
)
187 printk(" unable to read partition table\n");
193 * sysfs bindings for partitions
196 struct part_attribute
{
197 struct attribute attr
;
198 ssize_t (*show
)(struct hd_struct
*,char *);
199 ssize_t (*store
)(struct hd_struct
*,const char *, size_t);
203 part_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * page
)
205 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
206 struct part_attribute
* part_attr
= container_of(attr
,struct part_attribute
,attr
);
209 ret
= part_attr
->show(p
, page
);
213 part_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
214 const char *page
, size_t count
)
216 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
217 struct part_attribute
* part_attr
= container_of(attr
,struct part_attribute
,attr
);
220 if (part_attr
->store
)
221 ret
= part_attr
->store(p
, page
, count
);
225 static struct sysfs_ops part_sysfs_ops
= {
226 .show
= part_attr_show
,
227 .store
= part_attr_store
,
230 static ssize_t
part_uevent_store(struct hd_struct
* p
,
231 const char *page
, size_t count
)
233 kobject_uevent(&p
->kobj
, KOBJ_ADD
);
236 static ssize_t
part_dev_read(struct hd_struct
* p
, char *page
)
238 struct gendisk
*disk
= container_of(p
->kobj
.parent
,struct gendisk
,kobj
);
239 dev_t dev
= MKDEV(disk
->major
, disk
->first_minor
+ p
->partno
);
240 return print_dev_t(page
, dev
);
242 static ssize_t
part_start_read(struct hd_struct
* p
, char *page
)
244 return sprintf(page
, "%llu\n",(unsigned long long)p
->start_sect
);
246 static ssize_t
part_size_read(struct hd_struct
* p
, char *page
)
248 return sprintf(page
, "%llu\n",(unsigned long long)p
->nr_sects
);
250 static ssize_t
part_stat_read(struct hd_struct
* p
, char *page
)
252 return sprintf(page
, "%8u %8llu %8u %8llu\n",
253 p
->ios
[0], (unsigned long long)p
->sectors
[0],
254 p
->ios
[1], (unsigned long long)p
->sectors
[1]);
256 static struct part_attribute part_attr_uevent
= {
257 .attr
= {.name
= "uevent", .mode
= S_IWUSR
},
258 .store
= part_uevent_store
260 static struct part_attribute part_attr_dev
= {
261 .attr
= {.name
= "dev", .mode
= S_IRUGO
},
262 .show
= part_dev_read
264 static struct part_attribute part_attr_start
= {
265 .attr
= {.name
= "start", .mode
= S_IRUGO
},
266 .show
= part_start_read
268 static struct part_attribute part_attr_size
= {
269 .attr
= {.name
= "size", .mode
= S_IRUGO
},
270 .show
= part_size_read
272 static struct part_attribute part_attr_stat
= {
273 .attr
= {.name
= "stat", .mode
= S_IRUGO
},
274 .show
= part_stat_read
277 static struct attribute
* default_attrs
[] = {
278 &part_attr_uevent
.attr
,
280 &part_attr_start
.attr
,
281 &part_attr_size
.attr
,
282 &part_attr_stat
.attr
,
286 extern struct subsystem block_subsys
;
288 static void part_release(struct kobject
*kobj
)
290 struct hd_struct
* p
= container_of(kobj
,struct hd_struct
,kobj
);
294 struct kobj_type ktype_part
= {
295 .release
= part_release
,
296 .default_attrs
= default_attrs
,
297 .sysfs_ops
= &part_sysfs_ops
,
300 void delete_partition(struct gendisk
*disk
, int part
)
302 struct hd_struct
*p
= disk
->part
[part
-1];
307 disk
->part
[part
-1] = NULL
;
310 p
->ios
[0] = p
->ios
[1] = 0;
311 p
->sectors
[0] = p
->sectors
[1] = 0;
312 devfs_remove("%s/part%d", disk
->devfs_name
, part
);
313 kobject_unregister(&p
->kobj
);
316 void add_partition(struct gendisk
*disk
, int part
, sector_t start
, sector_t len
)
320 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
324 memset(p
, 0, sizeof(*p
));
325 p
->start_sect
= start
;
329 devfs_mk_bdev(MKDEV(disk
->major
, disk
->first_minor
+ part
),
330 S_IFBLK
|S_IRUSR
|S_IWUSR
,
331 "%s/part%d", disk
->devfs_name
, part
);
333 if (isdigit(disk
->kobj
.name
[strlen(disk
->kobj
.name
)-1]))
334 snprintf(p
->kobj
.name
,KOBJ_NAME_LEN
,"%sp%d",disk
->kobj
.name
,part
);
336 snprintf(p
->kobj
.name
,KOBJ_NAME_LEN
,"%s%d",disk
->kobj
.name
,part
);
337 p
->kobj
.parent
= &disk
->kobj
;
338 p
->kobj
.ktype
= &ktype_part
;
339 kobject_register(&p
->kobj
);
340 disk
->part
[part
-1] = p
;
343 static char *make_block_name(struct gendisk
*disk
)
346 static char *block_str
= "block:";
349 size
= strlen(block_str
) + strlen(disk
->disk_name
) + 1;
350 name
= kmalloc(size
, GFP_KERNEL
);
353 strcpy(name
, block_str
);
354 strcat(name
, disk
->disk_name
);
358 static void disk_sysfs_symlinks(struct gendisk
*disk
)
360 struct device
*target
= get_device(disk
->driverfs_dev
);
362 char *disk_name
= make_block_name(disk
);
363 sysfs_create_link(&disk
->kobj
,&target
->kobj
,"device");
365 sysfs_create_link(&target
->kobj
,&disk
->kobj
,disk_name
);
371 /* Not exported, helper to add_disk(). */
372 void register_disk(struct gendisk
*disk
)
374 struct block_device
*bdev
;
378 strlcpy(disk
->kobj
.name
,disk
->disk_name
,KOBJ_NAME_LEN
);
379 /* ewww... some of these buggers have / in name... */
380 s
= strchr(disk
->kobj
.name
, '/');
383 if ((err
= kobject_add(&disk
->kobj
)))
385 disk_sysfs_symlinks(disk
);
386 kobject_uevent(&disk
->kobj
, KOBJ_ADD
);
388 /* No minors to use for partitions */
389 if (disk
->minors
== 1) {
390 if (disk
->devfs_name
[0] != '\0')
391 devfs_add_disk(disk
);
395 /* always add handle for the whole disk */
396 devfs_add_partitioned(disk
);
398 /* No such device (e.g., media were just removed) */
399 if (!get_capacity(disk
))
402 bdev
= bdget_disk(disk
, 0);
406 bdev
->bd_invalidated
= 1;
407 if (blkdev_get(bdev
, FMODE_READ
, 0) < 0)
412 int rescan_partitions(struct gendisk
*disk
, struct block_device
*bdev
)
414 struct parsed_partitions
*state
;
417 if (bdev
->bd_part_count
)
419 res
= invalidate_partition(disk
, 0);
422 bdev
->bd_invalidated
= 0;
423 for (p
= 1; p
< disk
->minors
; p
++)
424 delete_partition(disk
, p
);
425 if (disk
->fops
->revalidate_disk
)
426 disk
->fops
->revalidate_disk(disk
);
427 if (!get_capacity(disk
) || !(state
= check_partition(disk
, bdev
)))
429 for (p
= 1; p
< state
->limit
; p
++) {
430 sector_t size
= state
->parts
[p
].size
;
431 sector_t from
= state
->parts
[p
].from
;
434 add_partition(disk
, p
, from
, size
);
435 #ifdef CONFIG_BLK_DEV_MD
436 if (state
->parts
[p
].flags
)
437 md_autodetect_dev(bdev
->bd_dev
+p
);
444 unsigned char *read_dev_sector(struct block_device
*bdev
, sector_t n
, Sector
*p
)
446 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
449 page
= read_cache_page(mapping
, (pgoff_t
)(n
>> (PAGE_CACHE_SHIFT
-9)),
450 (filler_t
*)mapping
->a_ops
->readpage
, NULL
);
452 wait_on_page_locked(page
);
453 if (!PageUptodate(page
))
458 return (unsigned char *)page_address(page
) + ((n
& ((1 << (PAGE_CACHE_SHIFT
- 9)) - 1)) << 9);
460 page_cache_release(page
);
466 EXPORT_SYMBOL(read_dev_sector
);
468 void del_gendisk(struct gendisk
*disk
)
472 /* invalidate stuff */
473 for (p
= disk
->minors
- 1; p
> 0; p
--) {
474 invalidate_partition(disk
, p
);
475 delete_partition(disk
, p
);
477 invalidate_partition(disk
, 0);
479 disk
->flags
&= ~GENHD_FL_UP
;
480 unlink_gendisk(disk
);
481 disk_stat_set_all(disk
, 0);
484 devfs_remove_disk(disk
);
486 if (disk
->driverfs_dev
) {
487 char *disk_name
= make_block_name(disk
);
488 sysfs_remove_link(&disk
->kobj
, "device");
490 sysfs_remove_link(&disk
->driverfs_dev
->kobj
, disk_name
);
493 put_device(disk
->driverfs_dev
);
495 kobject_uevent(&disk
->kobj
, KOBJ_REMOVE
);
496 kobject_del(&disk
->kobj
);