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