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