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); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame^] | 636 | int cpu; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 637 | |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame^] | 638 | cpu = disk_stat_lock(); |
| 639 | disk_round_stats(cpu, disk); |
| 640 | disk_stat_unlock(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 641 | return sprintf(buf, |
Ben Woodard | 837c787 | 2006-03-22 08:09:31 +0100 | [diff] [blame] | 642 | "%8lu %8lu %8llu %8u " |
| 643 | "%8lu %8lu %8llu %8u " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | "%8u %8u %8u" |
| 645 | "\n", |
Jens Axboe | 47a0041 | 2005-11-09 13:38:47 +0100 | [diff] [blame] | 646 | disk_stat_read(disk, ios[READ]), |
| 647 | disk_stat_read(disk, merges[READ]), |
| 648 | (unsigned long long)disk_stat_read(disk, sectors[READ]), |
| 649 | jiffies_to_msecs(disk_stat_read(disk, ticks[READ])), |
| 650 | disk_stat_read(disk, ios[WRITE]), |
| 651 | disk_stat_read(disk, merges[WRITE]), |
| 652 | (unsigned long long)disk_stat_read(disk, sectors[WRITE]), |
| 653 | jiffies_to_msecs(disk_stat_read(disk, ticks[WRITE])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | disk->in_flight, |
| 655 | jiffies_to_msecs(disk_stat_read(disk, io_ticks)), |
| 656 | jiffies_to_msecs(disk_stat_read(disk, time_in_queue))); |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 659 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 660 | static ssize_t disk_fail_show(struct device *dev, |
| 661 | struct device_attribute *attr, char *buf) |
| 662 | { |
| 663 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 664 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 665 | return sprintf(buf, "%d\n", disk->flags & GENHD_FL_FAIL ? 1 : 0); |
| 666 | } |
| 667 | |
| 668 | static ssize_t disk_fail_store(struct device *dev, |
| 669 | struct device_attribute *attr, |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 670 | const char *buf, size_t count) |
| 671 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 672 | struct gendisk *disk = dev_to_disk(dev); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 673 | int i; |
| 674 | |
| 675 | if (count > 0 && sscanf(buf, "%d", &i) > 0) { |
| 676 | if (i == 0) |
| 677 | disk->flags &= ~GENHD_FL_FAIL; |
| 678 | else |
| 679 | disk->flags |= GENHD_FL_FAIL; |
| 680 | } |
| 681 | |
| 682 | return count; |
| 683 | } |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 684 | |
| 685 | #endif |
| 686 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 687 | static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL); |
| 688 | static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 689 | static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 690 | static DEVICE_ATTR(size, S_IRUGO, disk_size_show, NULL); |
| 691 | static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); |
| 692 | static DEVICE_ATTR(stat, S_IRUGO, disk_stat_show, NULL); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 693 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 694 | static struct device_attribute dev_attr_fail = |
| 695 | __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] | 696 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 697 | |
| 698 | static struct attribute *disk_attrs[] = { |
| 699 | &dev_attr_range.attr, |
| 700 | &dev_attr_removable.attr, |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 701 | &dev_attr_ro.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 702 | &dev_attr_size.attr, |
| 703 | &dev_attr_capability.attr, |
| 704 | &dev_attr_stat.attr, |
| 705 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 706 | &dev_attr_fail.attr, |
| 707 | #endif |
| 708 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | }; |
| 710 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 711 | static struct attribute_group disk_attr_group = { |
| 712 | .attrs = disk_attrs, |
| 713 | }; |
| 714 | |
| 715 | static struct attribute_group *disk_attr_groups[] = { |
| 716 | &disk_attr_group, |
| 717 | NULL |
| 718 | }; |
| 719 | |
| 720 | static void disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 722 | struct gendisk *disk = dev_to_disk(dev); |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | kfree(disk->random); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 725 | kfree(disk->__part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | free_disk_stats(disk); |
| 727 | kfree(disk); |
| 728 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 729 | struct class block_class = { |
| 730 | .name = "block", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | }; |
| 732 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 733 | static struct device_type disk_type = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 734 | .name = "disk", |
| 735 | .groups = disk_attr_groups, |
| 736 | .release = disk_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | }; |
| 738 | |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 739 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 740 | /* |
| 741 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 742 | * entries do, above, but makes them available through one seq_file. |
| 743 | * |
| 744 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 745 | * extra fields. |
| 746 | */ |
| 747 | static int diskstats_show(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
| 749 | struct gendisk *gp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 750 | struct disk_part_iter piter; |
| 751 | struct hd_struct *hd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | char buf[BDEVNAME_SIZE]; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame^] | 753 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | /* |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 756 | if (&gp->dev.kobj.entry == block_class.devices.next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 757 | seq_puts(seqf, "major minor name" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | " rio rmerge rsect ruse wio wmerge " |
| 759 | "wsect wuse running use aveq" |
| 760 | "\n\n"); |
| 761 | */ |
| 762 | |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame^] | 763 | cpu = disk_stat_lock(); |
| 764 | disk_round_stats(cpu, gp); |
| 765 | disk_stat_unlock(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 766 | 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] | 767 | MAJOR(disk_devt(gp)), MINOR(disk_devt(gp)), |
| 768 | disk_name(gp, 0, buf), |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 769 | disk_stat_read(gp, ios[0]), disk_stat_read(gp, merges[0]), |
| 770 | (unsigned long long)disk_stat_read(gp, sectors[0]), |
| 771 | jiffies_to_msecs(disk_stat_read(gp, ticks[0])), |
| 772 | disk_stat_read(gp, ios[1]), disk_stat_read(gp, merges[1]), |
| 773 | (unsigned long long)disk_stat_read(gp, sectors[1]), |
| 774 | jiffies_to_msecs(disk_stat_read(gp, ticks[1])), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | gp->in_flight, |
| 776 | jiffies_to_msecs(disk_stat_read(gp, io_ticks)), |
| 777 | jiffies_to_msecs(disk_stat_read(gp, time_in_queue))); |
| 778 | |
| 779 | /* now show all non-0 size partitions of it */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 780 | disk_part_iter_init(&piter, gp, 0); |
| 781 | while ((hd = disk_part_iter_next(&piter))) { |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame^] | 782 | cpu = disk_stat_lock(); |
| 783 | part_round_stats(cpu, hd); |
| 784 | disk_stat_unlock(); |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 785 | seq_printf(seqf, "%4d %4d %s %lu %lu %llu " |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 786 | "%u %lu %lu %llu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 787 | MAJOR(part_devt(hd)), MINOR(part_devt(hd)), |
| 788 | disk_name(gp, hd->partno, buf), |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 789 | part_stat_read(hd, ios[0]), |
| 790 | part_stat_read(hd, merges[0]), |
| 791 | (unsigned long long)part_stat_read(hd, sectors[0]), |
| 792 | jiffies_to_msecs(part_stat_read(hd, ticks[0])), |
| 793 | part_stat_read(hd, ios[1]), |
| 794 | part_stat_read(hd, merges[1]), |
| 795 | (unsigned long long)part_stat_read(hd, sectors[1]), |
| 796 | jiffies_to_msecs(part_stat_read(hd, ticks[1])), |
| 797 | hd->in_flight, |
| 798 | jiffies_to_msecs(part_stat_read(hd, io_ticks)), |
| 799 | jiffies_to_msecs(part_stat_read(hd, time_in_queue)) |
| 800 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 802 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
Jan Engelhardt | 12f32bb | 2008-01-29 20:57:51 +0100 | [diff] [blame] | 807 | const struct seq_operations diskstats_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 808 | .start = disk_seqf_start, |
| 809 | .next = disk_seqf_next, |
| 810 | .stop = disk_seqf_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | .show = diskstats_show |
| 812 | }; |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 813 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 815 | static void media_change_notify_thread(struct work_struct *work) |
| 816 | { |
| 817 | struct gendisk *gd = container_of(work, struct gendisk, async_notify); |
| 818 | char event[] = "MEDIA_CHANGE=1"; |
| 819 | char *envp[] = { event, NULL }; |
| 820 | |
| 821 | /* |
| 822 | * set enviroment vars to indicate which event this is for |
| 823 | * so that user space will know to go check the media status. |
| 824 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 825 | kobject_uevent_env(&gd->dev.kobj, KOBJ_CHANGE, envp); |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 826 | put_device(gd->driverfs_dev); |
| 827 | } |
| 828 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 829 | #if 0 |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 830 | void genhd_media_change_notify(struct gendisk *disk) |
| 831 | { |
| 832 | get_device(disk->driverfs_dev); |
| 833 | schedule_work(&disk->async_notify); |
| 834 | } |
| 835 | EXPORT_SYMBOL_GPL(genhd_media_change_notify); |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 836 | #endif /* 0 */ |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 837 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 838 | dev_t blk_lookup_devt(const char *name, int partno) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 839 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 840 | dev_t devt = MKDEV(0, 0); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 841 | struct class_dev_iter iter; |
| 842 | struct device *dev; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 843 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 844 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 845 | while ((dev = class_dev_iter_next(&iter))) { |
| 846 | struct gendisk *disk = dev_to_disk(dev); |
| 847 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 848 | if (strcmp(dev->bus_id, name)) |
| 849 | continue; |
| 850 | if (partno < 0 || partno > disk_max_parts(disk)) |
| 851 | continue; |
| 852 | |
| 853 | if (partno == 0) |
| 854 | devt = disk_devt(disk); |
| 855 | else { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 856 | struct hd_struct *part; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 857 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 858 | part = disk_get_part(disk, partno); |
| 859 | if (!part || !part->nr_sects) { |
| 860 | disk_put_part(part); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 861 | continue; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 862 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 863 | |
| 864 | devt = part_devt(part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 865 | disk_put_part(part); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 866 | } |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 867 | break; |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 868 | } |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 869 | class_dev_iter_exit(&iter); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 870 | return devt; |
| 871 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 872 | EXPORT_SYMBOL(blk_lookup_devt); |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | struct gendisk *alloc_disk(int minors) |
| 875 | { |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 876 | return alloc_disk_node(minors, -1); |
| 877 | } |
| 878 | |
| 879 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 880 | { |
| 881 | struct gendisk *disk; |
| 882 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 883 | disk = kmalloc_node(sizeof(struct gendisk), |
| 884 | GFP_KERNEL | __GFP_ZERO, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | if (disk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (!init_disk_stats(disk)) { |
| 887 | kfree(disk); |
| 888 | return NULL; |
| 889 | } |
| 890 | if (minors > 1) { |
| 891 | int size = (minors - 1) * sizeof(struct hd_struct *); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 892 | disk->__part = kmalloc_node(size, |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 893 | GFP_KERNEL | __GFP_ZERO, node_id); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 894 | if (!disk->__part) { |
Jerome Marchand | c767403 | 2007-11-23 09:17:53 +0100 | [diff] [blame] | 895 | free_disk_stats(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | kfree(disk); |
| 897 | return NULL; |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
| 900 | disk->minors = minors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | rand_initialize_disk(disk); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 902 | disk->dev.class = &block_class; |
| 903 | disk->dev.type = &disk_type; |
| 904 | device_initialize(&disk->dev); |
Kristen Carlson Accardi | 8ce7ad7 | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 905 | INIT_WORK(&disk->async_notify, |
| 906 | media_change_notify_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | return disk; |
| 909 | } |
| 910 | |
| 911 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 912 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | struct kobject *get_disk(struct gendisk *disk) |
| 915 | { |
| 916 | struct module *owner; |
| 917 | struct kobject *kobj; |
| 918 | |
| 919 | if (!disk->fops) |
| 920 | return NULL; |
| 921 | owner = disk->fops->owner; |
| 922 | if (owner && !try_module_get(owner)) |
| 923 | return NULL; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 924 | kobj = kobject_get(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | if (kobj == NULL) { |
| 926 | module_put(owner); |
| 927 | return NULL; |
| 928 | } |
| 929 | return kobj; |
| 930 | |
| 931 | } |
| 932 | |
| 933 | EXPORT_SYMBOL(get_disk); |
| 934 | |
| 935 | void put_disk(struct gendisk *disk) |
| 936 | { |
| 937 | if (disk) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 938 | kobject_put(&disk->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | EXPORT_SYMBOL(put_disk); |
| 942 | |
| 943 | void set_device_ro(struct block_device *bdev, int flag) |
| 944 | { |
| 945 | if (bdev->bd_contains != bdev) |
| 946 | bdev->bd_part->policy = flag; |
| 947 | else |
| 948 | bdev->bd_disk->policy = flag; |
| 949 | } |
| 950 | |
| 951 | EXPORT_SYMBOL(set_device_ro); |
| 952 | |
| 953 | void set_disk_ro(struct gendisk *disk, int flag) |
| 954 | { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 955 | struct disk_part_iter piter; |
| 956 | struct hd_struct *part; |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | disk->policy = flag; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 959 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY); |
| 960 | while ((part = disk_part_iter_next(&piter))) |
| 961 | part->policy = flag; |
| 962 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | EXPORT_SYMBOL(set_disk_ro); |
| 966 | |
| 967 | int bdev_read_only(struct block_device *bdev) |
| 968 | { |
| 969 | if (!bdev) |
| 970 | return 0; |
| 971 | else if (bdev->bd_contains != bdev) |
| 972 | return bdev->bd_part->policy; |
| 973 | else |
| 974 | return bdev->bd_disk->policy; |
| 975 | } |
| 976 | |
| 977 | EXPORT_SYMBOL(bdev_read_only); |
| 978 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 979 | int invalidate_partition(struct gendisk *disk, int partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | int res = 0; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 982 | struct block_device *bdev = bdget_disk(disk, partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 984 | fsync_bdev(bdev); |
| 985 | res = __invalidate_device(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | bdput(bdev); |
| 987 | } |
| 988 | return res; |
| 989 | } |
| 990 | |
| 991 | EXPORT_SYMBOL(invalidate_partition); |