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