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