5 #include <linux/config.h>
6 #include <linux/module.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
;
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
;
43 struct blk_major_name
*bd
;
47 * iterate over a list of blkdev_info structures. allows
48 * the major_names array to be iterated over from outside this file
49 * must be called with the block_subsys_sem held
51 void *get_next_blkdev(void *dev
)
53 struct blkdev_info
*info
;
56 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
60 info
->bd
= major_names
[info
->index
];
67 while (info
->index
< ARRAY_SIZE(major_names
)) {
69 info
->bd
= info
->bd
->next
;
73 * No devices on this chain, move to the next
76 info
->bd
= (info
->index
< ARRAY_SIZE(major_names
)) ?
77 major_names
[info
->index
] : NULL
;
86 void *acquire_blkdev_list(void)
88 down(&block_subsys_sem
);
89 return get_next_blkdev(NULL
);
92 void release_blkdev_list(void *dev
)
94 up(&block_subsys_sem
);
100 * Count the number of records in the blkdev_list.
101 * must be called with the block_subsys_sem held
103 int count_blkdev_list(void)
105 struct blk_major_name
*n
;
110 for (i
= 0; i
< ARRAY_SIZE(major_names
); i
++) {
111 for (n
= major_names
[i
]; n
; n
= n
->next
)
119 * extract the major and name values from a blkdev_info struct
120 * passed in as a void to *dev. Must be called with
121 * block_subsys_sem held
123 int get_blkdev_info(void *dev
, int *major
, char **name
)
125 struct blkdev_info
*info
= dev
;
127 if (info
->bd
== NULL
)
130 *major
= info
->bd
->major
;
131 *name
= info
->bd
->name
;
136 int register_blkdev(unsigned int major
, const char *name
)
138 struct blk_major_name
**n
, *p
;
141 down(&block_subsys_sem
);
145 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
146 if (major_names
[index
] == NULL
)
151 printk("register_blkdev: failed to get major for %s\n",
160 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
167 strlcpy(p
->name
, name
, sizeof(p
->name
));
169 index
= major_to_index(major
);
171 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
172 if ((*n
)->major
== major
)
181 printk("register_blkdev: cannot get major %d for %s\n",
186 up(&block_subsys_sem
);
190 EXPORT_SYMBOL(register_blkdev
);
192 /* todo: make void - error printk here */
193 int unregister_blkdev(unsigned int major
, const char *name
)
195 struct blk_major_name
**n
;
196 struct blk_major_name
*p
= NULL
;
197 int index
= major_to_index(major
);
200 down(&block_subsys_sem
);
201 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
202 if ((*n
)->major
== major
)
204 if (!*n
|| strcmp((*n
)->name
, name
))
210 up(&block_subsys_sem
);
216 EXPORT_SYMBOL(unregister_blkdev
);
218 static struct kobj_map
*bdev_map
;
221 * Register device numbers dev..(dev+range-1)
222 * range must be nonzero
223 * The hash chain is sorted on range, so that subranges can override.
225 void blk_register_region(dev_t dev
, unsigned long range
, struct module
*module
,
226 struct kobject
*(*probe
)(dev_t
, int *, void *),
227 int (*lock
)(dev_t
, void *), void *data
)
229 kobj_map(bdev_map
, dev
, range
, module
, probe
, lock
, data
);
232 EXPORT_SYMBOL(blk_register_region
);
234 void blk_unregister_region(dev_t dev
, unsigned long range
)
236 kobj_unmap(bdev_map
, dev
, range
);
239 EXPORT_SYMBOL(blk_unregister_region
);
241 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
243 struct gendisk
*p
= data
;
247 static int exact_lock(dev_t dev
, void *data
)
249 struct gendisk
*p
= data
;
257 * add_disk - add partitioning information to kernel list
258 * @disk: per-device partitioning information
260 * This function registers the partitioning information in @disk
263 void add_disk(struct gendisk
*disk
)
265 disk
->flags
|= GENHD_FL_UP
;
266 blk_register_region(MKDEV(disk
->major
, disk
->first_minor
),
267 disk
->minors
, NULL
, exact_match
, exact_lock
, disk
);
269 blk_register_queue(disk
);
272 EXPORT_SYMBOL(add_disk
);
273 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
275 void unlink_gendisk(struct gendisk
*disk
)
277 blk_unregister_queue(disk
);
278 blk_unregister_region(MKDEV(disk
->major
, disk
->first_minor
),
282 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
285 * get_gendisk - get partitioning information for a given device
286 * @dev: device to get partitioning information for
288 * This function gets the structure containing partitioning
289 * information for the given device @dev.
291 struct gendisk
*get_gendisk(dev_t dev
, int *part
)
293 struct kobject
*kobj
= kobj_lookup(bdev_map
, dev
, part
);
294 return kobj
? to_disk(kobj
) : NULL
;
297 #ifdef CONFIG_PROC_FS
299 static void *part_start(struct seq_file
*part
, loff_t
*pos
)
304 down(&block_subsys_sem
);
305 list_for_each(p
, &block_subsys
.kset
.list
)
307 return list_entry(p
, struct gendisk
, kobj
.entry
);
311 static void *part_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
313 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
315 return p
==&block_subsys
.kset
.list
? NULL
:
316 list_entry(p
, struct gendisk
, kobj
.entry
);
319 static void part_stop(struct seq_file
*part
, void *v
)
321 up(&block_subsys_sem
);
324 static int show_partition(struct seq_file
*part
, void *v
)
326 struct gendisk
*sgp
= v
;
328 char buf
[BDEVNAME_SIZE
];
330 if (&sgp
->kobj
.entry
== block_subsys
.kset
.list
.next
)
331 seq_puts(part
, "major minor #blocks name\n\n");
333 /* Don't show non-partitionable removeable devices or empty devices */
334 if (!get_capacity(sgp
) ||
335 (sgp
->minors
== 1 && (sgp
->flags
& GENHD_FL_REMOVABLE
)))
337 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
340 /* show the full disk and all non-0 size partitions of it */
341 seq_printf(part
, "%4d %4d %10llu %s\n",
342 sgp
->major
, sgp
->first_minor
,
343 (unsigned long long)get_capacity(sgp
) >> 1,
344 disk_name(sgp
, 0, buf
));
345 for (n
= 0; n
< sgp
->minors
- 1; n
++) {
348 if (sgp
->part
[n
]->nr_sects
== 0)
350 seq_printf(part
, "%4d %4d %10llu %s\n",
351 sgp
->major
, n
+ 1 + sgp
->first_minor
,
352 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1 ,
353 disk_name(sgp
, n
+ 1, buf
));
359 struct seq_operations partitions_op
= {
363 .show
= show_partition
368 extern int blk_dev_init(void);
370 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
372 if (request_module("block-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
373 /* Make old-style 2.4 aliases work */
374 request_module("block-major-%d", MAJOR(dev
));
378 static int __init
genhd_device_init(void)
380 bdev_map
= kobj_map_init(base_probe
, &block_subsys_sem
);
382 subsystem_register(&block_subsys
);
386 subsys_initcall(genhd_device_init
);
391 * kobject & sysfs bindings for block devices
393 static ssize_t
disk_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
396 struct gendisk
*disk
= to_disk(kobj
);
397 struct disk_attribute
*disk_attr
=
398 container_of(attr
,struct disk_attribute
,attr
);
402 ret
= disk_attr
->show(disk
,page
);
406 static ssize_t
disk_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
407 const char *page
, size_t count
)
409 struct gendisk
*disk
= to_disk(kobj
);
410 struct disk_attribute
*disk_attr
=
411 container_of(attr
,struct disk_attribute
,attr
);
414 if (disk_attr
->store
)
415 ret
= disk_attr
->store(disk
, page
, count
);
419 static struct sysfs_ops disk_sysfs_ops
= {
420 .show
= &disk_attr_show
,
421 .store
= &disk_attr_store
,
424 static ssize_t
disk_uevent_store(struct gendisk
* disk
,
425 const char *buf
, size_t count
)
427 kobject_uevent(&disk
->kobj
, KOBJ_ADD
);
430 static ssize_t
disk_dev_read(struct gendisk
* disk
, char *page
)
432 dev_t base
= MKDEV(disk
->major
, disk
->first_minor
);
433 return print_dev_t(page
, base
);
435 static ssize_t
disk_range_read(struct gendisk
* disk
, char *page
)
437 return sprintf(page
, "%d\n", disk
->minors
);
439 static ssize_t
disk_removable_read(struct gendisk
* disk
, char *page
)
441 return sprintf(page
, "%d\n",
442 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
445 static ssize_t
disk_size_read(struct gendisk
* disk
, char *page
)
447 return sprintf(page
, "%llu\n", (unsigned long long)get_capacity(disk
));
450 static ssize_t
disk_stats_read(struct gendisk
* disk
, char *page
)
453 disk_round_stats(disk
);
460 disk_stat_read(disk
, ios
[READ
]),
461 disk_stat_read(disk
, merges
[READ
]),
462 (unsigned long long)disk_stat_read(disk
, sectors
[READ
]),
463 jiffies_to_msecs(disk_stat_read(disk
, ticks
[READ
])),
464 disk_stat_read(disk
, ios
[WRITE
]),
465 disk_stat_read(disk
, merges
[WRITE
]),
466 (unsigned long long)disk_stat_read(disk
, sectors
[WRITE
]),
467 jiffies_to_msecs(disk_stat_read(disk
, ticks
[WRITE
])),
469 jiffies_to_msecs(disk_stat_read(disk
, io_ticks
)),
470 jiffies_to_msecs(disk_stat_read(disk
, time_in_queue
)));
472 static struct disk_attribute disk_attr_uevent
= {
473 .attr
= {.name
= "uevent", .mode
= S_IWUSR
},
474 .store
= disk_uevent_store
476 static struct disk_attribute disk_attr_dev
= {
477 .attr
= {.name
= "dev", .mode
= S_IRUGO
},
478 .show
= disk_dev_read
480 static struct disk_attribute disk_attr_range
= {
481 .attr
= {.name
= "range", .mode
= S_IRUGO
},
482 .show
= disk_range_read
484 static struct disk_attribute disk_attr_removable
= {
485 .attr
= {.name
= "removable", .mode
= S_IRUGO
},
486 .show
= disk_removable_read
488 static struct disk_attribute disk_attr_size
= {
489 .attr
= {.name
= "size", .mode
= S_IRUGO
},
490 .show
= disk_size_read
492 static struct disk_attribute disk_attr_stat
= {
493 .attr
= {.name
= "stat", .mode
= S_IRUGO
},
494 .show
= disk_stats_read
497 static struct attribute
* default_attrs
[] = {
498 &disk_attr_uevent
.attr
,
500 &disk_attr_range
.attr
,
501 &disk_attr_removable
.attr
,
502 &disk_attr_size
.attr
,
503 &disk_attr_stat
.attr
,
507 static void disk_release(struct kobject
* kobj
)
509 struct gendisk
*disk
= to_disk(kobj
);
512 free_disk_stats(disk
);
516 static struct kobj_type ktype_block
= {
517 .release
= disk_release
,
518 .sysfs_ops
= &disk_sysfs_ops
,
519 .default_attrs
= default_attrs
,
522 extern struct kobj_type ktype_part
;
524 static int block_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
526 struct kobj_type
*ktype
= get_ktype(kobj
);
528 return ((ktype
== &ktype_block
) || (ktype
== &ktype_part
));
531 static int block_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
532 int num_envp
, char *buffer
, int buffer_size
)
534 struct kobj_type
*ktype
= get_ktype(kobj
);
535 struct device
*physdev
;
536 struct gendisk
*disk
;
537 struct hd_struct
*part
;
541 if (ktype
== &ktype_block
) {
542 disk
= container_of(kobj
, struct gendisk
, kobj
);
543 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
544 &length
, "MINOR=%u", disk
->first_minor
);
545 } else if (ktype
== &ktype_part
) {
546 disk
= container_of(kobj
->parent
, struct gendisk
, kobj
);
547 part
= container_of(kobj
, struct hd_struct
, kobj
);
548 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
550 disk
->first_minor
+ part
->partno
);
554 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &length
,
555 "MAJOR=%u", disk
->major
);
557 /* add physical device, backing this device */
558 physdev
= disk
->driverfs_dev
;
560 char *path
= kobject_get_path(&physdev
->kobj
, GFP_KERNEL
);
562 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
563 &length
, "PHYSDEVPATH=%s", path
);
567 add_uevent_var(envp
, num_envp
, &i
,
568 buffer
, buffer_size
, &length
,
573 add_uevent_var(envp
, num_envp
, &i
,
574 buffer
, buffer_size
, &length
,
576 physdev
->driver
->name
);
579 /* terminate, set to next free slot, shrink available space */
583 buffer
= &buffer
[length
];
584 buffer_size
-= length
;
589 static struct kset_uevent_ops block_uevent_ops
= {
590 .filter
= block_uevent_filter
,
591 .uevent
= block_uevent
,
594 /* declare block_subsys. */
595 static decl_subsys(block
, &ktype_block
, &block_uevent_ops
);
599 * aggregate disk stat collector. Uses the same stats that the sysfs
600 * entries do, above, but makes them available through one seq_file.
601 * Watching a few disks may be efficient through sysfs, but watching
602 * all of them will be more efficient through this interface.
604 * The output looks suspiciously like /proc/partitions with a bunch of
609 static void *diskstats_start(struct seq_file
*part
, loff_t
*pos
)
614 down(&block_subsys_sem
);
615 list_for_each(p
, &block_subsys
.kset
.list
)
617 return list_entry(p
, struct gendisk
, kobj
.entry
);
621 static void *diskstats_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
623 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
625 return p
==&block_subsys
.kset
.list
? NULL
:
626 list_entry(p
, struct gendisk
, kobj
.entry
);
629 static void diskstats_stop(struct seq_file
*part
, void *v
)
631 up(&block_subsys_sem
);
634 static int diskstats_show(struct seq_file
*s
, void *v
)
636 struct gendisk
*gp
= v
;
637 char buf
[BDEVNAME_SIZE
];
641 if (&sgp->kobj.entry == block_subsys.kset.list.next)
642 seq_puts(s, "major minor name"
643 " rio rmerge rsect ruse wio wmerge "
644 "wsect wuse running use aveq"
649 disk_round_stats(gp
);
651 seq_printf(s
, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
652 gp
->major
, n
+ gp
->first_minor
, disk_name(gp
, n
, buf
),
653 disk_stat_read(gp
, ios
[0]), disk_stat_read(gp
, merges
[0]),
654 (unsigned long long)disk_stat_read(gp
, sectors
[0]),
655 jiffies_to_msecs(disk_stat_read(gp
, ticks
[0])),
656 disk_stat_read(gp
, ios
[1]), disk_stat_read(gp
, merges
[1]),
657 (unsigned long long)disk_stat_read(gp
, sectors
[1]),
658 jiffies_to_msecs(disk_stat_read(gp
, ticks
[1])),
660 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
661 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
663 /* now show all non-0 size partitions of it */
664 for (n
= 0; n
< gp
->minors
- 1; n
++) {
665 struct hd_struct
*hd
= gp
->part
[n
];
667 if (hd
&& hd
->nr_sects
)
668 seq_printf(s
, "%4d %4d %s %u %u %u %u\n",
669 gp
->major
, n
+ gp
->first_minor
+ 1,
670 disk_name(gp
, n
+ 1, buf
),
671 hd
->ios
[0], hd
->sectors
[0],
672 hd
->ios
[1], hd
->sectors
[1]);
678 struct seq_operations diskstats_op
= {
679 .start
= diskstats_start
,
680 .next
= diskstats_next
,
681 .stop
= diskstats_stop
,
682 .show
= diskstats_show
685 struct gendisk
*alloc_disk(int minors
)
687 return alloc_disk_node(minors
, -1);
690 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
692 struct gendisk
*disk
;
694 disk
= kmalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
696 memset(disk
, 0, sizeof(struct gendisk
));
697 if (!init_disk_stats(disk
)) {
702 int size
= (minors
- 1) * sizeof(struct hd_struct
*);
703 disk
->part
= kmalloc_node(size
, GFP_KERNEL
, node_id
);
708 memset(disk
->part
, 0, size
);
710 disk
->minors
= minors
;
711 kobj_set_kset_s(disk
,block_subsys
);
712 kobject_init(&disk
->kobj
);
713 rand_initialize_disk(disk
);
718 EXPORT_SYMBOL(alloc_disk
);
719 EXPORT_SYMBOL(alloc_disk_node
);
721 struct kobject
*get_disk(struct gendisk
*disk
)
723 struct module
*owner
;
724 struct kobject
*kobj
;
728 owner
= disk
->fops
->owner
;
729 if (owner
&& !try_module_get(owner
))
731 kobj
= kobject_get(&disk
->kobj
);
740 EXPORT_SYMBOL(get_disk
);
742 void put_disk(struct gendisk
*disk
)
745 kobject_put(&disk
->kobj
);
748 EXPORT_SYMBOL(put_disk
);
750 void set_device_ro(struct block_device
*bdev
, int flag
)
752 if (bdev
->bd_contains
!= bdev
)
753 bdev
->bd_part
->policy
= flag
;
755 bdev
->bd_disk
->policy
= flag
;
758 EXPORT_SYMBOL(set_device_ro
);
760 void set_disk_ro(struct gendisk
*disk
, int flag
)
764 for (i
= 0; i
< disk
->minors
- 1; i
++)
765 if (disk
->part
[i
]) disk
->part
[i
]->policy
= flag
;
768 EXPORT_SYMBOL(set_disk_ro
);
770 int bdev_read_only(struct block_device
*bdev
)
774 else if (bdev
->bd_contains
!= bdev
)
775 return bdev
->bd_part
->policy
;
777 return bdev
->bd_disk
->policy
;
780 EXPORT_SYMBOL(bdev_read_only
);
782 int invalidate_partition(struct gendisk
*disk
, int index
)
785 struct block_device
*bdev
= bdget_disk(disk
, index
);
788 res
= __invalidate_device(bdev
);
794 EXPORT_SYMBOL(invalidate_partition
);