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