Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * gendisk handling |
| 3 | */ |
| 4 | |
| 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> |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 17 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #define MAX_PROBE_HASH 255 /* random */ |
| 20 | |
| 21 | static struct subsystem block_subsys; |
| 22 | |
| 23 | static DECLARE_MUTEX(block_subsys_sem); |
| 24 | |
| 25 | /* |
| 26 | * Can be deleted altogether. Later. |
| 27 | * |
| 28 | */ |
| 29 | static struct blk_major_name { |
| 30 | struct blk_major_name *next; |
| 31 | int major; |
| 32 | char name[16]; |
| 33 | } *major_names[MAX_PROBE_HASH]; |
| 34 | |
| 35 | /* index in the above - for now: assume no multimajor ranges */ |
| 36 | static inline int major_to_index(int major) |
| 37 | { |
| 38 | return major % MAX_PROBE_HASH; |
| 39 | } |
| 40 | |
| 41 | #ifdef CONFIG_PROC_FS |
| 42 | /* get block device names in somewhat random order */ |
Neil Horman | ac20427 | 2005-06-23 00:09:11 -0700 | [diff] [blame] | 43 | int get_blkdev_list(char *p, int used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | struct blk_major_name *n; |
| 46 | int i, len; |
| 47 | |
Neil Horman | f62c6d0 | 2005-09-06 15:17:30 -0700 | [diff] [blame] | 48 | len = snprintf(p, (PAGE_SIZE-used), "\nBlock devices:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | down(&block_subsys_sem); |
| 51 | for (i = 0; i < ARRAY_SIZE(major_names); i++) { |
Neil Horman | ac20427 | 2005-06-23 00:09:11 -0700 | [diff] [blame] | 52 | for (n = major_names[i]; n; n = n->next) { |
| 53 | /* |
| 54 | * If the curent string plus the 5 extra characters |
| 55 | * in the line would run us off the page, then we're done |
| 56 | */ |
| 57 | if ((len + used + strlen(n->name) + 5) >= PAGE_SIZE) |
| 58 | goto page_full; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | len += sprintf(p+len, "%3d %s\n", |
| 60 | n->major, n->name); |
Neil Horman | ac20427 | 2005-06-23 00:09:11 -0700 | [diff] [blame] | 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
Neil Horman | ac20427 | 2005-06-23 00:09:11 -0700 | [diff] [blame] | 63 | page_full: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | up(&block_subsys_sem); |
| 65 | |
| 66 | return len; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | int register_blkdev(unsigned int major, const char *name) |
| 71 | { |
| 72 | struct blk_major_name **n, *p; |
| 73 | int index, ret = 0; |
| 74 | |
| 75 | down(&block_subsys_sem); |
| 76 | |
| 77 | /* temporary */ |
| 78 | if (major == 0) { |
| 79 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 80 | if (major_names[index] == NULL) |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | if (index == 0) { |
| 85 | printk("register_blkdev: failed to get major for %s\n", |
| 86 | name); |
| 87 | ret = -EBUSY; |
| 88 | goto out; |
| 89 | } |
| 90 | major = index; |
| 91 | ret = major; |
| 92 | } |
| 93 | |
| 94 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 95 | if (p == NULL) { |
| 96 | ret = -ENOMEM; |
| 97 | goto out; |
| 98 | } |
| 99 | |
| 100 | p->major = major; |
| 101 | strlcpy(p->name, name, sizeof(p->name)); |
| 102 | p->next = NULL; |
| 103 | index = major_to_index(major); |
| 104 | |
| 105 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 106 | if ((*n)->major == major) |
| 107 | break; |
| 108 | } |
| 109 | if (!*n) |
| 110 | *n = p; |
| 111 | else |
| 112 | ret = -EBUSY; |
| 113 | |
| 114 | if (ret < 0) { |
| 115 | printk("register_blkdev: cannot get major %d for %s\n", |
| 116 | major, name); |
| 117 | kfree(p); |
| 118 | } |
| 119 | out: |
| 120 | up(&block_subsys_sem); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | EXPORT_SYMBOL(register_blkdev); |
| 125 | |
| 126 | /* todo: make void - error printk here */ |
| 127 | int unregister_blkdev(unsigned int major, const char *name) |
| 128 | { |
| 129 | struct blk_major_name **n; |
| 130 | struct blk_major_name *p = NULL; |
| 131 | int index = major_to_index(major); |
| 132 | int ret = 0; |
| 133 | |
| 134 | down(&block_subsys_sem); |
| 135 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 136 | if ((*n)->major == major) |
| 137 | break; |
| 138 | if (!*n || strcmp((*n)->name, name)) |
| 139 | ret = -EINVAL; |
| 140 | else { |
| 141 | p = *n; |
| 142 | *n = p->next; |
| 143 | } |
| 144 | up(&block_subsys_sem); |
| 145 | kfree(p); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | EXPORT_SYMBOL(unregister_blkdev); |
| 151 | |
| 152 | static struct kobj_map *bdev_map; |
| 153 | |
| 154 | /* |
| 155 | * Register device numbers dev..(dev+range-1) |
| 156 | * range must be nonzero |
| 157 | * The hash chain is sorted on range, so that subranges can override. |
| 158 | */ |
| 159 | void blk_register_region(dev_t dev, unsigned long range, struct module *module, |
| 160 | struct kobject *(*probe)(dev_t, int *, void *), |
| 161 | int (*lock)(dev_t, void *), void *data) |
| 162 | { |
| 163 | kobj_map(bdev_map, dev, range, module, probe, lock, data); |
| 164 | } |
| 165 | |
| 166 | EXPORT_SYMBOL(blk_register_region); |
| 167 | |
| 168 | void blk_unregister_region(dev_t dev, unsigned long range) |
| 169 | { |
| 170 | kobj_unmap(bdev_map, dev, range); |
| 171 | } |
| 172 | |
| 173 | EXPORT_SYMBOL(blk_unregister_region); |
| 174 | |
| 175 | static struct kobject *exact_match(dev_t dev, int *part, void *data) |
| 176 | { |
| 177 | struct gendisk *p = data; |
| 178 | return &p->kobj; |
| 179 | } |
| 180 | |
| 181 | static int exact_lock(dev_t dev, void *data) |
| 182 | { |
| 183 | struct gendisk *p = data; |
| 184 | |
| 185 | if (!get_disk(p)) |
| 186 | return -1; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * add_disk - add partitioning information to kernel list |
| 192 | * @disk: per-device partitioning information |
| 193 | * |
| 194 | * This function registers the partitioning information in @disk |
| 195 | * with the kernel. |
| 196 | */ |
| 197 | void add_disk(struct gendisk *disk) |
| 198 | { |
| 199 | disk->flags |= GENHD_FL_UP; |
| 200 | blk_register_region(MKDEV(disk->major, disk->first_minor), |
| 201 | disk->minors, NULL, exact_match, exact_lock, disk); |
| 202 | register_disk(disk); |
| 203 | blk_register_queue(disk); |
| 204 | } |
| 205 | |
| 206 | EXPORT_SYMBOL(add_disk); |
| 207 | EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */ |
| 208 | |
| 209 | void unlink_gendisk(struct gendisk *disk) |
| 210 | { |
| 211 | blk_unregister_queue(disk); |
| 212 | blk_unregister_region(MKDEV(disk->major, disk->first_minor), |
| 213 | disk->minors); |
| 214 | } |
| 215 | |
| 216 | #define to_disk(obj) container_of(obj,struct gendisk,kobj) |
| 217 | |
| 218 | /** |
| 219 | * get_gendisk - get partitioning information for a given device |
| 220 | * @dev: device to get partitioning information for |
| 221 | * |
| 222 | * This function gets the structure containing partitioning |
| 223 | * information for the given device @dev. |
| 224 | */ |
| 225 | struct gendisk *get_gendisk(dev_t dev, int *part) |
| 226 | { |
| 227 | struct kobject *kobj = kobj_lookup(bdev_map, dev, part); |
| 228 | return kobj ? to_disk(kobj) : NULL; |
| 229 | } |
| 230 | |
| 231 | #ifdef CONFIG_PROC_FS |
| 232 | /* iterator */ |
| 233 | static void *part_start(struct seq_file *part, loff_t *pos) |
| 234 | { |
| 235 | struct list_head *p; |
| 236 | loff_t l = *pos; |
| 237 | |
| 238 | down(&block_subsys_sem); |
| 239 | list_for_each(p, &block_subsys.kset.list) |
| 240 | if (!l--) |
| 241 | return list_entry(p, struct gendisk, kobj.entry); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static void *part_next(struct seq_file *part, void *v, loff_t *pos) |
| 246 | { |
| 247 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 248 | ++*pos; |
| 249 | return p==&block_subsys.kset.list ? NULL : |
| 250 | list_entry(p, struct gendisk, kobj.entry); |
| 251 | } |
| 252 | |
| 253 | static void part_stop(struct seq_file *part, void *v) |
| 254 | { |
| 255 | up(&block_subsys_sem); |
| 256 | } |
| 257 | |
| 258 | static int show_partition(struct seq_file *part, void *v) |
| 259 | { |
| 260 | struct gendisk *sgp = v; |
| 261 | int n; |
| 262 | char buf[BDEVNAME_SIZE]; |
| 263 | |
| 264 | if (&sgp->kobj.entry == block_subsys.kset.list.next) |
| 265 | seq_puts(part, "major minor #blocks name\n\n"); |
| 266 | |
| 267 | /* Don't show non-partitionable removeable devices or empty devices */ |
| 268 | if (!get_capacity(sgp) || |
| 269 | (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE))) |
| 270 | return 0; |
| 271 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 272 | return 0; |
| 273 | |
| 274 | /* show the full disk and all non-0 size partitions of it */ |
| 275 | seq_printf(part, "%4d %4d %10llu %s\n", |
| 276 | sgp->major, sgp->first_minor, |
| 277 | (unsigned long long)get_capacity(sgp) >> 1, |
| 278 | disk_name(sgp, 0, buf)); |
| 279 | for (n = 0; n < sgp->minors - 1; n++) { |
| 280 | if (!sgp->part[n]) |
| 281 | continue; |
| 282 | if (sgp->part[n]->nr_sects == 0) |
| 283 | continue; |
| 284 | seq_printf(part, "%4d %4d %10llu %s\n", |
| 285 | sgp->major, n + 1 + sgp->first_minor, |
| 286 | (unsigned long long)sgp->part[n]->nr_sects >> 1 , |
| 287 | disk_name(sgp, n + 1, buf)); |
| 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | struct seq_operations partitions_op = { |
| 294 | .start =part_start, |
| 295 | .next = part_next, |
| 296 | .stop = part_stop, |
| 297 | .show = show_partition |
| 298 | }; |
| 299 | #endif |
| 300 | |
| 301 | |
| 302 | extern int blk_dev_init(void); |
| 303 | |
| 304 | static struct kobject *base_probe(dev_t dev, int *part, void *data) |
| 305 | { |
| 306 | if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0) |
| 307 | /* Make old-style 2.4 aliases work */ |
| 308 | request_module("block-major-%d", MAJOR(dev)); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | static int __init genhd_device_init(void) |
| 313 | { |
| 314 | bdev_map = kobj_map_init(base_probe, &block_subsys_sem); |
| 315 | blk_dev_init(); |
| 316 | subsystem_register(&block_subsys); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | subsys_initcall(genhd_device_init); |
| 321 | |
| 322 | |
| 323 | |
| 324 | /* |
| 325 | * kobject & sysfs bindings for block devices |
| 326 | */ |
| 327 | static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr, |
| 328 | char *page) |
| 329 | { |
| 330 | struct gendisk *disk = to_disk(kobj); |
| 331 | struct disk_attribute *disk_attr = |
| 332 | container_of(attr,struct disk_attribute,attr); |
Dmitry Torokhov | 6c1852a | 2005-04-29 01:26:06 -0500 | [diff] [blame] | 333 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | if (disk_attr->show) |
| 336 | ret = disk_attr->show(disk,page); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | static struct sysfs_ops disk_sysfs_ops = { |
| 341 | .show = &disk_attr_show, |
| 342 | }; |
| 343 | |
| 344 | static ssize_t disk_dev_read(struct gendisk * disk, char *page) |
| 345 | { |
| 346 | dev_t base = MKDEV(disk->major, disk->first_minor); |
| 347 | return print_dev_t(page, base); |
| 348 | } |
| 349 | static ssize_t disk_range_read(struct gendisk * disk, char *page) |
| 350 | { |
| 351 | return sprintf(page, "%d\n", disk->minors); |
| 352 | } |
| 353 | static ssize_t disk_removable_read(struct gendisk * disk, char *page) |
| 354 | { |
| 355 | return sprintf(page, "%d\n", |
| 356 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
| 357 | |
| 358 | } |
| 359 | static ssize_t disk_size_read(struct gendisk * disk, char *page) |
| 360 | { |
| 361 | return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk)); |
| 362 | } |
| 363 | |
| 364 | static ssize_t disk_stats_read(struct gendisk * disk, char *page) |
| 365 | { |
| 366 | preempt_disable(); |
| 367 | disk_round_stats(disk); |
| 368 | preempt_enable(); |
| 369 | return sprintf(page, |
| 370 | "%8u %8u %8llu %8u " |
| 371 | "%8u %8u %8llu %8u " |
| 372 | "%8u %8u %8u" |
| 373 | "\n", |
| 374 | disk_stat_read(disk, reads), disk_stat_read(disk, read_merges), |
| 375 | (unsigned long long)disk_stat_read(disk, read_sectors), |
| 376 | jiffies_to_msecs(disk_stat_read(disk, read_ticks)), |
| 377 | disk_stat_read(disk, writes), |
| 378 | disk_stat_read(disk, write_merges), |
| 379 | (unsigned long long)disk_stat_read(disk, write_sectors), |
| 380 | jiffies_to_msecs(disk_stat_read(disk, write_ticks)), |
| 381 | disk->in_flight, |
| 382 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 383 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 384 | } |
| 385 | static struct disk_attribute disk_attr_dev = { |
| 386 | .attr = {.name = "dev", .mode = S_IRUGO }, |
| 387 | .show = disk_dev_read |
| 388 | }; |
| 389 | static struct disk_attribute disk_attr_range = { |
| 390 | .attr = {.name = "range", .mode = S_IRUGO }, |
| 391 | .show = disk_range_read |
| 392 | }; |
| 393 | static struct disk_attribute disk_attr_removable = { |
| 394 | .attr = {.name = "removable", .mode = S_IRUGO }, |
| 395 | .show = disk_removable_read |
| 396 | }; |
| 397 | static struct disk_attribute disk_attr_size = { |
| 398 | .attr = {.name = "size", .mode = S_IRUGO }, |
| 399 | .show = disk_size_read |
| 400 | }; |
| 401 | static struct disk_attribute disk_attr_stat = { |
| 402 | .attr = {.name = "stat", .mode = S_IRUGO }, |
| 403 | .show = disk_stats_read |
| 404 | }; |
| 405 | |
| 406 | static struct attribute * default_attrs[] = { |
| 407 | &disk_attr_dev.attr, |
| 408 | &disk_attr_range.attr, |
| 409 | &disk_attr_removable.attr, |
| 410 | &disk_attr_size.attr, |
| 411 | &disk_attr_stat.attr, |
| 412 | NULL, |
| 413 | }; |
| 414 | |
| 415 | static void disk_release(struct kobject * kobj) |
| 416 | { |
| 417 | struct gendisk *disk = to_disk(kobj); |
| 418 | kfree(disk->random); |
| 419 | kfree(disk->part); |
| 420 | free_disk_stats(disk); |
| 421 | kfree(disk); |
| 422 | } |
| 423 | |
| 424 | static struct kobj_type ktype_block = { |
| 425 | .release = disk_release, |
| 426 | .sysfs_ops = &disk_sysfs_ops, |
| 427 | .default_attrs = default_attrs, |
| 428 | }; |
| 429 | |
| 430 | extern struct kobj_type ktype_part; |
| 431 | |
| 432 | static int block_hotplug_filter(struct kset *kset, struct kobject *kobj) |
| 433 | { |
| 434 | struct kobj_type *ktype = get_ktype(kobj); |
| 435 | |
| 436 | return ((ktype == &ktype_block) || (ktype == &ktype_part)); |
| 437 | } |
| 438 | |
| 439 | static int block_hotplug(struct kset *kset, struct kobject *kobj, char **envp, |
| 440 | int num_envp, char *buffer, int buffer_size) |
| 441 | { |
| 442 | struct kobj_type *ktype = get_ktype(kobj); |
| 443 | struct device *physdev; |
| 444 | struct gendisk *disk; |
| 445 | struct hd_struct *part; |
| 446 | int length = 0; |
| 447 | int i = 0; |
| 448 | |
| 449 | if (ktype == &ktype_block) { |
| 450 | disk = container_of(kobj, struct gendisk, kobj); |
| 451 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, |
| 452 | &length, "MINOR=%u", disk->first_minor); |
| 453 | } else if (ktype == &ktype_part) { |
| 454 | disk = container_of(kobj->parent, struct gendisk, kobj); |
| 455 | part = container_of(kobj, struct hd_struct, kobj); |
| 456 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, |
| 457 | &length, "MINOR=%u", |
| 458 | disk->first_minor + part->partno); |
| 459 | } else |
| 460 | return 0; |
| 461 | |
| 462 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &length, |
| 463 | "MAJOR=%u", disk->major); |
| 464 | |
| 465 | /* add physical device, backing this device */ |
| 466 | physdev = disk->driverfs_dev; |
| 467 | if (physdev) { |
| 468 | char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL); |
| 469 | |
| 470 | add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, |
| 471 | &length, "PHYSDEVPATH=%s", path); |
| 472 | kfree(path); |
| 473 | |
| 474 | if (physdev->bus) |
| 475 | add_hotplug_env_var(envp, num_envp, &i, |
| 476 | buffer, buffer_size, &length, |
| 477 | "PHYSDEVBUS=%s", |
| 478 | physdev->bus->name); |
| 479 | |
| 480 | if (physdev->driver) |
| 481 | add_hotplug_env_var(envp, num_envp, &i, |
| 482 | buffer, buffer_size, &length, |
| 483 | "PHYSDEVDRIVER=%s", |
| 484 | physdev->driver->name); |
| 485 | } |
| 486 | |
| 487 | /* terminate, set to next free slot, shrink available space */ |
| 488 | envp[i] = NULL; |
| 489 | envp = &envp[i]; |
| 490 | num_envp -= i; |
| 491 | buffer = &buffer[length]; |
| 492 | buffer_size -= length; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static struct kset_hotplug_ops block_hotplug_ops = { |
| 498 | .filter = block_hotplug_filter, |
| 499 | .hotplug = block_hotplug, |
| 500 | }; |
| 501 | |
| 502 | /* declare block_subsys. */ |
| 503 | static decl_subsys(block, &ktype_block, &block_hotplug_ops); |
| 504 | |
| 505 | |
| 506 | /* |
| 507 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 508 | * entries do, above, but makes them available through one seq_file. |
| 509 | * Watching a few disks may be efficient through sysfs, but watching |
| 510 | * all of them will be more efficient through this interface. |
| 511 | * |
| 512 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 513 | * extra fields. |
| 514 | */ |
| 515 | |
| 516 | /* iterator */ |
| 517 | static void *diskstats_start(struct seq_file *part, loff_t *pos) |
| 518 | { |
| 519 | loff_t k = *pos; |
| 520 | struct list_head *p; |
| 521 | |
| 522 | down(&block_subsys_sem); |
| 523 | list_for_each(p, &block_subsys.kset.list) |
| 524 | if (!k--) |
| 525 | return list_entry(p, struct gendisk, kobj.entry); |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos) |
| 530 | { |
| 531 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 532 | ++*pos; |
| 533 | return p==&block_subsys.kset.list ? NULL : |
| 534 | list_entry(p, struct gendisk, kobj.entry); |
| 535 | } |
| 536 | |
| 537 | static void diskstats_stop(struct seq_file *part, void *v) |
| 538 | { |
| 539 | up(&block_subsys_sem); |
| 540 | } |
| 541 | |
| 542 | static int diskstats_show(struct seq_file *s, void *v) |
| 543 | { |
| 544 | struct gendisk *gp = v; |
| 545 | char buf[BDEVNAME_SIZE]; |
| 546 | int n = 0; |
| 547 | |
| 548 | /* |
| 549 | if (&sgp->kobj.entry == block_subsys.kset.list.next) |
| 550 | seq_puts(s, "major minor name" |
| 551 | " rio rmerge rsect ruse wio wmerge " |
| 552 | "wsect wuse running use aveq" |
| 553 | "\n\n"); |
| 554 | */ |
| 555 | |
| 556 | preempt_disable(); |
| 557 | disk_round_stats(gp); |
| 558 | preempt_enable(); |
| 559 | seq_printf(s, "%4d %4d %s %u %u %llu %u %u %u %llu %u %u %u %u\n", |
| 560 | gp->major, n + gp->first_minor, disk_name(gp, n, buf), |
| 561 | disk_stat_read(gp, reads), disk_stat_read(gp, read_merges), |
| 562 | (unsigned long long)disk_stat_read(gp, read_sectors), |
| 563 | jiffies_to_msecs(disk_stat_read(gp, read_ticks)), |
| 564 | disk_stat_read(gp, writes), disk_stat_read(gp, write_merges), |
| 565 | (unsigned long long)disk_stat_read(gp, write_sectors), |
| 566 | jiffies_to_msecs(disk_stat_read(gp, write_ticks)), |
| 567 | gp->in_flight, |
| 568 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 569 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 570 | |
| 571 | /* now show all non-0 size partitions of it */ |
| 572 | for (n = 0; n < gp->minors - 1; n++) { |
| 573 | struct hd_struct *hd = gp->part[n]; |
| 574 | |
| 575 | if (hd && hd->nr_sects) |
| 576 | seq_printf(s, "%4d %4d %s %u %u %u %u\n", |
| 577 | gp->major, n + gp->first_minor + 1, |
| 578 | disk_name(gp, n + 1, buf), |
| 579 | hd->reads, hd->read_sectors, |
| 580 | hd->writes, hd->write_sectors); |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | struct seq_operations diskstats_op = { |
| 587 | .start = diskstats_start, |
| 588 | .next = diskstats_next, |
| 589 | .stop = diskstats_stop, |
| 590 | .show = diskstats_show |
| 591 | }; |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct gendisk *alloc_disk(int minors) |
| 594 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 595 | return alloc_disk_node(minors, -1); |
| 596 | } |
| 597 | |
| 598 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 599 | { |
| 600 | struct gendisk *disk; |
| 601 | |
| 602 | disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (disk) { |
| 604 | memset(disk, 0, sizeof(struct gendisk)); |
| 605 | if (!init_disk_stats(disk)) { |
| 606 | kfree(disk); |
| 607 | return NULL; |
| 608 | } |
| 609 | if (minors > 1) { |
| 610 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 611 | disk->part = kmalloc_node(size, GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (!disk->part) { |
| 613 | kfree(disk); |
| 614 | return NULL; |
| 615 | } |
| 616 | memset(disk->part, 0, size); |
| 617 | } |
| 618 | disk->minors = minors; |
| 619 | kobj_set_kset_s(disk,block_subsys); |
| 620 | kobject_init(&disk->kobj); |
| 621 | rand_initialize_disk(disk); |
| 622 | } |
| 623 | return disk; |
| 624 | } |
| 625 | |
| 626 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 627 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | struct kobject *get_disk(struct gendisk *disk) |
| 630 | { |
| 631 | struct module *owner; |
| 632 | struct kobject *kobj; |
| 633 | |
| 634 | if (!disk->fops) |
| 635 | return NULL; |
| 636 | owner = disk->fops->owner; |
| 637 | if (owner && !try_module_get(owner)) |
| 638 | return NULL; |
| 639 | kobj = kobject_get(&disk->kobj); |
| 640 | if (kobj == NULL) { |
| 641 | module_put(owner); |
| 642 | return NULL; |
| 643 | } |
| 644 | return kobj; |
| 645 | |
| 646 | } |
| 647 | |
| 648 | EXPORT_SYMBOL(get_disk); |
| 649 | |
| 650 | void put_disk(struct gendisk *disk) |
| 651 | { |
| 652 | if (disk) |
| 653 | kobject_put(&disk->kobj); |
| 654 | } |
| 655 | |
| 656 | EXPORT_SYMBOL(put_disk); |
| 657 | |
| 658 | void set_device_ro(struct block_device *bdev, int flag) |
| 659 | { |
| 660 | if (bdev->bd_contains != bdev) |
| 661 | bdev->bd_part->policy = flag; |
| 662 | else |
| 663 | bdev->bd_disk->policy = flag; |
| 664 | } |
| 665 | |
| 666 | EXPORT_SYMBOL(set_device_ro); |
| 667 | |
| 668 | void set_disk_ro(struct gendisk *disk, int flag) |
| 669 | { |
| 670 | int i; |
| 671 | disk->policy = flag; |
| 672 | for (i = 0; i < disk->minors - 1; i++) |
| 673 | if (disk->part[i]) disk->part[i]->policy = flag; |
| 674 | } |
| 675 | |
| 676 | EXPORT_SYMBOL(set_disk_ro); |
| 677 | |
| 678 | int bdev_read_only(struct block_device *bdev) |
| 679 | { |
| 680 | if (!bdev) |
| 681 | return 0; |
| 682 | else if (bdev->bd_contains != bdev) |
| 683 | return bdev->bd_part->policy; |
| 684 | else |
| 685 | return bdev->bd_disk->policy; |
| 686 | } |
| 687 | |
| 688 | EXPORT_SYMBOL(bdev_read_only); |
| 689 | |
| 690 | int invalidate_partition(struct gendisk *disk, int index) |
| 691 | { |
| 692 | int res = 0; |
| 693 | struct block_device *bdev = bdget_disk(disk, index); |
| 694 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 695 | fsync_bdev(bdev); |
| 696 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | bdput(bdev); |
| 698 | } |
| 699 | return res; |
| 700 | } |
| 701 | |
| 702 | EXPORT_SYMBOL(invalidate_partition); |