x86: force timer broadcast on late AMD C1E detection
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / genhd.c
blobe609996f2e766d7009d5984aa8b4fbc29c813bbf
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 void unregister_blkdev(unsigned int major, const char *name)
113 struct blk_major_name **n;
114 struct blk_major_name *p = NULL;
115 int index = major_to_index(major);
117 mutex_lock(&block_subsys_lock);
118 for (n = &major_names[index]; *n; n = &(*n)->next)
119 if ((*n)->major == major)
120 break;
121 if (!*n || strcmp((*n)->name, name)) {
122 WARN_ON(1);
123 } else {
124 p = *n;
125 *n = p->next;
127 mutex_unlock(&block_subsys_lock);
128 kfree(p);
131 EXPORT_SYMBOL(unregister_blkdev);
133 static struct kobj_map *bdev_map;
136 * Register device numbers dev..(dev+range-1)
137 * range must be nonzero
138 * The hash chain is sorted on range, so that subranges can override.
140 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
141 struct kobject *(*probe)(dev_t, int *, void *),
142 int (*lock)(dev_t, void *), void *data)
144 kobj_map(bdev_map, dev, range, module, probe, lock, data);
147 EXPORT_SYMBOL(blk_register_region);
149 void blk_unregister_region(dev_t dev, unsigned long range)
151 kobj_unmap(bdev_map, dev, range);
154 EXPORT_SYMBOL(blk_unregister_region);
156 static struct kobject *exact_match(dev_t dev, int *part, void *data)
158 struct gendisk *p = data;
159 return &p->kobj;
162 static int exact_lock(dev_t dev, void *data)
164 struct gendisk *p = data;
166 if (!get_disk(p))
167 return -1;
168 return 0;
172 * add_disk - add partitioning information to kernel list
173 * @disk: per-device partitioning information
175 * This function registers the partitioning information in @disk
176 * with the kernel.
178 void add_disk(struct gendisk *disk)
180 disk->flags |= GENHD_FL_UP;
181 blk_register_region(MKDEV(disk->major, disk->first_minor),
182 disk->minors, NULL, exact_match, exact_lock, disk);
183 register_disk(disk);
184 blk_register_queue(disk);
187 EXPORT_SYMBOL(add_disk);
188 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
190 void unlink_gendisk(struct gendisk *disk)
192 blk_unregister_queue(disk);
193 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
194 disk->minors);
197 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
200 * get_gendisk - get partitioning information for a given device
201 * @dev: device to get partitioning information for
203 * This function gets the structure containing partitioning
204 * information for the given device @dev.
206 struct gendisk *get_gendisk(dev_t dev, int *part)
208 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
209 return kobj ? to_disk(kobj) : NULL;
213 * print a full list of all partitions - intended for places where the root
214 * filesystem can't be mounted and thus to give the victim some idea of what
215 * went wrong
217 void __init printk_all_partitions(void)
219 int n;
220 struct gendisk *sgp;
222 mutex_lock(&block_subsys_lock);
223 /* For each block device... */
224 list_for_each_entry(sgp, &block_subsys.list, kobj.entry) {
225 char buf[BDEVNAME_SIZE];
227 * Don't show empty devices or things that have been surpressed
229 if (get_capacity(sgp) == 0 ||
230 (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO))
231 continue;
234 * Note, unlike /proc/partitions, I am showing the numbers in
235 * hex - the same format as the root= option takes.
237 printk("%02x%02x %10llu %s",
238 sgp->major, sgp->first_minor,
239 (unsigned long long)get_capacity(sgp) >> 1,
240 disk_name(sgp, 0, buf));
241 if (sgp->driverfs_dev != NULL &&
242 sgp->driverfs_dev->driver != NULL)
243 printk(" driver: %s\n",
244 sgp->driverfs_dev->driver->name);
245 else
246 printk(" (driver?)\n");
248 /* now show the partitions */
249 for (n = 0; n < sgp->minors - 1; ++n) {
250 if (sgp->part[n] == NULL)
251 continue;
252 if (sgp->part[n]->nr_sects == 0)
253 continue;
254 printk(" %02x%02x %10llu %s\n",
255 sgp->major, n + 1 + sgp->first_minor,
256 (unsigned long long)sgp->part[n]->nr_sects >> 1,
257 disk_name(sgp, n + 1, buf));
258 } /* partition subloop */
259 } /* Block device loop */
261 mutex_unlock(&block_subsys_lock);
262 return;
265 #ifdef CONFIG_PROC_FS
266 /* iterator */
267 static void *part_start(struct seq_file *part, loff_t *pos)
269 struct list_head *p;
270 loff_t l = *pos;
272 mutex_lock(&block_subsys_lock);
273 list_for_each(p, &block_subsys.list)
274 if (!l--)
275 return list_entry(p, struct gendisk, kobj.entry);
276 return NULL;
279 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
281 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
282 ++*pos;
283 return p==&block_subsys.list ? NULL :
284 list_entry(p, struct gendisk, kobj.entry);
287 static void part_stop(struct seq_file *part, void *v)
289 mutex_unlock(&block_subsys_lock);
292 static int show_partition(struct seq_file *part, void *v)
294 struct gendisk *sgp = v;
295 int n;
296 char buf[BDEVNAME_SIZE];
298 if (&sgp->kobj.entry == block_subsys.list.next)
299 seq_puts(part, "major minor #blocks name\n\n");
301 /* Don't show non-partitionable removeable devices or empty devices */
302 if (!get_capacity(sgp) ||
303 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
304 return 0;
305 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
306 return 0;
308 /* show the full disk and all non-0 size partitions of it */
309 seq_printf(part, "%4d %4d %10llu %s\n",
310 sgp->major, sgp->first_minor,
311 (unsigned long long)get_capacity(sgp) >> 1,
312 disk_name(sgp, 0, buf));
313 for (n = 0; n < sgp->minors - 1; n++) {
314 if (!sgp->part[n])
315 continue;
316 if (sgp->part[n]->nr_sects == 0)
317 continue;
318 seq_printf(part, "%4d %4d %10llu %s\n",
319 sgp->major, n + 1 + sgp->first_minor,
320 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
321 disk_name(sgp, n + 1, buf));
324 return 0;
327 struct seq_operations partitions_op = {
328 .start =part_start,
329 .next = part_next,
330 .stop = part_stop,
331 .show = show_partition
333 #endif
336 extern int blk_dev_init(void);
338 static struct kobject *base_probe(dev_t dev, int *part, void *data)
340 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
341 /* Make old-style 2.4 aliases work */
342 request_module("block-major-%d", MAJOR(dev));
343 return NULL;
346 static int __init genhd_device_init(void)
348 int err;
350 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
351 blk_dev_init();
352 err = subsystem_register(&block_subsys);
353 if (err < 0)
354 printk(KERN_WARNING "%s: subsystem_register error: %d\n",
355 __FUNCTION__, err);
356 return err;
359 subsys_initcall(genhd_device_init);
364 * kobject & sysfs bindings for block devices
366 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
367 char *page)
369 struct gendisk *disk = to_disk(kobj);
370 struct disk_attribute *disk_attr =
371 container_of(attr,struct disk_attribute,attr);
372 ssize_t ret = -EIO;
374 if (disk_attr->show)
375 ret = disk_attr->show(disk,page);
376 return ret;
379 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
380 const char *page, size_t count)
382 struct gendisk *disk = to_disk(kobj);
383 struct disk_attribute *disk_attr =
384 container_of(attr,struct disk_attribute,attr);
385 ssize_t ret = 0;
387 if (disk_attr->store)
388 ret = disk_attr->store(disk, page, count);
389 return ret;
392 static struct sysfs_ops disk_sysfs_ops = {
393 .show = &disk_attr_show,
394 .store = &disk_attr_store,
397 static ssize_t disk_uevent_store(struct gendisk * disk,
398 const char *buf, size_t count)
400 kobject_uevent(&disk->kobj, KOBJ_ADD);
401 return count;
403 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
405 dev_t base = MKDEV(disk->major, disk->first_minor);
406 return print_dev_t(page, base);
408 static ssize_t disk_range_read(struct gendisk * disk, char *page)
410 return sprintf(page, "%d\n", disk->minors);
412 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
414 return sprintf(page, "%d\n",
415 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
418 static ssize_t disk_size_read(struct gendisk * disk, char *page)
420 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
422 static ssize_t disk_capability_read(struct gendisk *disk, char *page)
424 return sprintf(page, "%x\n", disk->flags);
426 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
428 preempt_disable();
429 disk_round_stats(disk);
430 preempt_enable();
431 return sprintf(page,
432 "%8lu %8lu %8llu %8u "
433 "%8lu %8lu %8llu %8u "
434 "%8u %8u %8u"
435 "\n",
436 disk_stat_read(disk, ios[READ]),
437 disk_stat_read(disk, merges[READ]),
438 (unsigned long long)disk_stat_read(disk, sectors[READ]),
439 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
440 disk_stat_read(disk, ios[WRITE]),
441 disk_stat_read(disk, merges[WRITE]),
442 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
443 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
444 disk->in_flight,
445 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
446 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
448 static struct disk_attribute disk_attr_uevent = {
449 .attr = {.name = "uevent", .mode = S_IWUSR },
450 .store = disk_uevent_store
452 static struct disk_attribute disk_attr_dev = {
453 .attr = {.name = "dev", .mode = S_IRUGO },
454 .show = disk_dev_read
456 static struct disk_attribute disk_attr_range = {
457 .attr = {.name = "range", .mode = S_IRUGO },
458 .show = disk_range_read
460 static struct disk_attribute disk_attr_removable = {
461 .attr = {.name = "removable", .mode = S_IRUGO },
462 .show = disk_removable_read
464 static struct disk_attribute disk_attr_size = {
465 .attr = {.name = "size", .mode = S_IRUGO },
466 .show = disk_size_read
468 static struct disk_attribute disk_attr_capability = {
469 .attr = {.name = "capability", .mode = S_IRUGO },
470 .show = disk_capability_read
472 static struct disk_attribute disk_attr_stat = {
473 .attr = {.name = "stat", .mode = S_IRUGO },
474 .show = disk_stats_read
477 #ifdef CONFIG_FAIL_MAKE_REQUEST
479 static ssize_t disk_fail_store(struct gendisk * disk,
480 const char *buf, size_t count)
482 int i;
484 if (count > 0 && sscanf(buf, "%d", &i) > 0) {
485 if (i == 0)
486 disk->flags &= ~GENHD_FL_FAIL;
487 else
488 disk->flags |= GENHD_FL_FAIL;
491 return count;
493 static ssize_t disk_fail_read(struct gendisk * disk, char *page)
495 return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0);
497 static struct disk_attribute disk_attr_fail = {
498 .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR },
499 .store = disk_fail_store,
500 .show = disk_fail_read
503 #endif
505 static struct attribute * default_attrs[] = {
506 &disk_attr_uevent.attr,
507 &disk_attr_dev.attr,
508 &disk_attr_range.attr,
509 &disk_attr_removable.attr,
510 &disk_attr_size.attr,
511 &disk_attr_stat.attr,
512 &disk_attr_capability.attr,
513 #ifdef CONFIG_FAIL_MAKE_REQUEST
514 &disk_attr_fail.attr,
515 #endif
516 NULL,
519 static void disk_release(struct kobject * kobj)
521 struct gendisk *disk = to_disk(kobj);
522 kfree(disk->random);
523 kfree(disk->part);
524 free_disk_stats(disk);
525 kfree(disk);
528 static struct kobj_type ktype_block = {
529 .release = disk_release,
530 .sysfs_ops = &disk_sysfs_ops,
531 .default_attrs = default_attrs,
534 extern struct kobj_type ktype_part;
536 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
538 struct kobj_type *ktype = get_ktype(kobj);
540 return ((ktype == &ktype_block) || (ktype == &ktype_part));
543 static int block_uevent(struct kset *kset, struct kobject *kobj,
544 struct kobj_uevent_env *env)
546 struct kobj_type *ktype = get_ktype(kobj);
547 struct device *physdev;
548 struct gendisk *disk;
549 struct hd_struct *part;
551 if (ktype == &ktype_block) {
552 disk = container_of(kobj, struct gendisk, kobj);
553 add_uevent_var(env, "MINOR=%u", disk->first_minor);
554 } else if (ktype == &ktype_part) {
555 disk = container_of(kobj->parent, struct gendisk, kobj);
556 part = container_of(kobj, struct hd_struct, kobj);
557 add_uevent_var(env, "MINOR=%u",
558 disk->first_minor + part->partno);
559 } else
560 return 0;
562 add_uevent_var(env, "MAJOR=%u", disk->major);
564 /* add physical device, backing this device */
565 physdev = disk->driverfs_dev;
566 if (physdev) {
567 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
569 add_uevent_var(env, "PHYSDEVPATH=%s", path);
570 kfree(path);
572 if (physdev->bus)
573 add_uevent_var(env, "PHYSDEVBUS=%s", physdev->bus->name);
575 if (physdev->driver)
576 add_uevent_var(env, physdev->driver->name);
579 return 0;
582 static struct kset_uevent_ops block_uevent_ops = {
583 .filter = block_uevent_filter,
584 .uevent = block_uevent,
587 decl_subsys(block, &ktype_block, &block_uevent_ops);
590 * aggregate disk stat collector. Uses the same stats that the sysfs
591 * entries do, above, but makes them available through one seq_file.
592 * Watching a few disks may be efficient through sysfs, but watching
593 * all of them will be more efficient through this interface.
595 * The output looks suspiciously like /proc/partitions with a bunch of
596 * extra fields.
599 /* iterator */
600 static void *diskstats_start(struct seq_file *part, loff_t *pos)
602 loff_t k = *pos;
603 struct list_head *p;
605 mutex_lock(&block_subsys_lock);
606 list_for_each(p, &block_subsys.list)
607 if (!k--)
608 return list_entry(p, struct gendisk, kobj.entry);
609 return NULL;
612 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
614 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
615 ++*pos;
616 return p==&block_subsys.list ? NULL :
617 list_entry(p, struct gendisk, kobj.entry);
620 static void diskstats_stop(struct seq_file *part, void *v)
622 mutex_unlock(&block_subsys_lock);
625 static int diskstats_show(struct seq_file *s, void *v)
627 struct gendisk *gp = v;
628 char buf[BDEVNAME_SIZE];
629 int n = 0;
632 if (&sgp->kobj.entry == block_subsys.kset.list.next)
633 seq_puts(s, "major minor name"
634 " rio rmerge rsect ruse wio wmerge "
635 "wsect wuse running use aveq"
636 "\n\n");
639 preempt_disable();
640 disk_round_stats(gp);
641 preempt_enable();
642 seq_printf(s, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
643 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
644 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
645 (unsigned long long)disk_stat_read(gp, sectors[0]),
646 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
647 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
648 (unsigned long long)disk_stat_read(gp, sectors[1]),
649 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
650 gp->in_flight,
651 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
652 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
654 /* now show all non-0 size partitions of it */
655 for (n = 0; n < gp->minors - 1; n++) {
656 struct hd_struct *hd = gp->part[n];
658 if (hd && hd->nr_sects)
659 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
660 gp->major, n + gp->first_minor + 1,
661 disk_name(gp, n + 1, buf),
662 hd->ios[0], hd->sectors[0],
663 hd->ios[1], hd->sectors[1]);
666 return 0;
669 struct seq_operations diskstats_op = {
670 .start = diskstats_start,
671 .next = diskstats_next,
672 .stop = diskstats_stop,
673 .show = diskstats_show
676 static void media_change_notify_thread(struct work_struct *work)
678 struct gendisk *gd = container_of(work, struct gendisk, async_notify);
679 char event[] = "MEDIA_CHANGE=1";
680 char *envp[] = { event, NULL };
683 * set enviroment vars to indicate which event this is for
684 * so that user space will know to go check the media status.
686 kobject_uevent_env(&gd->kobj, KOBJ_CHANGE, envp);
687 put_device(gd->driverfs_dev);
690 void genhd_media_change_notify(struct gendisk *disk)
692 get_device(disk->driverfs_dev);
693 schedule_work(&disk->async_notify);
695 EXPORT_SYMBOL_GPL(genhd_media_change_notify);
697 struct gendisk *alloc_disk(int minors)
699 return alloc_disk_node(minors, -1);
702 struct gendisk *alloc_disk_node(int minors, int node_id)
704 struct gendisk *disk;
706 disk = kmalloc_node(sizeof(struct gendisk),
707 GFP_KERNEL | __GFP_ZERO, node_id);
708 if (disk) {
709 if (!init_disk_stats(disk)) {
710 kfree(disk);
711 return NULL;
713 if (minors > 1) {
714 int size = (minors - 1) * sizeof(struct hd_struct *);
715 disk->part = kmalloc_node(size,
716 GFP_KERNEL | __GFP_ZERO, node_id);
717 if (!disk->part) {
718 kfree(disk);
719 return NULL;
722 disk->minors = minors;
723 kobj_set_kset_s(disk,block_subsys);
724 kobject_init(&disk->kobj);
725 rand_initialize_disk(disk);
726 INIT_WORK(&disk->async_notify,
727 media_change_notify_thread);
729 return disk;
732 EXPORT_SYMBOL(alloc_disk);
733 EXPORT_SYMBOL(alloc_disk_node);
735 struct kobject *get_disk(struct gendisk *disk)
737 struct module *owner;
738 struct kobject *kobj;
740 if (!disk->fops)
741 return NULL;
742 owner = disk->fops->owner;
743 if (owner && !try_module_get(owner))
744 return NULL;
745 kobj = kobject_get(&disk->kobj);
746 if (kobj == NULL) {
747 module_put(owner);
748 return NULL;
750 return kobj;
754 EXPORT_SYMBOL(get_disk);
756 void put_disk(struct gendisk *disk)
758 if (disk)
759 kobject_put(&disk->kobj);
762 EXPORT_SYMBOL(put_disk);
764 void set_device_ro(struct block_device *bdev, int flag)
766 if (bdev->bd_contains != bdev)
767 bdev->bd_part->policy = flag;
768 else
769 bdev->bd_disk->policy = flag;
772 EXPORT_SYMBOL(set_device_ro);
774 void set_disk_ro(struct gendisk *disk, int flag)
776 int i;
777 disk->policy = flag;
778 for (i = 0; i < disk->minors - 1; i++)
779 if (disk->part[i]) disk->part[i]->policy = flag;
782 EXPORT_SYMBOL(set_disk_ro);
784 int bdev_read_only(struct block_device *bdev)
786 if (!bdev)
787 return 0;
788 else if (bdev->bd_contains != bdev)
789 return bdev->bd_part->policy;
790 else
791 return bdev->bd_disk->policy;
794 EXPORT_SYMBOL(bdev_read_only);
796 int invalidate_partition(struct gendisk *disk, int index)
798 int res = 0;
799 struct block_device *bdev = bdget_disk(disk, index);
800 if (bdev) {
801 fsync_bdev(bdev);
802 res = __invalidate_device(bdev);
803 bdput(bdev);
805 return res;
808 EXPORT_SYMBOL(invalidate_partition);