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