5 #include <linux/module.h>
7 #include <linux/genhd.h>
8 #include <linux/kernel.h>
9 #include <linux/blkdev.h>
10 #include <linux/init.h>
11 #include <linux/spinlock.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/kmod.h>
15 #include <linux/kobj_map.h>
16 #include <linux/buffer_head.h>
17 #include <linux/mutex.h>
19 struct subsystem block_subsys;
20 static DEFINE_MUTEX(block_subsys_lock);
23 * Can be deleted altogether. Later.
26 static struct blk_major_name {
27 struct blk_major_name *next;
30 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
32 /* index in the above - for now: assume no multimajor ranges */
33 static inline int major_to_index(int major)
35 return major % BLKDEV_MAJOR_HASH_SIZE;
40 void blkdev_show(struct seq_file *f, off_t offset)
42 struct blk_major_name *dp;
44 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
45 mutex_lock(&block_subsys_lock);
46 for (dp = major_names[offset]; dp; dp = dp->next)
47 seq_printf(f, "%3d %s\n", dp->major, dp->name);
48 mutex_unlock(&block_subsys_lock);
52 #endif /* CONFIG_PROC_FS */
54 int register_blkdev(unsigned int major, const char *name)
56 struct blk_major_name **n, *p;
59 mutex_lock(&block_subsys_lock);
63 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
64 if (major_names[index] == NULL)
69 printk("register_blkdev: failed to get major for %s\n",
78 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
85 strlcpy(p->name, name, sizeof(p->name));
87 index = major_to_index(major);
89 for (n = &major_names[index]; *n; n = &(*n)->next) {
90 if ((*n)->major == major)
99 printk("register_blkdev: cannot get major %d for %s\n",
104 mutex_unlock(&block_subsys_lock);
108 EXPORT_SYMBOL(register_blkdev);
110 /* todo: make void - error printk here */
111 int unregister_blkdev(unsigned int major, const char *name)
113 struct blk_major_name **n;
114 struct blk_major_name *p = NULL;
115 int index = major_to_index(major);
118 mutex_lock(&block_subsys_lock);
119 for (n = &major_names[index]; *n; n = &(*n)->next)
120 if ((*n)->major == major)
122 if (!*n || strcmp((*n)->name, name))
128 mutex_unlock(&block_subsys_lock);
134 EXPORT_SYMBOL(unregister_blkdev);
136 static struct kobj_map *bdev_map;
139 * Register device numbers dev..(dev+range-1)
140 * range must be nonzero
141 * The hash chain is sorted on range, so that subranges can override.
143 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
144 struct kobject *(*probe)(dev_t, int *, void *),
145 int (*lock)(dev_t, void *), void *data)
147 kobj_map(bdev_map, dev, range, module, probe, lock, data);
150 EXPORT_SYMBOL(blk_register_region);
152 void blk_unregister_region(dev_t dev, unsigned long range)
154 kobj_unmap(bdev_map, dev, range);
157 EXPORT_SYMBOL(blk_unregister_region);
159 static struct kobject *exact_match(dev_t dev, int *part, void *data)
161 struct gendisk *p = data;
165 static int exact_lock(dev_t dev, 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),
200 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
203 * get_gendisk - get partitioning information for a given device
204 * @dev: device to get partitioning information for
206 * This function gets the structure containing partitioning
207 * information for the given device @dev.
209 struct gendisk *get_gendisk(dev_t dev, int *part)
211 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
212 return kobj ? to_disk(kobj) : NULL;
215 #ifdef CONFIG_PROC_FS
217 static void *part_start(struct seq_file *part, loff_t *pos)
222 mutex_lock(&block_subsys_lock);
223 list_for_each(p, &block_subsys.kset.list)
225 return list_entry(p, struct gendisk, kobj.entry);
229 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
231 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
233 return p==&block_subsys.kset.list ? NULL :
234 list_entry(p, struct gendisk, kobj.entry);
237 static void part_stop(struct seq_file *part, void *v)
239 mutex_unlock(&block_subsys_lock);
242 static int show_partition(struct seq_file *part, void *v)
244 struct gendisk *sgp = v;
246 char buf[BDEVNAME_SIZE];
248 if (&sgp->kobj.entry == block_subsys.kset.list.next)
249 seq_puts(part, "major minor #blocks name\n\n");
251 /* Don't show non-partitionable removeable devices or empty devices */
252 if (!get_capacity(sgp) ||
253 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
255 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
258 /* show the full disk and all non-0 size partitions of it */
259 seq_printf(part, "%4d %4d %10llu %s\n",
260 sgp->major, sgp->first_minor,
261 (unsigned long long)get_capacity(sgp) >> 1,
262 disk_name(sgp, 0, buf));
263 for (n = 0; n < sgp->minors - 1; n++) {
266 if (sgp->part[n]->nr_sects == 0)
268 seq_printf(part, "%4d %4d %10llu %s\n",
269 sgp->major, n + 1 + sgp->first_minor,
270 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
271 disk_name(sgp, n + 1, buf));
277 struct seq_operations partitions_op = {
281 .show = show_partition
286 extern int blk_dev_init(void);
288 static struct kobject *base_probe(dev_t dev, int *part, void *data)
290 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
291 /* Make old-style 2.4 aliases work */
292 request_module("block-major-%d", MAJOR(dev));
296 static int __init genhd_device_init(void)
300 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
302 err = subsystem_register(&block_subsys);
304 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
309 subsys_initcall(genhd_device_init);
314 * kobject & sysfs bindings for block devices
316 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
319 struct gendisk *disk = to_disk(kobj);
320 struct disk_attribute *disk_attr =
321 container_of(attr,struct disk_attribute,attr);
325 ret = disk_attr->show(disk,page);
329 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
330 const char *page, size_t count)
332 struct gendisk *disk = to_disk(kobj);
333 struct disk_attribute *disk_attr =
334 container_of(attr,struct disk_attribute,attr);
337 if (disk_attr->store)
338 ret = disk_attr->store(disk, page, count);
342 static struct sysfs_ops disk_sysfs_ops = {
343 .show = &disk_attr_show,
344 .store = &disk_attr_store,
347 static ssize_t disk_uevent_store(struct gendisk * disk,
348 const char *buf, size_t count)
350 kobject_uevent(&disk->kobj, KOBJ_ADD);
353 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
355 dev_t base = MKDEV(disk->major, disk->first_minor);
356 return print_dev_t(page, base);
358 static ssize_t disk_range_read(struct gendisk * disk, char *page)
360 return sprintf(page, "%d\n", disk->minors);
362 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
364 return sprintf(page, "%d\n",
365 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
368 static ssize_t disk_size_read(struct gendisk * disk, char *page)
370 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
373 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
376 disk_round_stats(disk);
379 "%8lu %8lu %8llu %8u "
380 "%8lu %8lu %8llu %8u "
383 disk_stat_read(disk, ios[READ]),
384 disk_stat_read(disk, merges[READ]),
385 (unsigned long long)disk_stat_read(disk, sectors[READ]),
386 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
387 disk_stat_read(disk, ios[WRITE]),
388 disk_stat_read(disk, merges[WRITE]),
389 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
390 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
392 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
393 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
395 static struct disk_attribute disk_attr_uevent = {
396 .attr = {.name = "uevent", .mode = S_IWUSR },
397 .store = disk_uevent_store
399 static struct disk_attribute disk_attr_dev = {
400 .attr = {.name = "dev", .mode = S_IRUGO },
401 .show = disk_dev_read
403 static struct disk_attribute disk_attr_range = {
404 .attr = {.name = "range", .mode = S_IRUGO },
405 .show = disk_range_read
407 static struct disk_attribute disk_attr_removable = {
408 .attr = {.name = "removable", .mode = S_IRUGO },
409 .show = disk_removable_read
411 static struct disk_attribute disk_attr_size = {
412 .attr = {.name = "size", .mode = S_IRUGO },
413 .show = disk_size_read
415 static struct disk_attribute disk_attr_stat = {
416 .attr = {.name = "stat", .mode = S_IRUGO },
417 .show = disk_stats_read
420 static struct attribute * default_attrs[] = {
421 &disk_attr_uevent.attr,
423 &disk_attr_range.attr,
424 &disk_attr_removable.attr,
425 &disk_attr_size.attr,
426 &disk_attr_stat.attr,
430 static void disk_release(struct kobject * kobj)
432 struct gendisk *disk = to_disk(kobj);
435 free_disk_stats(disk);
439 static struct kobj_type ktype_block = {
440 .release = disk_release,
441 .sysfs_ops = &disk_sysfs_ops,
442 .default_attrs = default_attrs,
445 extern struct kobj_type ktype_part;
447 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
449 struct kobj_type *ktype = get_ktype(kobj);
451 return ((ktype == &ktype_block) || (ktype == &ktype_part));
454 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
455 int num_envp, char *buffer, int buffer_size)
457 struct kobj_type *ktype = get_ktype(kobj);
458 struct device *physdev;
459 struct gendisk *disk;
460 struct hd_struct *part;
464 if (ktype == &ktype_block) {
465 disk = container_of(kobj, struct gendisk, kobj);
466 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
467 &length, "MINOR=%u", disk->first_minor);
468 } else if (ktype == &ktype_part) {
469 disk = container_of(kobj->parent, struct gendisk, kobj);
470 part = container_of(kobj, struct hd_struct, kobj);
471 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
473 disk->first_minor + part->partno);
477 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
478 "MAJOR=%u", disk->major);
480 /* add physical device, backing this device */
481 physdev = disk->driverfs_dev;
483 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
485 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
486 &length, "PHYSDEVPATH=%s", path);
490 add_uevent_var(envp, num_envp, &i,
491 buffer, buffer_size, &length,
496 add_uevent_var(envp, num_envp, &i,
497 buffer, buffer_size, &length,
499 physdev->driver->name);
502 /* terminate, set to next free slot, shrink available space */
506 buffer = &buffer[length];
507 buffer_size -= length;
512 static struct kset_uevent_ops block_uevent_ops = {
513 .filter = block_uevent_filter,
514 .uevent = block_uevent,
517 decl_subsys(block, &ktype_block, &block_uevent_ops);
520 * aggregate disk stat collector. Uses the same stats that the sysfs
521 * entries do, above, but makes them available through one seq_file.
522 * Watching a few disks may be efficient through sysfs, but watching
523 * all of them will be more efficient through this interface.
525 * The output looks suspiciously like /proc/partitions with a bunch of
530 static void *diskstats_start(struct seq_file *part, loff_t *pos)
535 mutex_lock(&block_subsys_lock);
536 list_for_each(p, &block_subsys.kset.list)
538 return list_entry(p, struct gendisk, kobj.entry);
542 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
544 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
546 return p==&block_subsys.kset.list ? NULL :
547 list_entry(p, struct gendisk, kobj.entry);
550 static void diskstats_stop(struct seq_file *part, void *v)
552 mutex_unlock(&block_subsys_lock);
555 static int diskstats_show(struct seq_file *s, void *v)
557 struct gendisk *gp = v;
558 char buf[BDEVNAME_SIZE];
562 if (&sgp->kobj.entry == block_subsys.kset.list.next)
563 seq_puts(s, "major minor name"
564 " rio rmerge rsect ruse wio wmerge "
565 "wsect wuse running use aveq"
570 disk_round_stats(gp);
572 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
573 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
574 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
575 (unsigned long long)disk_stat_read(gp, sectors[0]),
576 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
577 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
578 (unsigned long long)disk_stat_read(gp, sectors[1]),
579 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
581 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
582 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
584 /* now show all non-0 size partitions of it */
585 for (n = 0; n < gp->minors - 1; n++) {
586 struct hd_struct *hd = gp->part[n];
588 if (hd && hd->nr_sects)
589 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
590 gp->major, n + gp->first_minor + 1,
591 disk_name(gp, n + 1, buf),
592 hd->ios[0], hd->sectors[0],
593 hd->ios[1], hd->sectors[1]);
599 struct seq_operations diskstats_op = {
600 .start = diskstats_start,
601 .next = diskstats_next,
602 .stop = diskstats_stop,
603 .show = diskstats_show
606 struct gendisk *alloc_disk(int minors)
608 return alloc_disk_node(minors, -1);
611 struct gendisk *alloc_disk_node(int minors, int node_id)
613 struct gendisk *disk;
615 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
617 memset(disk, 0, sizeof(struct gendisk));
618 if (!init_disk_stats(disk)) {
623 int size = (minors - 1) * sizeof(struct hd_struct *);
624 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
629 memset(disk->part, 0, size);
631 disk->minors = minors;
632 kobj_set_kset_s(disk,block_subsys);
633 kobject_init(&disk->kobj);
634 rand_initialize_disk(disk);
639 EXPORT_SYMBOL(alloc_disk);
640 EXPORT_SYMBOL(alloc_disk_node);
642 struct kobject *get_disk(struct gendisk *disk)
644 struct module *owner;
645 struct kobject *kobj;
649 owner = disk->fops->owner;
650 if (owner && !try_module_get(owner))
652 kobj = kobject_get(&disk->kobj);
661 EXPORT_SYMBOL(get_disk);
663 void put_disk(struct gendisk *disk)
666 kobject_put(&disk->kobj);
669 EXPORT_SYMBOL(put_disk);
671 void set_device_ro(struct block_device *bdev, int flag)
673 if (bdev->bd_contains != bdev)
674 bdev->bd_part->policy = flag;
676 bdev->bd_disk->policy = flag;
679 EXPORT_SYMBOL(set_device_ro);
681 void set_disk_ro(struct gendisk *disk, int flag)
685 for (i = 0; i < disk->minors - 1; i++)
686 if (disk->part[i]) disk->part[i]->policy = flag;
689 EXPORT_SYMBOL(set_disk_ro);
691 int bdev_read_only(struct block_device *bdev)
695 else if (bdev->bd_contains != bdev)
696 return bdev->bd_part->policy;
698 return bdev->bd_disk->policy;
701 EXPORT_SYMBOL(bdev_read_only);
703 int invalidate_partition(struct gendisk *disk, int index)
706 struct block_device *bdev = bdget_disk(disk, index);
709 res = __invalidate_device(bdev);
715 EXPORT_SYMBOL(invalidate_partition);