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