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>
18 #include <linux/mutex.h>
20 struct subsystem 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
;
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
;
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
;
60 mutex_lock(&block_subsys_lock
);
64 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
65 if (major_names
[index
] == NULL
)
70 printk("register_blkdev: failed to get major for %s\n",
79 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
86 strlcpy(p
->name
, name
, sizeof(p
->name
));
88 index
= major_to_index(major
);
90 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
91 if ((*n
)->major
== major
)
100 printk("register_blkdev: cannot get major %d for %s\n",
105 mutex_unlock(&block_subsys_lock
);
109 EXPORT_SYMBOL(register_blkdev
);
111 /* todo: make void - error printk here */
112 int 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
);
119 mutex_lock(&block_subsys_lock
);
120 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
121 if ((*n
)->major
== major
)
123 if (!*n
|| strcmp((*n
)->name
, name
))
129 mutex_unlock(&block_subsys_lock
);
135 EXPORT_SYMBOL(unregister_blkdev
);
137 static struct kobj_map
*bdev_map
;
140 * Register device numbers dev..(dev+range-1)
141 * range must be nonzero
142 * The hash chain is sorted on range, so that subranges can override.
144 void blk_register_region(dev_t dev
, unsigned long range
, struct module
*module
,
145 struct kobject
*(*probe
)(dev_t
, int *, void *),
146 int (*lock
)(dev_t
, void *), void *data
)
148 kobj_map(bdev_map
, dev
, range
, module
, probe
, lock
, data
);
151 EXPORT_SYMBOL(blk_register_region
);
153 void blk_unregister_region(dev_t dev
, unsigned long range
)
155 kobj_unmap(bdev_map
, dev
, range
);
158 EXPORT_SYMBOL(blk_unregister_region
);
160 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
162 struct gendisk
*p
= data
;
166 static int exact_lock(dev_t dev
, void *data
)
168 struct gendisk
*p
= data
;
176 * add_disk - add partitioning information to kernel list
177 * @disk: per-device partitioning information
179 * This function registers the partitioning information in @disk
182 void add_disk(struct gendisk
*disk
)
184 disk
->flags
|= GENHD_FL_UP
;
185 blk_register_region(MKDEV(disk
->major
, disk
->first_minor
),
186 disk
->minors
, NULL
, exact_match
, exact_lock
, disk
);
188 blk_register_queue(disk
);
191 EXPORT_SYMBOL(add_disk
);
192 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
194 void unlink_gendisk(struct gendisk
*disk
)
196 blk_unregister_queue(disk
);
197 blk_unregister_region(MKDEV(disk
->major
, disk
->first_minor
),
201 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
204 * get_gendisk - get partitioning information for a given device
205 * @dev: device to get partitioning information for
207 * This function gets the structure containing partitioning
208 * information for the given device @dev.
210 struct gendisk
*get_gendisk(dev_t dev
, int *part
)
212 struct kobject
*kobj
= kobj_lookup(bdev_map
, dev
, part
);
213 return kobj
? to_disk(kobj
) : NULL
;
216 #ifdef CONFIG_PROC_FS
218 static void *part_start(struct seq_file
*part
, loff_t
*pos
)
223 mutex_lock(&block_subsys_lock
);
224 list_for_each(p
, &block_subsys
.kset
.list
)
226 return list_entry(p
, struct gendisk
, kobj
.entry
);
230 static void *part_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
232 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
234 return p
==&block_subsys
.kset
.list
? NULL
:
235 list_entry(p
, struct gendisk
, kobj
.entry
);
238 static void part_stop(struct seq_file
*part
, void *v
)
240 mutex_unlock(&block_subsys_lock
);
243 static int show_partition(struct seq_file
*part
, void *v
)
245 struct gendisk
*sgp
= v
;
247 char buf
[BDEVNAME_SIZE
];
249 if (&sgp
->kobj
.entry
== block_subsys
.kset
.list
.next
)
250 seq_puts(part
, "major minor #blocks name\n\n");
252 /* Don't show non-partitionable removeable devices or empty devices */
253 if (!get_capacity(sgp
) ||
254 (sgp
->minors
== 1 && (sgp
->flags
& GENHD_FL_REMOVABLE
)))
256 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
259 /* show the full disk and all non-0 size partitions of it */
260 seq_printf(part
, "%4d %4d %10llu %s\n",
261 sgp
->major
, sgp
->first_minor
,
262 (unsigned long long)get_capacity(sgp
) >> 1,
263 disk_name(sgp
, 0, buf
));
264 for (n
= 0; n
< sgp
->minors
- 1; n
++) {
267 if (sgp
->part
[n
]->nr_sects
== 0)
269 seq_printf(part
, "%4d %4d %10llu %s\n",
270 sgp
->major
, n
+ 1 + sgp
->first_minor
,
271 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1 ,
272 disk_name(sgp
, n
+ 1, buf
));
278 struct seq_operations partitions_op
= {
282 .show
= show_partition
287 extern int blk_dev_init(void);
289 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
291 if (request_module("block-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
292 /* Make old-style 2.4 aliases work */
293 request_module("block-major-%d", MAJOR(dev
));
297 static int __init
genhd_device_init(void)
299 bdev_map
= kobj_map_init(base_probe
, &block_subsys_lock
);
301 subsystem_register(&block_subsys
);
305 subsys_initcall(genhd_device_init
);
310 * kobject & sysfs bindings for block devices
312 static ssize_t
disk_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
315 struct gendisk
*disk
= to_disk(kobj
);
316 struct disk_attribute
*disk_attr
=
317 container_of(attr
,struct disk_attribute
,attr
);
321 ret
= disk_attr
->show(disk
,page
);
325 static ssize_t
disk_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
326 const char *page
, size_t count
)
328 struct gendisk
*disk
= to_disk(kobj
);
329 struct disk_attribute
*disk_attr
=
330 container_of(attr
,struct disk_attribute
,attr
);
333 if (disk_attr
->store
)
334 ret
= disk_attr
->store(disk
, page
, count
);
338 static struct sysfs_ops disk_sysfs_ops
= {
339 .show
= &disk_attr_show
,
340 .store
= &disk_attr_store
,
343 static ssize_t
disk_uevent_store(struct gendisk
* disk
,
344 const char *buf
, size_t count
)
346 kobject_uevent(&disk
->kobj
, KOBJ_ADD
);
349 static ssize_t
disk_dev_read(struct gendisk
* disk
, char *page
)
351 dev_t base
= MKDEV(disk
->major
, disk
->first_minor
);
352 return print_dev_t(page
, base
);
354 static ssize_t
disk_range_read(struct gendisk
* disk
, char *page
)
356 return sprintf(page
, "%d\n", disk
->minors
);
358 static ssize_t
disk_removable_read(struct gendisk
* disk
, char *page
)
360 return sprintf(page
, "%d\n",
361 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
364 static ssize_t
disk_size_read(struct gendisk
* disk
, char *page
)
366 return sprintf(page
, "%llu\n", (unsigned long long)get_capacity(disk
));
369 static ssize_t
disk_stats_read(struct gendisk
* disk
, char *page
)
372 disk_round_stats(disk
);
375 "%8lu %8lu %8llu %8u "
376 "%8lu %8lu %8llu %8u "
379 disk_stat_read(disk
, ios
[READ
]),
380 disk_stat_read(disk
, merges
[READ
]),
381 (unsigned long long)disk_stat_read(disk
, sectors
[READ
]),
382 jiffies_to_msecs(disk_stat_read(disk
, ticks
[READ
])),
383 disk_stat_read(disk
, ios
[WRITE
]),
384 disk_stat_read(disk
, merges
[WRITE
]),
385 (unsigned long long)disk_stat_read(disk
, sectors
[WRITE
]),
386 jiffies_to_msecs(disk_stat_read(disk
, ticks
[WRITE
])),
388 jiffies_to_msecs(disk_stat_read(disk
, io_ticks
)),
389 jiffies_to_msecs(disk_stat_read(disk
, time_in_queue
)));
391 static struct disk_attribute disk_attr_uevent
= {
392 .attr
= {.name
= "uevent", .mode
= S_IWUSR
},
393 .store
= disk_uevent_store
395 static struct disk_attribute disk_attr_dev
= {
396 .attr
= {.name
= "dev", .mode
= S_IRUGO
},
397 .show
= disk_dev_read
399 static struct disk_attribute disk_attr_range
= {
400 .attr
= {.name
= "range", .mode
= S_IRUGO
},
401 .show
= disk_range_read
403 static struct disk_attribute disk_attr_removable
= {
404 .attr
= {.name
= "removable", .mode
= S_IRUGO
},
405 .show
= disk_removable_read
407 static struct disk_attribute disk_attr_size
= {
408 .attr
= {.name
= "size", .mode
= S_IRUGO
},
409 .show
= disk_size_read
411 static struct disk_attribute disk_attr_stat
= {
412 .attr
= {.name
= "stat", .mode
= S_IRUGO
},
413 .show
= disk_stats_read
416 static struct attribute
* default_attrs
[] = {
417 &disk_attr_uevent
.attr
,
419 &disk_attr_range
.attr
,
420 &disk_attr_removable
.attr
,
421 &disk_attr_size
.attr
,
422 &disk_attr_stat
.attr
,
426 static void disk_release(struct kobject
* kobj
)
428 struct gendisk
*disk
= to_disk(kobj
);
431 free_disk_stats(disk
);
435 static struct kobj_type ktype_block
= {
436 .release
= disk_release
,
437 .sysfs_ops
= &disk_sysfs_ops
,
438 .default_attrs
= default_attrs
,
441 extern struct kobj_type ktype_part
;
443 static int block_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
445 struct kobj_type
*ktype
= get_ktype(kobj
);
447 return ((ktype
== &ktype_block
) || (ktype
== &ktype_part
));
450 static int block_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
451 int num_envp
, char *buffer
, int buffer_size
)
453 struct kobj_type
*ktype
= get_ktype(kobj
);
454 struct device
*physdev
;
455 struct gendisk
*disk
;
456 struct hd_struct
*part
;
460 if (ktype
== &ktype_block
) {
461 disk
= container_of(kobj
, struct gendisk
, kobj
);
462 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
463 &length
, "MINOR=%u", disk
->first_minor
);
464 } else if (ktype
== &ktype_part
) {
465 disk
= container_of(kobj
->parent
, struct gendisk
, kobj
);
466 part
= container_of(kobj
, struct hd_struct
, kobj
);
467 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
469 disk
->first_minor
+ part
->partno
);
473 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &length
,
474 "MAJOR=%u", disk
->major
);
476 /* add physical device, backing this device */
477 physdev
= disk
->driverfs_dev
;
479 char *path
= kobject_get_path(&physdev
->kobj
, GFP_KERNEL
);
481 add_uevent_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
482 &length
, "PHYSDEVPATH=%s", path
);
486 add_uevent_var(envp
, num_envp
, &i
,
487 buffer
, buffer_size
, &length
,
492 add_uevent_var(envp
, num_envp
, &i
,
493 buffer
, buffer_size
, &length
,
495 physdev
->driver
->name
);
498 /* terminate, set to next free slot, shrink available space */
502 buffer
= &buffer
[length
];
503 buffer_size
-= length
;
508 static struct kset_uevent_ops block_uevent_ops
= {
509 .filter
= block_uevent_filter
,
510 .uevent
= block_uevent
,
513 decl_subsys(block
, &ktype_block
, &block_uevent_ops
);
516 * aggregate disk stat collector. Uses the same stats that the sysfs
517 * entries do, above, but makes them available through one seq_file.
518 * Watching a few disks may be efficient through sysfs, but watching
519 * all of them will be more efficient through this interface.
521 * The output looks suspiciously like /proc/partitions with a bunch of
526 static void *diskstats_start(struct seq_file
*part
, loff_t
*pos
)
531 mutex_lock(&block_subsys_lock
);
532 list_for_each(p
, &block_subsys
.kset
.list
)
534 return list_entry(p
, struct gendisk
, kobj
.entry
);
538 static void *diskstats_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
540 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
542 return p
==&block_subsys
.kset
.list
? NULL
:
543 list_entry(p
, struct gendisk
, kobj
.entry
);
546 static void diskstats_stop(struct seq_file
*part
, void *v
)
548 mutex_unlock(&block_subsys_lock
);
551 static int diskstats_show(struct seq_file
*s
, void *v
)
553 struct gendisk
*gp
= v
;
554 char buf
[BDEVNAME_SIZE
];
558 if (&sgp->kobj.entry == block_subsys.kset.list.next)
559 seq_puts(s, "major minor name"
560 " rio rmerge rsect ruse wio wmerge "
561 "wsect wuse running use aveq"
566 disk_round_stats(gp
);
568 seq_printf(s
, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n",
569 gp
->major
, n
+ gp
->first_minor
, disk_name(gp
, n
, buf
),
570 disk_stat_read(gp
, ios
[0]), disk_stat_read(gp
, merges
[0]),
571 (unsigned long long)disk_stat_read(gp
, sectors
[0]),
572 jiffies_to_msecs(disk_stat_read(gp
, ticks
[0])),
573 disk_stat_read(gp
, ios
[1]), disk_stat_read(gp
, merges
[1]),
574 (unsigned long long)disk_stat_read(gp
, sectors
[1]),
575 jiffies_to_msecs(disk_stat_read(gp
, ticks
[1])),
577 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
578 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
580 /* now show all non-0 size partitions of it */
581 for (n
= 0; n
< gp
->minors
- 1; n
++) {
582 struct hd_struct
*hd
= gp
->part
[n
];
584 if (hd
&& hd
->nr_sects
)
585 seq_printf(s
, "%4d %4d %s %u %u %u %u\n",
586 gp
->major
, n
+ gp
->first_minor
+ 1,
587 disk_name(gp
, n
+ 1, buf
),
588 hd
->ios
[0], hd
->sectors
[0],
589 hd
->ios
[1], hd
->sectors
[1]);
595 struct seq_operations diskstats_op
= {
596 .start
= diskstats_start
,
597 .next
= diskstats_next
,
598 .stop
= diskstats_stop
,
599 .show
= diskstats_show
602 struct gendisk
*alloc_disk(int minors
)
604 return alloc_disk_node(minors
, -1);
607 struct gendisk
*alloc_disk_node(int minors
, int node_id
)
609 struct gendisk
*disk
;
611 disk
= kmalloc_node(sizeof(struct gendisk
), GFP_KERNEL
, node_id
);
613 memset(disk
, 0, sizeof(struct gendisk
));
614 if (!init_disk_stats(disk
)) {
619 int size
= (minors
- 1) * sizeof(struct hd_struct
*);
620 disk
->part
= kmalloc_node(size
, GFP_KERNEL
, node_id
);
625 memset(disk
->part
, 0, size
);
627 disk
->minors
= minors
;
628 kobj_set_kset_s(disk
,block_subsys
);
629 kobject_init(&disk
->kobj
);
630 rand_initialize_disk(disk
);
635 EXPORT_SYMBOL(alloc_disk
);
636 EXPORT_SYMBOL(alloc_disk_node
);
638 struct kobject
*get_disk(struct gendisk
*disk
)
640 struct module
*owner
;
641 struct kobject
*kobj
;
645 owner
= disk
->fops
->owner
;
646 if (owner
&& !try_module_get(owner
))
648 kobj
= kobject_get(&disk
->kobj
);
657 EXPORT_SYMBOL(get_disk
);
659 void put_disk(struct gendisk
*disk
)
662 kobject_put(&disk
->kobj
);
665 EXPORT_SYMBOL(put_disk
);
667 void set_device_ro(struct block_device
*bdev
, int flag
)
669 if (bdev
->bd_contains
!= bdev
)
670 bdev
->bd_part
->policy
= flag
;
672 bdev
->bd_disk
->policy
= flag
;
675 EXPORT_SYMBOL(set_device_ro
);
677 void set_disk_ro(struct gendisk
*disk
, int flag
)
681 for (i
= 0; i
< disk
->minors
- 1; i
++)
682 if (disk
->part
[i
]) disk
->part
[i
]->policy
= flag
;
685 EXPORT_SYMBOL(set_disk_ro
);
687 int bdev_read_only(struct block_device
*bdev
)
691 else if (bdev
->bd_contains
!= bdev
)
692 return bdev
->bd_part
->policy
;
694 return bdev
->bd_disk
->policy
;
697 EXPORT_SYMBOL(bdev_read_only
);
699 int invalidate_partition(struct gendisk
*disk
, int index
)
702 struct block_device
*bdev
= bdget_disk(disk
, index
);
705 res
= __invalidate_device(bdev
);
711 EXPORT_SYMBOL(invalidate_partition
);