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