[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / block / genhd.c
blob8bbe01d4b487cf5c9a03b474e8ffd2e57bbf1e8f
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 #ifdef CONFIG_PROC_FS
42 /* get block device names in somewhat random order */
43 int get_blkdev_list(char *p)
45 struct blk_major_name *n;
46 int i, len;
48 len = sprintf(p, "\nBlock devices:\n");
50 down(&block_subsys_sem);
51 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
52 for (n = major_names[i]; n; n = n->next)
53 len += sprintf(p+len, "%3d %s\n",
54 n->major, n->name);
56 up(&block_subsys_sem);
58 return len;
60 #endif
62 int register_blkdev(unsigned int major, const char *name)
64 struct blk_major_name **n, *p;
65 int index, ret = 0;
67 down(&block_subsys_sem);
69 /* temporary */
70 if (major == 0) {
71 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
72 if (major_names[index] == NULL)
73 break;
76 if (index == 0) {
77 printk("register_blkdev: failed to get major for %s\n",
78 name);
79 ret = -EBUSY;
80 goto out;
82 major = index;
83 ret = major;
86 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
87 if (p == NULL) {
88 ret = -ENOMEM;
89 goto out;
92 p->major = major;
93 strlcpy(p->name, name, sizeof(p->name));
94 p->next = NULL;
95 index = major_to_index(major);
97 for (n = &major_names[index]; *n; n = &(*n)->next) {
98 if ((*n)->major == major)
99 break;
101 if (!*n)
102 *n = p;
103 else
104 ret = -EBUSY;
106 if (ret < 0) {
107 printk("register_blkdev: cannot get major %d for %s\n",
108 major, name);
109 kfree(p);
111 out:
112 up(&block_subsys_sem);
113 return ret;
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);
124 int ret = 0;
126 down(&block_subsys_sem);
127 for (n = &major_names[index]; *n; n = &(*n)->next)
128 if ((*n)->major == major)
129 break;
130 if (!*n || strcmp((*n)->name, name))
131 ret = -EINVAL;
132 else {
133 p = *n;
134 *n = p->next;
136 up(&block_subsys_sem);
137 kfree(p);
139 return ret;
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;
170 return &p->kobj;
173 static int exact_lock(dev_t dev, void *data)
175 struct gendisk *p = data;
177 if (!get_disk(p))
178 return -1;
179 return 0;
183 * add_disk - add partitioning information to kernel list
184 * @disk: per-device partitioning information
186 * This function registers the partitioning information in @disk
187 * with the kernel.
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);
194 register_disk(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),
205 disk->minors);
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
224 /* iterator */
225 static void *part_start(struct seq_file *part, loff_t *pos)
227 struct list_head *p;
228 loff_t l = *pos;
230 down(&block_subsys_sem);
231 list_for_each(p, &block_subsys.kset.list)
232 if (!l--)
233 return list_entry(p, struct gendisk, kobj.entry);
234 return NULL;
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;
240 ++*pos;
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 up(&block_subsys_sem);
250 static int show_partition(struct seq_file *part, void *v)
252 struct gendisk *sgp = v;
253 int n;
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)))
262 return 0;
263 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
264 return 0;
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++) {
272 if (!sgp->part[n])
273 continue;
274 if (sgp->part[n]->nr_sects == 0)
275 continue;
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));
282 return 0;
285 struct seq_operations partitions_op = {
286 .start =part_start,
287 .next = part_next,
288 .stop = part_stop,
289 .show = show_partition
291 #endif
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));
301 return NULL;
304 static int __init genhd_device_init(void)
306 bdev_map = kobj_map_init(base_probe, &block_subsys_sem);
307 blk_dev_init();
308 subsystem_register(&block_subsys);
309 return 0;
312 subsys_initcall(genhd_device_init);
317 * kobject & sysfs bindings for block devices
319 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
320 char *page)
322 struct gendisk *disk = to_disk(kobj);
323 struct disk_attribute *disk_attr =
324 container_of(attr,struct disk_attribute,attr);
325 ssize_t ret = 0;
327 if (disk_attr->show)
328 ret = disk_attr->show(disk,page);
329 return ret;
332 static struct sysfs_ops disk_sysfs_ops = {
333 .show = &disk_attr_show,
336 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
338 dev_t base = MKDEV(disk->major, disk->first_minor);
339 return print_dev_t(page, base);
341 static ssize_t disk_range_read(struct gendisk * disk, char *page)
343 return sprintf(page, "%d\n", disk->minors);
345 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
347 return sprintf(page, "%d\n",
348 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
351 static ssize_t disk_size_read(struct gendisk * disk, char *page)
353 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
356 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
358 preempt_disable();
359 disk_round_stats(disk);
360 preempt_enable();
361 return sprintf(page,
362 "%8u %8u %8llu %8u "
363 "%8u %8u %8llu %8u "
364 "%8u %8u %8u"
365 "\n",
366 disk_stat_read(disk, reads), disk_stat_read(disk, read_merges),
367 (unsigned long long)disk_stat_read(disk, read_sectors),
368 jiffies_to_msecs(disk_stat_read(disk, read_ticks)),
369 disk_stat_read(disk, writes),
370 disk_stat_read(disk, write_merges),
371 (unsigned long long)disk_stat_read(disk, write_sectors),
372 jiffies_to_msecs(disk_stat_read(disk, write_ticks)),
373 disk->in_flight,
374 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
375 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
377 static struct disk_attribute disk_attr_dev = {
378 .attr = {.name = "dev", .mode = S_IRUGO },
379 .show = disk_dev_read
381 static struct disk_attribute disk_attr_range = {
382 .attr = {.name = "range", .mode = S_IRUGO },
383 .show = disk_range_read
385 static struct disk_attribute disk_attr_removable = {
386 .attr = {.name = "removable", .mode = S_IRUGO },
387 .show = disk_removable_read
389 static struct disk_attribute disk_attr_size = {
390 .attr = {.name = "size", .mode = S_IRUGO },
391 .show = disk_size_read
393 static struct disk_attribute disk_attr_stat = {
394 .attr = {.name = "stat", .mode = S_IRUGO },
395 .show = disk_stats_read
398 static struct attribute * default_attrs[] = {
399 &disk_attr_dev.attr,
400 &disk_attr_range.attr,
401 &disk_attr_removable.attr,
402 &disk_attr_size.attr,
403 &disk_attr_stat.attr,
404 NULL,
407 static void disk_release(struct kobject * kobj)
409 struct gendisk *disk = to_disk(kobj);
410 kfree(disk->random);
411 kfree(disk->part);
412 free_disk_stats(disk);
413 kfree(disk);
416 static struct kobj_type ktype_block = {
417 .release = disk_release,
418 .sysfs_ops = &disk_sysfs_ops,
419 .default_attrs = default_attrs,
422 extern struct kobj_type ktype_part;
424 static int block_hotplug_filter(struct kset *kset, struct kobject *kobj)
426 struct kobj_type *ktype = get_ktype(kobj);
428 return ((ktype == &ktype_block) || (ktype == &ktype_part));
431 static int block_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
432 int num_envp, char *buffer, int buffer_size)
434 struct kobj_type *ktype = get_ktype(kobj);
435 struct device *physdev;
436 struct gendisk *disk;
437 struct hd_struct *part;
438 int length = 0;
439 int i = 0;
441 if (ktype == &ktype_block) {
442 disk = container_of(kobj, struct gendisk, kobj);
443 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
444 &length, "MINOR=%u", disk->first_minor);
445 } else if (ktype == &ktype_part) {
446 disk = container_of(kobj->parent, struct gendisk, kobj);
447 part = container_of(kobj, struct hd_struct, kobj);
448 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
449 &length, "MINOR=%u",
450 disk->first_minor + part->partno);
451 } else
452 return 0;
454 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &length,
455 "MAJOR=%u", disk->major);
457 /* add physical device, backing this device */
458 physdev = disk->driverfs_dev;
459 if (physdev) {
460 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
462 add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size,
463 &length, "PHYSDEVPATH=%s", path);
464 kfree(path);
466 if (physdev->bus)
467 add_hotplug_env_var(envp, num_envp, &i,
468 buffer, buffer_size, &length,
469 "PHYSDEVBUS=%s",
470 physdev->bus->name);
472 if (physdev->driver)
473 add_hotplug_env_var(envp, num_envp, &i,
474 buffer, buffer_size, &length,
475 "PHYSDEVDRIVER=%s",
476 physdev->driver->name);
479 /* terminate, set to next free slot, shrink available space */
480 envp[i] = NULL;
481 envp = &envp[i];
482 num_envp -= i;
483 buffer = &buffer[length];
484 buffer_size -= length;
486 return 0;
489 static struct kset_hotplug_ops block_hotplug_ops = {
490 .filter = block_hotplug_filter,
491 .hotplug = block_hotplug,
494 /* declare block_subsys. */
495 static decl_subsys(block, &ktype_block, &block_hotplug_ops);
499 * aggregate disk stat collector. Uses the same stats that the sysfs
500 * entries do, above, but makes them available through one seq_file.
501 * Watching a few disks may be efficient through sysfs, but watching
502 * all of them will be more efficient through this interface.
504 * The output looks suspiciously like /proc/partitions with a bunch of
505 * extra fields.
508 /* iterator */
509 static void *diskstats_start(struct seq_file *part, loff_t *pos)
511 loff_t k = *pos;
512 struct list_head *p;
514 down(&block_subsys_sem);
515 list_for_each(p, &block_subsys.kset.list)
516 if (!k--)
517 return list_entry(p, struct gendisk, kobj.entry);
518 return NULL;
521 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
523 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
524 ++*pos;
525 return p==&block_subsys.kset.list ? NULL :
526 list_entry(p, struct gendisk, kobj.entry);
529 static void diskstats_stop(struct seq_file *part, void *v)
531 up(&block_subsys_sem);
534 static int diskstats_show(struct seq_file *s, void *v)
536 struct gendisk *gp = v;
537 char buf[BDEVNAME_SIZE];
538 int n = 0;
541 if (&sgp->kobj.entry == block_subsys.kset.list.next)
542 seq_puts(s, "major minor name"
543 " rio rmerge rsect ruse wio wmerge "
544 "wsect wuse running use aveq"
545 "\n\n");
548 preempt_disable();
549 disk_round_stats(gp);
550 preempt_enable();
551 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
552 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
553 disk_stat_read(gp, reads), disk_stat_read(gp, read_merges),
554 (unsigned long long)disk_stat_read(gp, read_sectors),
555 jiffies_to_msecs(disk_stat_read(gp, read_ticks)),
556 disk_stat_read(gp, writes), disk_stat_read(gp, write_merges),
557 (unsigned long long)disk_stat_read(gp, write_sectors),
558 jiffies_to_msecs(disk_stat_read(gp, write_ticks)),
559 gp->in_flight,
560 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
561 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
563 /* now show all non-0 size partitions of it */
564 for (n = 0; n < gp->minors - 1; n++) {
565 struct hd_struct *hd = gp->part[n];
567 if (hd && hd->nr_sects)
568 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
569 gp->major, n + gp->first_minor + 1,
570 disk_name(gp, n + 1, buf),
571 hd->reads, hd->read_sectors,
572 hd->writes, hd->write_sectors);
575 return 0;
578 struct seq_operations diskstats_op = {
579 .start = diskstats_start,
580 .next = diskstats_next,
581 .stop = diskstats_stop,
582 .show = diskstats_show
586 struct gendisk *alloc_disk(int minors)
588 struct gendisk *disk = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
589 if (disk) {
590 memset(disk, 0, sizeof(struct gendisk));
591 if (!init_disk_stats(disk)) {
592 kfree(disk);
593 return NULL;
595 if (minors > 1) {
596 int size = (minors - 1) * sizeof(struct hd_struct *);
597 disk->part = kmalloc(size, GFP_KERNEL);
598 if (!disk->part) {
599 kfree(disk);
600 return NULL;
602 memset(disk->part, 0, size);
604 disk->minors = minors;
605 kobj_set_kset_s(disk,block_subsys);
606 kobject_init(&disk->kobj);
607 rand_initialize_disk(disk);
609 return disk;
612 EXPORT_SYMBOL(alloc_disk);
614 struct kobject *get_disk(struct gendisk *disk)
616 struct module *owner;
617 struct kobject *kobj;
619 if (!disk->fops)
620 return NULL;
621 owner = disk->fops->owner;
622 if (owner && !try_module_get(owner))
623 return NULL;
624 kobj = kobject_get(&disk->kobj);
625 if (kobj == NULL) {
626 module_put(owner);
627 return NULL;
629 return kobj;
633 EXPORT_SYMBOL(get_disk);
635 void put_disk(struct gendisk *disk)
637 if (disk)
638 kobject_put(&disk->kobj);
641 EXPORT_SYMBOL(put_disk);
643 void set_device_ro(struct block_device *bdev, int flag)
645 if (bdev->bd_contains != bdev)
646 bdev->bd_part->policy = flag;
647 else
648 bdev->bd_disk->policy = flag;
651 EXPORT_SYMBOL(set_device_ro);
653 void set_disk_ro(struct gendisk *disk, int flag)
655 int i;
656 disk->policy = flag;
657 for (i = 0; i < disk->minors - 1; i++)
658 if (disk->part[i]) disk->part[i]->policy = flag;
661 EXPORT_SYMBOL(set_disk_ro);
663 int bdev_read_only(struct block_device *bdev)
665 if (!bdev)
666 return 0;
667 else if (bdev->bd_contains != bdev)
668 return bdev->bd_part->policy;
669 else
670 return bdev->bd_disk->policy;
673 EXPORT_SYMBOL(bdev_read_only);
675 int invalidate_partition(struct gendisk *disk, int index)
677 int res = 0;
678 struct block_device *bdev = bdget_disk(disk, index);
679 if (bdev) {
680 fsync_bdev(bdev);
681 res = __invalidate_device(bdev);
682 bdput(bdev);
684 return res;
687 EXPORT_SYMBOL(invalidate_partition);