[PATCH] hrtimer: switch clock_nanosleep to hrtimer nanosleep API
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / genhd.c
blobf1ed83f3f083761cd80d04d65a83f8da40afd484
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, int used)
45 struct blk_major_name *n;
46 int i, len;
48 len = snprintf(p, (PAGE_SIZE-used), "\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) {
54 * If the curent string plus the 5 extra characters
55 * in the line would run us off the page, then we're done
57 if ((len + used + strlen(n->name) + 5) >= PAGE_SIZE)
58 goto page_full;
59 len += sprintf(p+len, "%3d %s\n",
60 n->major, n->name);
63 page_full:
64 up(&block_subsys_sem);
66 return len;
68 #endif
70 int register_blkdev(unsigned int major, const char *name)
72 struct blk_major_name **n, *p;
73 int index, ret = 0;
75 down(&block_subsys_sem);
77 /* temporary */
78 if (major == 0) {
79 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
80 if (major_names[index] == NULL)
81 break;
84 if (index == 0) {
85 printk("register_blkdev: failed to get major for %s\n",
86 name);
87 ret = -EBUSY;
88 goto out;
90 major = index;
91 ret = major;
94 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
95 if (p == NULL) {
96 ret = -ENOMEM;
97 goto out;
100 p->major = major;
101 strlcpy(p->name, name, sizeof(p->name));
102 p->next = NULL;
103 index = major_to_index(major);
105 for (n = &major_names[index]; *n; n = &(*n)->next) {
106 if ((*n)->major == major)
107 break;
109 if (!*n)
110 *n = p;
111 else
112 ret = -EBUSY;
114 if (ret < 0) {
115 printk("register_blkdev: cannot get major %d for %s\n",
116 major, name);
117 kfree(p);
119 out:
120 up(&block_subsys_sem);
121 return ret;
124 EXPORT_SYMBOL(register_blkdev);
126 /* todo: make void - error printk here */
127 int unregister_blkdev(unsigned int major, const char *name)
129 struct blk_major_name **n;
130 struct blk_major_name *p = NULL;
131 int index = major_to_index(major);
132 int ret = 0;
134 down(&block_subsys_sem);
135 for (n = &major_names[index]; *n; n = &(*n)->next)
136 if ((*n)->major == major)
137 break;
138 if (!*n || strcmp((*n)->name, name))
139 ret = -EINVAL;
140 else {
141 p = *n;
142 *n = p->next;
144 up(&block_subsys_sem);
145 kfree(p);
147 return ret;
150 EXPORT_SYMBOL(unregister_blkdev);
152 static struct kobj_map *bdev_map;
155 * Register device numbers dev..(dev+range-1)
156 * range must be nonzero
157 * The hash chain is sorted on range, so that subranges can override.
159 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
160 struct kobject *(*probe)(dev_t, int *, void *),
161 int (*lock)(dev_t, void *), void *data)
163 kobj_map(bdev_map, dev, range, module, probe, lock, data);
166 EXPORT_SYMBOL(blk_register_region);
168 void blk_unregister_region(dev_t dev, unsigned long range)
170 kobj_unmap(bdev_map, dev, range);
173 EXPORT_SYMBOL(blk_unregister_region);
175 static struct kobject *exact_match(dev_t dev, int *part, void *data)
177 struct gendisk *p = data;
178 return &p->kobj;
181 static int exact_lock(dev_t dev, void *data)
183 struct gendisk *p = data;
185 if (!get_disk(p))
186 return -1;
187 return 0;
191 * add_disk - add partitioning information to kernel list
192 * @disk: per-device partitioning information
194 * This function registers the partitioning information in @disk
195 * with the kernel.
197 void add_disk(struct gendisk *disk)
199 disk->flags |= GENHD_FL_UP;
200 blk_register_region(MKDEV(disk->major, disk->first_minor),
201 disk->minors, NULL, exact_match, exact_lock, disk);
202 register_disk(disk);
203 blk_register_queue(disk);
206 EXPORT_SYMBOL(add_disk);
207 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
209 void unlink_gendisk(struct gendisk *disk)
211 blk_unregister_queue(disk);
212 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
213 disk->minors);
216 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
219 * get_gendisk - get partitioning information for a given device
220 * @dev: device to get partitioning information for
222 * This function gets the structure containing partitioning
223 * information for the given device @dev.
225 struct gendisk *get_gendisk(dev_t dev, int *part)
227 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
228 return kobj ? to_disk(kobj) : NULL;
231 #ifdef CONFIG_PROC_FS
232 /* iterator */
233 static void *part_start(struct seq_file *part, loff_t *pos)
235 struct list_head *p;
236 loff_t l = *pos;
238 down(&block_subsys_sem);
239 list_for_each(p, &block_subsys.kset.list)
240 if (!l--)
241 return list_entry(p, struct gendisk, kobj.entry);
242 return NULL;
245 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
247 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
248 ++*pos;
249 return p==&block_subsys.kset.list ? NULL :
250 list_entry(p, struct gendisk, kobj.entry);
253 static void part_stop(struct seq_file *part, void *v)
255 up(&block_subsys_sem);
258 static int show_partition(struct seq_file *part, void *v)
260 struct gendisk *sgp = v;
261 int n;
262 char buf[BDEVNAME_SIZE];
264 if (&sgp->kobj.entry == block_subsys.kset.list.next)
265 seq_puts(part, "major minor #blocks name\n\n");
267 /* Don't show non-partitionable removeable devices or empty devices */
268 if (!get_capacity(sgp) ||
269 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
270 return 0;
271 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
272 return 0;
274 /* show the full disk and all non-0 size partitions of it */
275 seq_printf(part, "%4d %4d %10llu %s\n",
276 sgp->major, sgp->first_minor,
277 (unsigned long long)get_capacity(sgp) >> 1,
278 disk_name(sgp, 0, buf));
279 for (n = 0; n < sgp->minors - 1; n++) {
280 if (!sgp->part[n])
281 continue;
282 if (sgp->part[n]->nr_sects == 0)
283 continue;
284 seq_printf(part, "%4d %4d %10llu %s\n",
285 sgp->major, n + 1 + sgp->first_minor,
286 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
287 disk_name(sgp, n + 1, buf));
290 return 0;
293 struct seq_operations partitions_op = {
294 .start =part_start,
295 .next = part_next,
296 .stop = part_stop,
297 .show = show_partition
299 #endif
302 extern int blk_dev_init(void);
304 static struct kobject *base_probe(dev_t dev, int *part, void *data)
306 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
307 /* Make old-style 2.4 aliases work */
308 request_module("block-major-%d", MAJOR(dev));
309 return NULL;
312 static int __init genhd_device_init(void)
314 bdev_map = kobj_map_init(base_probe, &block_subsys_sem);
315 blk_dev_init();
316 subsystem_register(&block_subsys);
317 return 0;
320 subsys_initcall(genhd_device_init);
325 * kobject & sysfs bindings for block devices
327 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
328 char *page)
330 struct gendisk *disk = to_disk(kobj);
331 struct disk_attribute *disk_attr =
332 container_of(attr,struct disk_attribute,attr);
333 ssize_t ret = -EIO;
335 if (disk_attr->show)
336 ret = disk_attr->show(disk,page);
337 return ret;
340 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
341 const char *page, size_t count)
343 struct gendisk *disk = to_disk(kobj);
344 struct disk_attribute *disk_attr =
345 container_of(attr,struct disk_attribute,attr);
346 ssize_t ret = 0;
348 if (disk_attr->store)
349 ret = disk_attr->store(disk, page, count);
350 return ret;
353 static struct sysfs_ops disk_sysfs_ops = {
354 .show = &disk_attr_show,
355 .store = &disk_attr_store,
358 static ssize_t disk_uevent_store(struct gendisk * disk,
359 const char *buf, size_t count)
361 kobject_uevent(&disk->kobj, KOBJ_ADD);
362 return count;
364 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
366 dev_t base = MKDEV(disk->major, disk->first_minor);
367 return print_dev_t(page, base);
369 static ssize_t disk_range_read(struct gendisk * disk, char *page)
371 return sprintf(page, "%d\n", disk->minors);
373 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
375 return sprintf(page, "%d\n",
376 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
379 static ssize_t disk_size_read(struct gendisk * disk, char *page)
381 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
384 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
386 preempt_disable();
387 disk_round_stats(disk);
388 preempt_enable();
389 return sprintf(page,
390 "%8u %8u %8llu %8u "
391 "%8u %8u %8llu %8u "
392 "%8u %8u %8u"
393 "\n",
394 disk_stat_read(disk, ios[READ]),
395 disk_stat_read(disk, merges[READ]),
396 (unsigned long long)disk_stat_read(disk, sectors[READ]),
397 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
398 disk_stat_read(disk, ios[WRITE]),
399 disk_stat_read(disk, merges[WRITE]),
400 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
401 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
402 disk->in_flight,
403 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
404 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
406 static struct disk_attribute disk_attr_uevent = {
407 .attr = {.name = "uevent", .mode = S_IWUSR },
408 .store = disk_uevent_store
410 static struct disk_attribute disk_attr_dev = {
411 .attr = {.name = "dev", .mode = S_IRUGO },
412 .show = disk_dev_read
414 static struct disk_attribute disk_attr_range = {
415 .attr = {.name = "range", .mode = S_IRUGO },
416 .show = disk_range_read
418 static struct disk_attribute disk_attr_removable = {
419 .attr = {.name = "removable", .mode = S_IRUGO },
420 .show = disk_removable_read
422 static struct disk_attribute disk_attr_size = {
423 .attr = {.name = "size", .mode = S_IRUGO },
424 .show = disk_size_read
426 static struct disk_attribute disk_attr_stat = {
427 .attr = {.name = "stat", .mode = S_IRUGO },
428 .show = disk_stats_read
431 static struct attribute * default_attrs[] = {
432 &disk_attr_uevent.attr,
433 &disk_attr_dev.attr,
434 &disk_attr_range.attr,
435 &disk_attr_removable.attr,
436 &disk_attr_size.attr,
437 &disk_attr_stat.attr,
438 NULL,
441 static void disk_release(struct kobject * kobj)
443 struct gendisk *disk = to_disk(kobj);
444 kfree(disk->random);
445 kfree(disk->part);
446 free_disk_stats(disk);
447 kfree(disk);
450 static struct kobj_type ktype_block = {
451 .release = disk_release,
452 .sysfs_ops = &disk_sysfs_ops,
453 .default_attrs = default_attrs,
456 extern struct kobj_type ktype_part;
458 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
460 struct kobj_type *ktype = get_ktype(kobj);
462 return ((ktype == &ktype_block) || (ktype == &ktype_part));
465 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
466 int num_envp, char *buffer, int buffer_size)
468 struct kobj_type *ktype = get_ktype(kobj);
469 struct device *physdev;
470 struct gendisk *disk;
471 struct hd_struct *part;
472 int length = 0;
473 int i = 0;
475 if (ktype == &ktype_block) {
476 disk = container_of(kobj, struct gendisk, kobj);
477 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
478 &length, "MINOR=%u", disk->first_minor);
479 } else if (ktype == &ktype_part) {
480 disk = container_of(kobj->parent, struct gendisk, kobj);
481 part = container_of(kobj, struct hd_struct, kobj);
482 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
483 &length, "MINOR=%u",
484 disk->first_minor + part->partno);
485 } else
486 return 0;
488 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
489 "MAJOR=%u", disk->major);
491 /* add physical device, backing this device */
492 physdev = disk->driverfs_dev;
493 if (physdev) {
494 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
496 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
497 &length, "PHYSDEVPATH=%s", path);
498 kfree(path);
500 if (physdev->bus)
501 add_uevent_var(envp, num_envp, &i,
502 buffer, buffer_size, &length,
503 "PHYSDEVBUS=%s",
504 physdev->bus->name);
506 if (physdev->driver)
507 add_uevent_var(envp, num_envp, &i,
508 buffer, buffer_size, &length,
509 "PHYSDEVDRIVER=%s",
510 physdev->driver->name);
513 /* terminate, set to next free slot, shrink available space */
514 envp[i] = NULL;
515 envp = &envp[i];
516 num_envp -= i;
517 buffer = &buffer[length];
518 buffer_size -= length;
520 return 0;
523 static struct kset_uevent_ops block_uevent_ops = {
524 .filter = block_uevent_filter,
525 .uevent = block_uevent,
528 /* declare block_subsys. */
529 static decl_subsys(block, &ktype_block, &block_uevent_ops);
533 * aggregate disk stat collector. Uses the same stats that the sysfs
534 * entries do, above, but makes them available through one seq_file.
535 * Watching a few disks may be efficient through sysfs, but watching
536 * all of them will be more efficient through this interface.
538 * The output looks suspiciously like /proc/partitions with a bunch of
539 * extra fields.
542 /* iterator */
543 static void *diskstats_start(struct seq_file *part, loff_t *pos)
545 loff_t k = *pos;
546 struct list_head *p;
548 down(&block_subsys_sem);
549 list_for_each(p, &block_subsys.kset.list)
550 if (!k--)
551 return list_entry(p, struct gendisk, kobj.entry);
552 return NULL;
555 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
557 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
558 ++*pos;
559 return p==&block_subsys.kset.list ? NULL :
560 list_entry(p, struct gendisk, kobj.entry);
563 static void diskstats_stop(struct seq_file *part, void *v)
565 up(&block_subsys_sem);
568 static int diskstats_show(struct seq_file *s, void *v)
570 struct gendisk *gp = v;
571 char buf[BDEVNAME_SIZE];
572 int n = 0;
575 if (&sgp->kobj.entry == block_subsys.kset.list.next)
576 seq_puts(s, "major minor name"
577 " rio rmerge rsect ruse wio wmerge "
578 "wsect wuse running use aveq"
579 "\n\n");
582 preempt_disable();
583 disk_round_stats(gp);
584 preempt_enable();
585 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
586 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
587 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
588 (unsigned long long)disk_stat_read(gp, sectors[0]),
589 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
590 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
591 (unsigned long long)disk_stat_read(gp, sectors[1]),
592 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
593 gp->in_flight,
594 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
595 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
597 /* now show all non-0 size partitions of it */
598 for (n = 0; n < gp->minors - 1; n++) {
599 struct hd_struct *hd = gp->part[n];
601 if (hd && hd->nr_sects)
602 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
603 gp->major, n + gp->first_minor + 1,
604 disk_name(gp, n + 1, buf),
605 hd->ios[0], hd->sectors[0],
606 hd->ios[1], hd->sectors[1]);
609 return 0;
612 struct seq_operations diskstats_op = {
613 .start = diskstats_start,
614 .next = diskstats_next,
615 .stop = diskstats_stop,
616 .show = diskstats_show
619 struct gendisk *alloc_disk(int minors)
621 return alloc_disk_node(minors, -1);
624 struct gendisk *alloc_disk_node(int minors, int node_id)
626 struct gendisk *disk;
628 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
629 if (disk) {
630 memset(disk, 0, sizeof(struct gendisk));
631 if (!init_disk_stats(disk)) {
632 kfree(disk);
633 return NULL;
635 if (minors > 1) {
636 int size = (minors - 1) * sizeof(struct hd_struct *);
637 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
638 if (!disk->part) {
639 kfree(disk);
640 return NULL;
642 memset(disk->part, 0, size);
644 disk->minors = minors;
645 kobj_set_kset_s(disk,block_subsys);
646 kobject_init(&disk->kobj);
647 rand_initialize_disk(disk);
649 return disk;
652 EXPORT_SYMBOL(alloc_disk);
653 EXPORT_SYMBOL(alloc_disk_node);
655 struct kobject *get_disk(struct gendisk *disk)
657 struct module *owner;
658 struct kobject *kobj;
660 if (!disk->fops)
661 return NULL;
662 owner = disk->fops->owner;
663 if (owner && !try_module_get(owner))
664 return NULL;
665 kobj = kobject_get(&disk->kobj);
666 if (kobj == NULL) {
667 module_put(owner);
668 return NULL;
670 return kobj;
674 EXPORT_SYMBOL(get_disk);
676 void put_disk(struct gendisk *disk)
678 if (disk)
679 kobject_put(&disk->kobj);
682 EXPORT_SYMBOL(put_disk);
684 void set_device_ro(struct block_device *bdev, int flag)
686 if (bdev->bd_contains != bdev)
687 bdev->bd_part->policy = flag;
688 else
689 bdev->bd_disk->policy = flag;
692 EXPORT_SYMBOL(set_device_ro);
694 void set_disk_ro(struct gendisk *disk, int flag)
696 int i;
697 disk->policy = flag;
698 for (i = 0; i < disk->minors - 1; i++)
699 if (disk->part[i]) disk->part[i]->policy = flag;
702 EXPORT_SYMBOL(set_disk_ro);
704 int bdev_read_only(struct block_device *bdev)
706 if (!bdev)
707 return 0;
708 else if (bdev->bd_contains != bdev)
709 return bdev->bd_part->policy;
710 else
711 return bdev->bd_disk->policy;
714 EXPORT_SYMBOL(bdev_read_only);
716 int invalidate_partition(struct gendisk *disk, int index)
718 int res = 0;
719 struct block_device *bdev = bdget_disk(disk, index);
720 if (bdev) {
721 fsync_bdev(bdev);
722 res = __invalidate_device(bdev);
723 bdput(bdev);
725 return res;
728 EXPORT_SYMBOL(invalidate_partition);