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 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 20 | struct subsystem 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 | |
| 216 | #ifdef CONFIG_PROC_FS |
| 217 | /* iterator */ |
| 218 | static void *part_start(struct seq_file *part, loff_t *pos) |
| 219 | { |
| 220 | struct list_head *p; |
| 221 | loff_t l = *pos; |
| 222 | |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 223 | mutex_lock(&block_subsys_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | list_for_each(p, &block_subsys.kset.list) |
| 225 | if (!l--) |
| 226 | return list_entry(p, struct gendisk, kobj.entry); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static void *part_next(struct seq_file *part, void *v, loff_t *pos) |
| 231 | { |
| 232 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 233 | ++*pos; |
| 234 | return p==&block_subsys.kset.list ? NULL : |
| 235 | list_entry(p, struct gendisk, kobj.entry); |
| 236 | } |
| 237 | |
| 238 | static void part_stop(struct seq_file *part, void *v) |
| 239 | { |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 240 | mutex_unlock(&block_subsys_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static int show_partition(struct seq_file *part, void *v) |
| 244 | { |
| 245 | struct gendisk *sgp = v; |
| 246 | int n; |
| 247 | char buf[BDEVNAME_SIZE]; |
| 248 | |
| 249 | if (&sgp->kobj.entry == block_subsys.kset.list.next) |
| 250 | seq_puts(part, "major minor #blocks name\n\n"); |
| 251 | |
| 252 | /* Don't show non-partitionable removeable devices or empty devices */ |
| 253 | if (!get_capacity(sgp) || |
| 254 | (sgp->minors == 1 && (sgp->flags & GENHD_FL_REMOVABLE))) |
| 255 | return 0; |
| 256 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 257 | return 0; |
| 258 | |
| 259 | /* show the full disk and all non-0 size partitions of it */ |
| 260 | seq_printf(part, "%4d %4d %10llu %s\n", |
| 261 | sgp->major, sgp->first_minor, |
| 262 | (unsigned long long)get_capacity(sgp) >> 1, |
| 263 | disk_name(sgp, 0, buf)); |
| 264 | for (n = 0; n < sgp->minors - 1; n++) { |
| 265 | if (!sgp->part[n]) |
| 266 | continue; |
| 267 | if (sgp->part[n]->nr_sects == 0) |
| 268 | continue; |
| 269 | seq_printf(part, "%4d %4d %10llu %s\n", |
| 270 | sgp->major, n + 1 + sgp->first_minor, |
| 271 | (unsigned long long)sgp->part[n]->nr_sects >> 1 , |
| 272 | disk_name(sgp, n + 1, buf)); |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | struct seq_operations partitions_op = { |
| 279 | .start =part_start, |
| 280 | .next = part_next, |
| 281 | .stop = part_stop, |
| 282 | .show = show_partition |
| 283 | }; |
| 284 | #endif |
| 285 | |
| 286 | |
| 287 | extern int blk_dev_init(void); |
| 288 | |
| 289 | static struct kobject *base_probe(dev_t dev, int *part, void *data) |
| 290 | { |
| 291 | if (request_module("block-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0) |
| 292 | /* Make old-style 2.4 aliases work */ |
| 293 | request_module("block-major-%d", MAJOR(dev)); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | static int __init genhd_device_init(void) |
| 298 | { |
Randy Dunlap | 87a5726 | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 299 | int err; |
| 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(); |
Randy Dunlap | 87a5726 | 2006-09-29 01:58:56 -0700 | [diff] [blame] | 303 | err = subsystem_register(&block_subsys); |
| 304 | if (err < 0) |
| 305 | printk(KERN_WARNING "%s: subsystem_register error: %d\n", |
| 306 | __FUNCTION__, err); |
| 307 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | subsys_initcall(genhd_device_init); |
| 311 | |
| 312 | |
| 313 | |
| 314 | /* |
| 315 | * kobject & sysfs bindings for block devices |
| 316 | */ |
| 317 | static ssize_t disk_attr_show(struct kobject *kobj, struct attribute *attr, |
| 318 | char *page) |
| 319 | { |
| 320 | struct gendisk *disk = to_disk(kobj); |
| 321 | struct disk_attribute *disk_attr = |
| 322 | container_of(attr,struct disk_attribute,attr); |
Dmitry Torokhov | 6c1852a | 2005-04-29 01:26:06 -0500 | [diff] [blame] | 323 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | if (disk_attr->show) |
| 326 | ret = disk_attr->show(disk,page); |
| 327 | return ret; |
| 328 | } |
| 329 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 330 | static ssize_t disk_attr_store(struct kobject * kobj, struct attribute * attr, |
| 331 | const char *page, size_t count) |
| 332 | { |
| 333 | struct gendisk *disk = to_disk(kobj); |
| 334 | struct disk_attribute *disk_attr = |
| 335 | container_of(attr,struct disk_attribute,attr); |
| 336 | ssize_t ret = 0; |
| 337 | |
| 338 | if (disk_attr->store) |
| 339 | ret = disk_attr->store(disk, page, count); |
| 340 | return ret; |
| 341 | } |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | static struct sysfs_ops disk_sysfs_ops = { |
| 344 | .show = &disk_attr_show, |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 345 | .store = &disk_attr_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 348 | static ssize_t disk_uevent_store(struct gendisk * disk, |
| 349 | const char *buf, size_t count) |
| 350 | { |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 351 | kobject_uevent(&disk->kobj, KOBJ_ADD); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 352 | return count; |
| 353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | static ssize_t disk_dev_read(struct gendisk * disk, char *page) |
| 355 | { |
| 356 | dev_t base = MKDEV(disk->major, disk->first_minor); |
| 357 | return print_dev_t(page, base); |
| 358 | } |
| 359 | static ssize_t disk_range_read(struct gendisk * disk, char *page) |
| 360 | { |
| 361 | return sprintf(page, "%d\n", disk->minors); |
| 362 | } |
| 363 | static ssize_t disk_removable_read(struct gendisk * disk, char *page) |
| 364 | { |
| 365 | return sprintf(page, "%d\n", |
| 366 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
| 367 | |
| 368 | } |
| 369 | static ssize_t disk_size_read(struct gendisk * disk, char *page) |
| 370 | { |
| 371 | return sprintf(page, "%llu\n", (unsigned long long)get_capacity(disk)); |
| 372 | } |
| 373 | |
| 374 | static ssize_t disk_stats_read(struct gendisk * disk, char *page) |
| 375 | { |
| 376 | preempt_disable(); |
| 377 | disk_round_stats(disk); |
| 378 | preempt_enable(); |
| 379 | return sprintf(page, |
Ben Woodard | 837c787 | 2006-03-22 08:09:31 +0100 | [diff] [blame] | 380 | "%8lu %8lu %8llu %8u " |
| 381 | "%8lu %8lu %8llu %8u " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | "%8u %8u %8u" |
| 383 | "\n", |
Jens Axboe | 47a0041 | 2005-11-09 13:38:47 +0100 | [diff] [blame] | 384 | disk_stat_read(disk, ios[READ]), |
| 385 | disk_stat_read(disk, merges[READ]), |
| 386 | (unsigned long long)disk_stat_read(disk, sectors[READ]), |
| 387 | jiffies_to_msecs(disk_stat_read(disk, ticks[READ])), |
| 388 | disk_stat_read(disk, ios[WRITE]), |
| 389 | disk_stat_read(disk, merges[WRITE]), |
| 390 | (unsigned long long)disk_stat_read(disk, sectors[WRITE]), |
| 391 | jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | disk->in_flight, |
| 393 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 394 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 395 | } |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 396 | static struct disk_attribute disk_attr_uevent = { |
| 397 | .attr = {.name = "uevent", .mode = S_IWUSR }, |
| 398 | .store = disk_uevent_store |
| 399 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | static struct disk_attribute disk_attr_dev = { |
| 401 | .attr = {.name = "dev", .mode = S_IRUGO }, |
| 402 | .show = disk_dev_read |
| 403 | }; |
| 404 | static struct disk_attribute disk_attr_range = { |
| 405 | .attr = {.name = "range", .mode = S_IRUGO }, |
| 406 | .show = disk_range_read |
| 407 | }; |
| 408 | static struct disk_attribute disk_attr_removable = { |
| 409 | .attr = {.name = "removable", .mode = S_IRUGO }, |
| 410 | .show = disk_removable_read |
| 411 | }; |
| 412 | static struct disk_attribute disk_attr_size = { |
| 413 | .attr = {.name = "size", .mode = S_IRUGO }, |
| 414 | .show = disk_size_read |
| 415 | }; |
| 416 | static struct disk_attribute disk_attr_stat = { |
| 417 | .attr = {.name = "stat", .mode = S_IRUGO }, |
| 418 | .show = disk_stats_read |
| 419 | }; |
| 420 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 421 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 422 | |
| 423 | static ssize_t disk_fail_store(struct gendisk * disk, |
| 424 | const char *buf, size_t count) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | if (count > 0 && sscanf(buf, "%d", &i) > 0) { |
| 429 | if (i == 0) |
| 430 | disk->flags &= ~GENHD_FL_FAIL; |
| 431 | else |
| 432 | disk->flags |= GENHD_FL_FAIL; |
| 433 | } |
| 434 | |
| 435 | return count; |
| 436 | } |
| 437 | static ssize_t disk_fail_read(struct gendisk * disk, char *page) |
| 438 | { |
| 439 | return sprintf(page, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0); |
| 440 | } |
| 441 | static struct disk_attribute disk_attr_fail = { |
| 442 | .attr = {.name = "make-it-fail", .mode = S_IRUGO | S_IWUSR }, |
| 443 | .store = disk_fail_store, |
| 444 | .show = disk_fail_read |
| 445 | }; |
| 446 | |
| 447 | #endif |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | static struct attribute * default_attrs[] = { |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 450 | &disk_attr_uevent.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | &disk_attr_dev.attr, |
| 452 | &disk_attr_range.attr, |
| 453 | &disk_attr_removable.attr, |
| 454 | &disk_attr_size.attr, |
| 455 | &disk_attr_stat.attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 456 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 457 | &disk_attr_fail.attr, |
| 458 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | NULL, |
| 460 | }; |
| 461 | |
| 462 | static void disk_release(struct kobject * kobj) |
| 463 | { |
| 464 | struct gendisk *disk = to_disk(kobj); |
| 465 | kfree(disk->random); |
| 466 | kfree(disk->part); |
| 467 | free_disk_stats(disk); |
| 468 | kfree(disk); |
| 469 | } |
| 470 | |
| 471 | static struct kobj_type ktype_block = { |
| 472 | .release = disk_release, |
| 473 | .sysfs_ops = &disk_sysfs_ops, |
| 474 | .default_attrs = default_attrs, |
| 475 | }; |
| 476 | |
| 477 | extern struct kobj_type ktype_part; |
| 478 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 479 | static int block_uevent_filter(struct kset *kset, struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | struct kobj_type *ktype = get_ktype(kobj); |
| 482 | |
| 483 | return ((ktype == &ktype_block) || (ktype == &ktype_part)); |
| 484 | } |
| 485 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 486 | static int block_uevent(struct kset *kset, struct kobject *kobj, char **envp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | int num_envp, char *buffer, int buffer_size) |
| 488 | { |
| 489 | struct kobj_type *ktype = get_ktype(kobj); |
| 490 | struct device *physdev; |
| 491 | struct gendisk *disk; |
| 492 | struct hd_struct *part; |
| 493 | int length = 0; |
| 494 | int i = 0; |
| 495 | |
| 496 | if (ktype == &ktype_block) { |
| 497 | disk = container_of(kobj, struct gendisk, kobj); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 498 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 499 | &length, "MINOR=%u", disk->first_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } else if (ktype == &ktype_part) { |
| 501 | disk = container_of(kobj->parent, struct gendisk, kobj); |
| 502 | part = container_of(kobj, struct hd_struct, kobj); |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 503 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 504 | &length, "MINOR=%u", |
| 505 | disk->first_minor + part->partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } else |
| 507 | return 0; |
| 508 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 509 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, |
| 510 | "MAJOR=%u", disk->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | /* add physical device, backing this device */ |
| 513 | physdev = disk->driverfs_dev; |
| 514 | if (physdev) { |
| 515 | char *path = kobject_get_path(&physdev->kobj, GFP_KERNEL); |
| 516 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 517 | add_uevent_var(envp, num_envp, &i, buffer, buffer_size, |
| 518 | &length, "PHYSDEVPATH=%s", path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | kfree(path); |
| 520 | |
| 521 | if (physdev->bus) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 522 | add_uevent_var(envp, num_envp, &i, |
| 523 | buffer, buffer_size, &length, |
| 524 | "PHYSDEVBUS=%s", |
| 525 | physdev->bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | if (physdev->driver) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 528 | add_uevent_var(envp, num_envp, &i, |
| 529 | buffer, buffer_size, &length, |
| 530 | "PHYSDEVDRIVER=%s", |
| 531 | physdev->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* terminate, set to next free slot, shrink available space */ |
| 535 | envp[i] = NULL; |
| 536 | envp = &envp[i]; |
| 537 | num_envp -= i; |
| 538 | buffer = &buffer[length]; |
| 539 | buffer_size -= length; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 544 | static struct kset_uevent_ops block_uevent_ops = { |
| 545 | .filter = block_uevent_filter, |
| 546 | .uevent = block_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 549 | decl_subsys(block, &ktype_block, &block_uevent_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 553 | * entries do, above, but makes them available through one seq_file. |
| 554 | * Watching a few disks may be efficient through sysfs, but watching |
| 555 | * all of them will be more efficient through this interface. |
| 556 | * |
| 557 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 558 | * extra fields. |
| 559 | */ |
| 560 | |
| 561 | /* iterator */ |
| 562 | static void *diskstats_start(struct seq_file *part, loff_t *pos) |
| 563 | { |
| 564 | loff_t k = *pos; |
| 565 | struct list_head *p; |
| 566 | |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 567 | mutex_lock(&block_subsys_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | list_for_each(p, &block_subsys.kset.list) |
| 569 | if (!k--) |
| 570 | return list_entry(p, struct gendisk, kobj.entry); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | static void *diskstats_next(struct seq_file *part, void *v, loff_t *pos) |
| 575 | { |
| 576 | struct list_head *p = ((struct gendisk *)v)->kobj.entry.next; |
| 577 | ++*pos; |
| 578 | return p==&block_subsys.kset.list ? NULL : |
| 579 | list_entry(p, struct gendisk, kobj.entry); |
| 580 | } |
| 581 | |
| 582 | static void diskstats_stop(struct seq_file *part, void *v) |
| 583 | { |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 584 | mutex_unlock(&block_subsys_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int diskstats_show(struct seq_file *s, void *v) |
| 588 | { |
| 589 | struct gendisk *gp = v; |
| 590 | char buf[BDEVNAME_SIZE]; |
| 591 | int n = 0; |
| 592 | |
| 593 | /* |
| 594 | if (&sgp->kobj.entry == block_subsys.kset.list.next) |
| 595 | seq_puts(s, "major minor name" |
| 596 | " rio rmerge rsect ruse wio wmerge " |
| 597 | "wsect wuse running use aveq" |
| 598 | "\n\n"); |
| 599 | */ |
| 600 | |
| 601 | preempt_disable(); |
| 602 | disk_round_stats(gp); |
| 603 | preempt_enable(); |
Ben Woodard | 837c787 | 2006-03-22 08:09:31 +0100 | [diff] [blame] | 604 | 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] | 605 | gp->major, n + gp->first_minor, disk_name(gp, n, buf), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 606 | disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]), |
| 607 | (unsigned long long)disk_stat_read(gp, sectors[0]), |
| 608 | jiffies_to_msecs(disk_stat_read(gp, ticks[0])), |
| 609 | disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]), |
| 610 | (unsigned long long)disk_stat_read(gp, sectors[1]), |
| 611 | jiffies_to_msecs(disk_stat_read(gp, ticks[1])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | gp->in_flight, |
| 613 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 614 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 615 | |
| 616 | /* now show all non-0 size partitions of it */ |
| 617 | for (n = 0; n < gp->minors - 1; n++) { |
| 618 | struct hd_struct *hd = gp->part[n]; |
| 619 | |
| 620 | if (hd && hd->nr_sects) |
| 621 | seq_printf(s, "%4d %4d %s %u %u %u %u\n", |
| 622 | gp->major, n + gp->first_minor + 1, |
| 623 | disk_name(gp, n + 1, buf), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 624 | hd->ios[0], hd->sectors[0], |
| 625 | hd->ios[1], hd->sectors[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | struct seq_operations diskstats_op = { |
| 632 | .start = diskstats_start, |
| 633 | .next = diskstats_next, |
| 634 | .stop = diskstats_stop, |
| 635 | .show = diskstats_show |
| 636 | }; |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | struct gendisk *alloc_disk(int minors) |
| 639 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 640 | return alloc_disk_node(minors, -1); |
| 641 | } |
| 642 | |
| 643 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 644 | { |
| 645 | struct gendisk *disk; |
| 646 | |
| 647 | disk = kmalloc_node(sizeof(struct gendisk), GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (disk) { |
| 649 | memset(disk, 0, sizeof(struct gendisk)); |
| 650 | if (!init_disk_stats(disk)) { |
| 651 | kfree(disk); |
| 652 | return NULL; |
| 653 | } |
| 654 | if (minors > 1) { |
| 655 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 656 | disk->part = kmalloc_node(size, GFP_KERNEL, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (!disk->part) { |
| 658 | kfree(disk); |
| 659 | return NULL; |
| 660 | } |
| 661 | memset(disk->part, 0, size); |
| 662 | } |
| 663 | disk->minors = minors; |
| 664 | kobj_set_kset_s(disk,block_subsys); |
| 665 | kobject_init(&disk->kobj); |
| 666 | rand_initialize_disk(disk); |
| 667 | } |
| 668 | return disk; |
| 669 | } |
| 670 | |
| 671 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 672 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | struct kobject *get_disk(struct gendisk *disk) |
| 675 | { |
| 676 | struct module *owner; |
| 677 | struct kobject *kobj; |
| 678 | |
| 679 | if (!disk->fops) |
| 680 | return NULL; |
| 681 | owner = disk->fops->owner; |
| 682 | if (owner && !try_module_get(owner)) |
| 683 | return NULL; |
| 684 | kobj = kobject_get(&disk->kobj); |
| 685 | if (kobj == NULL) { |
| 686 | module_put(owner); |
| 687 | return NULL; |
| 688 | } |
| 689 | return kobj; |
| 690 | |
| 691 | } |
| 692 | |
| 693 | EXPORT_SYMBOL(get_disk); |
| 694 | |
| 695 | void put_disk(struct gendisk *disk) |
| 696 | { |
| 697 | if (disk) |
| 698 | kobject_put(&disk->kobj); |
| 699 | } |
| 700 | |
| 701 | EXPORT_SYMBOL(put_disk); |
| 702 | |
| 703 | void set_device_ro(struct block_device *bdev, int flag) |
| 704 | { |
| 705 | if (bdev->bd_contains != bdev) |
| 706 | bdev->bd_part->policy = flag; |
| 707 | else |
| 708 | bdev->bd_disk->policy = flag; |
| 709 | } |
| 710 | |
| 711 | EXPORT_SYMBOL(set_device_ro); |
| 712 | |
| 713 | void set_disk_ro(struct gendisk *disk, int flag) |
| 714 | { |
| 715 | int i; |
| 716 | disk->policy = flag; |
| 717 | for (i = 0; i < disk->minors - 1; i++) |
| 718 | if (disk->part[i]) disk->part[i]->policy = flag; |
| 719 | } |
| 720 | |
| 721 | EXPORT_SYMBOL(set_disk_ro); |
| 722 | |
| 723 | int bdev_read_only(struct block_device *bdev) |
| 724 | { |
| 725 | if (!bdev) |
| 726 | return 0; |
| 727 | else if (bdev->bd_contains != bdev) |
| 728 | return bdev->bd_part->policy; |
| 729 | else |
| 730 | return bdev->bd_disk->policy; |
| 731 | } |
| 732 | |
| 733 | EXPORT_SYMBOL(bdev_read_only); |
| 734 | |
| 735 | int invalidate_partition(struct gendisk *disk, int index) |
| 736 | { |
| 737 | int res = 0; |
| 738 | struct block_device *bdev = bdget_disk(disk, index); |
| 739 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 740 | fsync_bdev(bdev); |
| 741 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | bdput(bdev); |
| 743 | } |
| 744 | return res; |
| 745 | } |
| 746 | |
| 747 | EXPORT_SYMBOL(invalidate_partition); |