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