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 | |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 41 | struct blkdev_info { |
| 42 | int index; |
| 43 | struct blk_major_name *bd; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 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 |
| 50 | */ |
| 51 | void *get_next_blkdev(void *dev) |
| 52 | { |
| 53 | struct blkdev_info *info; |
| 54 | |
| 55 | if (dev == NULL) { |
| 56 | info = kmalloc(sizeof(*info), GFP_KERNEL); |
| 57 | if (!info) |
| 58 | goto out; |
| 59 | info->index=0; |
| 60 | info->bd = major_names[info->index]; |
| 61 | if (info->bd) |
| 62 | goto out; |
| 63 | } else { |
| 64 | info = dev; |
| 65 | } |
| 66 | |
| 67 | while (info->index < ARRAY_SIZE(major_names)) { |
| 68 | if (info->bd) |
| 69 | info->bd = info->bd->next; |
| 70 | if (info->bd) |
| 71 | goto out; |
| 72 | /* |
| 73 | * No devices on this chain, move to the next |
| 74 | */ |
| 75 | info->index++; |
| 76 | info->bd = (info->index < ARRAY_SIZE(major_names)) ? |
| 77 | major_names[info->index] : NULL; |
| 78 | if (info->bd) |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | out: |
| 83 | return info; |
| 84 | } |
| 85 | |
| 86 | void *acquire_blkdev_list(void) |
| 87 | { |
| 88 | down(&block_subsys_sem); |
| 89 | return get_next_blkdev(NULL); |
| 90 | } |
| 91 | |
| 92 | void release_blkdev_list(void *dev) |
| 93 | { |
| 94 | up(&block_subsys_sem); |
| 95 | kfree(dev); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * Count the number of records in the blkdev_list. |
| 101 | * must be called with the block_subsys_sem held |
| 102 | */ |
| 103 | int count_blkdev_list(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | struct blk_major_name *n; |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 106 | int i, count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 108 | count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | for (i = 0; i < ARRAY_SIZE(major_names); i++) { |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 111 | for (n = major_names[i]; n; n = n->next) |
| 112 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 115 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 117 | |
| 118 | /* |
| 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 |
| 122 | */ |
| 123 | int get_blkdev_info(void *dev, int *major, char **name) |
| 124 | { |
| 125 | struct blkdev_info *info = dev; |
| 126 | |
| 127 | if (info->bd == NULL) |
| 128 | return 1; |
| 129 | |
| 130 | *major = info->bd->major; |
| 131 | *name = info->bd->name; |
| 132 | return 0; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | int register_blkdev(unsigned int major, const char *name) |
| 137 | { |
| 138 | struct blk_major_name **n, *p; |
| 139 | int index, ret = 0; |
| 140 | |
| 141 | down(&block_subsys_sem); |
| 142 | |
| 143 | /* temporary */ |
| 144 | if (major == 0) { |
| 145 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 146 | if (major_names[index] == NULL) |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | if (index == 0) { |
| 151 | printk("register_blkdev: failed to get major for %s\n", |
| 152 | name); |
| 153 | ret = -EBUSY; |
| 154 | goto out; |
| 155 | } |
| 156 | major = index; |
| 157 | ret = major; |
| 158 | } |
| 159 | |
| 160 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 161 | if (p == NULL) { |
| 162 | ret = -ENOMEM; |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | p->major = major; |
| 167 | strlcpy(p->name, name, sizeof(p->name)); |
| 168 | p->next = NULL; |
| 169 | index = major_to_index(major); |
| 170 | |
| 171 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 172 | if ((*n)->major == major) |
| 173 | break; |
| 174 | } |
| 175 | if (!*n) |
| 176 | *n = p; |
| 177 | else |
| 178 | ret = -EBUSY; |
| 179 | |
| 180 | if (ret < 0) { |
| 181 | printk("register_blkdev: cannot get major %d for %s\n", |
| 182 | major, name); |
| 183 | kfree(p); |
| 184 | } |
| 185 | out: |
| 186 | up(&block_subsys_sem); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | EXPORT_SYMBOL(register_blkdev); |
| 191 | |
| 192 | /* todo: make void - error printk here */ |
| 193 | int unregister_blkdev(unsigned int major, const char *name) |
| 194 | { |
| 195 | struct blk_major_name **n; |
| 196 | struct blk_major_name *p = NULL; |
| 197 | int index = major_to_index(major); |
| 198 | int ret = 0; |
| 199 | |
| 200 | down(&block_subsys_sem); |
| 201 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 202 | if ((*n)->major == major) |
| 203 | break; |
| 204 | if (!*n || strcmp((*n)->name, name)) |
| 205 | ret = -EINVAL; |
| 206 | else { |
| 207 | p = *n; |
| 208 | *n = p->next; |
| 209 | } |
| 210 | up(&block_subsys_sem); |
| 211 | kfree(p); |
| 212 | |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | EXPORT_SYMBOL(unregister_blkdev); |
| 217 | |
| 218 | static struct kobj_map *bdev_map; |
| 219 | |
| 220 | /* |
| 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. |
| 224 | */ |
| 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) |
| 228 | { |
| 229 | kobj_map(bdev_map, dev, range, module, probe, lock, data); |
| 230 | } |
| 231 | |
| 232 | EXPORT_SYMBOL(blk_register_region); |
| 233 | |
| 234 | void blk_unregister_region(dev_t dev, unsigned long range) |
| 235 | { |
| 236 | kobj_unmap(bdev_map, dev, range); |
| 237 | } |
| 238 | |
| 239 | EXPORT_SYMBOL(blk_unregister_region); |
| 240 | |
| 241 | static struct kobject *exact_match(dev_t dev, int *part, void *data) |
| 242 | { |
| 243 | struct gendisk *p = data; |
| 244 | return &p->kobj; |
| 245 | } |
| 246 | |
| 247 | static int exact_lock(dev_t dev, void *data) |
| 248 | { |
| 249 | struct gendisk *p = data; |
| 250 | |
| 251 | if (!get_disk(p)) |
| 252 | return -1; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * add_disk - add partitioning information to kernel list |
| 258 | * @disk: per-device partitioning information |
| 259 | * |
| 260 | * This function registers the partitioning information in @disk |
| 261 | * with the kernel. |
| 262 | */ |
| 263 | void add_disk(struct gendisk *disk) |
| 264 | { |
| 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); |
| 268 | register_disk(disk); |
| 269 | blk_register_queue(disk); |
| 270 | } |
| 271 | |
| 272 | EXPORT_SYMBOL(add_disk); |
| 273 | EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */ |
| 274 | |
| 275 | void unlink_gendisk(struct gendisk *disk) |
| 276 | { |
| 277 | blk_unregister_queue(disk); |
| 278 | blk_unregister_region(MKDEV(disk->major, disk->first_minor), |
| 279 | disk->minors); |
| 280 | } |
| 281 | |
| 282 | #define to_disk(obj) container_of(obj,struct gendisk,kobj) |
| 283 | |
| 284 | /** |
| 285 | * get_gendisk - get partitioning information for a given device |
| 286 | * @dev: device to get partitioning information for |
| 287 | * |
| 288 | * This function gets the structure containing partitioning |
| 289 | * information for the given device @dev. |
| 290 | */ |
| 291 | struct gendisk *get_gendisk(dev_t dev, int *part) |
| 292 | { |
| 293 | struct kobject *kobj = kobj_lookup(bdev_map, dev, part); |
| 294 | return kobj ? to_disk(kobj) : NULL; |
| 295 | } |
| 296 | |
| 297 | #ifdef CONFIG_PROC_FS |
| 298 | /* iterator */ |
| 299 | static void *part_start(struct seq_file *part, loff_t *pos) |
| 300 | { |
| 301 | struct list_head *p; |
| 302 | loff_t l = *pos; |
| 303 | |
| 304 | down(&block_subsys_sem); |
| 305 | list_for_each(p, &block_subsys.kset.list) |
| 306 | if (!l--) |
| 307 | return list_entry(p, struct gendisk, kobj.entry); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | static void *part_next(struct seq_file *part, void *v, loff_t *pos) |
| 312 | { |
| 313 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 314 | ++*pos; |
| 315 | return p==&block_subsys.kset.list ? NULL : |
| 316 | list_entry(p, struct gendisk, kobj.entry); |
| 317 | } |
| 318 | |
| 319 | static void part_stop(struct seq_file *part, void *v) |
| 320 | { |
| 321 | up(&block_subsys_sem); |
| 322 | } |
| 323 | |
| 324 | static int show_partition(struct seq_file *part, void *v) |
| 325 | { |
| 326 | struct gendisk *sgp = v; |
| 327 | int n; |
| 328 | char buf[BDEVNAME_SIZE]; |
| 329 | |
| 330 | if (&sgp->kobj.entry == block_subsys.kset.list.next) |
| 331 | seq_puts(part, "major minor #blocks name\n\n"); |
| 332 | |
| 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))) |
| 336 | return 0; |
| 337 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 338 | return 0; |
| 339 | |
| 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++) { |
| 346 | if (!sgp->part[n]) |
| 347 | continue; |
| 348 | if (sgp->part[n]->nr_sects == 0) |
| 349 | continue; |
| 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)); |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | struct seq_operations partitions_op = { |
| 360 | .start =part_start, |
| 361 | .next = part_next, |
| 362 | .stop = part_stop, |
| 363 | .show = show_partition |
| 364 | }; |
| 365 | #endif |
| 366 | |
| 367 | |
| 368 | extern int blk_dev_init(void); |
| 369 | |
| 370 | static struct kobject *base_probe(dev_t dev, int *part, void *data) |
| 371 | { |
| 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)); |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | static int __init genhd_device_init(void) |
| 379 | { |
| 380 | bdev_map = kobj_map_init(base_probe, &block_subsys_sem); |
| 381 | blk_dev_init(); |
| 382 | subsystem_register(&block_subsys); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | subsys_initcall(genhd_device_init); |
| 387 | |
| 388 | |
| 389 | |
| 390 | /* |
| 391 | * kobject & sysfs bindings for block devices |
| 392 | */ |
| 393 | static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr, |
| 394 | char *page) |
| 395 | { |
| 396 | struct gendisk *disk = to_disk(kobj); |
| 397 | struct disk_attribute *disk_attr = |
| 398 | container_of(attr,struct disk_attribute,attr); |
Dmitry Torokhov | 6c1852a | 2005-04-29 01:26:06 -0500 | [diff] [blame] | 399 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | if (disk_attr->show) |
| 402 | ret = disk_attr->show(disk,page); |
| 403 | return ret; |
| 404 | } |
| 405 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 406 | static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr, |
| 407 | const char *page, size_t count) |
| 408 | { |
| 409 | struct gendisk *disk = to_disk(kobj); |
| 410 | struct disk_attribute *disk_attr = |
| 411 | container_of(attr,struct disk_attribute,attr); |
| 412 | ssize_t ret = 0; |
| 413 | |
| 414 | if (disk_attr->store) |
| 415 | ret = disk_attr->store(disk, page, count); |
| 416 | return ret; |
| 417 | } |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | static struct sysfs_ops disk_sysfs_ops = { |
| 420 | .show = &disk_attr_show, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 421 | .store = &disk_attr_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 424 | static ssize_t disk_uevent_store(struct gendisk * disk, |
| 425 | const char *buf, size_t count) |
| 426 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 427 | kobject_uevent(&disk->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 428 | return count; |
| 429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | static ssize_t disk_dev_read(struct gendisk * disk, char *page) |
| 431 | { |
| 432 | dev_t base = MKDEV(disk->major, disk->first_minor); |
| 433 | return print_dev_t(page, base); |
| 434 | } |
| 435 | static ssize_t disk_range_read(struct gendisk * disk, char *page) |
| 436 | { |
| 437 | return sprintf(page, "%d\n", disk->minors); |
| 438 | } |
| 439 | static ssize_t disk_removable_read(struct gendisk * disk, char *page) |
| 440 | { |
| 441 | return sprintf(page, "%d\n", |
| 442 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
| 443 | |
| 444 | } |
| 445 | static ssize_t disk_size_read(struct gendisk * disk, char *page) |
| 446 | { |
| 447 | return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk)); |
| 448 | } |
| 449 | |
| 450 | static ssize_t disk_stats_read(struct gendisk * disk, char *page) |
| 451 | { |
| 452 | preempt_disable(); |
| 453 | disk_round_stats(disk); |
| 454 | preempt_enable(); |
| 455 | return sprintf(page, |
| 456 | "%8u %8u %8llu %8u " |
| 457 | "%8u %8u %8llu %8u " |
| 458 | "%8u %8u %8u" |
| 459 | "\n", |
Jens Axboe | 47a0041 | 2005-11-09 13:38:47 +0100 | [diff] [blame] | 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])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | disk->in_flight, |
| 469 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 470 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 471 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 472 | static struct disk_attribute disk_attr_uevent = { |
| 473 | .attr = {.name = "uevent", .mode = S_IWUSR }, |
| 474 | .store = disk_uevent_store |
| 475 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | static struct disk_attribute disk_attr_dev = { |
| 477 | .attr = {.name = "dev", .mode = S_IRUGO }, |
| 478 | .show = disk_dev_read |
| 479 | }; |
| 480 | static struct disk_attribute disk_attr_range = { |
| 481 | .attr = {.name = "range", .mode = S_IRUGO }, |
| 482 | .show = disk_range_read |
| 483 | }; |
| 484 | static struct disk_attribute disk_attr_removable = { |
| 485 | .attr = {.name = "removable", .mode = S_IRUGO }, |
| 486 | .show = disk_removable_read |
| 487 | }; |
| 488 | static struct disk_attribute disk_attr_size = { |
| 489 | .attr = {.name = "size", .mode = S_IRUGO }, |
| 490 | .show = disk_size_read |
| 491 | }; |
| 492 | static struct disk_attribute disk_attr_stat = { |
| 493 | .attr = {.name = "stat", .mode = S_IRUGO }, |
| 494 | .show = disk_stats_read |
| 495 | }; |
| 496 | |
| 497 | static struct attribute * default_attrs[] = { |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 498 | &disk_attr_uevent.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | &disk_attr_dev.attr, |
| 500 | &disk_attr_range.attr, |
| 501 | &disk_attr_removable.attr, |
| 502 | &disk_attr_size.attr, |
| 503 | &disk_attr_stat.attr, |
| 504 | NULL, |
| 505 | }; |
| 506 | |
| 507 | static void disk_release(struct kobject * kobj) |
| 508 | { |
| 509 | struct gendisk *disk = to_disk(kobj); |
| 510 | kfree(disk->random); |
| 511 | kfree(disk->part); |
| 512 | free_disk_stats(disk); |
| 513 | kfree(disk); |
| 514 | } |
| 515 | |
| 516 | static struct kobj_type ktype_block = { |
| 517 | .release = disk_release, |
| 518 | .sysfs_ops = &disk_sysfs_ops, |
| 519 | .default_attrs = default_attrs, |
| 520 | }; |
| 521 | |
| 522 | extern struct kobj_type ktype_part; |
| 523 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 524 | static int block_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | struct kobj_type *ktype = get_ktype(kobj); |
| 527 | |
| 528 | return ((ktype == &ktype_block) || (ktype == &ktype_part)); |
| 529 | } |
| 530 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 531 | static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | int num_envp, char *buffer, int buffer_size) |
| 533 | { |
| 534 | struct kobj_type *ktype = get_ktype(kobj); |
| 535 | struct device *physdev; |
| 536 | struct gendisk *disk; |
| 537 | struct hd_struct *part; |
| 538 | int length = 0; |
| 539 | int i = 0; |
| 540 | |
| 541 | if (ktype == &ktype_block) { |
| 542 | disk = container_of(kobj, struct gendisk, kobj); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 543 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 544 | &length, "MINOR=%u", disk->first_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } else if (ktype == &ktype_part) { |
| 546 | disk = container_of(kobj->parent, struct gendisk, kobj); |
| 547 | part = container_of(kobj, struct hd_struct, kobj); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 548 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 549 | &length, "MINOR=%u", |
| 550 | disk->first_minor + part->partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } else |
| 552 | return 0; |
| 553 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 554 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, |
| 555 | "MAJOR=%u", disk->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | /* add physical device, backing this device */ |
| 558 | physdev = disk->driverfs_dev; |
| 559 | if (physdev) { |
| 560 | char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL); |
| 561 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 562 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 563 | &length, "PHYSDEVPATH=%s", path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | kfree(path); |
| 565 | |
| 566 | if (physdev->bus) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 567 | add_uevent_var(envp, num_envp, &i, |
| 568 | buffer, buffer_size, &length, |
| 569 | "PHYSDEVBUS=%s", |
| 570 | physdev->bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
| 572 | if (physdev->driver) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 573 | add_uevent_var(envp, num_envp, &i, |
| 574 | buffer, buffer_size, &length, |
| 575 | "PHYSDEVDRIVER=%s", |
| 576 | physdev->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* terminate, set to next free slot, shrink available space */ |
| 580 | envp[i] = NULL; |
| 581 | envp = &envp[i]; |
| 582 | num_envp -= i; |
| 583 | buffer = &buffer[length]; |
| 584 | buffer_size -= length; |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 589 | static struct kset_uevent_ops block_uevent_ops = { |
| 590 | .filter = block_uevent_filter, |
| 591 | .uevent = block_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | /* declare block_subsys. */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 595 | static decl_subsys(block, &ktype_block, &block_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | |
| 598 | /* |
| 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. |
| 603 | * |
| 604 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 605 | * extra fields. |
| 606 | */ |
| 607 | |
| 608 | /* iterator */ |
| 609 | static void *diskstats_start(struct seq_file *part, loff_t *pos) |
| 610 | { |
| 611 | loff_t k = *pos; |
| 612 | struct list_head *p; |
| 613 | |
| 614 | down(&block_subsys_sem); |
| 615 | list_for_each(p, &block_subsys.kset.list) |
| 616 | if (!k--) |
| 617 | return list_entry(p, struct gendisk, kobj.entry); |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos) |
| 622 | { |
| 623 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 624 | ++*pos; |
| 625 | return p==&block_subsys.kset.list ? NULL : |
| 626 | list_entry(p, struct gendisk, kobj.entry); |
| 627 | } |
| 628 | |
| 629 | static void diskstats_stop(struct seq_file *part, void *v) |
| 630 | { |
| 631 | up(&block_subsys_sem); |
| 632 | } |
| 633 | |
| 634 | static int diskstats_show(struct seq_file *s, void *v) |
| 635 | { |
| 636 | struct gendisk *gp = v; |
| 637 | char buf[BDEVNAME_SIZE]; |
| 638 | int n = 0; |
| 639 | |
| 640 | /* |
| 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" |
| 645 | "\n\n"); |
| 646 | */ |
| 647 | |
| 648 | preempt_disable(); |
| 649 | disk_round_stats(gp); |
| 650 | preempt_enable(); |
| 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), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 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])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | gp->in_flight, |
| 660 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 661 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 662 | |
| 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]; |
| 666 | |
| 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), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 671 | hd->ios[0], hd->sectors[0], |
| 672 | hd->ios[1], hd->sectors[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | struct seq_operations diskstats_op = { |
| 679 | .start = diskstats_start, |
| 680 | .next = diskstats_next, |
| 681 | .stop = diskstats_stop, |
| 682 | .show = diskstats_show |
| 683 | }; |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | struct gendisk *alloc_disk(int minors) |
| 686 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 687 | return alloc_disk_node(minors, -1); |
| 688 | } |
| 689 | |
| 690 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 691 | { |
| 692 | struct gendisk *disk; |
| 693 | |
| 694 | disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if (disk) { |
| 696 | memset(disk, 0, sizeof(struct gendisk)); |
| 697 | if (!init_disk_stats(disk)) { |
| 698 | kfree(disk); |
| 699 | return NULL; |
| 700 | } |
| 701 | if (minors > 1) { |
| 702 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 703 | disk->part = kmalloc_node(size, GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | if (!disk->part) { |
| 705 | kfree(disk); |
| 706 | return NULL; |
| 707 | } |
| 708 | memset(disk->part, 0, size); |
| 709 | } |
| 710 | disk->minors = minors; |
| 711 | kobj_set_kset_s(disk,block_subsys); |
| 712 | kobject_init(&disk->kobj); |
| 713 | rand_initialize_disk(disk); |
| 714 | } |
| 715 | return disk; |
| 716 | } |
| 717 | |
| 718 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 719 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | struct kobject *get_disk(struct gendisk *disk) |
| 722 | { |
| 723 | struct module *owner; |
| 724 | struct kobject *kobj; |
| 725 | |
| 726 | if (!disk->fops) |
| 727 | return NULL; |
| 728 | owner = disk->fops->owner; |
| 729 | if (owner && !try_module_get(owner)) |
| 730 | return NULL; |
| 731 | kobj = kobject_get(&disk->kobj); |
| 732 | if (kobj == NULL) { |
| 733 | module_put(owner); |
| 734 | return NULL; |
| 735 | } |
| 736 | return kobj; |
| 737 | |
| 738 | } |
| 739 | |
| 740 | EXPORT_SYMBOL(get_disk); |
| 741 | |
| 742 | void put_disk(struct gendisk *disk) |
| 743 | { |
| 744 | if (disk) |
| 745 | kobject_put(&disk->kobj); |
| 746 | } |
| 747 | |
| 748 | EXPORT_SYMBOL(put_disk); |
| 749 | |
| 750 | void set_device_ro(struct block_device *bdev, int flag) |
| 751 | { |
| 752 | if (bdev->bd_contains != bdev) |
| 753 | bdev->bd_part->policy = flag; |
| 754 | else |
| 755 | bdev->bd_disk->policy = flag; |
| 756 | } |
| 757 | |
| 758 | EXPORT_SYMBOL(set_device_ro); |
| 759 | |
| 760 | void set_disk_ro(struct gendisk *disk, int flag) |
| 761 | { |
| 762 | int i; |
| 763 | disk->policy = flag; |
| 764 | for (i = 0; i < disk->minors - 1; i++) |
| 765 | if (disk->part[i]) disk->part[i]->policy = flag; |
| 766 | } |
| 767 | |
| 768 | EXPORT_SYMBOL(set_disk_ro); |
| 769 | |
| 770 | int bdev_read_only(struct block_device *bdev) |
| 771 | { |
| 772 | if (!bdev) |
| 773 | return 0; |
| 774 | else if (bdev->bd_contains != bdev) |
| 775 | return bdev->bd_part->policy; |
| 776 | else |
| 777 | return bdev->bd_disk->policy; |
| 778 | } |
| 779 | |
| 780 | EXPORT_SYMBOL(bdev_read_only); |
| 781 | |
| 782 | int invalidate_partition(struct gendisk *disk, int index) |
| 783 | { |
| 784 | int res = 0; |
| 785 | struct block_device *bdev = bdget_disk(disk, index); |
| 786 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 787 | fsync_bdev(bdev); |
| 788 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | bdput(bdev); |
| 790 | } |
| 791 | return res; |
| 792 | } |
| 793 | |
| 794 | EXPORT_SYMBOL(invalidate_partition); |