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