5 #include <linux/module.h>
7 #include <linux/genhd.h>
8 #include <linux/kdev_t.h>
9 #include <linux/kernel.h>
10 #include <linux/blkdev.h>
11 #include <linux/init.h>
12 #include <linux/spinlock.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/kobj_map.h>
17 #include <linux/buffer_head.h>
18 #include <linux/mutex.h>
22 static DEFINE_MUTEX(block_class_lock
);
23 #ifndef CONFIG_SYSFS_DEPRECATED
24 struct kobject
*block_depr
;
28 * Can be deleted altogether. Later.
31 static struct blk_major_name
{
32 struct blk_major_name
*next
;
35 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
37 /* index in the above - for now: assume no multimajor ranges */
38 static inline int major_to_index(int major
)
40 return major
% BLKDEV_MAJOR_HASH_SIZE
;
44 void blkdev_show(struct seq_file
*f
, off_t offset
)
46 struct blk_major_name
*dp
;
48 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
49 mutex_lock(&block_class_lock
);
50 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
51 seq_printf(f
, "%3d %s\n", dp
->major
, dp
->name
);
52 mutex_unlock(&block_class_lock
);
55 #endif /* CONFIG_PROC_FS */
57 int register_blkdev(unsigned int major
, const char *name
)
59 struct blk_major_name
**n
, *p
;
62 mutex_lock(&block_class_lock
);
66 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
67 if (major_names
[index
] == NULL
)
72 printk("register_blkdev: failed to get major for %s\n",
81 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
88 strlcpy(p
->name
, name
, sizeof(p
->name
));
90 index
= major_to_index(major
);
92 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
93 if ((*n
)->major
== major
)
102 printk("register_blkdev: cannot get major %d for %s\n",
107 mutex_unlock(&block_class_lock
);
111 EXPORT_SYMBOL(register_blkdev
);
113 void unregister_blkdev(unsigned int major
, const char *name
)
115 struct blk_major_name
**n
;
116 struct blk_major_name
*p
= NULL
;
117 int index
= major_to_index(major
);
119 mutex_lock(&block_class_lock
);
120 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
121 if ((*n
)->major
== major
)
123 if (!*n
|| strcmp((*n
)->name
, name
)) {
129 mutex_unlock(&block_class_lock
);
133 EXPORT_SYMBOL(unregister_blkdev
);
135 static struct kobj_map
*bdev_map
;
138 * Register device numbers dev..(dev+range-1)
139 * range must be nonzero
140 * The hash chain is sorted on range, so that subranges can override.
142 void blk_register_region(dev_t devt
, unsigned long range
, struct module
*module
,
143 struct kobject
*(*probe
)(dev_t
, int *, void *),
144 int (*lock
)(dev_t
, void *), void *data
)
146 kobj_map(bdev_map
, devt
, range
, module
, probe
, lock
, data
);
149 EXPORT_SYMBOL(blk_register_region
);
151 void blk_unregister_region(dev_t devt
, unsigned long range
)
153 kobj_unmap(bdev_map
, devt
, range
);
156 EXPORT_SYMBOL(blk_unregister_region
);
158 static struct kobject
*exact_match(dev_t devt
, int *part
, void *data
)
160 struct gendisk
*p
= data
;
165 static int exact_lock(dev_t devt
, void *data
)
167 struct gendisk
*p
= data
;
175 * add_disk - add partitioning information to kernel list
176 * @disk: per-device partitioning information
178 * This function registers the partitioning information in @disk
181 void add_disk(struct gendisk
*disk
)
183 disk
->flags
|= GENHD_FL_UP
;
184 blk_register_region(MKDEV(disk
->major
, disk
->first_minor
),
185 disk
->minors
, NULL
, exact_match
, exact_lock
, disk
);
187 blk_register_queue(disk
);
190 EXPORT_SYMBOL(add_disk
);
191 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
193 void unlink_gendisk(struct gendisk
*disk
)
195 blk_unregister_queue(disk
);
196 blk_unregister_region(MKDEV(disk
->major
, disk
->first_minor
),
201 * get_gendisk - get partitioning information for a given device
202 * @dev: device to get partitioning information for
204 * This function gets the structure containing partitioning
205 * information for the given device @dev.
207 struct gendisk
*get_gendisk(dev_t devt
, int *part
)
209 struct kobject
*kobj
= kobj_lookup(bdev_map
, devt
, part
);
210 struct device
*dev
= kobj_to_dev(kobj
);
212 return kobj
? dev_to_disk(dev
) : NULL
;
216 * print a full list of all partitions - intended for places where the root
217 * filesystem can't be mounted and thus to give the victim some idea of what
220 void __init
printk_all_partitions(void)
224 char buf
[BDEVNAME_SIZE
];
227 mutex_lock(&block_class_lock
);
228 /* For each block device... */
229 list_for_each_entry(dev
, &block_class
.devices
, node
) {
230 if (dev
->type
!= &disk_type
)
232 sgp
= dev_to_disk(dev
);
234 * Don't show empty devices or things that have been surpressed
236 if (get_capacity(sgp
) == 0 ||
237 (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
241 * Note, unlike /proc/partitions, I am showing the numbers in
242 * hex - the same format as the root= option takes.
244 printk("%02x%02x %10llu %s",
245 sgp
->major
, sgp
->first_minor
,
246 (unsigned long long)get_capacity(sgp
) >> 1,
247 disk_name(sgp
, 0, buf
));
248 if (sgp
->driverfs_dev
!= NULL
&&
249 sgp
->driverfs_dev
->driver
!= NULL
)
250 printk(" driver: %s\n",
251 sgp
->driverfs_dev
->driver
->name
);
253 printk(" (driver?)\n");
255 /* now show the partitions */
256 for (n
= 0; n
< sgp
->minors
- 1; ++n
) {
257 if (sgp
->part
[n
] == NULL
)
259 if (sgp
->part
[n
]->nr_sects
== 0)
261 printk(" %02x%02x %10llu %s\n",
262 sgp
->major
, n
+ 1 + sgp
->first_minor
,
263 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1,
264 disk_name(sgp
, n
+ 1, buf
));
268 mutex_unlock(&block_class_lock
);
271 #ifdef CONFIG_PROC_FS
273 static void *part_start(struct seq_file
*part
, loff_t
*pos
)
278 mutex_lock(&block_class_lock
);
279 list_for_each_entry(dev
, &block_class
.devices
, node
) {
280 if (dev
->type
!= &disk_type
)
283 return dev_to_disk(dev
);
288 static void *part_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
290 struct gendisk
*gp
= v
;
293 list_for_each_entry(dev
, &gp
->dev
.node
, node
) {
294 if (&dev
->node
== &block_class
.devices
)
296 if (dev
->type
== &disk_type
)
297 return dev_to_disk(dev
);
302 static void part_stop(struct seq_file
*part
, void *v
)
304 mutex_unlock(&block_class_lock
);
307 static int show_partition(struct seq_file
*part
, void *v
)
309 struct gendisk
*sgp
= v
;
311 char buf
[BDEVNAME_SIZE
];
313 if (&sgp
->dev
.node
== block_class
.devices
.next
)
314 seq_puts(part
, "major minor #blocks name\n\n");
316 /* Don't show non-partitionable removeable devices or empty devices */
317 if (!get_capacity(sgp
) ||
318 (sgp
->minors
== 1 && (sgp
->flags
& GENHD_FL_REMOVABLE
)))
320 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
323 /* show the full disk and all non-0 size partitions of it */
324 seq_printf(part
, "%4d %4d %10llu %s\n",
325 sgp
->major
, sgp
->first_minor
,
326 (unsigned long long)get_capacity(sgp
) >> 1,
327 disk_name(sgp
, 0, buf
));
328 for (n
= 0; n
< sgp
->minors
- 1; n
++) {
331 if (sgp
->part
[n
]->nr_sects
== 0)
333 seq_printf(part
, "%4d %4d %10llu %s\n",
334 sgp
->major
, n
+ 1 + sgp
->first_minor
,
335 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1 ,
336 disk_name(sgp
, n
+ 1, buf
));
342 const struct seq_operations partitions_op
= {
346 .show
= show_partition
351 static struct kobject
*base_probe(dev_t devt
, int *part
, void *data
)
353 if (request_module("block-major-%d-%d", MAJOR(devt
), MINOR(devt
)) > 0)
354 /* Make old-style 2.4 aliases work */
355 request_module("block-major-%d", MAJOR(devt
));
359 static int __init
genhd_device_init(void)
361 class_register(&block_class
);
362 bdev_map
= kobj_map_init(base_probe
, &block_class_lock
);
365 #ifndef CONFIG_SYSFS_DEPRECATED
366 /* create top-level block dir */
367 block_depr
= kobject_create_and_add("block", NULL
);
372 subsys_initcall(genhd_device_init
);
374 static ssize_t
disk_range_show(struct device
*dev
,
375 struct device_attribute
*attr
, char *buf
)
377 struct gendisk
*disk
= dev_to_disk(dev
);
379 return sprintf(buf
, "%d\n", disk
->minors
);
382 static ssize_t
disk_removable_show(struct device
*dev
,
383 struct device_attribute
*attr
, char *buf
)
385 struct gendisk
*disk
= dev_to_disk(dev
);
387 return sprintf(buf
, "%d\n",
388 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
391 static ssize_t
disk_size_show(struct device
*dev
,
392 struct device_attribute
*attr
, char *buf
)
394 struct gendisk
*disk
= dev_to_disk(dev
);
396 return sprintf(buf
, "%llu\n", (unsigned long long)get_capacity(disk
));
399 static ssize_t
disk_capability_show(struct device
*dev
,
400 struct device_attribute
*attr
, char *buf
)
402 struct gendisk
*disk
= dev_to_disk(dev
);
404 return sprintf(buf
, "%x\n", disk
->flags
);
407 static ssize_t
disk_stat_show(struct device
*dev
,
408 struct device_attribute
*attr
, char *buf
)
410 struct gendisk
*disk
= dev_to_disk(dev
);
413 disk_round_stats(disk
);
416 "%8lu %8lu %8llu %8u "
417 "%8lu %8lu %8llu %8u "
420 disk_stat_read(disk
, ios
[READ
]),
421 disk_stat_read(disk
, merges
[READ
]),
422 (unsigned long long)disk_stat_read(disk
, sectors
[READ
]),
423 jiffies_to_msecs(disk_stat_read(disk
, ticks
[READ
])),
424 disk_stat_read(disk
, ios
[WRITE
]),
425 disk_stat_read(disk
, merges
[WRITE
]),
426 (unsigned long long)disk_stat_read(disk
, sectors
[WRITE
]),
427 jiffies_to_msecs(disk_stat_read(disk
, ticks
[WRITE
])),
429 jiffies_to_msecs(disk_stat_read(disk
, io_ticks
)),
430 jiffies_to_msecs(disk_stat_read(disk
, time_in_queue
)));
433 #ifdef CONFIG_FAIL_MAKE_REQUEST
434 static ssize_t
disk_fail_show(struct device
*dev
,
435 struct device_attribute
*attr
, char *buf
)
437 struct gendisk
*disk
= dev_to_disk(dev
);
439 return sprintf(buf
, "%d\n", disk
->flags
& GENHD_FL_FAIL
? 1 : 0);
442 static ssize_t
disk_fail_store(struct device
*dev
,
443 struct device_attribute
*attr
,
444 const char *buf
, size_t count
)
446 struct gendisk
*disk
= dev_to_disk(dev
);
449 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0) {
451 disk
->flags
&= ~GENHD_FL_FAIL
;
453 disk
->flags
|= GENHD_FL_FAIL
;
461 static DEVICE_ATTR(range
, S_IRUGO
, disk_range_show
, NULL
);
462 static DEVICE_ATTR(removable
, S_IRUGO
, disk_removable_show
, NULL
);
463 static DEVICE_ATTR(size
, S_IRUGO
, disk_size_show
, NULL
);
464 static DEVICE_ATTR(capability
, S_IRUGO
, disk_capability_show
, NULL
);
465 static DEVICE_ATTR(stat
, S_IRUGO
, disk_stat_show
, NULL
);
466 #ifdef CONFIG_FAIL_MAKE_REQUEST
467 static struct device_attribute dev_attr_fail
=
468 __ATTR(make
-it
-fail
, S_IRUGO
|S_IWUSR
, disk_fail_show
, disk_fail_store
);
471 static struct attribute
*disk_attrs
[] = {
472 &dev_attr_range
.attr
,
473 &dev_attr_removable
.attr
,
475 &dev_attr_capability
.attr
,
477 #ifdef CONFIG_FAIL_MAKE_REQUEST
483 static struct attribute_group disk_attr_group
= {
487 static struct attribute_group
*disk_attr_groups
[] = {
492 static void disk_release(struct device
*dev
)
494 struct gendisk
*disk
= dev_to_disk(dev
);
498 free_disk_stats(disk
);
501 struct class block_class
= {
505 struct device_type disk_type
= {
507 .groups
= disk_attr_groups
,
508 .release
= disk_release
,
512 * aggregate disk stat collector. Uses the same stats that the sysfs
513 * entries do, above, but makes them available through one seq_file.
515 * The output looks suspiciously like /proc/partitions with a bunch of
519 static void *diskstats_start(struct seq_file
*part
, loff_t
*pos
)
524 mutex_lock(&block_class_lock
);
525 list_for_each_entry(dev
, &block_class
.devices
, node
) {
526 if (dev
->type
!= &disk_type
)
529 return dev_to_disk(dev
);
534 static void *diskstats_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
536 struct gendisk
*gp
= v
;
540 list_for_each_entry(dev
, &gp
->dev
.node
, node
) {
541 if (&dev
->node
== &block_class
.devices
)
543 if (dev
->type
== &disk_type
)
544 return dev_to_disk(dev
);
549 static void diskstats_stop(struct seq_file
*part
, void *v
)
551 mutex_unlock(&block_class_lock
);
554 static int diskstats_show(struct seq_file
*s
, void *v
)
556 struct gendisk
*gp
= v
;
557 char buf
[BDEVNAME_SIZE
];
561 if (&gp->dev.kobj.entry == block_class.devices.next)
562 seq_puts(s, "major minor name"
563 " rio rmerge rsect ruse wio wmerge "
564 "wsect wuse running use aveq"
569 disk_round_stats(gp
);
571 seq_printf(s
, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
572 gp
->major
, n
+ gp
->first_minor
, disk_name(gp
, n
, buf
),
573 disk_stat_read(gp
, ios
[0]), disk_stat_read(gp
, merges
[0]),
574 (unsigned long long)disk_stat_read(gp
, sectors
[0]),
575 jiffies_to_msecs(disk_stat_read(gp
, ticks
[0])),
576 disk_stat_read(gp
, ios
[1]), disk_stat_read(gp
, merges
[1]),
577 (unsigned long long)disk_stat_read(gp
, sectors
[1]),
578 jiffies_to_msecs(disk_stat_read(gp
, ticks
[1])),
580 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
581 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
583 /* now show all non-0 size partitions of it */
584 for (n
= 0; n
< gp
->minors
- 1; n
++) {
585 struct hd_struct
*hd
= gp
->part
[n
];
587 if (!hd
|| !hd
->nr_sects
)
591 part_round_stats(hd
);
593 seq_printf(s
, "%4d %4d %s %lu %lu %llu "
594 "%u %lu %lu %llu %u %u %u %u\n",
595 gp
->major
, n
+ gp
->first_minor
+ 1,
596 disk_name(gp
, n
+ 1, buf
),
597 part_stat_read(hd
, ios
[0]),
598 part_stat_read(hd
, merges
[0]),
599 (unsigned long long)part_stat_read(hd
, sectors
[0]),
600 jiffies_to_msecs(part_stat_read(hd
, ticks
[0])),
601 part_stat_read(hd
, ios
[1]),
602 part_stat_read(hd
, merges
[1]),
603 (unsigned long long)part_stat_read(hd
, sectors
[1]),
604 jiffies_to_msecs(part_stat_read(hd
, ticks
[1])),
606 jiffies_to_msecs(part_stat_read(hd
, io_ticks
)),
607 jiffies_to_msecs(part_stat_read(hd
, time_in_queue
))
614 const struct seq_operations diskstats_op
= {
615 .start
= diskstats_start
,
616 .next
= diskstats_next
,
617 .stop
= diskstats_stop
,
618 .show
= diskstats_show
621 static void media_change_notify_thread(struct work_struct
*work
)
623 struct gendisk
*gd
= container_of(work
, struct gendisk
, async_notify
);
624 char event
[] = "MEDIA_CHANGE=1";
625 char *envp
[] = { event
, NULL
};
628 * set enviroment vars to indicate which event this is for
629 * so that user space will know to go check the media status.
631 kobject_uevent_env(&gd
->dev
.kobj
, KOBJ_CHANGE
, envp
);
632 put_device(gd
->driverfs_dev
);
635 void genhd_media_change_notify(struct gendisk
*disk
)
637 get_device(disk
->driverfs_dev
);
638 schedule_work(&disk
->async_notify
);
640 EXPORT_SYMBOL_GPL(genhd_media_change_notify
);
642 dev_t
blk_lookup_devt(const char *name
)
645 dev_t devt
= MKDEV(0, 0);
647 mutex_lock(&block_class_lock
);
648 list_for_each_entry(dev
, &block_class
.devices
, node
) {
649 if (strcmp(dev
->bus_id
, name
) == 0) {
654 mutex_unlock(&block_class_lock
);
659 EXPORT_SYMBOL(blk_lookup_devt
);
661 struct gendisk
*alloc_disk(int minors
)
663 return alloc_disk_node(minors
, -1);
666 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
668 struct gendisk
*disk
;
670 disk
= kmalloc_node(sizeof(struct gendisk
),
671 GFP_KERNEL
| __GFP_ZERO
, node_id
);
673 if (!init_disk_stats(disk
)) {
678 int size
= (minors
- 1) * sizeof(struct hd_struct
*);
679 disk
->part
= kmalloc_node(size
,
680 GFP_KERNEL
| __GFP_ZERO
, node_id
);
682 free_disk_stats(disk
);
687 disk
->minors
= minors
;
688 rand_initialize_disk(disk
);
689 disk
->dev
.class = &block_class
;
690 disk
->dev
.type
= &disk_type
;
691 device_initialize(&disk
->dev
);
692 INIT_WORK(&disk
->async_notify
,
693 media_change_notify_thread
);
698 EXPORT_SYMBOL(alloc_disk
);
699 EXPORT_SYMBOL(alloc_disk_node
);
701 struct kobject
*get_disk(struct gendisk
*disk
)
703 struct module
*owner
;
704 struct kobject
*kobj
;
708 owner
= disk
->fops
->owner
;
709 if (owner
&& !try_module_get(owner
))
711 kobj
= kobject_get(&disk
->dev
.kobj
);
720 EXPORT_SYMBOL(get_disk
);
722 void put_disk(struct gendisk
*disk
)
725 kobject_put(&disk
->dev
.kobj
);
728 EXPORT_SYMBOL(put_disk
);
730 void set_device_ro(struct block_device
*bdev
, int flag
)
732 if (bdev
->bd_contains
!= bdev
)
733 bdev
->bd_part
->policy
= flag
;
735 bdev
->bd_disk
->policy
= flag
;
738 EXPORT_SYMBOL(set_device_ro
);
740 void set_disk_ro(struct gendisk
*disk
, int flag
)
744 for (i
= 0; i
< disk
->minors
- 1; i
++)
745 if (disk
->part
[i
]) disk
->part
[i
]->policy
= flag
;
748 EXPORT_SYMBOL(set_disk_ro
);
750 int bdev_read_only(struct block_device
*bdev
)
754 else if (bdev
->bd_contains
!= bdev
)
755 return bdev
->bd_part
->policy
;
757 return bdev
->bd_disk
->policy
;
760 EXPORT_SYMBOL(bdev_read_only
);
762 int invalidate_partition(struct gendisk
*disk
, int index
)
765 struct block_device
*bdev
= bdget_disk(disk
, index
);
768 res
= __invalidate_device(bdev
);
774 EXPORT_SYMBOL(invalidate_partition
);