5 #include <linux/config.h>
6 #include <linux/module.h>
8 #include <linux/genhd.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>
20 #define MAX_PROBE_HASH 255 /* random */
22 static struct subsystem block_subsys;
24 static DEFINE_MUTEX(block_subsys_lock);
27 * Can be deleted altogether. Later.
30 static struct blk_major_name {
31 struct blk_major_name *next;
34 } *major_names[MAX_PROBE_HASH];
36 /* index in the above - for now: assume no multimajor ranges */
37 static inline int major_to_index(int major)
39 return major % MAX_PROBE_HASH;
44 struct blk_major_name *bd;
48 * iterate over a list of blkdev_info structures. allows
49 * the major_names array to be iterated over from outside this file
50 * must be called with the block_subsys_lock held
52 void *get_next_blkdev(void *dev)
54 struct blkdev_info *info;
57 info = kmalloc(sizeof(*info), GFP_KERNEL);
61 info->bd = major_names[info->index];
68 while (info->index < ARRAY_SIZE(major_names)) {
70 info->bd = info->bd->next;
74 * No devices on this chain, move to the next
77 info->bd = (info->index < ARRAY_SIZE(major_names)) ?
78 major_names[info->index] : NULL;
87 void *acquire_blkdev_list(void)
89 mutex_lock(&block_subsys_lock);
90 return get_next_blkdev(NULL);
93 void release_blkdev_list(void *dev)
95 mutex_unlock(&block_subsys_lock);
101 * Count the number of records in the blkdev_list.
102 * must be called with the block_subsys_lock held
104 int count_blkdev_list(void)
106 struct blk_major_name *n;
111 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
112 for (n = major_names[i]; n; n = n->next)
120 * extract the major and name values from a blkdev_info struct
121 * passed in as a void to *dev. Must be called with
122 * block_subsys_lock held
124 int get_blkdev_info(void *dev, int *major, char **name)
126 struct blkdev_info *info = dev;
128 if (info->bd == NULL)
131 *major = info->bd->major;
132 *name = info->bd->name;
137 int register_blkdev(unsigned int major, const char *name)
139 struct blk_major_name **n, *p;
142 mutex_lock(&block_subsys_lock);
146 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
147 if (major_names[index] == NULL)
152 printk("register_blkdev: failed to get major for %s\n",
161 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
168 strlcpy(p->name, name, sizeof(p->name));
170 index = major_to_index(major);
172 for (n = &major_names[index]; *n; n = &(*n)->next) {
173 if ((*n)->major == major)
182 printk("register_blkdev: cannot get major %d for %s\n",
187 mutex_unlock(&block_subsys_lock);
191 EXPORT_SYMBOL(register_blkdev);
193 /* todo: make void - error printk here */
194 int unregister_blkdev(unsigned int major, const char *name)
196 struct blk_major_name **n;
197 struct blk_major_name *p = NULL;
198 int index = major_to_index(major);
201 mutex_lock(&block_subsys_lock);
202 for (n = &major_names[index]; *n; n = &(*n)->next)
203 if ((*n)->major == major)
205 if (!*n || strcmp((*n)->name, name))
211 mutex_unlock(&block_subsys_lock);
217 EXPORT_SYMBOL(unregister_blkdev);
219 static struct kobj_map *bdev_map;
222 * Register device numbers dev..(dev+range-1)
223 * range must be nonzero
224 * The hash chain is sorted on range, so that subranges can override.
226 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
227 struct kobject *(*probe)(dev_t, int *, void *),
228 int (*lock)(dev_t, void *), void *data)
230 kobj_map(bdev_map, dev, range, module, probe, lock, data);
233 EXPORT_SYMBOL(blk_register_region);
235 void blk_unregister_region(dev_t dev, unsigned long range)
237 kobj_unmap(bdev_map, dev, range);
240 EXPORT_SYMBOL(blk_unregister_region);
242 static struct kobject *exact_match(dev_t dev, int *part, void *data)
244 struct gendisk *p = data;
248 static int exact_lock(dev_t dev, void *data)
250 struct gendisk *p = data;
258 * add_disk - add partitioning information to kernel list
259 * @disk: per-device partitioning information
261 * This function registers the partitioning information in @disk
264 void add_disk(struct gendisk *disk)
266 disk->flags |= GENHD_FL_UP;
267 blk_register_region(MKDEV(disk->major, disk->first_minor),
268 disk->minors, NULL, exact_match, exact_lock, disk);
270 blk_register_queue(disk);
273 EXPORT_SYMBOL(add_disk);
274 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
276 void unlink_gendisk(struct gendisk *disk)
278 blk_unregister_queue(disk);
279 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
283 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
286 * get_gendisk - get partitioning information for a given device
287 * @dev: device to get partitioning information for
289 * This function gets the structure containing partitioning
290 * information for the given device @dev.
292 struct gendisk *get_gendisk(dev_t dev, int *part)
294 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
295 return kobj ? to_disk(kobj) : NULL;
298 #ifdef CONFIG_PROC_FS
300 static void *part_start(struct seq_file *part, loff_t *pos)
305 mutex_lock(&block_subsys_lock);
306 list_for_each(p, &block_subsys.kset.list)
308 return list_entry(p, struct gendisk, kobj.entry);
312 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
314 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
316 return p==&block_subsys.kset.list ? NULL :
317 list_entry(p, struct gendisk, kobj.entry);
320 static void part_stop(struct seq_file *part, void *v)
322 mutex_unlock(&block_subsys_lock);
325 static int show_partition(struct seq_file *part, void *v)
327 struct gendisk *sgp = v;
329 char buf[BDEVNAME_SIZE];
331 if (&sgp->kobj.entry == block_subsys.kset.list.next)
332 seq_puts(part, "major minor #blocks name\n\n");
334 /* Don't show non-partitionable removeable devices or empty devices */
335 if (!get_capacity(sgp) ||
336 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
338 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
341 /* show the full disk and all non-0 size partitions of it */
342 seq_printf(part, "%4d %4d %10llu %s\n",
343 sgp->major, sgp->first_minor,
344 (unsigned long long)get_capacity(sgp) >> 1,
345 disk_name(sgp, 0, buf));
346 for (n = 0; n < sgp->minors - 1; n++) {
349 if (sgp->part[n]->nr_sects == 0)
351 seq_printf(part, "%4d %4d %10llu %s\n",
352 sgp->major, n + 1 + sgp->first_minor,
353 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
354 disk_name(sgp, n + 1, buf));
360 struct seq_operations partitions_op = {
364 .show = show_partition
369 extern int blk_dev_init(void);
371 static struct kobject *base_probe(dev_t dev, int *part, void *data)
373 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
374 /* Make old-style 2.4 aliases work */
375 request_module("block-major-%d", MAJOR(dev));
379 static int __init genhd_device_init(void)
381 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
383 subsystem_register(&block_subsys);
387 subsys_initcall(genhd_device_init);
392 * kobject & sysfs bindings for block devices
394 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
397 struct gendisk *disk = to_disk(kobj);
398 struct disk_attribute *disk_attr =
399 container_of(attr,struct disk_attribute,attr);
403 ret = disk_attr->show(disk,page);
407 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
408 const char *page, size_t count)
410 struct gendisk *disk = to_disk(kobj);
411 struct disk_attribute *disk_attr =
412 container_of(attr,struct disk_attribute,attr);
415 if (disk_attr->store)
416 ret = disk_attr->store(disk, page, count);
420 static struct sysfs_ops disk_sysfs_ops = {
421 .show = &disk_attr_show,
422 .store = &disk_attr_store,
425 static ssize_t disk_uevent_store(struct gendisk * disk,
426 const char *buf, size_t count)
428 kobject_uevent(&disk->kobj, KOBJ_ADD);
431 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
433 dev_t base = MKDEV(disk->major, disk->first_minor);
434 return print_dev_t(page, base);
436 static ssize_t disk_range_read(struct gendisk * disk, char *page)
438 return sprintf(page, "%d\n", disk->minors);
440 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
442 return sprintf(page, "%d\n",
443 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
446 static ssize_t disk_size_read(struct gendisk * disk, char *page)
448 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
451 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
454 disk_round_stats(disk);
457 "%8lu %8lu %8llu %8u "
458 "%8lu %8lu %8llu %8u "
461 disk_stat_read(disk, ios[READ]),
462 disk_stat_read(disk, merges[READ]),
463 (unsigned long long)disk_stat_read(disk, sectors[READ]),
464 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
465 disk_stat_read(disk, ios[WRITE]),
466 disk_stat_read(disk, merges[WRITE]),
467 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
468 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
470 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
471 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
473 static struct disk_attribute disk_attr_uevent = {
474 .attr = {.name = "uevent", .mode = S_IWUSR },
475 .store = disk_uevent_store
477 static struct disk_attribute disk_attr_dev = {
478 .attr = {.name = "dev", .mode = S_IRUGO },
479 .show = disk_dev_read
481 static struct disk_attribute disk_attr_range = {
482 .attr = {.name = "range", .mode = S_IRUGO },
483 .show = disk_range_read
485 static struct disk_attribute disk_attr_removable = {
486 .attr = {.name = "removable", .mode = S_IRUGO },
487 .show = disk_removable_read
489 static struct disk_attribute disk_attr_size = {
490 .attr = {.name = "size", .mode = S_IRUGO },
491 .show = disk_size_read
493 static struct disk_attribute disk_attr_stat = {
494 .attr = {.name = "stat", .mode = S_IRUGO },
495 .show = disk_stats_read
498 static struct attribute * default_attrs[] = {
499 &disk_attr_uevent.attr,
501 &disk_attr_range.attr,
502 &disk_attr_removable.attr,
503 &disk_attr_size.attr,
504 &disk_attr_stat.attr,
508 static void disk_release(struct kobject * kobj)
510 struct gendisk *disk = to_disk(kobj);
513 free_disk_stats(disk);
517 static struct kobj_type ktype_block = {
518 .release = disk_release,
519 .sysfs_ops = &disk_sysfs_ops,
520 .default_attrs = default_attrs,
523 extern struct kobj_type ktype_part;
525 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
527 struct kobj_type *ktype = get_ktype(kobj);
529 return ((ktype == &ktype_block) || (ktype == &ktype_part));
532 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
533 int num_envp, char *buffer, int buffer_size)
535 struct kobj_type *ktype = get_ktype(kobj);
536 struct device *physdev;
537 struct gendisk *disk;
538 struct hd_struct *part;
542 if (ktype == &ktype_block) {
543 disk = container_of(kobj, struct gendisk, kobj);
544 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
545 &length, "MINOR=%u", disk->first_minor);
546 } else if (ktype == &ktype_part) {
547 disk = container_of(kobj->parent, struct gendisk, kobj);
548 part = container_of(kobj, struct hd_struct, kobj);
549 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
551 disk->first_minor + part->partno);
555 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
556 "MAJOR=%u", disk->major);
558 /* add physical device, backing this device */
559 physdev = disk->driverfs_dev;
561 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
563 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
564 &length, "PHYSDEVPATH=%s", path);
568 add_uevent_var(envp, num_envp, &i,
569 buffer, buffer_size, &length,
574 add_uevent_var(envp, num_envp, &i,
575 buffer, buffer_size, &length,
577 physdev->driver->name);
580 /* terminate, set to next free slot, shrink available space */
584 buffer = &buffer[length];
585 buffer_size -= length;
590 static struct kset_uevent_ops block_uevent_ops = {
591 .filter = block_uevent_filter,
592 .uevent = block_uevent,
595 /* declare block_subsys. */
596 static decl_subsys(block, &ktype_block, &block_uevent_ops);
600 * aggregate disk stat collector. Uses the same stats that the sysfs
601 * entries do, above, but makes them available through one seq_file.
602 * Watching a few disks may be efficient through sysfs, but watching
603 * all of them will be more efficient through this interface.
605 * The output looks suspiciously like /proc/partitions with a bunch of
610 static void *diskstats_start(struct seq_file *part, loff_t *pos)
615 mutex_lock(&block_subsys_lock);
616 list_for_each(p, &block_subsys.kset.list)
618 return list_entry(p, struct gendisk, kobj.entry);
622 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
624 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
626 return p==&block_subsys.kset.list ? NULL :
627 list_entry(p, struct gendisk, kobj.entry);
630 static void diskstats_stop(struct seq_file *part, void *v)
632 mutex_unlock(&block_subsys_lock);
635 static int diskstats_show(struct seq_file *s, void *v)
637 struct gendisk *gp = v;
638 char buf[BDEVNAME_SIZE];
642 if (&sgp->kobj.entry == block_subsys.kset.list.next)
643 seq_puts(s, "major minor name"
644 " rio rmerge rsect ruse wio wmerge "
645 "wsect wuse running use aveq"
650 disk_round_stats(gp);
652 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
653 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
654 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
655 (unsigned long long)disk_stat_read(gp, sectors[0]),
656 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
657 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
658 (unsigned long long)disk_stat_read(gp, sectors[1]),
659 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
661 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
662 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
664 /* now show all non-0 size partitions of it */
665 for (n = 0; n < gp->minors - 1; n++) {
666 struct hd_struct *hd = gp->part[n];
668 if (hd && hd->nr_sects)
669 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
670 gp->major, n + gp->first_minor + 1,
671 disk_name(gp, n + 1, buf),
672 hd->ios[0], hd->sectors[0],
673 hd->ios[1], hd->sectors[1]);
679 struct seq_operations diskstats_op = {
680 .start = diskstats_start,
681 .next = diskstats_next,
682 .stop = diskstats_stop,
683 .show = diskstats_show
686 struct gendisk *alloc_disk(int minors)
688 return alloc_disk_node(minors, -1);
691 struct gendisk *alloc_disk_node(int minors, int node_id)
693 struct gendisk *disk;
695 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
697 memset(disk, 0, sizeof(struct gendisk));
698 if (!init_disk_stats(disk)) {
703 int size = (minors - 1) * sizeof(struct hd_struct *);
704 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
709 memset(disk->part, 0, size);
711 disk->minors = minors;
712 kobj_set_kset_s(disk,block_subsys);
713 kobject_init(&disk->kobj);
714 rand_initialize_disk(disk);
719 EXPORT_SYMBOL(alloc_disk);
720 EXPORT_SYMBOL(alloc_disk_node);
722 struct kobject *get_disk(struct gendisk *disk)
724 struct module *owner;
725 struct kobject *kobj;
729 owner = disk->fops->owner;
730 if (owner && !try_module_get(owner))
732 kobj = kobject_get(&disk->kobj);
741 EXPORT_SYMBOL(get_disk);
743 void put_disk(struct gendisk *disk)
746 kobject_put(&disk->kobj);
749 EXPORT_SYMBOL(put_disk);
751 void set_device_ro(struct block_device *bdev, int flag)
753 if (bdev->bd_contains != bdev)
754 bdev->bd_part->policy = flag;
756 bdev->bd_disk->policy = flag;
759 EXPORT_SYMBOL(set_device_ro);
761 void set_disk_ro(struct gendisk *disk, int flag)
765 for (i = 0; i < disk->minors - 1; i++)
766 if (disk->part[i]) disk->part[i]->policy = flag;
769 EXPORT_SYMBOL(set_disk_ro);
771 int bdev_read_only(struct block_device *bdev)
775 else if (bdev->bd_contains != bdev)
776 return bdev->bd_part->policy;
778 return bdev->bd_disk->policy;
781 EXPORT_SYMBOL(bdev_read_only);
783 int invalidate_partition(struct gendisk *disk, int index)
786 struct block_device *bdev = bdget_disk(disk, index);
789 res = __invalidate_device(bdev);
795 EXPORT_SYMBOL(invalidate_partition);