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