powerpc: Make uImage default build output for MPC8540 ADS
[linux-2.6/mini2440.git] / block / genhd.c
blob64510fd88621245c82dc0064c67a9b312ea47072
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>
18 #include <linux/mutex.h>
20 #define MAX_PROBE_HASH 255 /* random */
22 static struct subsystem block_subsys;
24 static DEFINE_MUTEX(block_subsys_lock);
27 * Can be deleted altogether. Later.
30 static struct blk_major_name {
31 struct blk_major_name *next;
32 int major;
33 char name[16];
34 } *major_names[MAX_PROBE_HASH];
36 /* index in the above - for now: assume no multimajor ranges */
37 static inline int major_to_index(int major)
39 return major % MAX_PROBE_HASH;
42 struct blkdev_info {
43 int index;
44 struct blk_major_name *bd;
48 * iterate over a list of blkdev_info structures. allows
49 * the major_names array to be iterated over from outside this file
50 * must be called with the block_subsys_lock held
52 void *get_next_blkdev(void *dev)
54 struct blkdev_info *info;
56 if (dev == NULL) {
57 info = kmalloc(sizeof(*info), GFP_KERNEL);
58 if (!info)
59 goto out;
60 info->index=0;
61 info->bd = major_names[info->index];
62 if (info->bd)
63 goto out;
64 } else {
65 info = dev;
68 while (info->index < ARRAY_SIZE(major_names)) {
69 if (info->bd)
70 info->bd = info->bd->next;
71 if (info->bd)
72 goto out;
74 * No devices on this chain, move to the next
76 info->index++;
77 info->bd = (info->index < ARRAY_SIZE(major_names)) ?
78 major_names[info->index] : NULL;
79 if (info->bd)
80 goto out;
83 out:
84 return info;
87 void *acquire_blkdev_list(void)
89 mutex_lock(&block_subsys_lock);
90 return get_next_blkdev(NULL);
93 void release_blkdev_list(void *dev)
95 mutex_unlock(&block_subsys_lock);
96 kfree(dev);
101 * Count the number of records in the blkdev_list.
102 * must be called with the block_subsys_lock held
104 int count_blkdev_list(void)
106 struct blk_major_name *n;
107 int i, count;
109 count = 0;
111 for (i = 0; i < ARRAY_SIZE(major_names); i++) {
112 for (n = major_names[i]; n; n = n->next)
113 count++;
116 return count;
120 * extract the major and name values from a blkdev_info struct
121 * passed in as a void to *dev. Must be called with
122 * block_subsys_lock held
124 int get_blkdev_info(void *dev, int *major, char **name)
126 struct blkdev_info *info = dev;
128 if (info->bd == NULL)
129 return 1;
131 *major = info->bd->major;
132 *name = info->bd->name;
133 return 0;
137 int register_blkdev(unsigned int major, const char *name)
139 struct blk_major_name **n, *p;
140 int index, ret = 0;
142 mutex_lock(&block_subsys_lock);
144 /* temporary */
145 if (major == 0) {
146 for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) {
147 if (major_names[index] == NULL)
148 break;
151 if (index == 0) {
152 printk("register_blkdev: failed to get major for %s\n",
153 name);
154 ret = -EBUSY;
155 goto out;
157 major = index;
158 ret = major;
161 p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL);
162 if (p == NULL) {
163 ret = -ENOMEM;
164 goto out;
167 p->major = major;
168 strlcpy(p->name, name, sizeof(p->name));
169 p->next = NULL;
170 index = major_to_index(major);
172 for (n = &major_names[index]; *n; n = &(*n)->next) {
173 if ((*n)->major == major)
174 break;
176 if (!*n)
177 *n = p;
178 else
179 ret = -EBUSY;
181 if (ret < 0) {
182 printk("register_blkdev: cannot get major %d for %s\n",
183 major, name);
184 kfree(p);
186 out:
187 mutex_unlock(&block_subsys_lock);
188 return ret;
191 EXPORT_SYMBOL(register_blkdev);
193 /* todo: make void - error printk here */
194 int unregister_blkdev(unsigned int major, const char *name)
196 struct blk_major_name **n;
197 struct blk_major_name *p = NULL;
198 int index = major_to_index(major);
199 int ret = 0;
201 mutex_lock(&block_subsys_lock);
202 for (n = &major_names[index]; *n; n = &(*n)->next)
203 if ((*n)->major == major)
204 break;
205 if (!*n || strcmp((*n)->name, name))
206 ret = -EINVAL;
207 else {
208 p = *n;
209 *n = p->next;
211 mutex_unlock(&block_subsys_lock);
212 kfree(p);
214 return ret;
217 EXPORT_SYMBOL(unregister_blkdev);
219 static struct kobj_map *bdev_map;
222 * Register device numbers dev..(dev+range-1)
223 * range must be nonzero
224 * The hash chain is sorted on range, so that subranges can override.
226 void blk_register_region(dev_t dev, unsigned long range, struct module *module,
227 struct kobject *(*probe)(dev_t, int *, void *),
228 int (*lock)(dev_t, void *), void *data)
230 kobj_map(bdev_map, dev, range, module, probe, lock, data);
233 EXPORT_SYMBOL(blk_register_region);
235 void blk_unregister_region(dev_t dev, unsigned long range)
237 kobj_unmap(bdev_map, dev, range);
240 EXPORT_SYMBOL(blk_unregister_region);
242 static struct kobject *exact_match(dev_t dev, int *part, void *data)
244 struct gendisk *p = data;
245 return &p->kobj;
248 static int exact_lock(dev_t dev, void *data)
250 struct gendisk *p = data;
252 if (!get_disk(p))
253 return -1;
254 return 0;
258 * add_disk - add partitioning information to kernel list
259 * @disk: per-device partitioning information
261 * This function registers the partitioning information in @disk
262 * with the kernel.
264 void add_disk(struct gendisk *disk)
266 disk->flags |= GENHD_FL_UP;
267 blk_register_region(MKDEV(disk->major, disk->first_minor),
268 disk->minors, NULL, exact_match, exact_lock, disk);
269 register_disk(disk);
270 blk_register_queue(disk);
273 EXPORT_SYMBOL(add_disk);
274 EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */
276 void unlink_gendisk(struct gendisk *disk)
278 blk_unregister_queue(disk);
279 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
280 disk->minors);
283 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
286 * get_gendisk - get partitioning information for a given device
287 * @dev: device to get partitioning information for
289 * This function gets the structure containing partitioning
290 * information for the given device @dev.
292 struct gendisk *get_gendisk(dev_t dev, int *part)
294 struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
295 return kobj ? to_disk(kobj) : NULL;
298 #ifdef CONFIG_PROC_FS
299 /* iterator */
300 static void *part_start(struct seq_file *part, loff_t *pos)
302 struct list_head *p;
303 loff_t l = *pos;
305 mutex_lock(&block_subsys_lock);
306 list_for_each(p, &block_subsys.kset.list)
307 if (!l--)
308 return list_entry(p, struct gendisk, kobj.entry);
309 return NULL;
312 static void *part_next(struct seq_file *part, void *v, loff_t *pos)
314 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
315 ++*pos;
316 return p==&block_subsys.kset.list ? NULL :
317 list_entry(p, struct gendisk, kobj.entry);
320 static void part_stop(struct seq_file *part, void *v)
322 mutex_unlock(&block_subsys_lock);
325 static int show_partition(struct seq_file *part, void *v)
327 struct gendisk *sgp = v;
328 int n;
329 char buf[BDEVNAME_SIZE];
331 if (&sgp->kobj.entry == block_subsys.kset.list.next)
332 seq_puts(part, "major minor #blocks name\n\n");
334 /* Don't show non-partitionable removeable devices or empty devices */
335 if (!get_capacity(sgp) ||
336 (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE)))
337 return 0;
338 if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)
339 return 0;
341 /* show the full disk and all non-0 size partitions of it */
342 seq_printf(part, "%4d %4d %10llu %s\n",
343 sgp->major, sgp->first_minor,
344 (unsigned long long)get_capacity(sgp) >> 1,
345 disk_name(sgp, 0, buf));
346 for (n = 0; n < sgp->minors - 1; n++) {
347 if (!sgp->part[n])
348 continue;
349 if (sgp->part[n]->nr_sects == 0)
350 continue;
351 seq_printf(part, "%4d %4d %10llu %s\n",
352 sgp->major, n + 1 + sgp->first_minor,
353 (unsigned long long)sgp->part[n]->nr_sects >> 1 ,
354 disk_name(sgp, n + 1, buf));
357 return 0;
360 struct seq_operations partitions_op = {
361 .start =part_start,
362 .next = part_next,
363 .stop = part_stop,
364 .show = show_partition
366 #endif
369 extern int blk_dev_init(void);
371 static struct kobject *base_probe(dev_t dev, int *part, void *data)
373 if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
374 /* Make old-style 2.4 aliases work */
375 request_module("block-major-%d", MAJOR(dev));
376 return NULL;
379 static int __init genhd_device_init(void)
381 bdev_map = kobj_map_init(base_probe, &block_subsys_lock);
382 blk_dev_init();
383 subsystem_register(&block_subsys);
384 return 0;
387 subsys_initcall(genhd_device_init);
392 * kobject & sysfs bindings for block devices
394 static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr,
395 char *page)
397 struct gendisk *disk = to_disk(kobj);
398 struct disk_attribute *disk_attr =
399 container_of(attr,struct disk_attribute,attr);
400 ssize_t ret = -EIO;
402 if (disk_attr->show)
403 ret = disk_attr->show(disk,page);
404 return ret;
407 static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr,
408 const char *page, size_t count)
410 struct gendisk *disk = to_disk(kobj);
411 struct disk_attribute *disk_attr =
412 container_of(attr,struct disk_attribute,attr);
413 ssize_t ret = 0;
415 if (disk_attr->store)
416 ret = disk_attr->store(disk, page, count);
417 return ret;
420 static struct sysfs_ops disk_sysfs_ops = {
421 .show = &disk_attr_show,
422 .store = &disk_attr_store,
425 static ssize_t disk_uevent_store(struct gendisk * disk,
426 const char *buf, size_t count)
428 kobject_uevent(&disk->kobj, KOBJ_ADD);
429 return count;
431 static ssize_t disk_dev_read(struct gendisk * disk, char *page)
433 dev_t base = MKDEV(disk->major, disk->first_minor);
434 return print_dev_t(page, base);
436 static ssize_t disk_range_read(struct gendisk * disk, char *page)
438 return sprintf(page, "%d\n", disk->minors);
440 static ssize_t disk_removable_read(struct gendisk * disk, char *page)
442 return sprintf(page, "%d\n",
443 (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0));
446 static ssize_t disk_size_read(struct gendisk * disk, char *page)
448 return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk));
451 static ssize_t disk_stats_read(struct gendisk * disk, char *page)
453 preempt_disable();
454 disk_round_stats(disk);
455 preempt_enable();
456 return sprintf(page,
457 "%8u %8u %8llu %8u "
458 "%8u %8u %8llu %8u "
459 "%8u %8u %8u"
460 "\n",
461 disk_stat_read(disk, ios[READ]),
462 disk_stat_read(disk, merges[READ]),
463 (unsigned long long)disk_stat_read(disk, sectors[READ]),
464 jiffies_to_msecs(disk_stat_read(disk, ticks[READ])),
465 disk_stat_read(disk, ios[WRITE]),
466 disk_stat_read(disk, merges[WRITE]),
467 (unsigned long long)disk_stat_read(disk, sectors[WRITE]),
468 jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])),
469 disk->in_flight,
470 jiffies_to_msecs(disk_stat_read(disk, io_ticks)),
471 jiffies_to_msecs(disk_stat_read(disk, time_in_queue)));
473 static struct disk_attribute disk_attr_uevent = {
474 .attr = {.name = "uevent", .mode = S_IWUSR },
475 .store = disk_uevent_store
477 static struct disk_attribute disk_attr_dev = {
478 .attr = {.name = "dev", .mode = S_IRUGO },
479 .show = disk_dev_read
481 static struct disk_attribute disk_attr_range = {
482 .attr = {.name = "range", .mode = S_IRUGO },
483 .show = disk_range_read
485 static struct disk_attribute disk_attr_removable = {
486 .attr = {.name = "removable", .mode = S_IRUGO },
487 .show = disk_removable_read
489 static struct disk_attribute disk_attr_size = {
490 .attr = {.name = "size", .mode = S_IRUGO },
491 .show = disk_size_read
493 static struct disk_attribute disk_attr_stat = {
494 .attr = {.name = "stat", .mode = S_IRUGO },
495 .show = disk_stats_read
498 static struct attribute * default_attrs[] = {
499 &disk_attr_uevent.attr,
500 &disk_attr_dev.attr,
501 &disk_attr_range.attr,
502 &disk_attr_removable.attr,
503 &disk_attr_size.attr,
504 &disk_attr_stat.attr,
505 NULL,
508 static void disk_release(struct kobject * kobj)
510 struct gendisk *disk = to_disk(kobj);
511 kfree(disk->random);
512 kfree(disk->part);
513 free_disk_stats(disk);
514 kfree(disk);
517 static struct kobj_type ktype_block = {
518 .release = disk_release,
519 .sysfs_ops = &disk_sysfs_ops,
520 .default_attrs = default_attrs,
523 extern struct kobj_type ktype_part;
525 static int block_uevent_filter(struct kset *kset, struct kobject *kobj)
527 struct kobj_type *ktype = get_ktype(kobj);
529 return ((ktype == &ktype_block) || (ktype == &ktype_part));
532 static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp,
533 int num_envp, char *buffer, int buffer_size)
535 struct kobj_type *ktype = get_ktype(kobj);
536 struct device *physdev;
537 struct gendisk *disk;
538 struct hd_struct *part;
539 int length = 0;
540 int i = 0;
542 if (ktype == &ktype_block) {
543 disk = container_of(kobj, struct gendisk, kobj);
544 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
545 &length, "MINOR=%u", disk->first_minor);
546 } else if (ktype == &ktype_part) {
547 disk = container_of(kobj->parent, struct gendisk, kobj);
548 part = container_of(kobj, struct hd_struct, kobj);
549 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
550 &length, "MINOR=%u",
551 disk->first_minor + part->partno);
552 } else
553 return 0;
555 add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
556 "MAJOR=%u", disk->major);
558 /* add physical device, backing this device */
559 physdev = disk->driverfs_dev;
560 if (physdev) {
561 char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL);
563 add_uevent_var(envp, num_envp, &i, buffer, buffer_size,
564 &length, "PHYSDEVPATH=%s", path);
565 kfree(path);
567 if (physdev->bus)
568 add_uevent_var(envp, num_envp, &i,
569 buffer, buffer_size, &length,
570 "PHYSDEVBUS=%s",
571 physdev->bus->name);
573 if (physdev->driver)
574 add_uevent_var(envp, num_envp, &i,
575 buffer, buffer_size, &length,
576 "PHYSDEVDRIVER=%s",
577 physdev->driver->name);
580 /* terminate, set to next free slot, shrink available space */
581 envp[i] = NULL;
582 envp = &envp[i];
583 num_envp -= i;
584 buffer = &buffer[length];
585 buffer_size -= length;
587 return 0;
590 static struct kset_uevent_ops block_uevent_ops = {
591 .filter = block_uevent_filter,
592 .uevent = block_uevent,
595 /* declare block_subsys. */
596 static decl_subsys(block, &ktype_block, &block_uevent_ops);
600 * aggregate disk stat collector. Uses the same stats that the sysfs
601 * entries do, above, but makes them available through one seq_file.
602 * Watching a few disks may be efficient through sysfs, but watching
603 * all of them will be more efficient through this interface.
605 * The output looks suspiciously like /proc/partitions with a bunch of
606 * extra fields.
609 /* iterator */
610 static void *diskstats_start(struct seq_file *part, loff_t *pos)
612 loff_t k = *pos;
613 struct list_head *p;
615 mutex_lock(&block_subsys_lock);
616 list_for_each(p, &block_subsys.kset.list)
617 if (!k--)
618 return list_entry(p, struct gendisk, kobj.entry);
619 return NULL;
622 static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos)
624 struct list_head *p = ((struct gendisk *)v)->kobj.entry.next;
625 ++*pos;
626 return p==&block_subsys.kset.list ? NULL :
627 list_entry(p, struct gendisk, kobj.entry);
630 static void diskstats_stop(struct seq_file *part, void *v)
632 mutex_unlock(&block_subsys_lock);
635 static int diskstats_show(struct seq_file *s, void *v)
637 struct gendisk *gp = v;
638 char buf[BDEVNAME_SIZE];
639 int n = 0;
642 if (&sgp->kobj.entry == block_subsys.kset.list.next)
643 seq_puts(s, "major minor name"
644 " rio rmerge rsect ruse wio wmerge "
645 "wsect wuse running use aveq"
646 "\n\n");
649 preempt_disable();
650 disk_round_stats(gp);
651 preempt_enable();
652 seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
653 gp->major, n + gp->first_minor, disk_name(gp, n, buf),
654 disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]),
655 (unsigned long long)disk_stat_read(gp, sectors[0]),
656 jiffies_to_msecs(disk_stat_read(gp, ticks[0])),
657 disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]),
658 (unsigned long long)disk_stat_read(gp, sectors[1]),
659 jiffies_to_msecs(disk_stat_read(gp, ticks[1])),
660 gp->in_flight,
661 jiffies_to_msecs(disk_stat_read(gp, io_ticks)),
662 jiffies_to_msecs(disk_stat_read(gp, time_in_queue)));
664 /* now show all non-0 size partitions of it */
665 for (n = 0; n < gp->minors - 1; n++) {
666 struct hd_struct *hd = gp->part[n];
668 if (hd && hd->nr_sects)
669 seq_printf(s, "%4d %4d %s %u %u %u %u\n",
670 gp->major, n + gp->first_minor + 1,
671 disk_name(gp, n + 1, buf),
672 hd->ios[0], hd->sectors[0],
673 hd->ios[1], hd->sectors[1]);
676 return 0;
679 struct seq_operations diskstats_op = {
680 .start = diskstats_start,
681 .next = diskstats_next,
682 .stop = diskstats_stop,
683 .show = diskstats_show
686 struct gendisk *alloc_disk(int minors)
688 return alloc_disk_node(minors, -1);
691 struct gendisk *alloc_disk_node(int minors, int node_id)
693 struct gendisk *disk;
695 disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id);
696 if (disk) {
697 memset(disk, 0, sizeof(struct gendisk));
698 if (!init_disk_stats(disk)) {
699 kfree(disk);
700 return NULL;
702 if (minors > 1) {
703 int size = (minors - 1) * sizeof(struct hd_struct *);
704 disk->part = kmalloc_node(size, GFP_KERNEL, node_id);
705 if (!disk->part) {
706 kfree(disk);
707 return NULL;
709 memset(disk->part, 0, size);
711 disk->minors = minors;
712 kobj_set_kset_s(disk,block_subsys);
713 kobject_init(&disk->kobj);
714 rand_initialize_disk(disk);
716 return disk;
719 EXPORT_SYMBOL(alloc_disk);
720 EXPORT_SYMBOL(alloc_disk_node);
722 struct kobject *get_disk(struct gendisk *disk)
724 struct module *owner;
725 struct kobject *kobj;
727 if (!disk->fops)
728 return NULL;
729 owner = disk->fops->owner;
730 if (owner && !try_module_get(owner))
731 return NULL;
732 kobj = kobject_get(&disk->kobj);
733 if (kobj == NULL) {
734 module_put(owner);
735 return NULL;
737 return kobj;
741 EXPORT_SYMBOL(get_disk);
743 void put_disk(struct gendisk *disk)
745 if (disk)
746 kobject_put(&disk->kobj);
749 EXPORT_SYMBOL(put_disk);
751 void set_device_ro(struct block_device *bdev, int flag)
753 if (bdev->bd_contains != bdev)
754 bdev->bd_part->policy = flag;
755 else
756 bdev->bd_disk->policy = flag;
759 EXPORT_SYMBOL(set_device_ro);
761 void set_disk_ro(struct gendisk *disk, int flag)
763 int i;
764 disk->policy = flag;
765 for (i = 0; i < disk->minors - 1; i++)
766 if (disk->part[i]) disk->part[i]->policy = flag;
769 EXPORT_SYMBOL(set_disk_ro);
771 int bdev_read_only(struct block_device *bdev)
773 if (!bdev)
774 return 0;
775 else if (bdev->bd_contains != bdev)
776 return bdev->bd_part->policy;
777 else
778 return bdev->bd_disk->policy;
781 EXPORT_SYMBOL(bdev_read_only);
783 int invalidate_partition(struct gendisk *disk, int index)
785 int res = 0;
786 struct block_device *bdev = bdget_disk(disk, index);
787 if (bdev) {
788 fsync_bdev(bdev);
789 res = __invalidate_device(bdev);
790 bdput(bdev);
792 return res;
795 EXPORT_SYMBOL(invalidate_partition);