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
;
42 /* get block device names in somewhat random order */
43 int get_blkdev_list(char *p
)
45 struct blk_major_name
*n
;
48 len
= sprintf(p
, "\nBlock devices:\n");
50 down(&block_subsys_sem
);
51 for (i
= 0; i
< ARRAY_SIZE(major_names
); i
++) {
52 for (n
= major_names
[i
]; n
; n
= n
->next
)
53 len
+= sprintf(p
+len
, "%3d %s\n",
56 up(&block_subsys_sem
);
62 int register_blkdev(unsigned int major
, const char *name
)
64 struct blk_major_name
**n
, *p
;
67 down(&block_subsys_sem
);
71 for (index
= ARRAY_SIZE(major_names
)-1; index
> 0; index
--) {
72 if (major_names
[index
] == NULL
)
77 printk("register_blkdev: failed to get major for %s\n",
86 p
= kmalloc(sizeof(struct blk_major_name
), GFP_KERNEL
);
93 strlcpy(p
->name
, name
, sizeof(p
->name
));
95 index
= major_to_index(major
);
97 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
) {
98 if ((*n
)->major
== major
)
107 printk("register_blkdev: cannot get major %d for %s\n",
112 up(&block_subsys_sem
);
116 EXPORT_SYMBOL(register_blkdev
);
118 /* todo: make void - error printk here */
119 int unregister_blkdev(unsigned int major
, const char *name
)
121 struct blk_major_name
**n
;
122 struct blk_major_name
*p
= NULL
;
123 int index
= major_to_index(major
);
126 down(&block_subsys_sem
);
127 for (n
= &major_names
[index
]; *n
; n
= &(*n
)->next
)
128 if ((*n
)->major
== major
)
130 if (!*n
|| strcmp((*n
)->name
, name
))
136 up(&block_subsys_sem
);
142 EXPORT_SYMBOL(unregister_blkdev
);
144 static struct kobj_map
*bdev_map
;
147 * Register device numbers dev..(dev+range-1)
148 * range must be nonzero
149 * The hash chain is sorted on range, so that subranges can override.
151 void blk_register_region(dev_t dev
, unsigned long range
, struct module
*module
,
152 struct kobject
*(*probe
)(dev_t
, int *, void *),
153 int (*lock
)(dev_t
, void *), void *data
)
155 kobj_map(bdev_map
, dev
, range
, module
, probe
, lock
, data
);
158 EXPORT_SYMBOL(blk_register_region
);
160 void blk_unregister_region(dev_t dev
, unsigned long range
)
162 kobj_unmap(bdev_map
, dev
, range
);
165 EXPORT_SYMBOL(blk_unregister_region
);
167 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
169 struct gendisk
*p
= data
;
173 static int exact_lock(dev_t dev
, void *data
)
175 struct gendisk
*p
= data
;
183 * add_disk - add partitioning information to kernel list
184 * @disk: per-device partitioning information
186 * This function registers the partitioning information in @disk
189 void add_disk(struct gendisk
*disk
)
191 disk
->flags
|= GENHD_FL_UP
;
192 blk_register_region(MKDEV(disk
->major
, disk
->first_minor
),
193 disk
->minors
, NULL
, exact_match
, exact_lock
, disk
);
195 blk_register_queue(disk
);
198 EXPORT_SYMBOL(add_disk
);
199 EXPORT_SYMBOL(del_gendisk
); /* in partitions/check.c */
201 void unlink_gendisk(struct gendisk
*disk
)
203 blk_unregister_queue(disk
);
204 blk_unregister_region(MKDEV(disk
->major
, disk
->first_minor
),
208 #define to_disk(obj) container_of(obj,struct gendisk,kobj)
211 * get_gendisk - get partitioning information for a given device
212 * @dev: device to get partitioning information for
214 * This function gets the structure containing partitioning
215 * information for the given device @dev.
217 struct gendisk
*get_gendisk(dev_t dev
, int *part
)
219 struct kobject
*kobj
= kobj_lookup(bdev_map
, dev
, part
);
220 return kobj
? to_disk(kobj
) : NULL
;
223 #ifdef CONFIG_PROC_FS
225 static void *part_start(struct seq_file
*part
, loff_t
*pos
)
230 down(&block_subsys_sem
);
231 list_for_each(p
, &block_subsys
.kset
.list
)
233 return list_entry(p
, struct gendisk
, kobj
.entry
);
237 static void *part_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
239 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
241 return p
==&block_subsys
.kset
.list
? NULL
:
242 list_entry(p
, struct gendisk
, kobj
.entry
);
245 static void part_stop(struct seq_file
*part
, void *v
)
247 up(&block_subsys_sem
);
250 static int show_partition(struct seq_file
*part
, void *v
)
252 struct gendisk
*sgp
= v
;
254 char buf
[BDEVNAME_SIZE
];
256 if (&sgp
->kobj
.entry
== block_subsys
.kset
.list
.next
)
257 seq_puts(part
, "major minor #blocks name\n\n");
259 /* Don't show non-partitionable removeable devices or empty devices */
260 if (!get_capacity(sgp
) ||
261 (sgp
->minors
== 1 && (sgp
->flags
& GENHD_FL_REMOVABLE
)))
263 if (sgp
->flags
& GENHD_FL_SUPPRESS_PARTITION_INFO
)
266 /* show the full disk and all non-0 size partitions of it */
267 seq_printf(part
, "%4d %4d %10llu %s\n",
268 sgp
->major
, sgp
->first_minor
,
269 (unsigned long long)get_capacity(sgp
) >> 1,
270 disk_name(sgp
, 0, buf
));
271 for (n
= 0; n
< sgp
->minors
- 1; n
++) {
274 if (sgp
->part
[n
]->nr_sects
== 0)
276 seq_printf(part
, "%4d %4d %10llu %s\n",
277 sgp
->major
, n
+ 1 + sgp
->first_minor
,
278 (unsigned long long)sgp
->part
[n
]->nr_sects
>> 1 ,
279 disk_name(sgp
, n
+ 1, buf
));
285 struct seq_operations partitions_op
= {
289 .show
= show_partition
294 extern int blk_dev_init(void);
296 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
298 if (request_module("block-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
299 /* Make old-style 2.4 aliases work */
300 request_module("block-major-%d", MAJOR(dev
));
304 static int __init
genhd_device_init(void)
306 bdev_map
= kobj_map_init(base_probe
, &block_subsys_sem
);
308 subsystem_register(&block_subsys
);
312 subsys_initcall(genhd_device_init
);
317 * kobject & sysfs bindings for block devices
319 static ssize_t
disk_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
322 struct gendisk
*disk
= to_disk(kobj
);
323 struct disk_attribute
*disk_attr
=
324 container_of(attr
,struct disk_attribute
,attr
);
328 ret
= disk_attr
->show(disk
,page
);
332 static struct sysfs_ops disk_sysfs_ops
= {
333 .show
= &disk_attr_show
,
336 static ssize_t
disk_dev_read(struct gendisk
* disk
, char *page
)
338 dev_t base
= MKDEV(disk
->major
, disk
->first_minor
);
339 return print_dev_t(page
, base
);
341 static ssize_t
disk_range_read(struct gendisk
* disk
, char *page
)
343 return sprintf(page
, "%d\n", disk
->minors
);
345 static ssize_t
disk_removable_read(struct gendisk
* disk
, char *page
)
347 return sprintf(page
, "%d\n",
348 (disk
->flags
& GENHD_FL_REMOVABLE
? 1 : 0));
351 static ssize_t
disk_size_read(struct gendisk
* disk
, char *page
)
353 return sprintf(page
, "%llu\n", (unsigned long long)get_capacity(disk
));
356 static ssize_t
disk_stats_read(struct gendisk
* disk
, char *page
)
359 disk_round_stats(disk
);
366 disk_stat_read(disk
, reads
), disk_stat_read(disk
, read_merges
),
367 (unsigned long long)disk_stat_read(disk
, read_sectors
),
368 jiffies_to_msecs(disk_stat_read(disk
, read_ticks
)),
369 disk_stat_read(disk
, writes
),
370 disk_stat_read(disk
, write_merges
),
371 (unsigned long long)disk_stat_read(disk
, write_sectors
),
372 jiffies_to_msecs(disk_stat_read(disk
, write_ticks
)),
374 jiffies_to_msecs(disk_stat_read(disk
, io_ticks
)),
375 jiffies_to_msecs(disk_stat_read(disk
, time_in_queue
)));
377 static struct disk_attribute disk_attr_dev
= {
378 .attr
= {.name
= "dev", .mode
= S_IRUGO
},
379 .show
= disk_dev_read
381 static struct disk_attribute disk_attr_range
= {
382 .attr
= {.name
= "range", .mode
= S_IRUGO
},
383 .show
= disk_range_read
385 static struct disk_attribute disk_attr_removable
= {
386 .attr
= {.name
= "removable", .mode
= S_IRUGO
},
387 .show
= disk_removable_read
389 static struct disk_attribute disk_attr_size
= {
390 .attr
= {.name
= "size", .mode
= S_IRUGO
},
391 .show
= disk_size_read
393 static struct disk_attribute disk_attr_stat
= {
394 .attr
= {.name
= "stat", .mode
= S_IRUGO
},
395 .show
= disk_stats_read
398 static struct attribute
* default_attrs
[] = {
400 &disk_attr_range
.attr
,
401 &disk_attr_removable
.attr
,
402 &disk_attr_size
.attr
,
403 &disk_attr_stat
.attr
,
407 static void disk_release(struct kobject
* kobj
)
409 struct gendisk
*disk
= to_disk(kobj
);
412 free_disk_stats(disk
);
416 static struct kobj_type ktype_block
= {
417 .release
= disk_release
,
418 .sysfs_ops
= &disk_sysfs_ops
,
419 .default_attrs
= default_attrs
,
422 extern struct kobj_type ktype_part
;
424 static int block_hotplug_filter(struct kset
*kset
, struct kobject
*kobj
)
426 struct kobj_type
*ktype
= get_ktype(kobj
);
428 return ((ktype
== &ktype_block
) || (ktype
== &ktype_part
));
431 static int block_hotplug(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
432 int num_envp
, char *buffer
, int buffer_size
)
434 struct kobj_type
*ktype
= get_ktype(kobj
);
435 struct device
*physdev
;
436 struct gendisk
*disk
;
437 struct hd_struct
*part
;
441 if (ktype
== &ktype_block
) {
442 disk
= container_of(kobj
, struct gendisk
, kobj
);
443 add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
444 &length
, "MINOR=%u", disk
->first_minor
);
445 } else if (ktype
== &ktype_part
) {
446 disk
= container_of(kobj
->parent
, struct gendisk
, kobj
);
447 part
= container_of(kobj
, struct hd_struct
, kobj
);
448 add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
450 disk
->first_minor
+ part
->partno
);
454 add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
, &length
,
455 "MAJOR=%u", disk
->major
);
457 /* add physical device, backing this device */
458 physdev
= disk
->driverfs_dev
;
460 char *path
= kobject_get_path(&physdev
->kobj
, GFP_KERNEL
);
462 add_hotplug_env_var(envp
, num_envp
, &i
, buffer
, buffer_size
,
463 &length
, "PHYSDEVPATH=%s", path
);
467 add_hotplug_env_var(envp
, num_envp
, &i
,
468 buffer
, buffer_size
, &length
,
473 add_hotplug_env_var(envp
, num_envp
, &i
,
474 buffer
, buffer_size
, &length
,
476 physdev
->driver
->name
);
479 /* terminate, set to next free slot, shrink available space */
483 buffer
= &buffer
[length
];
484 buffer_size
-= length
;
489 static struct kset_hotplug_ops block_hotplug_ops
= {
490 .filter
= block_hotplug_filter
,
491 .hotplug
= block_hotplug
,
494 /* declare block_subsys. */
495 static decl_subsys(block
, &ktype_block
, &block_hotplug_ops
);
499 * aggregate disk stat collector. Uses the same stats that the sysfs
500 * entries do, above, but makes them available through one seq_file.
501 * Watching a few disks may be efficient through sysfs, but watching
502 * all of them will be more efficient through this interface.
504 * The output looks suspiciously like /proc/partitions with a bunch of
509 static void *diskstats_start(struct seq_file
*part
, loff_t
*pos
)
514 down(&block_subsys_sem
);
515 list_for_each(p
, &block_subsys
.kset
.list
)
517 return list_entry(p
, struct gendisk
, kobj
.entry
);
521 static void *diskstats_next(struct seq_file
*part
, void *v
, loff_t
*pos
)
523 struct list_head
*p
= ((struct gendisk
*)v
)->kobj
.entry
.next
;
525 return p
==&block_subsys
.kset
.list
? NULL
:
526 list_entry(p
, struct gendisk
, kobj
.entry
);
529 static void diskstats_stop(struct seq_file
*part
, void *v
)
531 up(&block_subsys_sem
);
534 static int diskstats_show(struct seq_file
*s
, void *v
)
536 struct gendisk
*gp
= v
;
537 char buf
[BDEVNAME_SIZE
];
541 if (&sgp->kobj.entry == block_subsys.kset.list.next)
542 seq_puts(s, "major minor name"
543 " rio rmerge rsect ruse wio wmerge "
544 "wsect wuse running use aveq"
549 disk_round_stats(gp
);
551 seq_printf(s
, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n",
552 gp
->major
, n
+ gp
->first_minor
, disk_name(gp
, n
, buf
),
553 disk_stat_read(gp
, reads
), disk_stat_read(gp
, read_merges
),
554 (unsigned long long)disk_stat_read(gp
, read_sectors
),
555 jiffies_to_msecs(disk_stat_read(gp
, read_ticks
)),
556 disk_stat_read(gp
, writes
), disk_stat_read(gp
, write_merges
),
557 (unsigned long long)disk_stat_read(gp
, write_sectors
),
558 jiffies_to_msecs(disk_stat_read(gp
, write_ticks
)),
560 jiffies_to_msecs(disk_stat_read(gp
, io_ticks
)),
561 jiffies_to_msecs(disk_stat_read(gp
, time_in_queue
)));
563 /* now show all non-0 size partitions of it */
564 for (n
= 0; n
< gp
->minors
- 1; n
++) {
565 struct hd_struct
*hd
= gp
->part
[n
];
567 if (hd
&& hd
->nr_sects
)
568 seq_printf(s
, "%4d %4d %s %u %u %u %u\n",
569 gp
->major
, n
+ gp
->first_minor
+ 1,
570 disk_name(gp
, n
+ 1, buf
),
571 hd
->reads
, hd
->read_sectors
,
572 hd
->writes
, hd
->write_sectors
);
578 struct seq_operations diskstats_op
= {
579 .start
= diskstats_start
,
580 .next
= diskstats_next
,
581 .stop
= diskstats_stop
,
582 .show
= diskstats_show
586 struct gendisk
*alloc_disk(int minors
)
588 struct gendisk
*disk
= kmalloc(sizeof(struct gendisk
), GFP_KERNEL
);
590 memset(disk
, 0, sizeof(struct gendisk
));
591 if (!init_disk_stats(disk
)) {
596 int size
= (minors
- 1) * sizeof(struct hd_struct
*);
597 disk
->part
= kmalloc(size
, GFP_KERNEL
);
602 memset(disk
->part
, 0, size
);
604 disk
->minors
= minors
;
605 kobj_set_kset_s(disk
,block_subsys
);
606 kobject_init(&disk
->kobj
);
607 rand_initialize_disk(disk
);
612 EXPORT_SYMBOL(alloc_disk
);
614 struct kobject
*get_disk(struct gendisk
*disk
)
616 struct module
*owner
;
617 struct kobject
*kobj
;
621 owner
= disk
->fops
->owner
;
622 if (owner
&& !try_module_get(owner
))
624 kobj
= kobject_get(&disk
->kobj
);
633 EXPORT_SYMBOL(get_disk
);
635 void put_disk(struct gendisk
*disk
)
638 kobject_put(&disk
->kobj
);
641 EXPORT_SYMBOL(put_disk
);
643 void set_device_ro(struct block_device
*bdev
, int flag
)
645 if (bdev
->bd_contains
!= bdev
)
646 bdev
->bd_part
->policy
= flag
;
648 bdev
->bd_disk
->policy
= flag
;
651 EXPORT_SYMBOL(set_device_ro
);
653 void set_disk_ro(struct gendisk
*disk
, int flag
)
657 for (i
= 0; i
< disk
->minors
- 1; i
++)
658 if (disk
->part
[i
]) disk
->part
[i
]->policy
= flag
;
661 EXPORT_SYMBOL(set_disk_ro
);
663 int bdev_read_only(struct block_device
*bdev
)
667 else if (bdev
->bd_contains
!= bdev
)
668 return bdev
->bd_part
->policy
;
670 return bdev
->bd_disk
->policy
;
673 EXPORT_SYMBOL(bdev_read_only
);
675 int invalidate_partition(struct gendisk
*disk
, int index
)
678 struct block_device
*bdev
= bdget_disk(disk
, index
);
681 res
= __invalidate_device(bdev
);
687 EXPORT_SYMBOL(invalidate_partition
);