Fix incorrect prototype for ipxrtr_route_packet()
[linux-2.6/kmemtrace.git] / block / genhd.c
blob93a2cf654597c25502da7fb128745c11c01581c9
1 /*
2 * gendisk handling
3 */
5 #include <linux/module.h>
6 #include <linux/fs.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>
20 struct kset block_subsys;
21 static DEFINE_MUTEX(block_subsys_lock);
24 * Can be deleted altogether. Later.
27 static struct blk_major_name {
28 struct blk_major_name *next;
29 int major;
30 char name[16];
31 } *major_names[BLKDEV_MAJOR_HASH_SIZE];
33 /* index in the above - for now: assume no multimajor ranges */
34 static inline int major_to_index(int major)
36 return major % BLKDEV_MAJOR_HASH_SIZE;
39 #ifdef CONFIG_PROC_FS
41 void blkdev_show(struct seq_file *f, off_t offset)
43 struct blk_major_name *dp;
45 if (offset < BLKDEV_MAJOR_HASH_SIZE) {
46 mutex_lock(&block_subsys_lock);
47 for (dp = major_names[offset]; dp; dp = dp->next)
48 seq_printf(f, "%3d %s\n", dp->major, dp->name);
49 mutex_unlock(&block_subsys_lock);
53 #endif /* CONFIG_PROC_FS */
55 int register_blkdev(unsigned int major, const char *name)
57 struct blk_major_name **n, *p;
58 int index, ret = 0;
60 mutex_lock(&block_subsys_lock);
62 /* temporary */
63 if (major == 0) {
64 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
65 if (major_names[index] == NULL)
66 break;
69 if (index == 0) {
70 printk("register_blkdev: failed to get major for %s\n",
71 name);
72 ret = -EBUSY;
73 goto out;
75 major = index;
76 ret = major;
79 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
80 if (p == NULL) {
81 ret = -ENOMEM;
82 goto out;
85 p->major = major;
86 strlcpy(p->name, name, sizeof(p->name));
87 p->next = NULL;
88 index = major_to_index(major);
90 for (n = &major_names[index]; *n; n = &(*n)->next) {
91 if ((*n)->major == major)
92 break;
94 if (!*n)
95 *n = p;
96 else
97 ret = -EBUSY;
99 if (ret < 0) {
100 printk("register_blkdev: cannot get major %d for %s\n",
101 major, name);
102 kfree(p);
104 out:
105 mutex_unlock(&block_subsys_lock);
106 return ret;
109 EXPORT_SYMBOL(register_blkdev);
111 /* todo: make void - error printk here */
112 int unregister_blkdev(unsigned int major, const char *name)
114 struct blk_major_name **n;
115 struct blk_major_name *p = NULL;
116 int index = major_to_index(major);
117 int ret = 0;
119 mutex_lock(&block_subsys_lock);
120 for (n = &major_names[index]; *n; n = &(*n)->next)
121 if ((*n)->major == major)
122 break;
123 if (!*n || strcmp((*n)->name, name))
124 ret = -EINVAL;
125 else {
126 p = *n;
127 *n = p->next;
129 mutex_unlock(&block_subsys_lock);
130 kfree(p);
132 return ret;
135 EXPORT_SYMBOL(unregister_blkdev);
137 static struct kobj_map *bdev_map;
140 * Register device numbers dev..(dev+range-1)
141 * range must be nonzero
142 * The hash chain is sorted on range, so that subranges can override.
144 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
145 struct kobject *(*probe)(dev_t, int *, void *),
146 int (*lock)(dev_t, void *), void *data)
148 kobj_map(bdev_map, dev, range, module, probe, lock, data);
151 EXPORT_SYMBOL(blk_register_region);
153 void blk_unregister_region(dev_t dev, unsigned long range)
155 kobj_unmap(bdev_map, dev, range);
158 EXPORT_SYMBOL(blk_unregister_region);
160 static struct kobject *exact_match(dev_t dev, int *part, void *data)
162 struct gendisk *p = data;
163 return &p->kobj;
166 static int exact_lock(dev_t dev, void *data)
168 struct gendisk *p = data;
170 if (!get_disk(p))
171 return -1;
172 return 0;
176 * add_disk - add partitioning information to kernel list
177 * @disk: per-device partitioning information
179 * This function registers the partitioning information in @disk
180 * with the kernel.
182 void add_disk(struct gendisk *disk)
184 disk->flags |= GENHD_FL_UP;
185 blk_register_region(MKDEV(disk->major, disk->first_minor),
186 disk->minors, NULL, exact_match, exact_lock, disk);
187 register_disk(disk);
188 blk_register_queue(disk);
191 EXPORT_SYMBOL(add_disk);
192 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
194 void unlink_gendisk(struct gendisk *disk)
196 blk_unregister_queue(disk);
197 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
198 disk->minors);
201 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
204 * get_gendisk - get partitioning information for a given device
205 * @dev: device to get partitioning information for
207 * This function gets the structure containing partitioning
208 * information for the given device @dev.
210 struct gendisk *get_gendisk(dev_t dev, int *part)
212 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
213 return kobj ? to_disk(kobj) : NULL;
217 * print a full list of all partitions - intended for places where the root
218 * filesystem can't be mounted and thus to give the victim some idea of what
219 * went wrong
221 void __init printk_all_partitions(void)
223 int n;
224 struct gendisk *sgp;
226 mutex_lock(&block_subsys_lock);
227 /* For each block device... */
228 list_for_each_entry(sgp, &block_subsys.list, kobj.entry) {
229 char buf[BDEVNAME_SIZE];
231 * Don't show empty devices or things that have been surpressed
233 if (get_capacity(sgp) == 0 ||
234 (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
235 continue;
238 * Note, unlike /proc/partitions, I am showing the numbers in
239 * hex - the same format as the root= option takes.
241 printk("%02x%02x %10llu %s",
242 sgp->major, sgp->first_minor,
243 (unsigned long long)get_capacity(sgp) >> 1,
244 disk_name(sgp, 0, buf));
245 if (sgp->driverfs_dev != NULL &&
246 sgp->driverfs_dev->driver != NULL)
247 printk(" driver: %s\n",
248 sgp->driverfs_dev->driver->name);
249 else
250 printk(" (driver?)\n");
252 /* now show the partitions */
253 for (n = 0; n < sgp->minors - 1; ++n) {
254 if (sgp->part[n] == NULL)
255 continue;
256 if (sgp->part[n]->nr_sects == 0)
257 continue;
258 printk(" %02x%02x %10llu %s\n",
259 sgp->major, n + 1 + sgp->first_minor,
260 (unsigned long long)sgp->part[n]->nr_sects >> 1,
261 disk_name(sgp, n + 1, buf));
262 } /* partition subloop */
263 } /* Block device loop */
265 mutex_unlock(&block_subsys_lock);
266 return;
269 #ifdef CONFIG_PROC_FS
270 /* iterator */
271 static void *part_start(struct seq_file *part, loff_t *pos)
273 struct list_head *p;
274 loff_t l = *pos;
276 mutex_lock(&block_subsys_lock);
277 list_for_each(p, &block_subsys.list)
278 if (!l--)
279 return list_entry(p, struct gendisk, kobj.entry);
280 return NULL;
283 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
285 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
286 ++*pos;
287 return p==&block_subsys.list ? NULL :
288 list_entry(p, struct gendisk, kobj.entry);
291 static void part_stop(struct seq_file *part, void *v)
293 mutex_unlock(&block_subsys_lock);
296 static int show_partition(struct seq_file *part, void *v)
298 struct gendisk *sgp = v;
299 int n;
300 char buf[BDEVNAME_SIZE];
302 if (&sgp->kobj.entry == block_subsys.list.next)
303 seq_puts(part, "major minor #blocks name\n\n");
305 /* Don't show non-partitionable removeable devices or empty devices */
306 if (!get_capacity(sgp) ||
307 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
308 return 0;
309 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
310 return 0;
312 /* show the full disk and all non-0 size partitions of it */
313 seq_printf(part, "%4d %4d %10llu %s\n",
314 sgp->major, sgp->first_minor,
315 (unsigned long long)get_capacity(sgp) >> 1,
316 disk_name(sgp, 0, buf));
317 for (n = 0; n < sgp->minors - 1; n++) {
318 if (!sgp->part[n])
319 continue;
320 if (sgp->part[n]->nr_sects == 0)
321 continue;
322 seq_printf(part, "%4d %4d %10llu %s\n",
323 sgp->major, n + 1 + sgp->first_minor,
324 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
325 disk_name(sgp, n + 1, buf));
328 return 0;
331 struct seq_operations partitions_op = {
332 .start =part_start,
333 .next = part_next,
334 .stop = part_stop,
335 .show = show_partition
337 #endif
340 extern int blk_dev_init(void);
342 static struct kobject *base_probe(dev_t dev, int *part, void *data)
344 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
345 /* Make old-style 2.4 aliases work */
346 request_module("block-major-%d", MAJOR(dev));
347 return NULL;
350 static int __init genhd_device_init(void)
352 int err;
354 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
355 blk_dev_init();
356 err = subsystem_register(&block_subsys);
357 if (err < 0)
358 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
359 __FUNCTION__, err);
360 return err;
363 subsys_initcall(genhd_device_init);
368 * kobject & sysfs bindings for block devices
370 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
371 char *page)
373 struct gendisk *disk = to_disk(kobj);
374 struct disk_attribute *disk_attr =
375 container_of(attr,struct disk_attribute,attr);
376 ssize_t ret = -EIO;
378 if (disk_attr->show)
379 ret = disk_attr->show(disk,page);
380 return ret;
383 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
384 const char *page, size_t count)
386 struct gendisk *disk = to_disk(kobj);
387 struct disk_attribute *disk_attr =
388 container_of(attr,struct disk_attribute,attr);
389 ssize_t ret = 0;
391 if (disk_attr->store)
392 ret = disk_attr->store(disk, page, count);
393 return ret;
396 static struct sysfs_ops disk_sysfs_ops = {
397 .show = &disk_attr_show,
398 .store = &disk_attr_store,
401 static ssize_t disk_uevent_store(struct gendisk * disk,
402 const char *buf, size_t count)
404 kobject_uevent(&disk->kobj, KOBJ_ADD);
405 return count;
407 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
409 dev_t base = MKDEV(disk->major, disk->first_minor);
410 return print_dev_t(page, base);
412 static ssize_t disk_range_read(struct gendisk * disk, char *page)
414 return sprintf(page, "%d\n", disk->minors);
416 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
418 return sprintf(page, "%d\n",
419 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
422 static ssize_t disk_size_read(struct gendisk * disk, char *page)
424 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
427 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
429 preempt_disable();
430 disk_round_stats(disk);
431 preempt_enable();
432 return sprintf(page,
433 "%8lu %8lu %8llu %8u "
434 "%8lu %8lu %8llu %8u "
435 "%8u %8u %8u"
436 "\n",
437 disk_stat_read(disk, ios[READ]),
438 disk_stat_read(disk, merges[READ]),
439 (unsigned long long)disk_stat_read(disk, sectors[READ]),
440 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
441 disk_stat_read(disk, ios[WRITE]),
442 disk_stat_read(disk, merges[WRITE]),
443 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
444 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
445 disk->in_flight,
446 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
447 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
449 static struct disk_attribute disk_attr_uevent = {
450 .attr = {.name = "uevent", .mode = S_IWUSR },
451 .store = disk_uevent_store
453 static struct disk_attribute disk_attr_dev = {
454 .attr = {.name = "dev", .mode = S_IRUGO },
455 .show = disk_dev_read
457 static struct disk_attribute disk_attr_range = {
458 .attr = {.name = "range", .mode = S_IRUGO },
459 .show = disk_range_read
461 static struct disk_attribute disk_attr_removable = {
462 .attr = {.name = "removable", .mode = S_IRUGO },
463 .show = disk_removable_read
465 static struct disk_attribute disk_attr_size = {
466 .attr = {.name = "size", .mode = S_IRUGO },
467 .show = disk_size_read
469 static struct disk_attribute disk_attr_stat = {
470 .attr = {.name = "stat", .mode = S_IRUGO },
471 .show = disk_stats_read
474 #ifdef CONFIG_FAIL_MAKE_REQUEST
476 static ssize_t disk_fail_store(struct gendisk * disk,
477 const char *buf, size_t count)
479 int i;
481 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
482 if (i == 0)
483 disk->flags &= ~GENHD_FL_FAIL;
484 else
485 disk->flags |= GENHD_FL_FAIL;
488 return count;
490 static ssize_t disk_fail_read(struct gendisk * disk, char *page)
492 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
494 static struct disk_attribute disk_attr_fail = {
495 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
496 .store = disk_fail_store,
497 .show = disk_fail_read
500 #endif
502 static struct attribute * default_attrs[] = {
503 &disk_attr_uevent.attr,
504 &disk_attr_dev.attr,
505 &disk_attr_range.attr,
506 &disk_attr_removable.attr,
507 &disk_attr_size.attr,
508 &disk_attr_stat.attr,
509 #ifdef CONFIG_FAIL_MAKE_REQUEST
510 &disk_attr_fail.attr,
511 #endif
512 NULL,
515 static void disk_release(struct kobject * kobj)
517 struct gendisk *disk = to_disk(kobj);
518 kfree(disk->random);
519 kfree(disk->part);
520 free_disk_stats(disk);
521 kfree(disk);
524 static struct kobj_type ktype_block = {
525 .release = disk_release,
526 .sysfs_ops = &disk_sysfs_ops,
527 .default_attrs = default_attrs,
530 extern struct kobj_type ktype_part;
532 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
534 struct kobj_type *ktype = get_ktype(kobj);
536 return ((ktype == &ktype_block) || (ktype == &ktype_part));
539 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
540 int num_envp, char *buffer, int buffer_size)
542 struct kobj_type *ktype = get_ktype(kobj);
543 struct device *physdev;
544 struct gendisk *disk;
545 struct hd_struct *part;
546 int length = 0;
547 int i = 0;
549 if (ktype == &ktype_block) {
550 disk = container_of(kobj, struct gendisk, kobj);
551 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
552 &length, "MINOR=%u", disk->first_minor);
553 } else if (ktype == &ktype_part) {
554 disk = container_of(kobj->parent, struct gendisk, kobj);
555 part = container_of(kobj, struct hd_struct, kobj);
556 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
557 &length, "MINOR=%u",
558 disk->first_minor + part->partno);
559 } else
560 return 0;
562 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
563 "MAJOR=%u", disk->major);
565 /* add physical device, backing this device */
566 physdev = disk->driverfs_dev;
567 if (physdev) {
568 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
570 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
571 &length, "PHYSDEVPATH=%s", path);
572 kfree(path);
574 if (physdev->bus)
575 add_uevent_var(envp, num_envp, &i,
576 buffer, buffer_size, &length,
577 "PHYSDEVBUS=%s",
578 physdev->bus->name);
580 if (physdev->driver)
581 add_uevent_var(envp, num_envp, &i,
582 buffer, buffer_size, &length,
583 "PHYSDEVDRIVER=%s",
584 physdev->driver->name);
587 /* terminate, set to next free slot, shrink available space */
588 envp[i] = NULL;
589 envp = &envp[i];
590 num_envp -= i;
591 buffer = &buffer[length];
592 buffer_size -= length;
594 return 0;
597 static struct kset_uevent_ops block_uevent_ops = {
598 .filter = block_uevent_filter,
599 .uevent = block_uevent,
602 decl_subsys(block, &ktype_block, &block_uevent_ops);
605 * aggregate disk stat collector. Uses the same stats that the sysfs
606 * entries do, above, but makes them available through one seq_file.
607 * Watching a few disks may be efficient through sysfs, but watching
608 * all of them will be more efficient through this interface.
610 * The output looks suspiciously like /proc/partitions with a bunch of
611 * extra fields.
614 /* iterator */
615 static void *diskstats_start(struct seq_file *part, loff_t *pos)
617 loff_t k = *pos;
618 struct list_head *p;
620 mutex_lock(&block_subsys_lock);
621 list_for_each(p, &block_subsys.list)
622 if (!k--)
623 return list_entry(p, struct gendisk, kobj.entry);
624 return NULL;
627 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
629 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
630 ++*pos;
631 return p==&block_subsys.list ? NULL :
632 list_entry(p, struct gendisk, kobj.entry);
635 static void diskstats_stop(struct seq_file *part, void *v)
637 mutex_unlock(&block_subsys_lock);
640 static int diskstats_show(struct seq_file *s, void *v)
642 struct gendisk *gp = v;
643 char buf[BDEVNAME_SIZE];
644 int n = 0;
647 if (&sgp->kobj.entry == block_subsys.kset.list.next)
648 seq_puts(s, "major minor name"
649 " rio rmerge rsect ruse wio wmerge "
650 "wsect wuse running use aveq"
651 "\n\n");
654 preempt_disable();
655 disk_round_stats(gp);
656 preempt_enable();
657 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
658 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
659 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
660 (unsigned long long)disk_stat_read(gp, sectors[0]),
661 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
662 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
663 (unsigned long long)disk_stat_read(gp, sectors[1]),
664 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
665 gp->in_flight,
666 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
667 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
669 /* now show all non-0 size partitions of it */
670 for (n = 0; n < gp->minors - 1; n++) {
671 struct hd_struct *hd = gp->part[n];
673 if (hd && hd->nr_sects)
674 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
675 gp->major, n + gp->first_minor + 1,
676 disk_name(gp, n + 1, buf),
677 hd->ios[0], hd->sectors[0],
678 hd->ios[1], hd->sectors[1]);
681 return 0;
684 struct seq_operations diskstats_op = {
685 .start = diskstats_start,
686 .next = diskstats_next,
687 .stop = diskstats_stop,
688 .show = diskstats_show
691 struct gendisk *alloc_disk(int minors)
693 return alloc_disk_node(minors, -1);
696 struct gendisk *alloc_disk_node(int minors, int node_id)
698 struct gendisk *disk;
700 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
701 if (disk) {
702 memset(disk, 0, sizeof(struct gendisk));
703 if (!init_disk_stats(disk)) {
704 kfree(disk);
705 return NULL;
707 if (minors > 1) {
708 int size = (minors - 1) * sizeof(struct hd_struct *);
709 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
710 if (!disk->part) {
711 kfree(disk);
712 return NULL;
714 memset(disk->part, 0, size);
716 disk->minors = minors;
717 kobj_set_kset_s(disk,block_subsys);
718 kobject_init(&disk->kobj);
719 rand_initialize_disk(disk);
721 return disk;
724 EXPORT_SYMBOL(alloc_disk);
725 EXPORT_SYMBOL(alloc_disk_node);
727 struct kobject *get_disk(struct gendisk *disk)
729 struct module *owner;
730 struct kobject *kobj;
732 if (!disk->fops)
733 return NULL;
734 owner = disk->fops->owner;
735 if (owner && !try_module_get(owner))
736 return NULL;
737 kobj = kobject_get(&disk->kobj);
738 if (kobj == NULL) {
739 module_put(owner);
740 return NULL;
742 return kobj;
746 EXPORT_SYMBOL(get_disk);
748 void put_disk(struct gendisk *disk)
750 if (disk)
751 kobject_put(&disk->kobj);
754 EXPORT_SYMBOL(put_disk);
756 void set_device_ro(struct block_device *bdev, int flag)
758 if (bdev->bd_contains != bdev)
759 bdev->bd_part->policy = flag;
760 else
761 bdev->bd_disk->policy = flag;
764 EXPORT_SYMBOL(set_device_ro);
766 void set_disk_ro(struct gendisk *disk, int flag)
768 int i;
769 disk->policy = flag;
770 for (i = 0; i < disk->minors - 1; i++)
771 if (disk->part[i]) disk->part[i]->policy = flag;
774 EXPORT_SYMBOL(set_disk_ro);
776 int bdev_read_only(struct block_device *bdev)
778 if (!bdev)
779 return 0;
780 else if (bdev->bd_contains != bdev)
781 return bdev->bd_part->policy;
782 else
783 return bdev->bd_disk->policy;
786 EXPORT_SYMBOL(bdev_read_only);
788 int invalidate_partition(struct gendisk *disk, int index)
790 int res = 0;
791 struct block_device *bdev = bdget_disk(disk, index);
792 if (bdev) {
793 fsync_bdev(bdev);
794 res = __invalidate_device(bdev);
795 bdput(bdev);
797 return res;
800 EXPORT_SYMBOL(invalidate_partition);