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