IB/mthca: Add modify_device method to set node description
[linux-2.6/mini2440.git] / block / genhd.c
blobdb57546a709d96b77e7dbc8987fc2c61ec40cf80
1 /*
2 * gendisk handling
3 */
5 #include <linux/config.h>
6 #include <linux/module.h>
7 #include <linux/fs.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>
19 #define MAX_PROBE_HASH 255 /* random */
21 static struct subsystem block_subsys;
23 static DECLARE_MUTEX(block_subsys_sem);
26 * Can be deleted altogether. Later.
29 static struct blk_major_name {
30 struct blk_major_name *next;
31 int major;
32 char name[16];
33 } *major_names[MAX_PROBE_HASH];
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 struct blkdev_info {
42 int index;
43 struct blk_major_name *bd;
47 * iterate over a list of blkdev_info structures. allows
48 * the major_names array to be iterated over from outside this file
49 * must be called with the block_subsys_sem held
51 void *get_next_blkdev(void *dev)
53 struct blkdev_info *info;
55 if (dev == NULL) {
56 info = kmalloc(sizeof(*info), GFP_KERNEL);
57 if (!info)
58 goto out;
59 info->index=0;
60 info->bd = major_names[info->index];
61 if (info->bd)
62 goto out;
63 } else {
64 info = dev;
67 while (info->index < ARRAY_SIZE(major_names)) {
68 if (info->bd)
69 info->bd = info->bd->next;
70 if (info->bd)
71 goto out;
73 * No devices on this chain, move to the next
75 info->index++;
76 info->bd = (info->index < ARRAY_SIZE(major_names)) ?
77 major_names[info->index] : NULL;
78 if (info->bd)
79 goto out;
82 out:
83 return info;
86 void *acquire_blkdev_list(void)
88 down(&block_subsys_sem);
89 return get_next_blkdev(NULL);
92 void release_blkdev_list(void *dev)
94 up(&block_subsys_sem);
95 kfree(dev);
100 * Count the number of records in the blkdev_list.
101 * must be called with the block_subsys_sem held
103 int count_blkdev_list(void)
105 struct blk_major_name *n;
106 int i, count;
108 count = 0;
110 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
111 for (n = major_names[i]; n; n = n->next)
112 count++;
115 return count;
119 * extract the major and name values from a blkdev_info struct
120 * passed in as a void to *dev. Must be called with
121 * block_subsys_sem held
123 int get_blkdev_info(void *dev, int *major, char **name)
125 struct blkdev_info *info = dev;
127 if (info->bd == NULL)
128 return 1;
130 *major = info->bd->major;
131 *name = info->bd->name;
132 return 0;
136 int register_blkdev(unsigned int major, const char *name)
138 struct blk_major_name **n, *p;
139 int index, ret = 0;
141 down(&block_subsys_sem);
143 /* temporary */
144 if (major == 0) {
145 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
146 if (major_names[index] == NULL)
147 break;
150 if (index == 0) {
151 printk("register_blkdev: failed to get major for %s\n",
152 name);
153 ret = -EBUSY;
154 goto out;
156 major = index;
157 ret = major;
160 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
161 if (p == NULL) {
162 ret = -ENOMEM;
163 goto out;
166 p->major = major;
167 strlcpy(p->name, name, sizeof(p->name));
168 p->next = NULL;
169 index = major_to_index(major);
171 for (n = &major_names[index]; *n; n = &(*n)->next) {
172 if ((*n)->major == major)
173 break;
175 if (!*n)
176 *n = p;
177 else
178 ret = -EBUSY;
180 if (ret < 0) {
181 printk("register_blkdev: cannot get major %d for %s\n",
182 major, name);
183 kfree(p);
185 out:
186 up(&block_subsys_sem);
187 return ret;
190 EXPORT_SYMBOL(register_blkdev);
192 /* todo: make void - error printk here */
193 int unregister_blkdev(unsigned int major, const char *name)
195 struct blk_major_name **n;
196 struct blk_major_name *p = NULL;
197 int index = major_to_index(major);
198 int ret = 0;
200 down(&block_subsys_sem);
201 for (n = &major_names[index]; *n; n = &(*n)->next)
202 if ((*n)->major == major)
203 break;
204 if (!*n || strcmp((*n)->name, name))
205 ret = -EINVAL;
206 else {
207 p = *n;
208 *n = p->next;
210 up(&block_subsys_sem);
211 kfree(p);
213 return ret;
216 EXPORT_SYMBOL(unregister_blkdev);
218 static struct kobj_map *bdev_map;
221 * Register device numbers dev..(dev+range-1)
222 * range must be nonzero
223 * The hash chain is sorted on range, so that subranges can override.
225 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
226 struct kobject *(*probe)(dev_t, int *, void *),
227 int (*lock)(dev_t, void *), void *data)
229 kobj_map(bdev_map, dev, range, module, probe, lock, data);
232 EXPORT_SYMBOL(blk_register_region);
234 void blk_unregister_region(dev_t dev, unsigned long range)
236 kobj_unmap(bdev_map, dev, range);
239 EXPORT_SYMBOL(blk_unregister_region);
241 static struct kobject *exact_match(dev_t dev, int *part, void *data)
243 struct gendisk *p = data;
244 return &p->kobj;
247 static int exact_lock(dev_t dev, void *data)
249 struct gendisk *p = data;
251 if (!get_disk(p))
252 return -1;
253 return 0;
257 * add_disk - add partitioning information to kernel list
258 * @disk: per-device partitioning information
260 * This function registers the partitioning information in @disk
261 * with the kernel.
263 void add_disk(struct gendisk *disk)
265 disk->flags |= GENHD_FL_UP;
266 blk_register_region(MKDEV(disk->major, disk->first_minor),
267 disk->minors, NULL, exact_match, exact_lock, disk);
268 register_disk(disk);
269 blk_register_queue(disk);
272 EXPORT_SYMBOL(add_disk);
273 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
275 void unlink_gendisk(struct gendisk *disk)
277 blk_unregister_queue(disk);
278 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
279 disk->minors);
282 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
285 * get_gendisk - get partitioning information for a given device
286 * @dev: device to get partitioning information for
288 * This function gets the structure containing partitioning
289 * information for the given device @dev.
291 struct gendisk *get_gendisk(dev_t dev, int *part)
293 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
294 return kobj ? to_disk(kobj) : NULL;
297 #ifdef CONFIG_PROC_FS
298 /* iterator */
299 static void *part_start(struct seq_file *part, loff_t *pos)
301 struct list_head *p;
302 loff_t l = *pos;
304 down(&block_subsys_sem);
305 list_for_each(p, &block_subsys.kset.list)
306 if (!l--)
307 return list_entry(p, struct gendisk, kobj.entry);
308 return NULL;
311 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
313 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
314 ++*pos;
315 return p==&block_subsys.kset.list ? NULL :
316 list_entry(p, struct gendisk, kobj.entry);
319 static void part_stop(struct seq_file *part, void *v)
321 up(&block_subsys_sem);
324 static int show_partition(struct seq_file *part, void *v)
326 struct gendisk *sgp = v;
327 int n;
328 char buf[BDEVNAME_SIZE];
330 if (&sgp->kobj.entry == block_subsys.kset.list.next)
331 seq_puts(part, "major minor #blocks name\n\n");
333 /* Don't show non-partitionable removeable devices or empty devices */
334 if (!get_capacity(sgp) ||
335 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
336 return 0;
337 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
338 return 0;
340 /* show the full disk and all non-0 size partitions of it */
341 seq_printf(part, "%4d %4d %10llu %s\n",
342 sgp->major, sgp->first_minor,
343 (unsigned long long)get_capacity(sgp) >> 1,
344 disk_name(sgp, 0, buf));
345 for (n = 0; n < sgp->minors - 1; n++) {
346 if (!sgp->part[n])
347 continue;
348 if (sgp->part[n]->nr_sects == 0)
349 continue;
350 seq_printf(part, "%4d %4d %10llu %s\n",
351 sgp->major, n + 1 + sgp->first_minor,
352 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
353 disk_name(sgp, n + 1, buf));
356 return 0;
359 struct seq_operations partitions_op = {
360 .start =part_start,
361 .next = part_next,
362 .stop = part_stop,
363 .show = show_partition
365 #endif
368 extern int blk_dev_init(void);
370 static struct kobject *base_probe(dev_t dev, int *part, void *data)
372 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
373 /* Make old-style 2.4 aliases work */
374 request_module("block-major-%d", MAJOR(dev));
375 return NULL;
378 static int __init genhd_device_init(void)
380 bdev_map = kobj_map_init(base_probe, &block_subsys_sem);
381 blk_dev_init();
382 subsystem_register(&block_subsys);
383 return 0;
386 subsys_initcall(genhd_device_init);
391 * kobject & sysfs bindings for block devices
393 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
394 char *page)
396 struct gendisk *disk = to_disk(kobj);
397 struct disk_attribute *disk_attr =
398 container_of(attr,struct disk_attribute,attr);
399 ssize_t ret = -EIO;
401 if (disk_attr->show)
402 ret = disk_attr->show(disk,page);
403 return ret;
406 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
407 const char *page, size_t count)
409 struct gendisk *disk = to_disk(kobj);
410 struct disk_attribute *disk_attr =
411 container_of(attr,struct disk_attribute,attr);
412 ssize_t ret = 0;
414 if (disk_attr->store)
415 ret = disk_attr->store(disk, page, count);
416 return ret;
419 static struct sysfs_ops disk_sysfs_ops = {
420 .show = &disk_attr_show,
421 .store = &disk_attr_store,
424 static ssize_t disk_uevent_store(struct gendisk * disk,
425 const char *buf, size_t count)
427 kobject_uevent(&disk->kobj, KOBJ_ADD);
428 return count;
430 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
432 dev_t base = MKDEV(disk->major, disk->first_minor);
433 return print_dev_t(page, base);
435 static ssize_t disk_range_read(struct gendisk * disk, char *page)
437 return sprintf(page, "%d\n", disk->minors);
439 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
441 return sprintf(page, "%d\n",
442 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
445 static ssize_t disk_size_read(struct gendisk * disk, char *page)
447 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
450 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
452 preempt_disable();
453 disk_round_stats(disk);
454 preempt_enable();
455 return sprintf(page,
456 "%8u %8u %8llu %8u "
457 "%8u %8u %8llu %8u "
458 "%8u %8u %8u"
459 "\n",
460 disk_stat_read(disk, ios[READ]),
461 disk_stat_read(disk, merges[READ]),
462 (unsigned long long)disk_stat_read(disk, sectors[READ]),
463 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
464 disk_stat_read(disk, ios[WRITE]),
465 disk_stat_read(disk, merges[WRITE]),
466 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
467 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
468 disk->in_flight,
469 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
470 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
472 static struct disk_attribute disk_attr_uevent = {
473 .attr = {.name = "uevent", .mode = S_IWUSR },
474 .store = disk_uevent_store
476 static struct disk_attribute disk_attr_dev = {
477 .attr = {.name = "dev", .mode = S_IRUGO },
478 .show = disk_dev_read
480 static struct disk_attribute disk_attr_range = {
481 .attr = {.name = "range", .mode = S_IRUGO },
482 .show = disk_range_read
484 static struct disk_attribute disk_attr_removable = {
485 .attr = {.name = "removable", .mode = S_IRUGO },
486 .show = disk_removable_read
488 static struct disk_attribute disk_attr_size = {
489 .attr = {.name = "size", .mode = S_IRUGO },
490 .show = disk_size_read
492 static struct disk_attribute disk_attr_stat = {
493 .attr = {.name = "stat", .mode = S_IRUGO },
494 .show = disk_stats_read
497 static struct attribute * default_attrs[] = {
498 &disk_attr_uevent.attr,
499 &disk_attr_dev.attr,
500 &disk_attr_range.attr,
501 &disk_attr_removable.attr,
502 &disk_attr_size.attr,
503 &disk_attr_stat.attr,
504 NULL,
507 static void disk_release(struct kobject * kobj)
509 struct gendisk *disk = to_disk(kobj);
510 kfree(disk->random);
511 kfree(disk->part);
512 free_disk_stats(disk);
513 kfree(disk);
516 static struct kobj_type ktype_block = {
517 .release = disk_release,
518 .sysfs_ops = &disk_sysfs_ops,
519 .default_attrs = default_attrs,
522 extern struct kobj_type ktype_part;
524 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
526 struct kobj_type *ktype = get_ktype(kobj);
528 return ((ktype == &ktype_block) || (ktype == &ktype_part));
531 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
532 int num_envp, char *buffer, int buffer_size)
534 struct kobj_type *ktype = get_ktype(kobj);
535 struct device *physdev;
536 struct gendisk *disk;
537 struct hd_struct *part;
538 int length = 0;
539 int i = 0;
541 if (ktype == &ktype_block) {
542 disk = container_of(kobj, struct gendisk, kobj);
543 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
544 &length, "MINOR=%u", disk->first_minor);
545 } else if (ktype == &ktype_part) {
546 disk = container_of(kobj->parent, struct gendisk, kobj);
547 part = container_of(kobj, struct hd_struct, kobj);
548 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
549 &length, "MINOR=%u",
550 disk->first_minor + part->partno);
551 } else
552 return 0;
554 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
555 "MAJOR=%u", disk->major);
557 /* add physical device, backing this device */
558 physdev = disk->driverfs_dev;
559 if (physdev) {
560 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
562 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
563 &length, "PHYSDEVPATH=%s", path);
564 kfree(path);
566 if (physdev->bus)
567 add_uevent_var(envp, num_envp, &i,
568 buffer, buffer_size, &length,
569 "PHYSDEVBUS=%s",
570 physdev->bus->name);
572 if (physdev->driver)
573 add_uevent_var(envp, num_envp, &i,
574 buffer, buffer_size, &length,
575 "PHYSDEVDRIVER=%s",
576 physdev->driver->name);
579 /* terminate, set to next free slot, shrink available space */
580 envp[i] = NULL;
581 envp = &envp[i];
582 num_envp -= i;
583 buffer = &buffer[length];
584 buffer_size -= length;
586 return 0;
589 static struct kset_uevent_ops block_uevent_ops = {
590 .filter = block_uevent_filter,
591 .uevent = block_uevent,
594 /* declare block_subsys. */
595 static decl_subsys(block, &ktype_block, &block_uevent_ops);
599 * aggregate disk stat collector. Uses the same stats that the sysfs
600 * entries do, above, but makes them available through one seq_file.
601 * Watching a few disks may be efficient through sysfs, but watching
602 * all of them will be more efficient through this interface.
604 * The output looks suspiciously like /proc/partitions with a bunch of
605 * extra fields.
608 /* iterator */
609 static void *diskstats_start(struct seq_file *part, loff_t *pos)
611 loff_t k = *pos;
612 struct list_head *p;
614 down(&block_subsys_sem);
615 list_for_each(p, &block_subsys.kset.list)
616 if (!k--)
617 return list_entry(p, struct gendisk, kobj.entry);
618 return NULL;
621 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
623 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
624 ++*pos;
625 return p==&block_subsys.kset.list ? NULL :
626 list_entry(p, struct gendisk, kobj.entry);
629 static void diskstats_stop(struct seq_file *part, void *v)
631 up(&block_subsys_sem);
634 static int diskstats_show(struct seq_file *s, void *v)
636 struct gendisk *gp = v;
637 char buf[BDEVNAME_SIZE];
638 int n = 0;
641 if (&sgp->kobj.entry == block_subsys.kset.list.next)
642 seq_puts(s, "major minor name"
643 " rio rmerge rsect ruse wio wmerge "
644 "wsect wuse running use aveq"
645 "\n\n");
648 preempt_disable();
649 disk_round_stats(gp);
650 preempt_enable();
651 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
652 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
653 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
654 (unsigned long long)disk_stat_read(gp, sectors[0]),
655 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
656 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
657 (unsigned long long)disk_stat_read(gp, sectors[1]),
658 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
659 gp->in_flight,
660 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
661 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
663 /* now show all non-0 size partitions of it */
664 for (n = 0; n < gp->minors - 1; n++) {
665 struct hd_struct *hd = gp->part[n];
667 if (hd && hd->nr_sects)
668 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
669 gp->major, n + gp->first_minor + 1,
670 disk_name(gp, n + 1, buf),
671 hd->ios[0], hd->sectors[0],
672 hd->ios[1], hd->sectors[1]);
675 return 0;
678 struct seq_operations diskstats_op = {
679 .start = diskstats_start,
680 .next = diskstats_next,
681 .stop = diskstats_stop,
682 .show = diskstats_show
685 struct gendisk *alloc_disk(int minors)
687 return alloc_disk_node(minors, -1);
690 struct gendisk *alloc_disk_node(int minors, int node_id)
692 struct gendisk *disk;
694 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
695 if (disk) {
696 memset(disk, 0, sizeof(struct gendisk));
697 if (!init_disk_stats(disk)) {
698 kfree(disk);
699 return NULL;
701 if (minors > 1) {
702 int size = (minors - 1) * sizeof(struct hd_struct *);
703 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
704 if (!disk->part) {
705 kfree(disk);
706 return NULL;
708 memset(disk->part, 0, size);
710 disk->minors = minors;
711 kobj_set_kset_s(disk,block_subsys);
712 kobject_init(&disk->kobj);
713 rand_initialize_disk(disk);
715 return disk;
718 EXPORT_SYMBOL(alloc_disk);
719 EXPORT_SYMBOL(alloc_disk_node);
721 struct kobject *get_disk(struct gendisk *disk)
723 struct module *owner;
724 struct kobject *kobj;
726 if (!disk->fops)
727 return NULL;
728 owner = disk->fops->owner;
729 if (owner && !try_module_get(owner))
730 return NULL;
731 kobj = kobject_get(&disk->kobj);
732 if (kobj == NULL) {
733 module_put(owner);
734 return NULL;
736 return kobj;
740 EXPORT_SYMBOL(get_disk);
742 void put_disk(struct gendisk *disk)
744 if (disk)
745 kobject_put(&disk->kobj);
748 EXPORT_SYMBOL(put_disk);
750 void set_device_ro(struct block_device *bdev, int flag)
752 if (bdev->bd_contains != bdev)
753 bdev->bd_part->policy = flag;
754 else
755 bdev->bd_disk->policy = flag;
758 EXPORT_SYMBOL(set_device_ro);
760 void set_disk_ro(struct gendisk *disk, int flag)
762 int i;
763 disk->policy = flag;
764 for (i = 0; i < disk->minors - 1; i++)
765 if (disk->part[i]) disk->part[i]->policy = flag;
768 EXPORT_SYMBOL(set_disk_ro);
770 int bdev_read_only(struct block_device *bdev)
772 if (!bdev)
773 return 0;
774 else if (bdev->bd_contains != bdev)
775 return bdev->bd_part->policy;
776 else
777 return bdev->bd_disk->policy;
780 EXPORT_SYMBOL(bdev_read_only);
782 int invalidate_partition(struct gendisk *disk, int index)
784 int res = 0;
785 struct block_device *bdev = bdget_disk(disk, index);
786 if (bdev) {
787 fsync_bdev(bdev);
788 res = __invalidate_device(bdev);
789 bdput(bdev);
791 return res;
794 EXPORT_SYMBOL(invalidate_partition);