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