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