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--) {
65 * Disallow the LANANA-assigned LOCAL/EXPERIMENTAL
68 if ((60 <= index && index <= 63) ||
69 (120 <= index && index <= 127) ||
70 (240 <= index && index <= 254))
72 if (major_names[index] == NULL)
77 printk("register_blkdev: failed to get major for %s\n",
86 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
93 strlcpy(p->name, name, sizeof(p->name));
95 index = major_to_index(major);
97 for (n = &major_names[index]; *n; n = &(*n)->next) {
98 if ((*n)->major == major)
107 printk("register_blkdev: cannot get major %d for %s\n",
112 mutex_unlock(&block_subsys_lock);
116 EXPORT_SYMBOL(register_blkdev);
118 /* todo: make void - error printk here */
119 int unregister_blkdev(unsigned int major, const char *name)
121 struct blk_major_name **n;
122 struct blk_major_name *p = NULL;
123 int index = major_to_index(major);
126 mutex_lock(&block_subsys_lock);
127 for (n = &major_names[index]; *n; n = &(*n)->next)
128 if ((*n)->major == major)
130 if (!*n || strcmp((*n)->name, name))
136 mutex_unlock(&block_subsys_lock);
142 EXPORT_SYMBOL(unregister_blkdev);
144 static struct kobj_map *bdev_map;
147 * Register device numbers dev..(dev+range-1)
148 * range must be nonzero
149 * The hash chain is sorted on range, so that subranges can override.
151 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
152 struct kobject *(*probe)(dev_t, int *, void *),
153 int (*lock)(dev_t, void *), void *data)
155 kobj_map(bdev_map, dev, range, module, probe, lock, data);
158 EXPORT_SYMBOL(blk_register_region);
160 void blk_unregister_region(dev_t dev, unsigned long range)
162 kobj_unmap(bdev_map, dev, range);
165 EXPORT_SYMBOL(blk_unregister_region);
167 static struct kobject *exact_match(dev_t dev, int *part, void *data)
169 struct gendisk *p = data;
173 static int exact_lock(dev_t dev, void *data)
175 struct gendisk *p = data;
183 * add_disk - add partitioning information to kernel list
184 * @disk: per-device partitioning information
186 * This function registers the partitioning information in @disk
189 void add_disk(struct gendisk *disk)
191 disk->flags |= GENHD_FL_UP;
192 blk_register_region(MKDEV(disk->major, disk->first_minor),
193 disk->minors, NULL, exact_match, exact_lock, disk);
195 blk_register_queue(disk);
198 EXPORT_SYMBOL(add_disk);
199 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
201 void unlink_gendisk(struct gendisk *disk)
203 blk_unregister_queue(disk);
204 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
208 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
211 * get_gendisk - get partitioning information for a given device
212 * @dev: device to get partitioning information for
214 * This function gets the structure containing partitioning
215 * information for the given device @dev.
217 struct gendisk *get_gendisk(dev_t dev, int *part)
219 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
220 return kobj ? to_disk(kobj) : NULL;
223 #ifdef CONFIG_PROC_FS
225 static void *part_start(struct seq_file *part, loff_t *pos)
230 mutex_lock(&block_subsys_lock);
231 list_for_each(p, &block_subsys.kset.list)
233 return list_entry(p, struct gendisk, kobj.entry);
237 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
239 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
241 return p==&block_subsys.kset.list ? NULL :
242 list_entry(p, struct gendisk, kobj.entry);
245 static void part_stop(struct seq_file *part, void *v)
247 mutex_unlock(&block_subsys_lock);
250 static int show_partition(struct seq_file *part, void *v)
252 struct gendisk *sgp = v;
254 char buf[BDEVNAME_SIZE];
256 if (&sgp->kobj.entry == block_subsys.kset.list.next)
257 seq_puts(part, "major minor #blocks name\n\n");
259 /* Don't show non-partitionable removeable devices or empty devices */
260 if (!get_capacity(sgp) ||
261 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
263 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
266 /* show the full disk and all non-0 size partitions of it */
267 seq_printf(part, "%4d %4d %10llu %s\n",
268 sgp->major, sgp->first_minor,
269 (unsigned long long)get_capacity(sgp) >> 1,
270 disk_name(sgp, 0, buf));
271 for (n = 0; n < sgp->minors - 1; n++) {
274 if (sgp->part[n]->nr_sects == 0)
276 seq_printf(part, "%4d %4d %10llu %s\n",
277 sgp->major, n + 1 + sgp->first_minor,
278 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
279 disk_name(sgp, n + 1, buf));
285 struct seq_operations partitions_op = {
289 .show = show_partition
294 extern int blk_dev_init(void);
296 static struct kobject *base_probe(dev_t dev, int *part, void *data)
298 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
299 /* Make old-style 2.4 aliases work */
300 request_module("block-major-%d", MAJOR(dev));
304 static int __init genhd_device_init(void)
308 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
310 err = subsystem_register(&block_subsys);
312 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
317 subsys_initcall(genhd_device_init);
322 * kobject & sysfs bindings for block devices
324 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
327 struct gendisk *disk = to_disk(kobj);
328 struct disk_attribute *disk_attr =
329 container_of(attr,struct disk_attribute,attr);
333 ret = disk_attr->show(disk,page);
337 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
338 const char *page, size_t count)
340 struct gendisk *disk = to_disk(kobj);
341 struct disk_attribute *disk_attr =
342 container_of(attr,struct disk_attribute,attr);
345 if (disk_attr->store)
346 ret = disk_attr->store(disk, page, count);
350 static struct sysfs_ops disk_sysfs_ops = {
351 .show = &disk_attr_show,
352 .store = &disk_attr_store,
355 static ssize_t disk_uevent_store(struct gendisk * disk,
356 const char *buf, size_t count)
358 kobject_uevent(&disk->kobj, KOBJ_ADD);
361 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
363 dev_t base = MKDEV(disk->major, disk->first_minor);
364 return print_dev_t(page, base);
366 static ssize_t disk_range_read(struct gendisk * disk, char *page)
368 return sprintf(page, "%d\n", disk->minors);
370 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
372 return sprintf(page, "%d\n",
373 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
376 static ssize_t disk_size_read(struct gendisk * disk, char *page)
378 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
381 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
384 disk_round_stats(disk);
387 "%8lu %8lu %8llu %8u "
388 "%8lu %8lu %8llu %8u "
391 disk_stat_read(disk, ios[READ]),
392 disk_stat_read(disk, merges[READ]),
393 (unsigned long long)disk_stat_read(disk, sectors[READ]),
394 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
395 disk_stat_read(disk, ios[WRITE]),
396 disk_stat_read(disk, merges[WRITE]),
397 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
398 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
400 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
401 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
403 static struct disk_attribute disk_attr_uevent = {
404 .attr = {.name = "uevent", .mode = S_IWUSR },
405 .store = disk_uevent_store
407 static struct disk_attribute disk_attr_dev = {
408 .attr = {.name = "dev", .mode = S_IRUGO },
409 .show = disk_dev_read
411 static struct disk_attribute disk_attr_range = {
412 .attr = {.name = "range", .mode = S_IRUGO },
413 .show = disk_range_read
415 static struct disk_attribute disk_attr_removable = {
416 .attr = {.name = "removable", .mode = S_IRUGO },
417 .show = disk_removable_read
419 static struct disk_attribute disk_attr_size = {
420 .attr = {.name = "size", .mode = S_IRUGO },
421 .show = disk_size_read
423 static struct disk_attribute disk_attr_stat = {
424 .attr = {.name = "stat", .mode = S_IRUGO },
425 .show = disk_stats_read
428 #ifdef CONFIG_FAIL_MAKE_REQUEST
430 static ssize_t disk_fail_store(struct gendisk * disk,
431 const char *buf, size_t count)
435 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
437 disk->flags &= ~GENHD_FL_FAIL;
439 disk->flags |= GENHD_FL_FAIL;
444 static ssize_t disk_fail_read(struct gendisk * disk, char *page)
446 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
448 static struct disk_attribute disk_attr_fail = {
449 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
450 .store = disk_fail_store,
451 .show = disk_fail_read
456 static struct attribute * default_attrs[] = {
457 &disk_attr_uevent.attr,
459 &disk_attr_range.attr,
460 &disk_attr_removable.attr,
461 &disk_attr_size.attr,
462 &disk_attr_stat.attr,
463 #ifdef CONFIG_FAIL_MAKE_REQUEST
464 &disk_attr_fail.attr,
469 static void disk_release(struct kobject * kobj)
471 struct gendisk *disk = to_disk(kobj);
474 free_disk_stats(disk);
478 static struct kobj_type ktype_block = {
479 .release = disk_release,
480 .sysfs_ops = &disk_sysfs_ops,
481 .default_attrs = default_attrs,
484 extern struct kobj_type ktype_part;
486 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
488 struct kobj_type *ktype = get_ktype(kobj);
490 return ((ktype == &ktype_block) || (ktype == &ktype_part));
493 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
494 int num_envp, char *buffer, int buffer_size)
496 struct kobj_type *ktype = get_ktype(kobj);
497 struct device *physdev;
498 struct gendisk *disk;
499 struct hd_struct *part;
503 if (ktype == &ktype_block) {
504 disk = container_of(kobj, struct gendisk, kobj);
505 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
506 &length, "MINOR=%u", disk->first_minor);
507 } else if (ktype == &ktype_part) {
508 disk = container_of(kobj->parent, struct gendisk, kobj);
509 part = container_of(kobj, struct hd_struct, kobj);
510 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
512 disk->first_minor + part->partno);
516 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
517 "MAJOR=%u", disk->major);
519 /* add physical device, backing this device */
520 physdev = disk->driverfs_dev;
522 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
524 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
525 &length, "PHYSDEVPATH=%s", path);
529 add_uevent_var(envp, num_envp, &i,
530 buffer, buffer_size, &length,
535 add_uevent_var(envp, num_envp, &i,
536 buffer, buffer_size, &length,
538 physdev->driver->name);
541 /* terminate, set to next free slot, shrink available space */
545 buffer = &buffer[length];
546 buffer_size -= length;
551 static struct kset_uevent_ops block_uevent_ops = {
552 .filter = block_uevent_filter,
553 .uevent = block_uevent,
556 decl_subsys(block, &ktype_block, &block_uevent_ops);
559 * aggregate disk stat collector. Uses the same stats that the sysfs
560 * entries do, above, but makes them available through one seq_file.
561 * Watching a few disks may be efficient through sysfs, but watching
562 * all of them will be more efficient through this interface.
564 * The output looks suspiciously like /proc/partitions with a bunch of
569 static void *diskstats_start(struct seq_file *part, loff_t *pos)
574 mutex_lock(&block_subsys_lock);
575 list_for_each(p, &block_subsys.kset.list)
577 return list_entry(p, struct gendisk, kobj.entry);
581 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
583 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
585 return p==&block_subsys.kset.list ? NULL :
586 list_entry(p, struct gendisk, kobj.entry);
589 static void diskstats_stop(struct seq_file *part, void *v)
591 mutex_unlock(&block_subsys_lock);
594 static int diskstats_show(struct seq_file *s, void *v)
596 struct gendisk *gp = v;
597 char buf[BDEVNAME_SIZE];
601 if (&sgp->kobj.entry == block_subsys.kset.list.next)
602 seq_puts(s, "major minor name"
603 " rio rmerge rsect ruse wio wmerge "
604 "wsect wuse running use aveq"
609 disk_round_stats(gp);
611 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
612 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
613 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
614 (unsigned long long)disk_stat_read(gp, sectors[0]),
615 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
616 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
617 (unsigned long long)disk_stat_read(gp, sectors[1]),
618 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
620 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
621 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
623 /* now show all non-0 size partitions of it */
624 for (n = 0; n < gp->minors - 1; n++) {
625 struct hd_struct *hd = gp->part[n];
627 if (hd && hd->nr_sects)
628 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
629 gp->major, n + gp->first_minor + 1,
630 disk_name(gp, n + 1, buf),
631 hd->ios[0], hd->sectors[0],
632 hd->ios[1], hd->sectors[1]);
638 struct seq_operations diskstats_op = {
639 .start = diskstats_start,
640 .next = diskstats_next,
641 .stop = diskstats_stop,
642 .show = diskstats_show
645 struct gendisk *alloc_disk(int minors)
647 return alloc_disk_node(minors, -1);
650 struct gendisk *alloc_disk_node(int minors, int node_id)
652 struct gendisk *disk;
654 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
656 memset(disk, 0, sizeof(struct gendisk));
657 if (!init_disk_stats(disk)) {
662 int size = (minors - 1) * sizeof(struct hd_struct *);
663 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
668 memset(disk->part, 0, size);
670 disk->minors = minors;
671 kobj_set_kset_s(disk,block_subsys);
672 kobject_init(&disk->kobj);
673 rand_initialize_disk(disk);
678 EXPORT_SYMBOL(alloc_disk);
679 EXPORT_SYMBOL(alloc_disk_node);
681 struct kobject *get_disk(struct gendisk *disk)
683 struct module *owner;
684 struct kobject *kobj;
688 owner = disk->fops->owner;
689 if (owner && !try_module_get(owner))
691 kobj = kobject_get(&disk->kobj);
700 EXPORT_SYMBOL(get_disk);
702 void put_disk(struct gendisk *disk)
705 kobject_put(&disk->kobj);
708 EXPORT_SYMBOL(put_disk);
710 void set_device_ro(struct block_device *bdev, int flag)
712 if (bdev->bd_contains != bdev)
713 bdev->bd_part->policy = flag;
715 bdev->bd_disk->policy = flag;
718 EXPORT_SYMBOL(set_device_ro);
720 void set_disk_ro(struct gendisk *disk, int flag)
724 for (i = 0; i < disk->minors - 1; i++)
725 if (disk->part[i]) disk->part[i]->policy = flag;
728 EXPORT_SYMBOL(set_disk_ro);
730 int bdev_read_only(struct block_device *bdev)
734 else if (bdev->bd_contains != bdev)
735 return bdev->bd_part->policy;
737 return bdev->bd_disk->policy;
740 EXPORT_SYMBOL(bdev_read_only);
742 int invalidate_partition(struct gendisk *disk, int index)
745 struct block_device *bdev = bdget_disk(disk, index);
748 res = __invalidate_device(bdev);
754 EXPORT_SYMBOL(invalidate_partition);