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>
18 #define MAX_PROBE_HASH 255 /* random */
20 static struct subsystem block_subsys;
23 * Can be deleted altogether. Later.
25 * Modified under both block_subsys.rwsem and major_names_lock.
27 static struct blk_major_name {
28 struct blk_major_name *next;
31 } *major_names[MAX_PROBE_HASH];
33 static spinlock_t major_names_lock = SPIN_LOCK_UNLOCKED;
35 /* index in the above - for now: assume no multimajor ranges */
36 static inline int major_to_index(int major)
38 return major % MAX_PROBE_HASH;
41 /* get block device names in somewhat random order */
42 int get_blkdev_list(char *p)
44 struct blk_major_name *n;
47 len = sprintf(p, "\nBlock devices:\n");
49 down_read(&block_subsys.rwsem);
50 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
51 for (n = major_names[i]; n; n = n->next)
52 len += sprintf(p+len, "%3d %s\n",
55 up_read(&block_subsys.rwsem);
60 int register_blkdev(unsigned int major, const char *name)
62 struct blk_major_name **n, *p;
66 down_write(&block_subsys.rwsem);
70 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
71 if (major_names[index] == NULL)
76 printk("register_blkdev: failed to get major for %s\n",
85 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
92 strlcpy(p->name, name, sizeof(p->name));
94 index = major_to_index(major);
96 spin_lock_irqsave(&major_names_lock, flags);
97 for (n = &major_names[index]; *n; n = &(*n)->next) {
98 if ((*n)->major == major)
105 spin_unlock_irqrestore(&major_names_lock, flags);
108 printk("register_blkdev: cannot get major %d for %s\n",
113 up_write(&block_subsys.rwsem);
117 EXPORT_SYMBOL(register_blkdev);
119 /* todo: make void - error printk here */
120 int unregister_blkdev(unsigned int major, const char *name)
122 struct blk_major_name **n;
123 struct blk_major_name *p = NULL;
124 int index = major_to_index(major);
128 down_write(&block_subsys.rwsem);
129 spin_lock_irqsave(&major_names_lock, flags);
130 for (n = &major_names[index]; *n; n = &(*n)->next)
131 if ((*n)->major == major)
133 if (!*n || strcmp((*n)->name, name))
139 spin_unlock_irqrestore(&major_names_lock, flags);
140 up_write(&block_subsys.rwsem);
146 EXPORT_SYMBOL(unregister_blkdev);
148 static struct kobj_map *bdev_map;
151 * Register device numbers dev..(dev+range-1)
152 * range must be nonzero
153 * The hash chain is sorted on range, so that subranges can override.
155 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
156 struct kobject *(*probe)(dev_t, int *, void *),
157 int (*lock)(dev_t, void *), void *data)
159 kobj_map(bdev_map, dev, range, module, probe, lock, data);
162 EXPORT_SYMBOL(blk_register_region);
164 void blk_unregister_region(dev_t dev, unsigned long range)
166 kobj_unmap(bdev_map, dev, range);
169 EXPORT_SYMBOL(blk_unregister_region);
171 static struct kobject *exact_match(dev_t dev, int *part, void *data)
173 struct gendisk *p = data;
177 static int exact_lock(dev_t dev, void *data)
179 struct gendisk *p = data;
187 * add_disk - add partitioning information to kernel list
188 * @disk: per-device partitioning information
190 * This function registers the partitioning information in @disk
193 void add_disk(struct gendisk *disk)
195 disk->flags |= GENHD_FL_UP;
196 blk_register_region(MKDEV(disk->major, disk->first_minor),
197 disk->minors, NULL, exact_match, exact_lock, disk);
199 blk_register_queue(disk);
202 EXPORT_SYMBOL(add_disk);
203 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
205 void unlink_gendisk(struct gendisk *disk)
207 blk_unregister_queue(disk);
208 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
212 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
215 * get_gendisk - get partitioning information for a given device
216 * @dev: device to get partitioning information for
218 * This function gets the structure containing partitioning
219 * information for the given device @dev.
221 struct gendisk *get_gendisk(dev_t dev, int *part)
223 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
224 return kobj ? to_disk(kobj) : NULL;
227 #ifdef CONFIG_PROC_FS
229 static void *part_start(struct seq_file *part, loff_t *pos)
234 down_read(&block_subsys.rwsem);
235 list_for_each(p, &block_subsys.kset.list)
237 return list_entry(p, struct gendisk, kobj.entry);
241 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
243 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
245 return p==&block_subsys.kset.list ? NULL :
246 list_entry(p, struct gendisk, kobj.entry);
249 static void part_stop(struct seq_file *part, void *v)
251 up_read(&block_subsys.rwsem);
254 static int show_partition(struct seq_file *part, void *v)
256 struct gendisk *sgp = v;
258 char buf[BDEVNAME_SIZE];
260 if (&sgp->kobj.entry == block_subsys.kset.list.next)
261 seq_puts(part, "major minor #blocks name\n\n");
263 /* Don't show non-partitionable removeable devices or empty devices */
264 if (!get_capacity(sgp) ||
265 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
267 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
270 /* show the full disk and all non-0 size partitions of it */
271 seq_printf(part, "%4d %4d %10llu %s\n",
272 sgp->major, sgp->first_minor,
273 (unsigned long long)get_capacity(sgp) >> 1,
274 disk_name(sgp, 0, buf));
275 for (n = 0; n < sgp->minors - 1; n++) {
278 if (sgp->part[n]->nr_sects == 0)
280 seq_printf(part, "%4d %4d %10llu %s\n",
281 sgp->major, n + 1 + sgp->first_minor,
282 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
283 disk_name(sgp, n + 1, buf));
289 struct seq_operations partitions_op = {
293 .show = show_partition
298 extern int blk_dev_init(void);
300 static struct kobject *base_probe(dev_t dev, int *part, void *data)
302 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
303 /* Make old-style 2.4 aliases work */
304 request_module("block-major-%d", MAJOR(dev));
308 int __init device_init(void)
310 bdev_map = kobj_map_init(base_probe, &block_subsys);
312 subsystem_register(&block_subsys);
316 subsys_initcall(device_init);
321 * kobject & sysfs bindings for block devices
324 struct disk_attribute {
325 struct attribute attr;
326 ssize_t (*show)(struct gendisk *, char *);
329 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
332 struct gendisk *disk = to_disk(kobj);
333 struct disk_attribute *disk_attr =
334 container_of(attr,struct disk_attribute,attr);
338 ret = disk_attr->show(disk,page);
342 static struct sysfs_ops disk_sysfs_ops = {
343 .show = &disk_attr_show,
346 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
348 dev_t base = MKDEV(disk->major, disk->first_minor);
349 return print_dev_t(page, base);
351 static ssize_t disk_range_read(struct gendisk * disk, char *page)
353 return sprintf(page, "%d\n", disk->minors);
355 static ssize_t disk_size_read(struct gendisk * disk, char *page)
357 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
360 static inline unsigned jiffies_to_msec(unsigned jif)
363 return jif * (1000 / HZ);
365 return jif / (HZ / 1000);
367 return (jif / HZ) * 1000 + (jif % HZ) * 1000 / HZ;
370 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
372 disk_round_stats(disk);
378 disk_stat_read(disk, reads), disk_stat_read(disk, read_merges),
379 (unsigned long long)disk_stat_read(disk, read_sectors),
380 jiffies_to_msec(disk_stat_read(disk, read_ticks)),
381 disk_stat_read(disk, writes),
382 disk_stat_read(disk, write_merges),
383 (unsigned long long)disk_stat_read(disk, write_sectors),
384 jiffies_to_msec(disk_stat_read(disk, write_ticks)),
386 jiffies_to_msec(disk_stat_read(disk, io_ticks)),
387 jiffies_to_msec(disk_stat_read(disk, time_in_queue)));
389 static struct disk_attribute disk_attr_dev = {
390 .attr = {.name = "dev", .mode = S_IRUGO },
391 .show = disk_dev_read
393 static struct disk_attribute disk_attr_range = {
394 .attr = {.name = "range", .mode = S_IRUGO },
395 .show = disk_range_read
397 static struct disk_attribute disk_attr_size = {
398 .attr = {.name = "size", .mode = S_IRUGO },
399 .show = disk_size_read
401 static struct disk_attribute disk_attr_stat = {
402 .attr = {.name = "stat", .mode = S_IRUGO },
403 .show = disk_stats_read
406 static struct attribute * default_attrs[] = {
408 &disk_attr_range.attr,
409 &disk_attr_size.attr,
410 &disk_attr_stat.attr,
414 static void disk_release(struct kobject * kobj)
416 struct gendisk *disk = to_disk(kobj);
419 free_disk_stats(disk);
423 static struct kobj_type ktype_block = {
424 .release = disk_release,
425 .sysfs_ops = &disk_sysfs_ops,
426 .default_attrs = default_attrs,
429 extern struct kobj_type ktype_part;
431 static int block_hotplug_filter(struct kset *kset, struct kobject *kobj)
433 struct kobj_type *ktype = get_ktype(kobj);
435 return ((ktype == &ktype_block) || (ktype == &ktype_part));
438 static struct kset_hotplug_ops block_hotplug_ops = {
439 .filter = block_hotplug_filter,
442 /* declare block_subsys. */
443 static decl_subsys(block, &ktype_block, &block_hotplug_ops);
447 * aggregate disk stat collector. Uses the same stats that the sysfs
448 * entries do, above, but makes them available through one seq_file.
449 * Watching a few disks may be efficient through sysfs, but watching
450 * all of them will be more efficient through this interface.
452 * The output looks suspiciously like /proc/partitions with a bunch of
457 static void *diskstats_start(struct seq_file *part, loff_t *pos)
462 down_read(&block_subsys.rwsem);
463 list_for_each(p, &block_subsys.kset.list)
465 return list_entry(p, struct gendisk, kobj.entry);
469 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
471 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
473 return p==&block_subsys.kset.list ? NULL :
474 list_entry(p, struct gendisk, kobj.entry);
477 static void diskstats_stop(struct seq_file *part, void *v)
479 up_read(&block_subsys.rwsem);
482 static int diskstats_show(struct seq_file *s, void *v)
484 struct gendisk *gp = v;
485 char buf[BDEVNAME_SIZE];
489 if (&sgp->kobj.entry == block_subsys.kset.list.next)
490 seq_puts(s, "major minor name"
491 " rio rmerge rsect ruse wio wmerge "
492 "wsect wuse running use aveq"
496 disk_round_stats(gp);
497 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
498 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
499 disk_stat_read(gp, reads), disk_stat_read(gp, read_merges),
500 (unsigned long long)disk_stat_read(gp, read_sectors),
501 jiffies_to_msec(disk_stat_read(gp, read_ticks)),
502 disk_stat_read(gp, writes), disk_stat_read(gp, write_merges),
503 (unsigned long long)disk_stat_read(gp, write_sectors),
504 jiffies_to_msec(disk_stat_read(gp, write_ticks)),
506 jiffies_to_msec(disk_stat_read(gp, io_ticks)),
507 jiffies_to_msec(disk_stat_read(gp, time_in_queue)));
509 /* now show all non-0 size partitions of it */
510 for (n = 0; n < gp->minors - 1; n++) {
511 struct hd_struct *hd = gp->part[n];
513 if (hd && hd->nr_sects)
514 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
515 gp->major, n + gp->first_minor + 1,
516 disk_name(gp, n + 1, buf),
517 hd->reads, hd->read_sectors,
518 hd->writes, hd->write_sectors);
524 struct seq_operations diskstats_op = {
525 .start = diskstats_start,
526 .next = diskstats_next,
527 .stop = diskstats_stop,
528 .show = diskstats_show
532 struct gendisk *alloc_disk(int minors)
534 struct gendisk *disk = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
536 memset(disk, 0, sizeof(struct gendisk));
537 if (!init_disk_stats(disk)) {
542 int size = (minors - 1) * sizeof(struct hd_struct *);
543 disk->part = kmalloc(size, GFP_KERNEL);
548 memset(disk->part, 0, size);
550 disk->minors = minors;
551 kobj_set_kset_s(disk,block_subsys);
552 kobject_init(&disk->kobj);
553 rand_initialize_disk(disk);
558 EXPORT_SYMBOL(alloc_disk);
560 struct kobject *get_disk(struct gendisk *disk)
562 struct module *owner;
563 struct kobject *kobj;
567 owner = disk->fops->owner;
568 if (owner && !try_module_get(owner))
570 kobj = kobject_get(&disk->kobj);
579 EXPORT_SYMBOL(get_disk);
581 void put_disk(struct gendisk *disk)
584 kobject_put(&disk->kobj);
587 EXPORT_SYMBOL(put_disk);
589 void set_device_ro(struct block_device *bdev, int flag)
591 if (bdev->bd_contains != bdev)
592 bdev->bd_part->policy = flag;
594 bdev->bd_disk->policy = flag;
597 EXPORT_SYMBOL(set_device_ro);
599 void set_disk_ro(struct gendisk *disk, int flag)
603 for (i = 0; i < disk->minors - 1; i++)
604 if (disk->part[i]) disk->part[i]->policy = flag;
607 EXPORT_SYMBOL(set_disk_ro);
609 int bdev_read_only(struct block_device *bdev)
613 else if (bdev->bd_contains != bdev)
614 return bdev->bd_part->policy;
616 return bdev->bd_disk->policy;
619 EXPORT_SYMBOL(bdev_read_only);
621 int invalidate_partition(struct gendisk *disk, int index)
624 struct block_device *bdev = bdget_disk(disk, index);
626 res = __invalidate_device(bdev, 1);
631 EXPORT_SYMBOL(invalidate_partition);