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 | |
Adrian Bunk | ff88972 | 2008-03-04 11:23:45 +0100 | [diff] [blame] | 20 | #include "blk.h" |
| 21 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 22 | static DEFINE_MUTEX(block_class_lock); |
| 23 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 24 | struct kobject *block_depr; |
| 25 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 27 | static struct device_type disk_type; |
| 28 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 29 | /** |
| 30 | * disk_get_part - get partition |
| 31 | * @disk: disk to look partition from |
| 32 | * @partno: partition number |
| 33 | * |
| 34 | * Look for partition @partno from @disk. If found, increment |
| 35 | * reference count and return it. |
| 36 | * |
| 37 | * CONTEXT: |
| 38 | * Don't care. |
| 39 | * |
| 40 | * RETURNS: |
| 41 | * Pointer to the found partition on success, NULL if not found. |
| 42 | */ |
| 43 | struct hd_struct *disk_get_part(struct gendisk *disk, int partno) |
| 44 | { |
| 45 | struct hd_struct *part; |
| 46 | |
| 47 | if (unlikely(partno < 1 || partno > disk_max_parts(disk))) |
| 48 | return NULL; |
| 49 | rcu_read_lock(); |
| 50 | part = rcu_dereference(disk->__part[partno - 1]); |
| 51 | if (part) |
| 52 | get_device(&part->dev); |
| 53 | rcu_read_unlock(); |
| 54 | |
| 55 | return part; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(disk_get_part); |
| 58 | |
| 59 | /** |
| 60 | * disk_part_iter_init - initialize partition iterator |
| 61 | * @piter: iterator to initialize |
| 62 | * @disk: disk to iterate over |
| 63 | * @flags: DISK_PITER_* flags |
| 64 | * |
| 65 | * Initialize @piter so that it iterates over partitions of @disk. |
| 66 | * |
| 67 | * CONTEXT: |
| 68 | * Don't care. |
| 69 | */ |
| 70 | void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk, |
| 71 | unsigned int flags) |
| 72 | { |
| 73 | piter->disk = disk; |
| 74 | piter->part = NULL; |
| 75 | |
| 76 | if (flags & DISK_PITER_REVERSE) |
| 77 | piter->idx = disk_max_parts(piter->disk) - 1; |
| 78 | else |
| 79 | piter->idx = 0; |
| 80 | |
| 81 | piter->flags = flags; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(disk_part_iter_init); |
| 84 | |
| 85 | /** |
| 86 | * disk_part_iter_next - proceed iterator to the next partition and return it |
| 87 | * @piter: iterator of interest |
| 88 | * |
| 89 | * Proceed @piter to the next partition and return it. |
| 90 | * |
| 91 | * CONTEXT: |
| 92 | * Don't care. |
| 93 | */ |
| 94 | struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter) |
| 95 | { |
| 96 | int inc, end; |
| 97 | |
| 98 | /* put the last partition */ |
| 99 | disk_put_part(piter->part); |
| 100 | piter->part = NULL; |
| 101 | |
| 102 | rcu_read_lock(); |
| 103 | |
| 104 | /* determine iteration parameters */ |
| 105 | if (piter->flags & DISK_PITER_REVERSE) { |
| 106 | inc = -1; |
| 107 | end = -1; |
| 108 | } else { |
| 109 | inc = 1; |
| 110 | end = disk_max_parts(piter->disk); |
| 111 | } |
| 112 | |
| 113 | /* iterate to the next partition */ |
| 114 | for (; piter->idx != end; piter->idx += inc) { |
| 115 | struct hd_struct *part; |
| 116 | |
| 117 | part = rcu_dereference(piter->disk->__part[piter->idx]); |
| 118 | if (!part) |
| 119 | continue; |
| 120 | if (!(piter->flags & DISK_PITER_INCL_EMPTY) && !part->nr_sects) |
| 121 | continue; |
| 122 | |
| 123 | get_device(&part->dev); |
| 124 | piter->part = part; |
| 125 | piter->idx += inc; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | rcu_read_unlock(); |
| 130 | |
| 131 | return piter->part; |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(disk_part_iter_next); |
| 134 | |
| 135 | /** |
| 136 | * disk_part_iter_exit - finish up partition iteration |
| 137 | * @piter: iter of interest |
| 138 | * |
| 139 | * Called when iteration is over. Cleans up @piter. |
| 140 | * |
| 141 | * CONTEXT: |
| 142 | * Don't care. |
| 143 | */ |
| 144 | void disk_part_iter_exit(struct disk_part_iter *piter) |
| 145 | { |
| 146 | disk_put_part(piter->part); |
| 147 | piter->part = NULL; |
| 148 | } |
| 149 | EXPORT_SYMBOL_GPL(disk_part_iter_exit); |
| 150 | |
| 151 | /** |
| 152 | * disk_map_sector_rcu - map sector to partition |
| 153 | * @disk: gendisk of interest |
| 154 | * @sector: sector to map |
| 155 | * |
| 156 | * Find out which partition @sector maps to on @disk. This is |
| 157 | * primarily used for stats accounting. |
| 158 | * |
| 159 | * CONTEXT: |
| 160 | * RCU read locked. The returned partition pointer is valid only |
| 161 | * while preemption is disabled. |
| 162 | * |
| 163 | * RETURNS: |
| 164 | * Found partition on success, NULL if there's no matching partition. |
| 165 | */ |
| 166 | struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector) |
| 167 | { |
| 168 | int i; |
| 169 | |
| 170 | for (i = 0; i < disk_max_parts(disk); i++) { |
| 171 | struct hd_struct *part = rcu_dereference(disk->__part[i]); |
| 172 | |
| 173 | if (part && part->start_sect <= sector && |
| 174 | sector < part->start_sect + part->nr_sects) |
| 175 | return part; |
| 176 | } |
| 177 | return NULL; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(disk_map_sector_rcu); |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* |
| 182 | * Can be deleted altogether. Later. |
| 183 | * |
| 184 | */ |
| 185 | static struct blk_major_name { |
| 186 | struct blk_major_name *next; |
| 187 | int major; |
| 188 | char name[16]; |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 189 | } *major_names[BLKDEV_MAJOR_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | /* index in the above - for now: assume no multimajor ranges */ |
| 192 | static inline int major_to_index(int major) |
| 193 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 194 | return major % BLKDEV_MAJOR_HASH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 197 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 198 | void blkdev_show(struct seq_file *seqf, off_t offset) |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 199 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 200 | struct blk_major_name *dp; |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 201 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 202 | if (offset < BLKDEV_MAJOR_HASH_SIZE) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 203 | mutex_lock(&block_class_lock); |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 204 | for (dp = major_names[offset]; dp; dp = dp->next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 205 | seq_printf(seqf, "%3d %s\n", dp->major, dp->name); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 206 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 209 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | int register_blkdev(unsigned int major, const char *name) |
| 212 | { |
| 213 | struct blk_major_name **n, *p; |
| 214 | int index, ret = 0; |
| 215 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 216 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | /* temporary */ |
| 219 | if (major == 0) { |
| 220 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 221 | if (major_names[index] == NULL) |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | if (index == 0) { |
| 226 | printk("register_blkdev: failed to get major for %s\n", |
| 227 | name); |
| 228 | ret = -EBUSY; |
| 229 | goto out; |
| 230 | } |
| 231 | major = index; |
| 232 | ret = major; |
| 233 | } |
| 234 | |
| 235 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 236 | if (p == NULL) { |
| 237 | ret = -ENOMEM; |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | p->major = major; |
| 242 | strlcpy(p->name, name, sizeof(p->name)); |
| 243 | p->next = NULL; |
| 244 | index = major_to_index(major); |
| 245 | |
| 246 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 247 | if ((*n)->major == major) |
| 248 | break; |
| 249 | } |
| 250 | if (!*n) |
| 251 | *n = p; |
| 252 | else |
| 253 | ret = -EBUSY; |
| 254 | |
| 255 | if (ret < 0) { |
| 256 | printk("register_blkdev: cannot get major %d for %s\n", |
| 257 | major, name); |
| 258 | kfree(p); |
| 259 | } |
| 260 | out: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 261 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | EXPORT_SYMBOL(register_blkdev); |
| 266 | |
Akinobu Mita | f448024 | 2007-07-17 04:03:47 -0700 | [diff] [blame] | 267 | void unregister_blkdev(unsigned int major, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | struct blk_major_name **n; |
| 270 | struct blk_major_name *p = NULL; |
| 271 | int index = major_to_index(major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 273 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 275 | if ((*n)->major == major) |
| 276 | break; |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 277 | if (!*n || strcmp((*n)->name, name)) { |
| 278 | WARN_ON(1); |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 279 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | p = *n; |
| 281 | *n = p->next; |
| 282 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 283 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | EXPORT_SYMBOL(unregister_blkdev); |
| 288 | |
| 289 | static struct kobj_map *bdev_map; |
| 290 | |
| 291 | /* |
| 292 | * Register device numbers dev..(dev+range-1) |
| 293 | * range must be nonzero |
| 294 | * The hash chain is sorted on range, so that subranges can override. |
| 295 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 296 | void blk_register_region(dev_t devt, unsigned long range, struct module *module, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | struct kobject *(*probe)(dev_t, int *, void *), |
| 298 | int (*lock)(dev_t, void *), void *data) |
| 299 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 300 | kobj_map(bdev_map, devt, range, module, probe, lock, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | EXPORT_SYMBOL(blk_register_region); |
| 304 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 305 | void blk_unregister_region(dev_t devt, unsigned long range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 307 | kobj_unmap(bdev_map, devt, range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | EXPORT_SYMBOL(blk_unregister_region); |
| 311 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 312 | static struct kobject *exact_match(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | struct gendisk *p = data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 315 | |
| 316 | return &p->dev.kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 319 | static int exact_lock(dev_t devt, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | struct gendisk *p = data; |
| 322 | |
| 323 | if (!get_disk(p)) |
| 324 | return -1; |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * add_disk - add partitioning information to kernel list |
| 330 | * @disk: per-device partitioning information |
| 331 | * |
| 332 | * This function registers the partitioning information in @disk |
| 333 | * with the kernel. |
| 334 | */ |
| 335 | void add_disk(struct gendisk *disk) |
| 336 | { |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 337 | struct backing_dev_info *bdi; |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 338 | int retval; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | disk->flags |= GENHD_FL_UP; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 341 | disk->dev.devt = MKDEV(disk->major, disk->first_minor); |
| 342 | blk_register_region(disk_devt(disk), disk->minors, NULL, |
| 343 | exact_match, exact_lock, disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | register_disk(disk); |
| 345 | blk_register_queue(disk); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 346 | |
| 347 | bdi = &disk->queue->backing_dev_info; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 348 | bdi_register_dev(bdi, disk_devt(disk)); |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 349 | retval = sysfs_create_link(&disk->dev.kobj, &bdi->dev->kobj, "bdi"); |
| 350 | WARN_ON(retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | EXPORT_SYMBOL(add_disk); |
| 354 | EXPORT_SYMBOL(del_gendisk); /* in partitions/check.c */ |
| 355 | |
| 356 | void unlink_gendisk(struct gendisk *disk) |
| 357 | { |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 358 | sysfs_remove_link(&disk->dev.kobj, "bdi"); |
| 359 | bdi_unregister(&disk->queue->backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | blk_unregister_queue(disk); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 361 | blk_unregister_region(disk_devt(disk), disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /** |
| 365 | * get_gendisk - get partitioning information for a given device |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 366 | * @devt: device to get partitioning information for |
| 367 | * @part: returned partition index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | * |
| 369 | * This function gets the structure containing partitioning |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 370 | * information for the given device @devt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 372 | struct gendisk *get_gendisk(dev_t devt, int *partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 374 | struct kobject *kobj = kobj_lookup(bdev_map, devt, partno); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 375 | struct device *dev = kobj_to_dev(kobj); |
| 376 | |
| 377 | return kobj ? dev_to_disk(dev) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 380 | /** |
| 381 | * bdget_disk - do bdget() by gendisk and partition number |
| 382 | * @disk: gendisk of interest |
| 383 | * @partno: partition number |
| 384 | * |
| 385 | * Find partition @partno from @disk, do bdget() on it. |
| 386 | * |
| 387 | * CONTEXT: |
| 388 | * Don't care. |
| 389 | * |
| 390 | * RETURNS: |
| 391 | * Resulting block_device on success, NULL on failure. |
| 392 | */ |
| 393 | extern struct block_device *bdget_disk(struct gendisk *disk, int partno) |
| 394 | { |
| 395 | dev_t devt = MKDEV(0, 0); |
| 396 | |
| 397 | if (partno == 0) |
| 398 | devt = disk_devt(disk); |
| 399 | else { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 400 | struct hd_struct *part; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 401 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 402 | part = disk_get_part(disk, partno); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 403 | if (part && part->nr_sects) |
| 404 | devt = part_devt(part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 405 | disk_put_part(part); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (likely(devt != MKDEV(0, 0))) |
| 409 | return bdget(devt); |
| 410 | return NULL; |
| 411 | } |
| 412 | EXPORT_SYMBOL(bdget_disk); |
| 413 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 414 | /* |
| 415 | * print a full list of all partitions - intended for places where the root |
| 416 | * filesystem can't be mounted and thus to give the victim some idea of what |
| 417 | * went wrong |
| 418 | */ |
| 419 | void __init printk_all_partitions(void) |
| 420 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 421 | struct class_dev_iter iter; |
| 422 | struct device *dev; |
| 423 | |
| 424 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 425 | while ((dev = class_dev_iter_next(&iter))) { |
| 426 | struct gendisk *disk = dev_to_disk(dev); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 427 | struct disk_part_iter piter; |
| 428 | struct hd_struct *part; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 429 | char buf[BDEVNAME_SIZE]; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Don't show empty devices or things that have been |
| 433 | * surpressed |
| 434 | */ |
| 435 | if (get_capacity(disk) == 0 || |
| 436 | (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) |
| 437 | continue; |
| 438 | |
| 439 | /* |
| 440 | * Note, unlike /proc/partitions, I am showing the |
| 441 | * numbers in hex - the same format as the root= |
| 442 | * option takes. |
| 443 | */ |
| 444 | printk("%02x%02x %10llu %s", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 445 | MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)), |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 446 | (unsigned long long)get_capacity(disk) >> 1, |
| 447 | disk_name(disk, 0, buf)); |
| 448 | if (disk->driverfs_dev != NULL && |
| 449 | disk->driverfs_dev->driver != NULL) |
| 450 | printk(" driver: %s\n", |
| 451 | disk->driverfs_dev->driver->name); |
| 452 | else |
| 453 | printk(" (driver?)\n"); |
| 454 | |
| 455 | /* now show the partitions */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 456 | disk_part_iter_init(&piter, disk, 0); |
| 457 | while ((part = disk_part_iter_next(&piter))) |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 458 | printk(" %02x%02x %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 459 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
| 460 | (unsigned long long)part->nr_sects >> 1, |
| 461 | disk_name(disk, part->partno, buf)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 462 | disk_part_iter_exit(&piter); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 463 | } |
| 464 | class_dev_iter_exit(&iter); |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | #ifdef CONFIG_PROC_FS |
| 468 | /* iterator */ |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 469 | static void *disk_seqf_start(struct seq_file *seqf, loff_t *pos) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 470 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 471 | loff_t skip = *pos; |
| 472 | struct class_dev_iter *iter; |
| 473 | struct device *dev; |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 474 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 475 | iter = kmalloc(GFP_KERNEL, sizeof(*iter)); |
| 476 | if (!iter) |
| 477 | return ERR_PTR(-ENOMEM); |
| 478 | |
| 479 | seqf->private = iter; |
| 480 | class_dev_iter_init(iter, &block_class, NULL, &disk_type); |
| 481 | do { |
| 482 | dev = class_dev_iter_next(iter); |
| 483 | if (!dev) |
| 484 | return NULL; |
| 485 | } while (skip--); |
| 486 | |
| 487 | return dev_to_disk(dev); |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 490 | static void *disk_seqf_next(struct seq_file *seqf, void *v, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 492 | struct device *dev; |
Greg Kroah-Hartman | 66c64af | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 493 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 494 | (*pos)++; |
| 495 | dev = class_dev_iter_next(seqf->private); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 496 | if (dev) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 497 | return dev_to_disk(dev); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | return NULL; |
| 500 | } |
| 501 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 502 | static void disk_seqf_stop(struct seq_file *seqf, void *v) |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 503 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 504 | struct class_dev_iter *iter = seqf->private; |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 505 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 506 | /* stop is called even after start failed :-( */ |
| 507 | if (iter) { |
| 508 | class_dev_iter_exit(iter); |
| 509 | kfree(iter); |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 513 | static void *show_partition_start(struct seq_file *seqf, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 515 | static void *p; |
| 516 | |
| 517 | p = disk_seqf_start(seqf, pos); |
| 518 | if (!IS_ERR(p) && p) |
| 519 | seq_puts(seqf, "major minor #blocks name\n\n"); |
| 520 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 523 | static int show_partition(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | struct gendisk *sgp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 526 | struct disk_part_iter piter; |
| 527 | struct hd_struct *part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | char buf[BDEVNAME_SIZE]; |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | /* Don't show non-partitionable removeable devices or empty devices */ |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 531 | if (!get_capacity(sgp) || (!disk_max_parts(sgp) && |
| 532 | (sgp->flags & GENHD_FL_REMOVABLE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return 0; |
| 534 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 535 | return 0; |
| 536 | |
| 537 | /* show the full disk and all non-0 size partitions of it */ |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 538 | seq_printf(seqf, "%4d %4d %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 539 | MAJOR(disk_devt(sgp)), MINOR(disk_devt(sgp)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | (unsigned long long)get_capacity(sgp) >> 1, |
| 541 | disk_name(sgp, 0, buf)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 542 | |
| 543 | disk_part_iter_init(&piter, sgp, 0); |
| 544 | while ((part = disk_part_iter_next(&piter))) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 545 | seq_printf(seqf, "%4d %4d %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 546 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
| 547 | (unsigned long long)part->nr_sects >> 1, |
| 548 | disk_name(sgp, part->partno, buf)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 549 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Jan Engelhardt | 12f32bb | 2008-01-29 20:57:51 +0100 | [diff] [blame] | 554 | const struct seq_operations partitions_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 555 | .start = show_partition_start, |
| 556 | .next = disk_seqf_next, |
| 557 | .stop = disk_seqf_stop, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 558 | .show = show_partition |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | }; |
| 560 | #endif |
| 561 | |
| 562 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 563 | static struct kobject *base_probe(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 565 | if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* Make old-style 2.4 aliases work */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 567 | request_module("block-major-%d", MAJOR(devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | static int __init genhd_device_init(void) |
| 572 | { |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 573 | int error; |
| 574 | |
| 575 | block_class.dev_kobj = sysfs_dev_block_kobj; |
| 576 | error = class_register(&block_class); |
Roland McGrath | ee27a55 | 2008-03-11 17:13:15 -0700 | [diff] [blame] | 577 | if (unlikely(error)) |
| 578 | return error; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 579 | bdev_map = kobj_map_init(base_probe, &block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | blk_dev_init(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 581 | |
| 582 | #ifndef CONFIG_SYSFS_DEPRECATED |
| 583 | /* create top-level block dir */ |
| 584 | block_depr = kobject_create_and_add("block", NULL); |
| 585 | #endif |
Greg Kroah-Hartman | 830d3cf | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 586 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | subsys_initcall(genhd_device_init); |
| 590 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 591 | static ssize_t disk_range_show(struct device *dev, |
| 592 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 594 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 596 | return sprintf(buf, "%d\n", disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 599 | static ssize_t disk_removable_show(struct device *dev, |
| 600 | struct device_attribute *attr, char *buf) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 601 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 602 | struct gendisk *disk = dev_to_disk(dev); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 603 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 604 | return sprintf(buf, "%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 608 | static ssize_t disk_ro_show(struct device *dev, |
| 609 | struct device_attribute *attr, char *buf) |
| 610 | { |
| 611 | struct gendisk *disk = dev_to_disk(dev); |
| 612 | |
| 613 | return sprintf(buf, "%d\n", disk->policy ? 1 : 0); |
| 614 | } |
| 615 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 616 | static ssize_t disk_size_show(struct device *dev, |
| 617 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 619 | struct gendisk *disk = dev_to_disk(dev); |
| 620 | |
| 621 | return sprintf(buf, "%llu\n", (unsigned long long)get_capacity(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 623 | |
| 624 | static ssize_t disk_capability_show(struct device *dev, |
| 625 | struct device_attribute *attr, char *buf) |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 626 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 627 | struct gendisk *disk = dev_to_disk(dev); |
| 628 | |
| 629 | return sprintf(buf, "%x\n", disk->flags); |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 630 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 631 | |
| 632 | static ssize_t disk_stat_show(struct device *dev, |
| 633 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 635 | struct gendisk *disk = dev_to_disk(dev); |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | preempt_disable(); |
| 638 | disk_round_stats(disk); |
| 639 | preempt_enable(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 640 | return sprintf(buf, |
Ben Woodard | 837c7878 | 2006-03-22 08:09:31 +0100 | [diff] [blame] | 641 | "%8lu %8lu %8llu %8u " |
| 642 | "%8lu %8lu %8llu %8u " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | "%8u %8u %8u" |
| 644 | "\n", |
Jens Axboe | 47a0041 | 2005-11-09 13:38:47 +0100 | [diff] [blame] | 645 | disk_stat_read(disk, ios[READ]), |
| 646 | disk_stat_read(disk, merges[READ]), |
| 647 | (unsigned long long)disk_stat_read(disk, sectors[READ]), |
| 648 | jiffies_to_msecs(disk_stat_read(disk, ticks[READ])), |
| 649 | disk_stat_read(disk, ios[WRITE]), |
| 650 | disk_stat_read(disk, merges[WRITE]), |
| 651 | (unsigned long long)disk_stat_read(disk, sectors[WRITE]), |
| 652 | jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | disk->in_flight, |
| 654 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 655 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 658 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 659 | static ssize_t disk_fail_show(struct device *dev, |
| 660 | struct device_attribute *attr, char *buf) |
| 661 | { |
| 662 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 663 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 664 | return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0); |
| 665 | } |
| 666 | |
| 667 | static ssize_t disk_fail_store(struct device *dev, |
| 668 | struct device_attribute *attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 669 | const char *buf, size_t count) |
| 670 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 671 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 672 | int i; |
| 673 | |
| 674 | if (count > 0 && sscanf(buf, "%d", &i) > 0) { |
| 675 | if (i == 0) |
| 676 | disk->flags &= ~GENHD_FL_FAIL; |
| 677 | else |
| 678 | disk->flags |= GENHD_FL_FAIL; |
| 679 | } |
| 680 | |
| 681 | return count; |
| 682 | } |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 683 | |
| 684 | #endif |
| 685 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 686 | static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL); |
| 687 | static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 688 | static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 689 | static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL); |
| 690 | static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); |
| 691 | static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 692 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 693 | static struct device_attribute dev_attr_fail = |
| 694 | __ATTR(make-it-fail, S_IRUGO|S_IWUSR, disk_fail_show, disk_fail_store); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 695 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 696 | |
| 697 | static struct attribute *disk_attrs[] = { |
| 698 | &dev_attr_range.attr, |
| 699 | &dev_attr_removable.attr, |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 700 | &dev_attr_ro.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 701 | &dev_attr_size.attr, |
| 702 | &dev_attr_capability.attr, |
| 703 | &dev_attr_stat.attr, |
| 704 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 705 | &dev_attr_fail.attr, |
| 706 | #endif |
| 707 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | }; |
| 709 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 710 | static struct attribute_group disk_attr_group = { |
| 711 | .attrs = disk_attrs, |
| 712 | }; |
| 713 | |
| 714 | static struct attribute_group *disk_attr_groups[] = { |
| 715 | &disk_attr_group, |
| 716 | NULL |
| 717 | }; |
| 718 | |
| 719 | static void disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 721 | struct gendisk *disk = dev_to_disk(dev); |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | kfree(disk->random); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 724 | kfree(disk->__part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | free_disk_stats(disk); |
| 726 | kfree(disk); |
| 727 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 728 | struct class block_class = { |
| 729 | .name = "block", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | }; |
| 731 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 732 | static struct device_type disk_type = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 733 | .name = "disk", |
| 734 | .groups = disk_attr_groups, |
| 735 | .release = disk_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 738 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 739 | /* |
| 740 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 741 | * entries do, above, but makes them available through one seq_file. |
| 742 | * |
| 743 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 744 | * extra fields. |
| 745 | */ |
| 746 | static int diskstats_show(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
| 748 | struct gendisk *gp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 749 | struct disk_part_iter piter; |
| 750 | struct hd_struct *hd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | char buf[BDEVNAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
| 753 | /* |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 754 | if (&gp->dev.kobj.entry == block_class.devices.next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 755 | seq_puts(seqf, "major minor name" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | " rio rmerge rsect ruse wio wmerge " |
| 757 | "wsect wuse running use aveq" |
| 758 | "\n\n"); |
| 759 | */ |
| 760 | |
| 761 | preempt_disable(); |
| 762 | disk_round_stats(gp); |
| 763 | preempt_enable(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 764 | seq_printf(seqf, "%4d %4d %s %lu %lu %llu %u %lu %lu %llu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 765 | MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)), |
| 766 | disk_name(gp, 0, buf), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 767 | disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]), |
| 768 | (unsigned long long)disk_stat_read(gp, sectors[0]), |
| 769 | jiffies_to_msecs(disk_stat_read(gp, ticks[0])), |
| 770 | disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]), |
| 771 | (unsigned long long)disk_stat_read(gp, sectors[1]), |
| 772 | jiffies_to_msecs(disk_stat_read(gp, ticks[1])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | gp->in_flight, |
| 774 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 775 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 776 | |
| 777 | /* now show all non-0 size partitions of it */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 778 | disk_part_iter_init(&piter, gp, 0); |
| 779 | while ((hd = disk_part_iter_next(&piter))) { |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 780 | preempt_disable(); |
| 781 | part_round_stats(hd); |
| 782 | preempt_enable(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 783 | seq_printf(seqf, "%4d %4d %s %lu %lu %llu " |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 784 | "%u %lu %lu %llu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 785 | MAJOR(part_devt(hd)), MINOR(part_devt(hd)), |
| 786 | disk_name(gp, hd->partno, buf), |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 787 | part_stat_read(hd, ios[0]), |
| 788 | part_stat_read(hd, merges[0]), |
| 789 | (unsigned long long)part_stat_read(hd, sectors[0]), |
| 790 | jiffies_to_msecs(part_stat_read(hd, ticks[0])), |
| 791 | part_stat_read(hd, ios[1]), |
| 792 | part_stat_read(hd, merges[1]), |
| 793 | (unsigned long long)part_stat_read(hd, sectors[1]), |
| 794 | jiffies_to_msecs(part_stat_read(hd, ticks[1])), |
| 795 | hd->in_flight, |
| 796 | jiffies_to_msecs(part_stat_read(hd, io_ticks)), |
| 797 | jiffies_to_msecs(part_stat_read(hd, time_in_queue)) |
| 798 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 800 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Jan Engelhardt | 12f32bb | 2008-01-29 20:57:51 +0100 | [diff] [blame] | 805 | const struct seq_operations diskstats_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 806 | .start = disk_seqf_start, |
| 807 | .next = disk_seqf_next, |
| 808 | .stop = disk_seqf_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | .show = diskstats_show |
| 810 | }; |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 811 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Kristen Carlson Accardi | 8ce7ad7b | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 813 | static void media_change_notify_thread(struct work_struct *work) |
| 814 | { |
| 815 | struct gendisk *gd = container_of(work, struct gendisk, async_notify); |
| 816 | char event[] = "MEDIA_CHANGE=1"; |
| 817 | char *envp[] = { event, NULL }; |
| 818 | |
| 819 | /* |
| 820 | * set enviroment vars to indicate which event this is for |
| 821 | * so that user space will know to go check the media status. |
| 822 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 823 | kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp); |
Kristen Carlson Accardi | 8ce7ad7b | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 824 | put_device(gd->driverfs_dev); |
| 825 | } |
| 826 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 827 | #if 0 |
Kristen Carlson Accardi | 8ce7ad7b | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 828 | void genhd_media_change_notify(struct gendisk *disk) |
| 829 | { |
| 830 | get_device(disk->driverfs_dev); |
| 831 | schedule_work(&disk->async_notify); |
| 832 | } |
| 833 | EXPORT_SYMBOL_GPL(genhd_media_change_notify); |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 834 | #endif /* 0 */ |
Kristen Carlson Accardi | 8ce7ad7b | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 835 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 836 | dev_t blk_lookup_devt(const char *name, int partno) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 837 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 838 | dev_t devt = MKDEV(0, 0); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 839 | struct class_dev_iter iter; |
| 840 | struct device *dev; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 841 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 842 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 843 | while ((dev = class_dev_iter_next(&iter))) { |
| 844 | struct gendisk *disk = dev_to_disk(dev); |
| 845 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 846 | if (strcmp(dev->bus_id, name)) |
| 847 | continue; |
| 848 | if (partno < 0 || partno > disk_max_parts(disk)) |
| 849 | continue; |
| 850 | |
| 851 | if (partno == 0) |
| 852 | devt = disk_devt(disk); |
| 853 | else { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 854 | struct hd_struct *part; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 855 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 856 | part = disk_get_part(disk, partno); |
| 857 | if (!part || !part->nr_sects) { |
| 858 | disk_put_part(part); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 859 | continue; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 860 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 861 | |
| 862 | devt = part_devt(part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 863 | disk_put_part(part); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 864 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 865 | break; |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 866 | } |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 867 | class_dev_iter_exit(&iter); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 868 | return devt; |
| 869 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 870 | EXPORT_SYMBOL(blk_lookup_devt); |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | struct gendisk *alloc_disk(int minors) |
| 873 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 874 | return alloc_disk_node(minors, -1); |
| 875 | } |
| 876 | |
| 877 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 878 | { |
| 879 | struct gendisk *disk; |
| 880 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 881 | disk = kmalloc_node(sizeof(struct gendisk), |
| 882 | GFP_KERNEL | __GFP_ZERO, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | if (disk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | if (!init_disk_stats(disk)) { |
| 885 | kfree(disk); |
| 886 | return NULL; |
| 887 | } |
| 888 | if (minors > 1) { |
| 889 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 890 | disk->__part = kmalloc_node(size, |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 891 | GFP_KERNEL | __GFP_ZERO, node_id); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 892 | if (!disk->__part) { |
Jerome Marchand | c767403 | 2007-11-23 09:17:53 +0100 | [diff] [blame] | 893 | free_disk_stats(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | kfree(disk); |
| 895 | return NULL; |
| 896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | disk->minors = minors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | rand_initialize_disk(disk); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 900 | disk->dev.class = &block_class; |
| 901 | disk->dev.type = &disk_type; |
| 902 | device_initialize(&disk->dev); |
Kristen Carlson Accardi | 8ce7ad7b | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 903 | INIT_WORK(&disk->async_notify, |
| 904 | media_change_notify_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | return disk; |
| 907 | } |
| 908 | |
| 909 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 910 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | struct kobject *get_disk(struct gendisk *disk) |
| 913 | { |
| 914 | struct module *owner; |
| 915 | struct kobject *kobj; |
| 916 | |
| 917 | if (!disk->fops) |
| 918 | return NULL; |
| 919 | owner = disk->fops->owner; |
| 920 | if (owner && !try_module_get(owner)) |
| 921 | return NULL; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 922 | kobj = kobject_get(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | if (kobj == NULL) { |
| 924 | module_put(owner); |
| 925 | return NULL; |
| 926 | } |
| 927 | return kobj; |
| 928 | |
| 929 | } |
| 930 | |
| 931 | EXPORT_SYMBOL(get_disk); |
| 932 | |
| 933 | void put_disk(struct gendisk *disk) |
| 934 | { |
| 935 | if (disk) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 936 | kobject_put(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | EXPORT_SYMBOL(put_disk); |
| 940 | |
| 941 | void set_device_ro(struct block_device *bdev, int flag) |
| 942 | { |
| 943 | if (bdev->bd_contains != bdev) |
| 944 | bdev->bd_part->policy = flag; |
| 945 | else |
| 946 | bdev->bd_disk->policy = flag; |
| 947 | } |
| 948 | |
| 949 | EXPORT_SYMBOL(set_device_ro); |
| 950 | |
| 951 | void set_disk_ro(struct gendisk *disk, int flag) |
| 952 | { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 953 | struct disk_part_iter piter; |
| 954 | struct hd_struct *part; |
| 955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | disk->policy = flag; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame^] | 957 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY); |
| 958 | while ((part = disk_part_iter_next(&piter))) |
| 959 | part->policy = flag; |
| 960 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | EXPORT_SYMBOL(set_disk_ro); |
| 964 | |
| 965 | int bdev_read_only(struct block_device *bdev) |
| 966 | { |
| 967 | if (!bdev) |
| 968 | return 0; |
| 969 | else if (bdev->bd_contains != bdev) |
| 970 | return bdev->bd_part->policy; |
| 971 | else |
| 972 | return bdev->bd_disk->policy; |
| 973 | } |
| 974 | |
| 975 | EXPORT_SYMBOL(bdev_read_only); |
| 976 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 977 | int invalidate_partition(struct gendisk *disk, int partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | { |
| 979 | int res = 0; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 980 | struct block_device *bdev = bdget_disk(disk, partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 982 | fsync_bdev(bdev); |
| 983 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | bdput(bdev); |
| 985 | } |
| 986 | return res; |
| 987 | } |
| 988 | |
| 989 | EXPORT_SYMBOL(invalidate_partition); |