5 #include <linux/module.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_kset
;
21 static struct kset_uevent_ops block_uevent_ops
;
22 static DEFINE_MUTEX(block_subsys_lock
);
25 * Can be deleted altogether. Later.
28 static struct blk_major_name
{
29 struct blk_major_name
*next
;
32 } *major_names
[BLKDEV_MAJOR_HASH_SIZE
];
34 /* index in the above - for now: assume no multimajor ranges */
35 static inline int major_to_index(int major
)
37 return major
% BLKDEV_MAJOR_HASH_SIZE
;
42 void blkdev_show(struct seq_file
*f
, off_t offset
)
44 struct blk_major_name
*dp
;
46 if (offset
< BLKDEV_MAJOR_HASH_SIZE
) {
47 mutex_lock(&block_subsys_lock
);
48 for (dp
= major_names
[offset
]; dp
; dp
= dp
->next
)
49 seq_printf(f
, "%3d %s\n", dp
->major
, dp
->name
);
50 mutex_unlock(&block_subsys_lock
);
54 #endif /* CONFIG_PROC_FS */
56 int register_blkdev(unsigned int major
, const char *name
)
58 struct blk_major_name
**n
, *p
;
61 mutex_lock(&block_subsys_lock
);
65 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
66 if (major_names
[index
] == NULL
)
71 printk("register_blkdev: failed to get major for %s\n",
80 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
87 strlcpy(p
->name
, name
, sizeof(p
->name
));
89 index
= major_to_index(major
);
91 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
92 if ((*n
)->major
== major
)
101 printk("register_blkdev: cannot get major %d for %s\n",
106 mutex_unlock(&block_subsys_lock
);
110 EXPORT_SYMBOL(register_blkdev
);
112 void unregister_blkdev(unsigned int major
, const char *name
)
114 struct blk_major_name
**n
;
115 struct blk_major_name
*p
= NULL
;
116 int index
= major_to_index(major
);
118 mutex_lock(&block_subsys_lock
);
119 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
120 if ((*n
)->major
== major
)
122 if (!*n
|| strcmp((*n
)->name
, name
)) {
128 mutex_unlock(&block_subsys_lock
);
132 EXPORT_SYMBOL(unregister_blkdev
);
134 static struct kobj_map
*bdev_map
;
137 * Register device numbers dev..(dev+range-1)
138 * range must be nonzero
139 * The hash chain is sorted on range, so that subranges can override.
141 void blk_register_region(dev_t dev
, unsigned long range
, struct module
*module
,
142 struct kobject
*(*probe
)(dev_t
, int *, void *),
143 int (*lock
)(dev_t
, void *), void *data
)
145 kobj_map(bdev_map
, dev
, range
, module
, probe
, lock
, data
);
148 EXPORT_SYMBOL(blk_register_region
);
150 void blk_unregister_region(dev_t dev
, unsigned long range
)
152 kobj_unmap(bdev_map
, dev
, range
);
155 EXPORT_SYMBOL(blk_unregister_region
);
157 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
159 struct gendisk
*p
= data
;
163 static int exact_lock(dev_t dev
, void *data
)
165 struct gendisk
*p
= data
;
173 * add_disk - add partitioning information to kernel list
174 * @disk: per-device partitioning information
176 * This function registers the partitioning information in @disk
179 void add_disk(struct gendisk
*disk
)
181 disk
->flags
|= GENHD_FL_UP
;
182 blk_register_region(MKDEV(disk
->major
, disk
->first_minor
),
183 disk
->minors
, NULL
, exact_match
, exact_lock
, disk
);
185 blk_register_queue(disk
);
188 EXPORT_SYMBOL(add_disk
);
189 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
191 void unlink_gendisk(struct gendisk
*disk
)
193 blk_unregister_queue(disk
);
194 blk_unregister_region(MKDEV(disk
->major
, disk
->first_minor
),
198 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
201 * get_gendisk - get partitioning information for a given device
202 * @dev: device to get partitioning information for
204 * This function gets the structure containing partitioning
205 * information for the given device @dev.
207 struct gendisk
*get_gendisk(dev_t dev
, int *part
)
209 struct kobject
*kobj
= kobj_lookup(bdev_map
, dev
, part
);
210 return kobj
? to_disk(kobj
) : NULL
;
214 * print a full list of all partitions - intended for places where the root
215 * filesystem can't be mounted and thus to give the victim some idea of what
218 void __init
printk_all_partitions(void)
223 mutex_lock(&block_subsys_lock
);
224 /* For each block device... */
225 list_for_each_entry(sgp
, &block_kset
->list
, kobj
.entry
) {
226 char buf
[BDEVNAME_SIZE
];
228 * Don't show empty devices or things that have been surpressed
230 if (get_capacity(sgp
) == 0 ||
231 (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
))
235 * Note, unlike /proc/partitions, I am showing the numbers in
236 * hex - the same format as the root= option takes.
238 printk("%02x%02x %10llu %s",
239 sgp
->major
, sgp
->first_minor
,
240 (unsigned long long)get_capacity(sgp
) >> 1,
241 disk_name(sgp
, 0, buf
));
242 if (sgp
->driverfs_dev
!= NULL
&&
243 sgp
->driverfs_dev
->driver
!= NULL
)
244 printk(" driver: %s\n",
245 sgp
->driverfs_dev
->driver
->name
);
247 printk(" (driver?)\n");
249 /* now show the partitions */
250 for (n
= 0; n
< sgp
->minors
- 1; ++n
) {
251 if (sgp
->part
[n
] == NULL
)
253 if (sgp
->part
[n
]->nr_sects
== 0)
255 printk(" %02x%02x %10llu %s\n",
256 sgp
->major
, n
+ 1 + sgp
->first_minor
,
257 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1,
258 disk_name(sgp
, n
+ 1, buf
));
259 } /* partition subloop */
260 } /* Block device loop */
262 mutex_unlock(&block_subsys_lock
);
266 #ifdef CONFIG_PROC_FS
268 static void *part_start(struct seq_file
*part
, loff_t
*pos
)
273 mutex_lock(&block_subsys_lock
);
274 list_for_each(p
, &block_kset
->list
)
276 return list_entry(p
, struct gendisk
, kobj
.entry
);
280 static void *part_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
282 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
284 return p
==&block_kset
->list
? NULL
:
285 list_entry(p
, struct gendisk
, kobj
.entry
);
288 static void part_stop(struct seq_file
*part
, void *v
)
290 mutex_unlock(&block_subsys_lock
);
293 static int show_partition(struct seq_file
*part
, void *v
)
295 struct gendisk
*sgp
= v
;
297 char buf
[BDEVNAME_SIZE
];
299 if (&sgp
->kobj
.entry
== block_kset
->list
.next
)
300 seq_puts(part
, "major minor #blocks name\n\n");
302 /* Don't show non-partitionable removeable devices or empty devices */
303 if (!get_capacity(sgp
) ||
304 (sgp
->minors
== 1 && (sgp
->flags
& GENHD_FL_REMOVABLE
)))
306 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
309 /* show the full disk and all non-0 size partitions of it */
310 seq_printf(part
, "%4d %4d %10llu %s\n",
311 sgp
->major
, sgp
->first_minor
,
312 (unsigned long long)get_capacity(sgp
) >> 1,
313 disk_name(sgp
, 0, buf
));
314 for (n
= 0; n
< sgp
->minors
- 1; n
++) {
317 if (sgp
->part
[n
]->nr_sects
== 0)
319 seq_printf(part
, "%4d %4d %10llu %s\n",
320 sgp
->major
, n
+ 1 + sgp
->first_minor
,
321 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1 ,
322 disk_name(sgp
, n
+ 1, buf
));
328 struct seq_operations partitions_op
= {
332 .show
= show_partition
337 extern int blk_dev_init(void);
339 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
341 if (request_module("block-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
342 /* Make old-style 2.4 aliases work */
343 request_module("block-major-%d", MAJOR(dev
));
347 static int __init
genhd_device_init(void)
349 bdev_map
= kobj_map_init(base_probe
, &block_subsys_lock
);
351 block_kset
= kset_create_and_register("block", &block_uevent_ops
, NULL
, NULL
);
353 printk(KERN_WARNING
"%s: kset_create error\n", __FUNCTION__
);
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
,
369 struct gendisk
*disk
= to_disk(kobj
);
370 struct disk_attribute
*disk_attr
=
371 container_of(attr
,struct disk_attribute
,attr
);
375 ret
= disk_attr
->show(disk
,page
);
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
);
387 if (disk_attr
->store
)
388 ret
= disk_attr
->store(disk
, page
, count
);
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
);
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
)
429 disk_round_stats(disk
);
432 "%8lu %8lu %8llu %8u "
433 "%8lu %8lu %8llu %8u "
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
])),
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
)
484 if (count
> 0 && sscanf(buf
, "%d", &i
) > 0) {
486 disk
->flags
&= ~GENHD_FL_FAIL
;
488 disk
->flags
|= GENHD_FL_FAIL
;
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
505 static struct attribute
* default_attrs
[] = {
506 &disk_attr_uevent
.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
,
519 static void disk_release(struct kobject
* kobj
)
521 struct gendisk
*disk
= to_disk(kobj
);
524 free_disk_stats(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
);
562 add_uevent_var(env
, "MAJOR=%u", disk
->major
);
564 /* add physical device, backing this device */
565 physdev
= disk
->driverfs_dev
;
567 char *path
= kobject_get_path(&physdev
->kobj
, GFP_KERNEL
);
569 add_uevent_var(env
, "PHYSDEVPATH=%s", path
);
573 add_uevent_var(env
, "PHYSDEVBUS=%s", physdev
->bus
->name
);
576 add_uevent_var(env
, physdev
->driver
->name
);
582 static struct kset_uevent_ops block_uevent_ops
= {
583 .filter
= block_uevent_filter
,
584 .uevent
= block_uevent
,
588 * aggregate disk stat collector. Uses the same stats that the sysfs
589 * entries do, above, but makes them available through one seq_file.
590 * Watching a few disks may be efficient through sysfs, but watching
591 * all of them will be more efficient through this interface.
593 * The output looks suspiciously like /proc/partitions with a bunch of
598 static void *diskstats_start(struct seq_file
*part
, loff_t
*pos
)
603 mutex_lock(&block_subsys_lock
);
604 list_for_each(p
, &block_kset
->list
)
606 return list_entry(p
, struct gendisk
, kobj
.entry
);
610 static void *diskstats_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
612 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
614 return p
==&block_kset
->list
? NULL
:
615 list_entry(p
, struct gendisk
, kobj
.entry
);
618 static void diskstats_stop(struct seq_file
*part
, void *v
)
620 mutex_unlock(&block_subsys_lock
);
623 static int diskstats_show(struct seq_file
*s
, void *v
)
625 struct gendisk
*gp
= v
;
626 char buf
[BDEVNAME_SIZE
];
630 if (&sgp->kobj.entry == block_kset->list.next)
631 seq_puts(s, "major minor name"
632 " rio rmerge rsect ruse wio wmerge "
633 "wsect wuse running use aveq"
638 disk_round_stats(gp
);
640 seq_printf(s
, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
641 gp
->major
, n
+ gp
->first_minor
, disk_name(gp
, n
, buf
),
642 disk_stat_read(gp
, ios
[0]), disk_stat_read(gp
, merges
[0]),
643 (unsigned long long)disk_stat_read(gp
, sectors
[0]),
644 jiffies_to_msecs(disk_stat_read(gp
, ticks
[0])),
645 disk_stat_read(gp
, ios
[1]), disk_stat_read(gp
, merges
[1]),
646 (unsigned long long)disk_stat_read(gp
, sectors
[1]),
647 jiffies_to_msecs(disk_stat_read(gp
, ticks
[1])),
649 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
650 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
652 /* now show all non-0 size partitions of it */
653 for (n
= 0; n
< gp
->minors
- 1; n
++) {
654 struct hd_struct
*hd
= gp
->part
[n
];
656 if (hd
&& hd
->nr_sects
)
657 seq_printf(s
, "%4d %4d %s %u %u %u %u\n",
658 gp
->major
, n
+ gp
->first_minor
+ 1,
659 disk_name(gp
, n
+ 1, buf
),
660 hd
->ios
[0], hd
->sectors
[0],
661 hd
->ios
[1], hd
->sectors
[1]);
667 struct seq_operations diskstats_op
= {
668 .start
= diskstats_start
,
669 .next
= diskstats_next
,
670 .stop
= diskstats_stop
,
671 .show
= diskstats_show
674 static void media_change_notify_thread(struct work_struct
*work
)
676 struct gendisk
*gd
= container_of(work
, struct gendisk
, async_notify
);
677 char event
[] = "MEDIA_CHANGE=1";
678 char *envp
[] = { event
, NULL
};
681 * set enviroment vars to indicate which event this is for
682 * so that user space will know to go check the media status.
684 kobject_uevent_env(&gd
->kobj
, KOBJ_CHANGE
, envp
);
685 put_device(gd
->driverfs_dev
);
688 void genhd_media_change_notify(struct gendisk
*disk
)
690 get_device(disk
->driverfs_dev
);
691 schedule_work(&disk
->async_notify
);
693 EXPORT_SYMBOL_GPL(genhd_media_change_notify
);
695 struct gendisk
*alloc_disk(int minors
)
697 return alloc_disk_node(minors
, -1);
700 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
702 struct gendisk
*disk
;
704 disk
= kmalloc_node(sizeof(struct gendisk
),
705 GFP_KERNEL
| __GFP_ZERO
, node_id
);
707 if (!init_disk_stats(disk
)) {
712 int size
= (minors
- 1) * sizeof(struct hd_struct
*);
713 disk
->part
= kmalloc_node(size
,
714 GFP_KERNEL
| __GFP_ZERO
, node_id
);
720 disk
->minors
= minors
;
721 kobject_init(&disk
->kobj
);
722 disk
->kobj
.kset
= block_kset
;
723 disk
->kobj
.ktype
= &ktype_block
;
724 rand_initialize_disk(disk
);
725 INIT_WORK(&disk
->async_notify
,
726 media_change_notify_thread
);
731 EXPORT_SYMBOL(alloc_disk
);
732 EXPORT_SYMBOL(alloc_disk_node
);
734 struct kobject
*get_disk(struct gendisk
*disk
)
736 struct module
*owner
;
737 struct kobject
*kobj
;
741 owner
= disk
->fops
->owner
;
742 if (owner
&& !try_module_get(owner
))
744 kobj
= kobject_get(&disk
->kobj
);
753 EXPORT_SYMBOL(get_disk
);
755 void put_disk(struct gendisk
*disk
)
758 kobject_put(&disk
->kobj
);
761 EXPORT_SYMBOL(put_disk
);
763 void set_device_ro(struct block_device
*bdev
, int flag
)
765 if (bdev
->bd_contains
!= bdev
)
766 bdev
->bd_part
->policy
= flag
;
768 bdev
->bd_disk
->policy
= flag
;
771 EXPORT_SYMBOL(set_device_ro
);
773 void set_disk_ro(struct gendisk
*disk
, int flag
)
777 for (i
= 0; i
< disk
->minors
- 1; i
++)
778 if (disk
->part
[i
]) disk
->part
[i
]->policy
= flag
;
781 EXPORT_SYMBOL(set_disk_ro
);
783 int bdev_read_only(struct block_device
*bdev
)
787 else if (bdev
->bd_contains
!= bdev
)
788 return bdev
->bd_part
->policy
;
790 return bdev
->bd_disk
->policy
;
793 EXPORT_SYMBOL(bdev_read_only
);
795 int invalidate_partition(struct gendisk
*disk
, int index
)
798 struct block_device
*bdev
= bdget_disk(disk
, index
);
801 res
= __invalidate_device(bdev
);
807 EXPORT_SYMBOL(invalidate_partition
);