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> |
Jes Sorensen | 58383af | 2006-02-06 14:12:43 -0800 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 19 | #include <linux/idr.h> |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 20 | #include <linux/log2.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 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 38 | static void disk_check_events(struct disk_events *ev, |
| 39 | unsigned int *clearing_ptr); |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 40 | static void disk_alloc_events(struct gendisk *disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 41 | static void disk_add_events(struct gendisk *disk); |
| 42 | static void disk_del_events(struct gendisk *disk); |
| 43 | static void disk_release_events(struct gendisk *disk); |
| 44 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 45 | /** |
| 46 | * disk_get_part - get partition |
| 47 | * @disk: disk to look partition from |
| 48 | * @partno: partition number |
| 49 | * |
| 50 | * Look for partition @partno from @disk. If found, increment |
| 51 | * reference count and return it. |
| 52 | * |
| 53 | * CONTEXT: |
| 54 | * Don't care. |
| 55 | * |
| 56 | * RETURNS: |
| 57 | * Pointer to the found partition on success, NULL if not found. |
| 58 | */ |
| 59 | struct hd_struct *disk_get_part(struct gendisk *disk, int partno) |
| 60 | { |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 61 | struct hd_struct *part = NULL; |
| 62 | struct disk_part_tbl *ptbl; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 63 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 64 | if (unlikely(partno < 0)) |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 65 | return NULL; |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 66 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 67 | rcu_read_lock(); |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 68 | |
| 69 | ptbl = rcu_dereference(disk->part_tbl); |
| 70 | if (likely(partno < ptbl->len)) { |
| 71 | part = rcu_dereference(ptbl->part[partno]); |
| 72 | if (part) |
| 73 | get_device(part_to_dev(part)); |
| 74 | } |
| 75 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 76 | rcu_read_unlock(); |
| 77 | |
| 78 | return part; |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(disk_get_part); |
| 81 | |
| 82 | /** |
| 83 | * disk_part_iter_init - initialize partition iterator |
| 84 | * @piter: iterator to initialize |
| 85 | * @disk: disk to iterate over |
| 86 | * @flags: DISK_PITER_* flags |
| 87 | * |
| 88 | * Initialize @piter so that it iterates over partitions of @disk. |
| 89 | * |
| 90 | * CONTEXT: |
| 91 | * Don't care. |
| 92 | */ |
| 93 | void disk_part_iter_init(struct disk_part_iter *piter, struct gendisk *disk, |
| 94 | unsigned int flags) |
| 95 | { |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 96 | struct disk_part_tbl *ptbl; |
| 97 | |
| 98 | rcu_read_lock(); |
| 99 | ptbl = rcu_dereference(disk->part_tbl); |
| 100 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 101 | piter->disk = disk; |
| 102 | piter->part = NULL; |
| 103 | |
| 104 | if (flags & DISK_PITER_REVERSE) |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 105 | piter->idx = ptbl->len - 1; |
Tejun Heo | 71982a4 | 2009-04-17 08:34:48 +0200 | [diff] [blame] | 106 | else if (flags & (DISK_PITER_INCL_PART0 | DISK_PITER_INCL_EMPTY_PART0)) |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 107 | piter->idx = 0; |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 108 | else |
| 109 | piter->idx = 1; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 110 | |
| 111 | piter->flags = flags; |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 112 | |
| 113 | rcu_read_unlock(); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 114 | } |
| 115 | EXPORT_SYMBOL_GPL(disk_part_iter_init); |
| 116 | |
| 117 | /** |
| 118 | * disk_part_iter_next - proceed iterator to the next partition and return it |
| 119 | * @piter: iterator of interest |
| 120 | * |
| 121 | * Proceed @piter to the next partition and return it. |
| 122 | * |
| 123 | * CONTEXT: |
| 124 | * Don't care. |
| 125 | */ |
| 126 | struct hd_struct *disk_part_iter_next(struct disk_part_iter *piter) |
| 127 | { |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 128 | struct disk_part_tbl *ptbl; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 129 | int inc, end; |
| 130 | |
| 131 | /* put the last partition */ |
| 132 | disk_put_part(piter->part); |
| 133 | piter->part = NULL; |
| 134 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 135 | /* get part_tbl */ |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 136 | rcu_read_lock(); |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 137 | ptbl = rcu_dereference(piter->disk->part_tbl); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 138 | |
| 139 | /* determine iteration parameters */ |
| 140 | if (piter->flags & DISK_PITER_REVERSE) { |
| 141 | inc = -1; |
Tejun Heo | 71982a4 | 2009-04-17 08:34:48 +0200 | [diff] [blame] | 142 | if (piter->flags & (DISK_PITER_INCL_PART0 | |
| 143 | DISK_PITER_INCL_EMPTY_PART0)) |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 144 | end = -1; |
| 145 | else |
| 146 | end = 0; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 147 | } else { |
| 148 | inc = 1; |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 149 | end = ptbl->len; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* iterate to the next partition */ |
| 153 | for (; piter->idx != end; piter->idx += inc) { |
| 154 | struct hd_struct *part; |
| 155 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 156 | part = rcu_dereference(ptbl->part[piter->idx]); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 157 | if (!part) |
| 158 | continue; |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 159 | if (!part_nr_sects_read(part) && |
Tejun Heo | 71982a4 | 2009-04-17 08:34:48 +0200 | [diff] [blame] | 160 | !(piter->flags & DISK_PITER_INCL_EMPTY) && |
| 161 | !(piter->flags & DISK_PITER_INCL_EMPTY_PART0 && |
| 162 | piter->idx == 0)) |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 163 | continue; |
| 164 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 165 | get_device(part_to_dev(part)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 166 | piter->part = part; |
| 167 | piter->idx += inc; |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | rcu_read_unlock(); |
| 172 | |
| 173 | return piter->part; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(disk_part_iter_next); |
| 176 | |
| 177 | /** |
| 178 | * disk_part_iter_exit - finish up partition iteration |
| 179 | * @piter: iter of interest |
| 180 | * |
| 181 | * Called when iteration is over. Cleans up @piter. |
| 182 | * |
| 183 | * CONTEXT: |
| 184 | * Don't care. |
| 185 | */ |
| 186 | void disk_part_iter_exit(struct disk_part_iter *piter) |
| 187 | { |
| 188 | disk_put_part(piter->part); |
| 189 | piter->part = NULL; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(disk_part_iter_exit); |
| 192 | |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 193 | static inline int sector_in_part(struct hd_struct *part, sector_t sector) |
| 194 | { |
| 195 | return part->start_sect <= sector && |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 196 | sector < part->start_sect + part_nr_sects_read(part); |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 199 | /** |
| 200 | * disk_map_sector_rcu - map sector to partition |
| 201 | * @disk: gendisk of interest |
| 202 | * @sector: sector to map |
| 203 | * |
| 204 | * Find out which partition @sector maps to on @disk. This is |
| 205 | * primarily used for stats accounting. |
| 206 | * |
| 207 | * CONTEXT: |
| 208 | * RCU read locked. The returned partition pointer is valid only |
| 209 | * while preemption is disabled. |
| 210 | * |
| 211 | * RETURNS: |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 212 | * Found partition on success, part0 is returned if no partition matches |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 213 | */ |
| 214 | struct hd_struct *disk_map_sector_rcu(struct gendisk *disk, sector_t sector) |
| 215 | { |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 216 | struct disk_part_tbl *ptbl; |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 217 | struct hd_struct *part; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 218 | int i; |
| 219 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 220 | ptbl = rcu_dereference(disk->part_tbl); |
| 221 | |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 222 | part = rcu_dereference(ptbl->last_lookup); |
| 223 | if (part && sector_in_part(part, sector)) |
| 224 | return part; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 225 | |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 226 | for (i = 1; i < ptbl->len; i++) { |
| 227 | part = rcu_dereference(ptbl->part[i]); |
| 228 | |
| 229 | if (part && sector_in_part(part, sector)) { |
| 230 | rcu_assign_pointer(ptbl->last_lookup, part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 231 | return part; |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 232 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 233 | } |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 234 | return &disk->part0; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL_GPL(disk_map_sector_rcu); |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Can be deleted altogether. Later. |
| 240 | * |
| 241 | */ |
| 242 | static struct blk_major_name { |
| 243 | struct blk_major_name *next; |
| 244 | int major; |
| 245 | char name[16]; |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 246 | } *major_names[BLKDEV_MAJOR_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | /* index in the above - for now: assume no multimajor ranges */ |
Yang Zhang | e61eb2e | 2010-12-17 09:00:18 +0100 | [diff] [blame] | 249 | static inline int major_to_index(unsigned major) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 251 | return major % BLKDEV_MAJOR_HASH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 254 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 255 | void blkdev_show(struct seq_file *seqf, off_t offset) |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 256 | { |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 257 | struct blk_major_name *dp; |
Neil Horman | 7170be5 | 2006-01-14 13:20:38 -0800 | [diff] [blame] | 258 | |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 259 | if (offset < BLKDEV_MAJOR_HASH_SIZE) { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 260 | mutex_lock(&block_class_lock); |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 261 | for (dp = major_names[offset]; dp; dp = dp->next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 262 | seq_printf(seqf, "%3d %s\n", dp->major, dp->name); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 263 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
Joe Korty | 68eef3b | 2006-03-31 02:30:32 -0800 | [diff] [blame] | 266 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Márton Németh | 9e8c0bc | 2009-02-20 08:12:51 +0100 | [diff] [blame] | 268 | /** |
| 269 | * register_blkdev - register a new block device |
| 270 | * |
| 271 | * @major: the requested major device number [1..255]. If @major=0, try to |
| 272 | * allocate any unused major number. |
| 273 | * @name: the name of the new block device as a zero terminated string |
| 274 | * |
| 275 | * The @name must be unique within the system. |
| 276 | * |
| 277 | * The return value depends on the @major input parameter. |
| 278 | * - if a major device number was requested in range [1..255] then the |
| 279 | * function returns zero on success, or a negative error code |
| 280 | * - if any unused major number was requested with @major=0 parameter |
| 281 | * then the return value is the allocated major number in range |
| 282 | * [1..255] or a negative error code otherwise |
| 283 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int register_blkdev(unsigned int major, const char *name) |
| 285 | { |
| 286 | struct blk_major_name **n, *p; |
| 287 | int index, ret = 0; |
| 288 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 289 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | /* temporary */ |
| 292 | if (major == 0) { |
| 293 | for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { |
| 294 | if (major_names[index] == NULL) |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | if (index == 0) { |
| 299 | printk("register_blkdev: failed to get major for %s\n", |
| 300 | name); |
| 301 | ret = -EBUSY; |
| 302 | goto out; |
| 303 | } |
| 304 | major = index; |
| 305 | ret = major; |
| 306 | } |
| 307 | |
| 308 | p = kmalloc(sizeof(struct blk_major_name), GFP_KERNEL); |
| 309 | if (p == NULL) { |
| 310 | ret = -ENOMEM; |
| 311 | goto out; |
| 312 | } |
| 313 | |
| 314 | p->major = major; |
| 315 | strlcpy(p->name, name, sizeof(p->name)); |
| 316 | p->next = NULL; |
| 317 | index = major_to_index(major); |
| 318 | |
| 319 | for (n = &major_names[index]; *n; n = &(*n)->next) { |
| 320 | if ((*n)->major == major) |
| 321 | break; |
| 322 | } |
| 323 | if (!*n) |
| 324 | *n = p; |
| 325 | else |
| 326 | ret = -EBUSY; |
| 327 | |
| 328 | if (ret < 0) { |
| 329 | printk("register_blkdev: cannot get major %d for %s\n", |
| 330 | major, name); |
| 331 | kfree(p); |
| 332 | } |
| 333 | out: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 334 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | EXPORT_SYMBOL(register_blkdev); |
| 339 | |
Akinobu Mita | f448024 | 2007-07-17 04:03:47 -0700 | [diff] [blame] | 340 | void unregister_blkdev(unsigned int major, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | struct blk_major_name **n; |
| 343 | struct blk_major_name *p = NULL; |
| 344 | int index = major_to_index(major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 346 | mutex_lock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | for (n = &major_names[index]; *n; n = &(*n)->next) |
| 348 | if ((*n)->major == major) |
| 349 | break; |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 350 | if (!*n || strcmp((*n)->name, name)) { |
| 351 | WARN_ON(1); |
Akinobu Mita | 294462a | 2007-07-17 04:03:45 -0700 | [diff] [blame] | 352 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | p = *n; |
| 354 | *n = p->next; |
| 355 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 356 | mutex_unlock(&block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | EXPORT_SYMBOL(unregister_blkdev); |
| 361 | |
| 362 | static struct kobj_map *bdev_map; |
| 363 | |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 364 | /** |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 365 | * blk_mangle_minor - scatter minor numbers apart |
| 366 | * @minor: minor number to mangle |
| 367 | * |
| 368 | * Scatter consecutively allocated @minor number apart if MANGLE_DEVT |
| 369 | * is enabled. Mangling twice gives the original value. |
| 370 | * |
| 371 | * RETURNS: |
| 372 | * Mangled value. |
| 373 | * |
| 374 | * CONTEXT: |
| 375 | * Don't care. |
| 376 | */ |
| 377 | static int blk_mangle_minor(int minor) |
| 378 | { |
| 379 | #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT |
| 380 | int i; |
| 381 | |
| 382 | for (i = 0; i < MINORBITS / 2; i++) { |
| 383 | int low = minor & (1 << i); |
| 384 | int high = minor & (1 << (MINORBITS - 1 - i)); |
| 385 | int distance = MINORBITS - 1 - 2 * i; |
| 386 | |
| 387 | minor ^= low | high; /* clear both bits */ |
| 388 | low <<= distance; /* swap the positions */ |
| 389 | high >>= distance; |
| 390 | minor |= low | high; /* and set */ |
| 391 | } |
| 392 | #endif |
| 393 | return minor; |
| 394 | } |
| 395 | |
| 396 | /** |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 397 | * blk_alloc_devt - allocate a dev_t for a partition |
| 398 | * @part: partition to allocate dev_t for |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 399 | * @devt: out parameter for resulting dev_t |
| 400 | * |
| 401 | * Allocate a dev_t for block device. |
| 402 | * |
| 403 | * RETURNS: |
| 404 | * 0 on success, allocated dev_t is returned in *@devt. -errno on |
| 405 | * failure. |
| 406 | * |
| 407 | * CONTEXT: |
| 408 | * Might sleep. |
| 409 | */ |
| 410 | int blk_alloc_devt(struct hd_struct *part, dev_t *devt) |
| 411 | { |
| 412 | struct gendisk *disk = part_to_disk(part); |
| 413 | int idx, rc; |
| 414 | |
| 415 | /* in consecutive minor range? */ |
| 416 | if (part->partno < disk->minors) { |
| 417 | *devt = MKDEV(disk->major, disk->first_minor + part->partno); |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* allocate ext devt */ |
| 422 | do { |
| 423 | if (!idr_pre_get(&ext_devt_idr, GFP_KERNEL)) |
| 424 | return -ENOMEM; |
| 425 | rc = idr_get_new(&ext_devt_idr, part, &idx); |
| 426 | } while (rc == -EAGAIN); |
| 427 | |
| 428 | if (rc) |
| 429 | return rc; |
| 430 | |
| 431 | if (idx > MAX_EXT_DEVT) { |
| 432 | idr_remove(&ext_devt_idr, idx); |
| 433 | return -EBUSY; |
| 434 | } |
| 435 | |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 436 | *devt = MKDEV(BLOCK_EXT_MAJOR, blk_mangle_minor(idx)); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * blk_free_devt - free a dev_t |
| 442 | * @devt: dev_t to free |
| 443 | * |
| 444 | * Free @devt which was allocated using blk_alloc_devt(). |
| 445 | * |
| 446 | * CONTEXT: |
| 447 | * Might sleep. |
| 448 | */ |
| 449 | void blk_free_devt(dev_t devt) |
| 450 | { |
| 451 | might_sleep(); |
| 452 | |
| 453 | if (devt == MKDEV(0, 0)) |
| 454 | return; |
| 455 | |
| 456 | if (MAJOR(devt) == BLOCK_EXT_MAJOR) { |
| 457 | mutex_lock(&ext_devt_mutex); |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 458 | idr_remove(&ext_devt_idr, blk_mangle_minor(MINOR(devt))); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 459 | mutex_unlock(&ext_devt_mutex); |
| 460 | } |
| 461 | } |
| 462 | |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 463 | static char *bdevt_str(dev_t devt, char *buf) |
| 464 | { |
| 465 | if (MAJOR(devt) <= 0xff && MINOR(devt) <= 0xff) { |
| 466 | char tbuf[BDEVT_SIZE]; |
| 467 | snprintf(tbuf, BDEVT_SIZE, "%02x%02x", MAJOR(devt), MINOR(devt)); |
| 468 | snprintf(buf, BDEVT_SIZE, "%-9s", tbuf); |
| 469 | } else |
| 470 | snprintf(buf, BDEVT_SIZE, "%03x:%05x", MAJOR(devt), MINOR(devt)); |
| 471 | |
| 472 | return buf; |
| 473 | } |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | /* |
| 476 | * Register device numbers dev..(dev+range-1) |
| 477 | * range must be nonzero |
| 478 | * The hash chain is sorted on range, so that subranges can override. |
| 479 | */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 480 | 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] | 481 | struct kobject *(*probe)(dev_t, int *, void *), |
| 482 | int (*lock)(dev_t, void *), void *data) |
| 483 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 484 | kobj_map(bdev_map, devt, range, module, probe, lock, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | EXPORT_SYMBOL(blk_register_region); |
| 488 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 489 | void blk_unregister_region(dev_t devt, unsigned long range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 491 | kobj_unmap(bdev_map, devt, range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | EXPORT_SYMBOL(blk_unregister_region); |
| 495 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 496 | static struct kobject *exact_match(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
| 498 | struct gendisk *p = data; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 499 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 500 | return &disk_to_dev(p)->kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 503 | static int exact_lock(dev_t devt, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | struct gendisk *p = data; |
| 506 | |
| 507 | if (!get_disk(p)) |
| 508 | return -1; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Al Viro | 4752bc3 | 2011-09-16 00:21:54 -0400 | [diff] [blame] | 512 | static void register_disk(struct gendisk *disk) |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 513 | { |
| 514 | struct device *ddev = disk_to_dev(disk); |
| 515 | struct block_device *bdev; |
| 516 | struct disk_part_iter piter; |
| 517 | struct hd_struct *part; |
| 518 | int err; |
| 519 | |
| 520 | ddev->parent = disk->driverfs_dev; |
| 521 | |
| 522 | dev_set_name(ddev, disk->disk_name); |
| 523 | |
| 524 | /* delay uevents, until we scanned partition table */ |
| 525 | dev_set_uevent_suppress(ddev, 1); |
| 526 | |
| 527 | if (device_add(ddev)) |
| 528 | return; |
| 529 | if (!sysfs_deprecated) { |
| 530 | err = sysfs_create_link(block_depr, &ddev->kobj, |
| 531 | kobject_name(&ddev->kobj)); |
| 532 | if (err) { |
| 533 | device_del(ddev); |
| 534 | return; |
| 535 | } |
| 536 | } |
| 537 | disk->part0.holder_dir = kobject_create_and_add("holders", &ddev->kobj); |
| 538 | disk->slave_dir = kobject_create_and_add("slaves", &ddev->kobj); |
| 539 | |
| 540 | /* No minors to use for partitions */ |
Tejun Heo | d27769e | 2011-08-23 20:01:04 +0200 | [diff] [blame] | 541 | if (!disk_part_scan_enabled(disk)) |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 542 | goto exit; |
| 543 | |
| 544 | /* No such device (e.g., media were just removed) */ |
| 545 | if (!get_capacity(disk)) |
| 546 | goto exit; |
| 547 | |
| 548 | bdev = bdget_disk(disk, 0); |
| 549 | if (!bdev) |
| 550 | goto exit; |
| 551 | |
| 552 | bdev->bd_invalidated = 1; |
| 553 | err = blkdev_get(bdev, FMODE_READ, NULL); |
| 554 | if (err < 0) |
| 555 | goto exit; |
| 556 | blkdev_put(bdev, FMODE_READ); |
| 557 | |
| 558 | exit: |
| 559 | /* announce disk after possible partitions are created */ |
| 560 | dev_set_uevent_suppress(ddev, 0); |
| 561 | kobject_uevent(&ddev->kobj, KOBJ_ADD); |
| 562 | |
| 563 | /* announce possible partitions */ |
| 564 | disk_part_iter_init(&piter, disk, 0); |
| 565 | while ((part = disk_part_iter_next(&piter))) |
| 566 | kobject_uevent(&part_to_dev(part)->kobj, KOBJ_ADD); |
| 567 | disk_part_iter_exit(&piter); |
| 568 | } |
| 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /** |
| 571 | * add_disk - add partitioning information to kernel list |
| 572 | * @disk: per-device partitioning information |
| 573 | * |
| 574 | * This function registers the partitioning information in @disk |
| 575 | * with the kernel. |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 576 | * |
| 577 | * FIXME: error handling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | */ |
| 579 | void add_disk(struct gendisk *disk) |
| 580 | { |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 581 | struct backing_dev_info *bdi; |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 582 | dev_t devt; |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 583 | int retval; |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 584 | |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 585 | /* minors == 0 indicates to use ext devt from part0 and should |
| 586 | * be accompanied with EXT_DEVT flag. Make sure all |
| 587 | * parameters make sense. |
| 588 | */ |
| 589 | WARN_ON(disk->minors && !(disk->major || disk->first_minor)); |
| 590 | WARN_ON(!disk->minors && !(disk->flags & GENHD_FL_EXT_DEVT)); |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | disk->flags |= GENHD_FL_UP; |
Tejun Heo | 3e1a7ff | 2008-08-25 19:56:17 +0900 | [diff] [blame] | 593 | |
| 594 | retval = blk_alloc_devt(&disk->part0, &devt); |
| 595 | if (retval) { |
| 596 | WARN_ON(1); |
| 597 | return; |
| 598 | } |
| 599 | disk_to_dev(disk)->devt = devt; |
| 600 | |
| 601 | /* ->major and ->first_minor aren't supposed to be |
| 602 | * dereferenced from here on, but set them just in case. |
| 603 | */ |
| 604 | disk->major = MAJOR(devt); |
| 605 | disk->first_minor = MINOR(devt); |
| 606 | |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 607 | disk_alloc_events(disk); |
| 608 | |
Wanlong Gao | 9f5e486 | 2011-06-13 10:45:43 +0200 | [diff] [blame] | 609 | /* Register BDI before referencing it from bdev */ |
Signed-off-by: Jan Kara | 01ea506 | 2010-09-16 20:36:36 +0200 | [diff] [blame] | 610 | bdi = &disk->queue->backing_dev_info; |
| 611 | bdi_register_dev(bdi, disk_devt(disk)); |
| 612 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 613 | blk_register_region(disk_devt(disk), disk->minors, NULL, |
| 614 | exact_match, exact_lock, disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | register_disk(disk); |
| 616 | blk_register_queue(disk); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 617 | |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 618 | /* |
| 619 | * Take an extra ref on queue which will be put on disk_release() |
| 620 | * so that it sticks around as long as @disk is there. |
| 621 | */ |
Tejun Heo | 09ac46c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 622 | WARN_ON_ONCE(!blk_get_queue(disk->queue)); |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 623 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 624 | retval = sysfs_create_link(&disk_to_dev(disk)->kobj, &bdi->dev->kobj, |
| 625 | "bdi"); |
Greg Kroah-Hartman | 6ffeea7 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 626 | WARN_ON(retval); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 627 | |
| 628 | disk_add_events(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | EXPORT_SYMBOL(add_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 632 | void del_gendisk(struct gendisk *disk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 634 | struct disk_part_iter piter; |
| 635 | struct hd_struct *part; |
| 636 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 637 | disk_del_events(disk); |
| 638 | |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 639 | /* invalidate stuff */ |
| 640 | disk_part_iter_init(&piter, disk, |
| 641 | DISK_PITER_INCL_EMPTY | DISK_PITER_REVERSE); |
| 642 | while ((part = disk_part_iter_next(&piter))) { |
| 643 | invalidate_partition(disk, part->partno); |
| 644 | delete_partition(disk, part->partno); |
| 645 | } |
| 646 | disk_part_iter_exit(&piter); |
| 647 | |
| 648 | invalidate_partition(disk, 0); |
| 649 | blk_free_devt(disk_to_dev(disk)->devt); |
| 650 | set_capacity(disk, 0); |
| 651 | disk->flags &= ~GENHD_FL_UP; |
| 652 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 653 | sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi"); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 654 | bdi_unregister(&disk->queue->backing_dev_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | blk_unregister_queue(disk); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 656 | blk_unregister_region(disk_devt(disk), disk->minors); |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 657 | |
| 658 | part_stat_set_all(&disk->part0, 0); |
| 659 | disk->part0.stamp = 0; |
| 660 | |
| 661 | kobject_put(disk->part0.holder_dir); |
| 662 | kobject_put(disk->slave_dir); |
| 663 | disk->driverfs_dev = NULL; |
| 664 | if (!sysfs_deprecated) |
| 665 | sysfs_remove_link(block_depr, dev_name(disk_to_dev(disk))); |
| 666 | device_del(disk_to_dev(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
Tejun Heo | d2bf1b6 | 2010-12-08 20:57:36 +0100 | [diff] [blame] | 668 | EXPORT_SYMBOL(del_gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | /** |
| 671 | * get_gendisk - get partitioning information for a given device |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 672 | * @devt: device to get partitioning information for |
Randy Dunlap | 496aa8a | 2008-10-16 07:46:23 +0200 | [diff] [blame] | 673 | * @partno: returned partition index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * |
| 675 | * This function gets the structure containing partitioning |
Randy Dunlap | 710027a | 2008-08-19 20:13:11 +0200 | [diff] [blame] | 676 | * information for the given device @devt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | */ |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 678 | struct gendisk *get_gendisk(dev_t devt, int *partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 680 | struct gendisk *disk = NULL; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 681 | |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 682 | if (MAJOR(devt) != BLOCK_EXT_MAJOR) { |
| 683 | struct kobject *kobj; |
| 684 | |
| 685 | kobj = kobj_lookup(bdev_map, devt, partno); |
| 686 | if (kobj) |
| 687 | disk = dev_to_disk(kobj_to_dev(kobj)); |
| 688 | } else { |
| 689 | struct hd_struct *part; |
| 690 | |
| 691 | mutex_lock(&ext_devt_mutex); |
Tejun Heo | 870d665 | 2008-08-25 19:47:25 +0900 | [diff] [blame] | 692 | part = idr_find(&ext_devt_idr, blk_mangle_minor(MINOR(devt))); |
Tejun Heo | bcce3de | 2008-08-25 19:47:22 +0900 | [diff] [blame] | 693 | if (part && get_disk(part_to_disk(part))) { |
| 694 | *partno = part->partno; |
| 695 | disk = part_to_disk(part); |
| 696 | } |
| 697 | mutex_unlock(&ext_devt_mutex); |
| 698 | } |
| 699 | |
| 700 | return disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
Divyesh Shah | b6ac23af | 2010-04-15 08:54:59 +0200 | [diff] [blame] | 702 | EXPORT_SYMBOL(get_gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 704 | /** |
| 705 | * bdget_disk - do bdget() by gendisk and partition number |
| 706 | * @disk: gendisk of interest |
| 707 | * @partno: partition number |
| 708 | * |
| 709 | * Find partition @partno from @disk, do bdget() on it. |
| 710 | * |
| 711 | * CONTEXT: |
| 712 | * Don't care. |
| 713 | * |
| 714 | * RETURNS: |
| 715 | * Resulting block_device on success, NULL on failure. |
| 716 | */ |
Harvey Harrison | aeb3d3a | 2008-08-28 09:27:42 +0200 | [diff] [blame] | 717 | struct block_device *bdget_disk(struct gendisk *disk, int partno) |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 718 | { |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 719 | struct hd_struct *part; |
| 720 | struct block_device *bdev = NULL; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 721 | |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 722 | part = disk_get_part(disk, partno); |
Tejun Heo | 2bbedcb | 2008-08-29 11:41:51 +0200 | [diff] [blame] | 723 | if (part) |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 724 | bdev = bdget(part_devt(part)); |
| 725 | disk_put_part(part); |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 726 | |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 727 | return bdev; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 728 | } |
| 729 | EXPORT_SYMBOL(bdget_disk); |
| 730 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 731 | /* |
| 732 | * print a full list of all partitions - intended for places where the root |
| 733 | * filesystem can't be mounted and thus to give the victim some idea of what |
| 734 | * went wrong |
| 735 | */ |
| 736 | void __init printk_all_partitions(void) |
| 737 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 738 | struct class_dev_iter iter; |
| 739 | struct device *dev; |
| 740 | |
| 741 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 742 | while ((dev = class_dev_iter_next(&iter))) { |
| 743 | struct gendisk *disk = dev_to_disk(dev); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 744 | struct disk_part_iter piter; |
| 745 | struct hd_struct *part; |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 746 | char name_buf[BDEVNAME_SIZE]; |
| 747 | char devt_buf[BDEVT_SIZE]; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 748 | |
| 749 | /* |
| 750 | * Don't show empty devices or things that have been |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 751 | * suppressed |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 752 | */ |
| 753 | if (get_capacity(disk) == 0 || |
| 754 | (disk->flags & GENHD_FL_SUPPRESS_PARTITION_INFO)) |
| 755 | continue; |
| 756 | |
| 757 | /* |
| 758 | * Note, unlike /proc/partitions, I am showing the |
| 759 | * numbers in hex - the same format as the root= |
| 760 | * option takes. |
| 761 | */ |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 762 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_PART0); |
| 763 | while ((part = disk_part_iter_next(&piter))) { |
| 764 | bool is_part0 = part == &disk->part0; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 765 | |
Will Drewry | b5af921 | 2010-08-31 15:47:07 -0500 | [diff] [blame] | 766 | printk("%s%s %10llu %s %s", is_part0 ? "" : " ", |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 767 | bdevt_str(part_devt(part), devt_buf), |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 768 | (unsigned long long)part_nr_sects_read(part) >> 1 |
| 769 | , disk_name(disk, part->partno, name_buf), |
Stephen Warren | 1ad7e89 | 2012-11-08 16:12:25 -0800 | [diff] [blame] | 770 | part->info ? part->info->uuid : ""); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 771 | if (is_part0) { |
| 772 | if (disk->driverfs_dev != NULL && |
| 773 | disk->driverfs_dev->driver != NULL) |
| 774 | printk(" driver: %s\n", |
| 775 | disk->driverfs_dev->driver->name); |
| 776 | else |
| 777 | printk(" (driver?)\n"); |
| 778 | } else |
| 779 | printk("\n"); |
| 780 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 781 | disk_part_iter_exit(&piter); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 782 | } |
| 783 | class_dev_iter_exit(&iter); |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | #ifdef CONFIG_PROC_FS |
| 787 | /* iterator */ |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 788 | 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] | 789 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 790 | loff_t skip = *pos; |
| 791 | struct class_dev_iter *iter; |
| 792 | struct device *dev; |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 793 | |
Harvey Harrison | aeb3d3a | 2008-08-28 09:27:42 +0200 | [diff] [blame] | 794 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 795 | if (!iter) |
| 796 | return ERR_PTR(-ENOMEM); |
| 797 | |
| 798 | seqf->private = iter; |
| 799 | class_dev_iter_init(iter, &block_class, NULL, &disk_type); |
| 800 | do { |
| 801 | dev = class_dev_iter_next(iter); |
| 802 | if (!dev) |
| 803 | return NULL; |
| 804 | } while (skip--); |
| 805 | |
| 806 | return dev_to_disk(dev); |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 809 | 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] | 810 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 811 | struct device *dev; |
Greg Kroah-Hartman | 66c64af | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 812 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 813 | (*pos)++; |
| 814 | dev = class_dev_iter_next(seqf->private); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 815 | if (dev) |
Greg Kroah-Hartman | 68c4d4a | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 816 | return dev_to_disk(dev); |
Tejun Heo | 2ac3cee | 2008-09-03 08:53:37 +0200 | [diff] [blame] | 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | return NULL; |
| 819 | } |
| 820 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 821 | static void disk_seqf_stop(struct seq_file *seqf, void *v) |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 822 | { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 823 | struct class_dev_iter *iter = seqf->private; |
Greg Kroah-Hartman | 27f3025 | 2008-05-22 17:21:08 -0400 | [diff] [blame] | 824 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 825 | /* stop is called even after start failed :-( */ |
| 826 | if (iter) { |
| 827 | class_dev_iter_exit(iter); |
| 828 | kfree(iter); |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 832 | static void *show_partition_start(struct seq_file *seqf, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Jianpeng Ma | 0676806 | 2012-08-03 10:42:00 +0200 | [diff] [blame] | 834 | void *p; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 835 | |
| 836 | p = disk_seqf_start(seqf, pos); |
Yang Zhang | b9f985b | 2010-12-17 08:58:36 +0100 | [diff] [blame] | 837 | if (!IS_ERR_OR_NULL(p) && !*pos) |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 838 | seq_puts(seqf, "major minor #blocks name\n\n"); |
| 839 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 842 | static int show_partition(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
| 844 | struct gendisk *sgp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 845 | struct disk_part_iter piter; |
| 846 | struct hd_struct *part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | char buf[BDEVNAME_SIZE]; |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* Don't show non-partitionable removeable devices or empty devices */ |
Tejun Heo | d27769e | 2011-08-23 20:01:04 +0200 | [diff] [blame] | 850 | if (!get_capacity(sgp) || (!disk_max_parts(sgp) && |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 851 | (sgp->flags & GENHD_FL_REMOVABLE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | return 0; |
| 853 | if (sgp->flags & GENHD_FL_SUPPRESS_PARTITION_INFO) |
| 854 | return 0; |
| 855 | |
| 856 | /* show the full disk and all non-0 size partitions of it */ |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 857 | disk_part_iter_init(&piter, sgp, DISK_PITER_INCL_PART0); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 858 | while ((part = disk_part_iter_next(&piter))) |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 859 | seq_printf(seqf, "%4d %7d %10llu %s\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 860 | MAJOR(part_devt(part)), MINOR(part_devt(part)), |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 861 | (unsigned long long)part_nr_sects_read(part) >> 1, |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 862 | disk_name(sgp, part->partno, buf)); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 863 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 868 | static const struct seq_operations partitions_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 869 | .start = show_partition_start, |
| 870 | .next = disk_seqf_next, |
| 871 | .stop = disk_seqf_stop, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 872 | .show = show_partition |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | }; |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 874 | |
| 875 | static int partitions_open(struct inode *inode, struct file *file) |
| 876 | { |
| 877 | return seq_open(file, &partitions_op); |
| 878 | } |
| 879 | |
| 880 | static const struct file_operations proc_partitions_operations = { |
| 881 | .open = partitions_open, |
| 882 | .read = seq_read, |
| 883 | .llseek = seq_lseek, |
| 884 | .release = seq_release, |
| 885 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | #endif |
| 887 | |
| 888 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 889 | static struct kobject *base_probe(dev_t devt, int *partno, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 891 | if (request_module("block-major-%d-%d", MAJOR(devt), MINOR(devt)) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* Make old-style 2.4 aliases work */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 893 | request_module("block-major-%d", MAJOR(devt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return NULL; |
| 895 | } |
| 896 | |
| 897 | static int __init genhd_device_init(void) |
| 898 | { |
Dan Williams | e105b8b | 2008-04-21 10:51:07 -0700 | [diff] [blame] | 899 | int error; |
| 900 | |
| 901 | block_class.dev_kobj = sysfs_dev_block_kobj; |
| 902 | error = class_register(&block_class); |
Roland McGrath | ee27a55 | 2008-03-11 17:13:15 -0700 | [diff] [blame] | 903 | if (unlikely(error)) |
| 904 | return error; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 905 | bdev_map = kobj_map_init(base_probe, &block_class_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | blk_dev_init(); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 907 | |
Zhang, Yanmin | 561ec68 | 2008-11-14 08:26:30 +0100 | [diff] [blame] | 908 | register_blkdev(BLOCK_EXT_MAJOR, "blkext"); |
| 909 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 910 | /* create top-level block dir */ |
Andi Kleen | e52eec1 | 2010-09-08 16:54:17 +0200 | [diff] [blame] | 911 | if (!sysfs_deprecated) |
| 912 | block_depr = kobject_create_and_add("block", NULL); |
Greg Kroah-Hartman | 830d3cf | 2007-11-06 10:36:58 -0800 | [diff] [blame] | 913 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | subsys_initcall(genhd_device_init); |
| 917 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 918 | static ssize_t disk_range_show(struct device *dev, |
| 919 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 921 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 923 | return sprintf(buf, "%d\n", disk->minors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 926 | static ssize_t disk_ext_range_show(struct device *dev, |
| 927 | struct device_attribute *attr, char *buf) |
| 928 | { |
| 929 | struct gendisk *disk = dev_to_disk(dev); |
| 930 | |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 931 | return sprintf(buf, "%d\n", disk_max_parts(disk)); |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 932 | } |
| 933 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 934 | static ssize_t disk_removable_show(struct device *dev, |
| 935 | struct device_attribute *attr, char *buf) |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 936 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 937 | struct gendisk *disk = dev_to_disk(dev); |
Kay Sievers | a7fd670 | 2005-10-01 14:49:43 +0200 | [diff] [blame] | 938 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 939 | return sprintf(buf, "%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | (disk->flags & GENHD_FL_REMOVABLE ? 1 : 0)); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 943 | static ssize_t disk_ro_show(struct device *dev, |
| 944 | struct device_attribute *attr, char *buf) |
| 945 | { |
| 946 | struct gendisk *disk = dev_to_disk(dev); |
| 947 | |
Tejun Heo | b7db995 | 2008-08-25 19:56:10 +0900 | [diff] [blame] | 948 | return sprintf(buf, "%d\n", get_disk_ro(disk) ? 1 : 0); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 951 | static ssize_t disk_capability_show(struct device *dev, |
| 952 | struct device_attribute *attr, char *buf) |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 953 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 954 | struct gendisk *disk = dev_to_disk(dev); |
| 955 | |
| 956 | return sprintf(buf, "%x\n", disk->flags); |
Kristen Carlson Accardi | 86ce18d | 2007-05-23 13:57:38 -0700 | [diff] [blame] | 957 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 958 | |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 959 | static ssize_t disk_alignment_offset_show(struct device *dev, |
| 960 | struct device_attribute *attr, |
| 961 | char *buf) |
| 962 | { |
| 963 | struct gendisk *disk = dev_to_disk(dev); |
| 964 | |
| 965 | return sprintf(buf, "%d\n", queue_alignment_offset(disk->queue)); |
| 966 | } |
| 967 | |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 968 | static ssize_t disk_discard_alignment_show(struct device *dev, |
| 969 | struct device_attribute *attr, |
| 970 | char *buf) |
| 971 | { |
| 972 | struct gendisk *disk = dev_to_disk(dev); |
| 973 | |
Martin K. Petersen | dd3d145 | 2010-01-11 03:21:48 -0500 | [diff] [blame] | 974 | return sprintf(buf, "%d\n", queue_discard_alignment(disk->queue)); |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 977 | static DEVICE_ATTR(range, S_IRUGO, disk_range_show, NULL); |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 978 | static DEVICE_ATTR(ext_range, S_IRUGO, disk_ext_range_show, NULL); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 979 | static DEVICE_ATTR(removable, S_IRUGO, disk_removable_show, NULL); |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 980 | static DEVICE_ATTR(ro, S_IRUGO, disk_ro_show, NULL); |
Tejun Heo | e561052 | 2008-08-25 19:56:09 +0900 | [diff] [blame] | 981 | static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL); |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 982 | 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] | 983 | static DEVICE_ATTR(discard_alignment, S_IRUGO, disk_discard_alignment_show, |
| 984 | NULL); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 985 | static DEVICE_ATTR(capability, S_IRUGO, disk_capability_show, NULL); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 986 | static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL); |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 987 | static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL); |
Akinobu Mita | c17bb49 | 2006-12-08 02:39:46 -0800 | [diff] [blame] | 988 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 989 | static struct device_attribute dev_attr_fail = |
Tejun Heo | eddb2e2 | 2008-08-25 19:56:13 +0900 | [diff] [blame] | 990 | __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] | 991 | #endif |
Jens Axboe | 581d4e2 | 2008-09-14 05:56:33 -0700 | [diff] [blame] | 992 | #ifdef CONFIG_FAIL_IO_TIMEOUT |
| 993 | static struct device_attribute dev_attr_fail_timeout = |
| 994 | __ATTR(io-timeout-fail, S_IRUGO|S_IWUSR, part_timeout_show, |
| 995 | part_timeout_store); |
| 996 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 997 | |
| 998 | static struct attribute *disk_attrs[] = { |
| 999 | &dev_attr_range.attr, |
Tejun Heo | 1f01429 | 2008-08-25 19:47:23 +0900 | [diff] [blame] | 1000 | &dev_attr_ext_range.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1001 | &dev_attr_removable.attr, |
Kay Sievers | 1c9ce52 | 2008-06-13 09:41:00 +0200 | [diff] [blame] | 1002 | &dev_attr_ro.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1003 | &dev_attr_size.attr, |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 1004 | &dev_attr_alignment_offset.attr, |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 1005 | &dev_attr_discard_alignment.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1006 | &dev_attr_capability.attr, |
| 1007 | &dev_attr_stat.attr, |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 1008 | &dev_attr_inflight.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1009 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
| 1010 | &dev_attr_fail.attr, |
| 1011 | #endif |
Jens Axboe | 581d4e2 | 2008-09-14 05:56:33 -0700 | [diff] [blame] | 1012 | #ifdef CONFIG_FAIL_IO_TIMEOUT |
| 1013 | &dev_attr_fail_timeout.attr, |
| 1014 | #endif |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1015 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | }; |
| 1017 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1018 | static struct attribute_group disk_attr_group = { |
| 1019 | .attrs = disk_attrs, |
| 1020 | }; |
| 1021 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 1022 | static const struct attribute_group *disk_attr_groups[] = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1023 | &disk_attr_group, |
| 1024 | NULL |
| 1025 | }; |
| 1026 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1027 | /** |
| 1028 | * disk_replace_part_tbl - replace disk->part_tbl in RCU-safe way |
| 1029 | * @disk: disk to replace part_tbl for |
| 1030 | * @new_ptbl: new part_tbl to install |
| 1031 | * |
| 1032 | * Replace disk->part_tbl with @new_ptbl in RCU-safe way. The |
| 1033 | * original ptbl is freed using RCU callback. |
| 1034 | * |
| 1035 | * LOCKING: |
| 1036 | * Matching bd_mutx locked. |
| 1037 | */ |
| 1038 | static void disk_replace_part_tbl(struct gendisk *disk, |
| 1039 | struct disk_part_tbl *new_ptbl) |
| 1040 | { |
| 1041 | struct disk_part_tbl *old_ptbl = disk->part_tbl; |
| 1042 | |
| 1043 | rcu_assign_pointer(disk->part_tbl, new_ptbl); |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 1044 | |
| 1045 | if (old_ptbl) { |
| 1046 | rcu_assign_pointer(old_ptbl->last_lookup, NULL); |
Lai Jiangshan | 57bdfbf | 2011-03-18 11:42:58 +0800 | [diff] [blame] | 1047 | kfree_rcu(old_ptbl, rcu_head); |
Jens Axboe | a6f2365 | 2008-10-24 12:52:42 +0200 | [diff] [blame] | 1048 | } |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * disk_expand_part_tbl - expand disk->part_tbl |
| 1053 | * @disk: disk to expand part_tbl for |
| 1054 | * @partno: expand such that this partno can fit in |
| 1055 | * |
| 1056 | * Expand disk->part_tbl such that @partno can fit in. disk->part_tbl |
| 1057 | * uses RCU to allow unlocked dereferencing for stats and other stuff. |
| 1058 | * |
| 1059 | * LOCKING: |
| 1060 | * Matching bd_mutex locked, might sleep. |
| 1061 | * |
| 1062 | * RETURNS: |
| 1063 | * 0 on success, -errno on failure. |
| 1064 | */ |
| 1065 | int disk_expand_part_tbl(struct gendisk *disk, int partno) |
| 1066 | { |
| 1067 | struct disk_part_tbl *old_ptbl = disk->part_tbl; |
| 1068 | struct disk_part_tbl *new_ptbl; |
| 1069 | int len = old_ptbl ? old_ptbl->len : 0; |
| 1070 | int target = partno + 1; |
| 1071 | size_t size; |
| 1072 | int i; |
| 1073 | |
| 1074 | /* disk_max_parts() is zero during initialization, ignore if so */ |
| 1075 | if (disk_max_parts(disk) && target > disk_max_parts(disk)) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | if (target <= len) |
| 1079 | return 0; |
| 1080 | |
| 1081 | size = sizeof(*new_ptbl) + target * sizeof(new_ptbl->part[0]); |
| 1082 | new_ptbl = kzalloc_node(size, GFP_KERNEL, disk->node_id); |
| 1083 | if (!new_ptbl) |
| 1084 | return -ENOMEM; |
| 1085 | |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1086 | new_ptbl->len = target; |
| 1087 | |
| 1088 | for (i = 0; i < len; i++) |
| 1089 | rcu_assign_pointer(new_ptbl->part[i], old_ptbl->part[i]); |
| 1090 | |
| 1091 | disk_replace_part_tbl(disk, new_ptbl); |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1095 | static void disk_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1097 | struct gendisk *disk = dev_to_disk(dev); |
| 1098 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1099 | disk_release_events(disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | kfree(disk->random); |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1101 | disk_replace_part_tbl(disk, NULL); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 1102 | free_part_stats(&disk->part0); |
Will Drewry | 6d1d805 | 2010-08-31 15:47:05 -0500 | [diff] [blame] | 1103 | free_part_info(&disk->part0); |
Tejun Heo | 523e1d3 | 2011-10-19 14:31:07 +0200 | [diff] [blame] | 1104 | if (disk->queue) |
| 1105 | blk_put_queue(disk->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | kfree(disk); |
| 1107 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1108 | struct class block_class = { |
| 1109 | .name = "block", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | }; |
| 1111 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 1112 | static char *block_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | b03f38b | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1113 | { |
| 1114 | struct gendisk *disk = dev_to_disk(dev); |
| 1115 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1116 | if (disk->devnode) |
| 1117 | return disk->devnode(disk, mode); |
Kay Sievers | b03f38b | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Adrian Bunk | 1826ead | 2008-03-04 11:23:46 +0100 | [diff] [blame] | 1121 | static struct device_type disk_type = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1122 | .name = "disk", |
| 1123 | .groups = disk_attr_groups, |
| 1124 | .release = disk_release, |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1125 | .devnode = block_devnode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | }; |
| 1127 | |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 1128 | #ifdef CONFIG_PROC_FS |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1129 | /* |
| 1130 | * aggregate disk stat collector. Uses the same stats that the sysfs |
| 1131 | * entries do, above, but makes them available through one seq_file. |
| 1132 | * |
| 1133 | * The output looks suspiciously like /proc/partitions with a bunch of |
| 1134 | * extra fields. |
| 1135 | */ |
| 1136 | static int diskstats_show(struct seq_file *seqf, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
| 1138 | struct gendisk *gp = v; |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1139 | struct disk_part_iter piter; |
| 1140 | struct hd_struct *hd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | char buf[BDEVNAME_SIZE]; |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 1142 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
| 1144 | /* |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1145 | if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next) |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1146 | seq_puts(seqf, "major minor name" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | " rio rmerge rsect ruse wio wmerge " |
| 1148 | "wsect wuse running use aveq" |
| 1149 | "\n\n"); |
| 1150 | */ |
Wanlong Gao | 9f5e486 | 2011-06-13 10:45:43 +0200 | [diff] [blame] | 1151 | |
Tejun Heo | 71982a4 | 2009-04-17 08:34:48 +0200 | [diff] [blame] | 1152 | disk_part_iter_init(&piter, gp, DISK_PITER_INCL_EMPTY_PART0); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1153 | while ((hd = disk_part_iter_next(&piter))) { |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 1154 | cpu = part_stat_lock(); |
Tejun Heo | c995905 | 2008-08-25 19:47:21 +0900 | [diff] [blame] | 1155 | part_round_stats(cpu, hd); |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 1156 | part_stat_unlock(); |
Herbert Poetzl | f95fe9c | 2011-08-02 12:43:50 +0200 | [diff] [blame] | 1157 | seq_printf(seqf, "%4d %7d %s %lu %lu %lu " |
| 1158 | "%u %lu %lu %lu %u %u %u %u\n", |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1159 | MAJOR(part_devt(hd)), MINOR(part_devt(hd)), |
| 1160 | disk_name(gp, hd->partno, buf), |
Liu Yuan | 53f2295 | 2011-03-02 11:00:15 -0500 | [diff] [blame] | 1161 | part_stat_read(hd, ios[READ]), |
| 1162 | part_stat_read(hd, merges[READ]), |
Herbert Poetzl | f95fe9c | 2011-08-02 12:43:50 +0200 | [diff] [blame] | 1163 | part_stat_read(hd, sectors[READ]), |
Liu Yuan | 53f2295 | 2011-03-02 11:00:15 -0500 | [diff] [blame] | 1164 | jiffies_to_msecs(part_stat_read(hd, ticks[READ])), |
| 1165 | part_stat_read(hd, ios[WRITE]), |
| 1166 | part_stat_read(hd, merges[WRITE]), |
Herbert Poetzl | f95fe9c | 2011-08-02 12:43:50 +0200 | [diff] [blame] | 1167 | part_stat_read(hd, sectors[WRITE]), |
Liu Yuan | 53f2295 | 2011-03-02 11:00:15 -0500 | [diff] [blame] | 1168 | jiffies_to_msecs(part_stat_read(hd, ticks[WRITE])), |
Nikanth Karthikesan | 316d315 | 2009-10-06 20:16:55 +0200 | [diff] [blame] | 1169 | part_in_flight(hd), |
Jerome Marchand | 28f39d5 | 2008-02-08 11:04:56 +0100 | [diff] [blame] | 1170 | jiffies_to_msecs(part_stat_read(hd, io_ticks)), |
| 1171 | jiffies_to_msecs(part_stat_read(hd, time_in_queue)) |
| 1172 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | } |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1174 | disk_part_iter_exit(&piter); |
Wanlong Gao | 9f5e486 | 2011-06-13 10:45:43 +0200 | [diff] [blame] | 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Alexey Dobriyan | 31d85ab | 2008-10-06 12:55:38 +0400 | [diff] [blame] | 1179 | static const struct seq_operations diskstats_op = { |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1180 | .start = disk_seqf_start, |
| 1181 | .next = disk_seqf_next, |
| 1182 | .stop = disk_seqf_stop, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | .show = diskstats_show |
| 1184 | }; |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 1185 | |
Alexey Dobriyan | 31d85ab | 2008-10-06 12:55:38 +0400 | [diff] [blame] | 1186 | static int diskstats_open(struct inode *inode, struct file *file) |
| 1187 | { |
| 1188 | return seq_open(file, &diskstats_op); |
| 1189 | } |
| 1190 | |
| 1191 | static const struct file_operations proc_diskstats_operations = { |
| 1192 | .open = diskstats_open, |
| 1193 | .read = seq_read, |
| 1194 | .llseek = seq_lseek, |
| 1195 | .release = seq_release, |
| 1196 | }; |
| 1197 | |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 1198 | static int __init proc_genhd_init(void) |
| 1199 | { |
Alexey Dobriyan | 31d85ab | 2008-10-06 12:55:38 +0400 | [diff] [blame] | 1200 | proc_create("diskstats", 0, NULL, &proc_diskstats_operations); |
Alexey Dobriyan | f500975 | 2008-10-04 23:53:21 +0400 | [diff] [blame] | 1201 | proc_create("partitions", 0, NULL, &proc_partitions_operations); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | module_init(proc_genhd_init); |
Randy Dunlap | a6e2ba8 | 2008-05-23 09:44:11 -0700 | [diff] [blame] | 1205 | #endif /* CONFIG_PROC_FS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1207 | dev_t blk_lookup_devt(const char *name, int partno) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1208 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1209 | dev_t devt = MKDEV(0, 0); |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1210 | struct class_dev_iter iter; |
| 1211 | struct device *dev; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1212 | |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1213 | class_dev_iter_init(&iter, &block_class, NULL, &disk_type); |
| 1214 | while ((dev = class_dev_iter_next(&iter))) { |
| 1215 | struct gendisk *disk = dev_to_disk(dev); |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 1216 | struct hd_struct *part; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1217 | |
Kay Sievers | 3ada8b7 | 2009-01-06 10:44:43 -0800 | [diff] [blame] | 1218 | if (strcmp(dev_name(dev), name)) |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1219 | continue; |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1220 | |
Neil Brown | 41b8c85 | 2009-02-18 10:33:59 +0100 | [diff] [blame] | 1221 | if (partno < disk->minors) { |
| 1222 | /* We need to return the right devno, even |
| 1223 | * if the partition doesn't exist yet. |
| 1224 | */ |
| 1225 | devt = MKDEV(MAJOR(dev->devt), |
| 1226 | MINOR(dev->devt) + partno); |
| 1227 | break; |
| 1228 | } |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 1229 | part = disk_get_part(disk, partno); |
Tejun Heo | 2bbedcb | 2008-08-29 11:41:51 +0200 | [diff] [blame] | 1230 | if (part) { |
Tejun Heo | f331c02 | 2008-09-03 09:01:48 +0200 | [diff] [blame] | 1231 | devt = part_devt(part); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1232 | disk_put_part(part); |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 1233 | break; |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1234 | } |
Tejun Heo | 548b10e | 2008-08-29 09:01:47 +0200 | [diff] [blame] | 1235 | disk_put_part(part); |
Kay Sievers | 5c0ef6d | 2008-08-16 14:30:30 +0200 | [diff] [blame] | 1236 | } |
Tejun Heo | def4e38 | 2008-09-03 08:57:12 +0200 | [diff] [blame] | 1237 | class_dev_iter_exit(&iter); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1238 | return devt; |
| 1239 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1240 | EXPORT_SYMBOL(blk_lookup_devt); |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | struct gendisk *alloc_disk(int minors) |
| 1243 | { |
Ezequiel Garcia | c304a51 | 2012-11-10 10:39:44 +0100 | [diff] [blame] | 1244 | return alloc_disk_node(minors, NUMA_NO_NODE); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1245 | } |
Tejun Heo | 689d6fa | 2008-08-25 19:56:16 +0900 | [diff] [blame] | 1246 | EXPORT_SYMBOL(alloc_disk); |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1247 | |
| 1248 | struct gendisk *alloc_disk_node(int minors, int node_id) |
| 1249 | { |
| 1250 | struct gendisk *disk; |
| 1251 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 1252 | disk = kmalloc_node(sizeof(struct gendisk), |
| 1253 | GFP_KERNEL | __GFP_ZERO, node_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (disk) { |
Tejun Heo | 074a7ac | 2008-08-25 19:56:14 +0900 | [diff] [blame] | 1255 | if (!init_part_stats(&disk->part0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | kfree(disk); |
| 1257 | return NULL; |
| 1258 | } |
Cheng Renquan | bf91db1 | 2008-11-20 08:37:37 +0100 | [diff] [blame] | 1259 | disk->node_id = node_id; |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1260 | if (disk_expand_part_tbl(disk, 0)) { |
| 1261 | free_part_stats(&disk->part0); |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 1262 | kfree(disk); |
| 1263 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
Tejun Heo | 540eed5 | 2008-08-25 19:56:15 +0900 | [diff] [blame] | 1265 | disk->part_tbl->part[0] = &disk->part0; |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 1266 | |
Vivek Goyal | c83f6bf | 2012-08-01 12:24:18 +0200 | [diff] [blame] | 1267 | /* |
| 1268 | * set_capacity() and get_capacity() currently don't use |
| 1269 | * seqcounter to read/update the part0->nr_sects. Still init |
| 1270 | * the counter as we can read the sectors in IO submission |
| 1271 | * patch using seqence counters. |
| 1272 | * |
| 1273 | * TODO: Ideally set_capacity() and get_capacity() should be |
| 1274 | * converted to make use of bd_mutex and sequence counters. |
| 1275 | */ |
| 1276 | seqcount_init(&disk->part0.nr_sects_seq); |
Jens Axboe | 6c23a96 | 2011-01-07 08:43:37 +0100 | [diff] [blame] | 1277 | hd_ref_init(&disk->part0); |
Tejun Heo | b5d0b9d | 2008-09-03 09:06:42 +0200 | [diff] [blame] | 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | disk->minors = minors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | rand_initialize_disk(disk); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1281 | disk_to_dev(disk)->class = &block_class; |
| 1282 | disk_to_dev(disk)->type = &disk_type; |
| 1283 | device_initialize(disk_to_dev(disk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | } |
| 1285 | return disk; |
| 1286 | } |
Christoph Lameter | 1946089 | 2005-06-23 00:08:19 -0700 | [diff] [blame] | 1287 | EXPORT_SYMBOL(alloc_disk_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | |
| 1289 | struct kobject *get_disk(struct gendisk *disk) |
| 1290 | { |
| 1291 | struct module *owner; |
| 1292 | struct kobject *kobj; |
| 1293 | |
| 1294 | if (!disk->fops) |
| 1295 | return NULL; |
| 1296 | owner = disk->fops->owner; |
| 1297 | if (owner && !try_module_get(owner)) |
| 1298 | return NULL; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1299 | kobj = kobject_get(&disk_to_dev(disk)->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | if (kobj == NULL) { |
| 1301 | module_put(owner); |
| 1302 | return NULL; |
| 1303 | } |
| 1304 | return kobj; |
| 1305 | |
| 1306 | } |
| 1307 | |
| 1308 | EXPORT_SYMBOL(get_disk); |
| 1309 | |
| 1310 | void put_disk(struct gendisk *disk) |
| 1311 | { |
| 1312 | if (disk) |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 1313 | kobject_put(&disk_to_dev(disk)->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | EXPORT_SYMBOL(put_disk); |
| 1317 | |
Hannes Reinecke | e3264a4 | 2009-07-28 09:13:13 +0200 | [diff] [blame] | 1318 | static void set_disk_ro_uevent(struct gendisk *gd, int ro) |
| 1319 | { |
| 1320 | char event[] = "DISK_RO=1"; |
| 1321 | char *envp[] = { event, NULL }; |
| 1322 | |
| 1323 | if (!ro) |
| 1324 | event[8] = '0'; |
| 1325 | kobject_uevent_env(&disk_to_dev(gd)->kobj, KOBJ_CHANGE, envp); |
| 1326 | } |
| 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | void set_device_ro(struct block_device *bdev, int flag) |
| 1329 | { |
Tejun Heo | b7db995 | 2008-08-25 19:56:10 +0900 | [diff] [blame] | 1330 | bdev->bd_part->policy = flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | EXPORT_SYMBOL(set_device_ro); |
| 1334 | |
| 1335 | void set_disk_ro(struct gendisk *disk, int flag) |
| 1336 | { |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1337 | struct disk_part_iter piter; |
| 1338 | struct hd_struct *part; |
| 1339 | |
Hannes Reinecke | e3264a4 | 2009-07-28 09:13:13 +0200 | [diff] [blame] | 1340 | if (disk->part0.policy != flag) { |
| 1341 | set_disk_ro_uevent(disk, flag); |
| 1342 | disk->part0.policy = flag; |
| 1343 | } |
| 1344 | |
| 1345 | disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY); |
Tejun Heo | e71bf0d | 2008-09-03 09:03:02 +0200 | [diff] [blame] | 1346 | while ((part = disk_part_iter_next(&piter))) |
| 1347 | part->policy = flag; |
| 1348 | disk_part_iter_exit(&piter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | EXPORT_SYMBOL(set_disk_ro); |
| 1352 | |
| 1353 | int bdev_read_only(struct block_device *bdev) |
| 1354 | { |
| 1355 | if (!bdev) |
| 1356 | return 0; |
Tejun Heo | b7db995 | 2008-08-25 19:56:10 +0900 | [diff] [blame] | 1357 | return bdev->bd_part->policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | EXPORT_SYMBOL(bdev_read_only); |
| 1361 | |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1362 | int invalidate_partition(struct gendisk *disk, int partno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
| 1364 | int res = 0; |
Tejun Heo | cf771cb | 2008-09-03 09:01:09 +0200 | [diff] [blame] | 1365 | struct block_device *bdev = bdget_disk(disk, partno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | if (bdev) { |
Christoph Hellwig | 2ef4163 | 2005-05-05 16:15:59 -0700 | [diff] [blame] | 1367 | fsync_bdev(bdev); |
NeilBrown | 93b270f | 2011-02-24 17:25:47 +1100 | [diff] [blame] | 1368 | res = __invalidate_device(bdev, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | bdput(bdev); |
| 1370 | } |
| 1371 | return res; |
| 1372 | } |
| 1373 | |
| 1374 | EXPORT_SYMBOL(invalidate_partition); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * Disk events - monitor disk events like media change and eject request. |
| 1378 | */ |
| 1379 | struct disk_events { |
| 1380 | struct list_head node; /* all disk_event's */ |
| 1381 | struct gendisk *disk; /* the associated disk */ |
| 1382 | spinlock_t lock; |
| 1383 | |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1384 | struct mutex block_mutex; /* protects blocking */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1385 | int block; /* event blocking depth */ |
| 1386 | unsigned int pending; /* events already sent out */ |
| 1387 | unsigned int clearing; /* events being cleared */ |
| 1388 | |
| 1389 | long poll_msecs; /* interval, -1 for default */ |
| 1390 | struct delayed_work dwork; |
| 1391 | }; |
| 1392 | |
| 1393 | static const char *disk_events_strs[] = { |
| 1394 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change", |
| 1395 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request", |
| 1396 | }; |
| 1397 | |
| 1398 | static char *disk_uevents[] = { |
| 1399 | [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1", |
| 1400 | [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1", |
| 1401 | }; |
| 1402 | |
| 1403 | /* list of all disk_events */ |
| 1404 | static DEFINE_MUTEX(disk_events_mutex); |
| 1405 | static LIST_HEAD(disk_events); |
| 1406 | |
| 1407 | /* disable in-kernel polling by default */ |
| 1408 | static unsigned long disk_events_dfl_poll_msecs = 0; |
| 1409 | |
| 1410 | static unsigned long disk_events_poll_jiffies(struct gendisk *disk) |
| 1411 | { |
| 1412 | struct disk_events *ev = disk->ev; |
| 1413 | long intv_msecs = 0; |
| 1414 | |
| 1415 | /* |
| 1416 | * If device-specific poll interval is set, always use it. If |
| 1417 | * the default is being used, poll iff there are events which |
| 1418 | * can't be monitored asynchronously. |
| 1419 | */ |
| 1420 | if (ev->poll_msecs >= 0) |
| 1421 | intv_msecs = ev->poll_msecs; |
| 1422 | else if (disk->events & ~disk->async_events) |
| 1423 | intv_msecs = disk_events_dfl_poll_msecs; |
| 1424 | |
| 1425 | return msecs_to_jiffies(intv_msecs); |
| 1426 | } |
| 1427 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1428 | /** |
| 1429 | * disk_block_events - block and flush disk event checking |
| 1430 | * @disk: disk to block events for |
| 1431 | * |
| 1432 | * On return from this function, it is guaranteed that event checking |
| 1433 | * isn't in progress and won't happen until unblocked by |
| 1434 | * disk_unblock_events(). Events blocking is counted and the actual |
| 1435 | * unblocking happens after the matching number of unblocks are done. |
| 1436 | * |
| 1437 | * Note that this intentionally does not block event checking from |
| 1438 | * disk_clear_events(). |
| 1439 | * |
| 1440 | * CONTEXT: |
| 1441 | * Might sleep. |
| 1442 | */ |
| 1443 | void disk_block_events(struct gendisk *disk) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1444 | { |
| 1445 | struct disk_events *ev = disk->ev; |
| 1446 | unsigned long flags; |
| 1447 | bool cancel; |
| 1448 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1449 | if (!ev) |
| 1450 | return; |
| 1451 | |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1452 | /* |
| 1453 | * Outer mutex ensures that the first blocker completes canceling |
| 1454 | * the event work before further blockers are allowed to finish. |
| 1455 | */ |
| 1456 | mutex_lock(&ev->block_mutex); |
| 1457 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1458 | spin_lock_irqsave(&ev->lock, flags); |
| 1459 | cancel = !ev->block++; |
| 1460 | spin_unlock_irqrestore(&ev->lock, flags); |
| 1461 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1462 | if (cancel) |
| 1463 | cancel_delayed_work_sync(&disk->ev->dwork); |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1464 | |
| 1465 | mutex_unlock(&ev->block_mutex); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static void __disk_unblock_events(struct gendisk *disk, bool check_now) |
| 1469 | { |
| 1470 | struct disk_events *ev = disk->ev; |
| 1471 | unsigned long intv; |
| 1472 | unsigned long flags; |
| 1473 | |
| 1474 | spin_lock_irqsave(&ev->lock, flags); |
| 1475 | |
| 1476 | if (WARN_ON_ONCE(ev->block <= 0)) |
| 1477 | goto out_unlock; |
| 1478 | |
| 1479 | if (--ev->block) |
| 1480 | goto out_unlock; |
| 1481 | |
| 1482 | /* |
| 1483 | * Not exactly a latency critical operation, set poll timer |
| 1484 | * slack to 25% and kick event check. |
| 1485 | */ |
| 1486 | intv = disk_events_poll_jiffies(disk); |
| 1487 | set_timer_slack(&ev->dwork.timer, intv / 4); |
| 1488 | if (check_now) |
Tejun Heo | 3b07e9c | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1489 | queue_delayed_work(system_freezable_wq, &ev->dwork, 0); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1490 | else if (intv) |
Tejun Heo | 3b07e9c | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1491 | queue_delayed_work(system_freezable_wq, &ev->dwork, intv); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1492 | out_unlock: |
| 1493 | spin_unlock_irqrestore(&ev->lock, flags); |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1497 | * disk_unblock_events - unblock disk event checking |
| 1498 | * @disk: disk to unblock events for |
| 1499 | * |
| 1500 | * Undo disk_block_events(). When the block count reaches zero, it |
| 1501 | * starts events polling if configured. |
| 1502 | * |
| 1503 | * CONTEXT: |
| 1504 | * Don't care. Safe to call from irq context. |
| 1505 | */ |
| 1506 | void disk_unblock_events(struct gendisk *disk) |
| 1507 | { |
| 1508 | if (disk->ev) |
Tejun Heo | facc31d | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1509 | __disk_unblock_events(disk, false); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /** |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1513 | * disk_flush_events - schedule immediate event checking and flushing |
| 1514 | * @disk: disk to check and flush events for |
| 1515 | * @mask: events to flush |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1516 | * |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1517 | * Schedule immediate event checking on @disk if not blocked. Events in |
| 1518 | * @mask are scheduled to be cleared from the driver. Note that this |
| 1519 | * doesn't clear the events from @disk->ev. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1520 | * |
| 1521 | * CONTEXT: |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1522 | * If @mask is non-zero must be called with bdev->bd_mutex held. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1523 | */ |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1524 | void disk_flush_events(struct gendisk *disk, unsigned int mask) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1525 | { |
Tejun Heo | a9dce2a | 2011-06-09 20:43:54 +0200 | [diff] [blame] | 1526 | struct disk_events *ev = disk->ev; |
Tejun Heo | a9dce2a | 2011-06-09 20:43:54 +0200 | [diff] [blame] | 1527 | |
| 1528 | if (!ev) |
| 1529 | return; |
| 1530 | |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1531 | spin_lock_irq(&ev->lock); |
| 1532 | ev->clearing |= mask; |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 1533 | if (!ev->block) |
Tejun Heo | 3b07e9c | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1534 | mod_delayed_work(system_freezable_wq, &ev->dwork, 0); |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1535 | spin_unlock_irq(&ev->lock); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1536 | } |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1537 | |
| 1538 | /** |
| 1539 | * disk_clear_events - synchronously check, clear and return pending events |
| 1540 | * @disk: disk to fetch and clear events from |
| 1541 | * @mask: mask of events to be fetched and clearted |
| 1542 | * |
| 1543 | * Disk events are synchronously checked and pending events in @mask |
| 1544 | * are cleared and returned. This ignores the block count. |
| 1545 | * |
| 1546 | * CONTEXT: |
| 1547 | * Might sleep. |
| 1548 | */ |
| 1549 | unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask) |
| 1550 | { |
| 1551 | const struct block_device_operations *bdops = disk->fops; |
| 1552 | struct disk_events *ev = disk->ev; |
| 1553 | unsigned int pending; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1554 | unsigned int clearing = mask; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1555 | |
| 1556 | if (!ev) { |
| 1557 | /* for drivers still using the old ->media_changed method */ |
| 1558 | if ((mask & DISK_EVENT_MEDIA_CHANGE) && |
| 1559 | bdops->media_changed && bdops->media_changed(disk)) |
| 1560 | return DISK_EVENT_MEDIA_CHANGE; |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1564 | disk_block_events(disk); |
| 1565 | |
| 1566 | /* |
| 1567 | * store the union of mask and ev->clearing on the stack so that the |
| 1568 | * race with disk_flush_events does not cause ambiguity (ev->clearing |
| 1569 | * can still be modified even if events are blocked). |
| 1570 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1571 | spin_lock_irq(&ev->lock); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1572 | clearing |= ev->clearing; |
| 1573 | ev->clearing = 0; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1574 | spin_unlock_irq(&ev->lock); |
| 1575 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1576 | disk_check_events(ev, &clearing); |
Derek Basehore | aea24a8 | 2012-12-18 12:27:18 -0800 | [diff] [blame] | 1577 | /* |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1578 | * if ev->clearing is not 0, the disk_flush_events got called in the |
| 1579 | * middle of this function, so we want to run the workfn without delay. |
Derek Basehore | aea24a8 | 2012-12-18 12:27:18 -0800 | [diff] [blame] | 1580 | */ |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1581 | __disk_unblock_events(disk, ev->clearing ? true : false); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1582 | |
| 1583 | /* then, fetch and clear pending events */ |
| 1584 | spin_lock_irq(&ev->lock); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1585 | pending = ev->pending & mask; |
| 1586 | ev->pending &= ~mask; |
| 1587 | spin_unlock_irq(&ev->lock); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1588 | WARN_ON_ONCE(clearing & mask); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1589 | |
| 1590 | return pending; |
| 1591 | } |
| 1592 | |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1593 | /* |
| 1594 | * Separate this part out so that a different pointer for clearing_ptr can be |
| 1595 | * passed in for disk_clear_events. |
| 1596 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1597 | static void disk_events_workfn(struct work_struct *work) |
| 1598 | { |
| 1599 | struct delayed_work *dwork = to_delayed_work(work); |
| 1600 | struct disk_events *ev = container_of(dwork, struct disk_events, dwork); |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1601 | |
| 1602 | disk_check_events(ev, &ev->clearing); |
| 1603 | } |
| 1604 | |
| 1605 | static void disk_check_events(struct disk_events *ev, |
| 1606 | unsigned int *clearing_ptr) |
| 1607 | { |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1608 | struct gendisk *disk = ev->disk; |
| 1609 | char *envp[ARRAY_SIZE(disk_uevents) + 1] = { }; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1610 | unsigned int clearing = *clearing_ptr; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1611 | unsigned int events; |
| 1612 | unsigned long intv; |
| 1613 | int nr_events = 0, i; |
| 1614 | |
| 1615 | /* check events */ |
| 1616 | events = disk->fops->check_events(disk, clearing); |
| 1617 | |
| 1618 | /* accumulate pending events and schedule next poll if necessary */ |
| 1619 | spin_lock_irq(&ev->lock); |
| 1620 | |
| 1621 | events &= ~ev->pending; |
| 1622 | ev->pending |= events; |
Derek Basehore | 12c2bdb | 2012-12-18 12:27:20 -0800 | [diff] [blame^] | 1623 | *clearing_ptr &= ~clearing; |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1624 | |
| 1625 | intv = disk_events_poll_jiffies(disk); |
| 1626 | if (!ev->block && intv) |
Tejun Heo | 3b07e9c | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1627 | queue_delayed_work(system_freezable_wq, &ev->dwork, intv); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1628 | |
| 1629 | spin_unlock_irq(&ev->lock); |
| 1630 | |
Tejun Heo | 7c88a16 | 2011-04-21 19:43:58 +0200 | [diff] [blame] | 1631 | /* |
| 1632 | * Tell userland about new events. Only the events listed in |
| 1633 | * @disk->events are reported. Unlisted events are processed the |
| 1634 | * same internally but never get reported to userland. |
| 1635 | */ |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1636 | for (i = 0; i < ARRAY_SIZE(disk_uevents); i++) |
Tejun Heo | 7c88a16 | 2011-04-21 19:43:58 +0200 | [diff] [blame] | 1637 | if (events & disk->events & (1 << i)) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1638 | envp[nr_events++] = disk_uevents[i]; |
| 1639 | |
| 1640 | if (nr_events) |
| 1641 | kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp); |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * A disk events enabled device has the following sysfs nodes under |
| 1646 | * its /sys/block/X/ directory. |
| 1647 | * |
| 1648 | * events : list of all supported events |
| 1649 | * events_async : list of events which can be detected w/o polling |
| 1650 | * events_poll_msecs : polling interval, 0: disable, -1: system default |
| 1651 | */ |
| 1652 | static ssize_t __disk_events_show(unsigned int events, char *buf) |
| 1653 | { |
| 1654 | const char *delim = ""; |
| 1655 | ssize_t pos = 0; |
| 1656 | int i; |
| 1657 | |
| 1658 | for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++) |
| 1659 | if (events & (1 << i)) { |
| 1660 | pos += sprintf(buf + pos, "%s%s", |
| 1661 | delim, disk_events_strs[i]); |
| 1662 | delim = " "; |
| 1663 | } |
| 1664 | if (pos) |
| 1665 | pos += sprintf(buf + pos, "\n"); |
| 1666 | return pos; |
| 1667 | } |
| 1668 | |
| 1669 | static ssize_t disk_events_show(struct device *dev, |
| 1670 | struct device_attribute *attr, char *buf) |
| 1671 | { |
| 1672 | struct gendisk *disk = dev_to_disk(dev); |
| 1673 | |
| 1674 | return __disk_events_show(disk->events, buf); |
| 1675 | } |
| 1676 | |
| 1677 | static ssize_t disk_events_async_show(struct device *dev, |
| 1678 | struct device_attribute *attr, char *buf) |
| 1679 | { |
| 1680 | struct gendisk *disk = dev_to_disk(dev); |
| 1681 | |
| 1682 | return __disk_events_show(disk->async_events, buf); |
| 1683 | } |
| 1684 | |
| 1685 | static ssize_t disk_events_poll_msecs_show(struct device *dev, |
| 1686 | struct device_attribute *attr, |
| 1687 | char *buf) |
| 1688 | { |
| 1689 | struct gendisk *disk = dev_to_disk(dev); |
| 1690 | |
| 1691 | return sprintf(buf, "%ld\n", disk->ev->poll_msecs); |
| 1692 | } |
| 1693 | |
| 1694 | static ssize_t disk_events_poll_msecs_store(struct device *dev, |
| 1695 | struct device_attribute *attr, |
| 1696 | const char *buf, size_t count) |
| 1697 | { |
| 1698 | struct gendisk *disk = dev_to_disk(dev); |
| 1699 | long intv; |
| 1700 | |
| 1701 | if (!count || !sscanf(buf, "%ld", &intv)) |
| 1702 | return -EINVAL; |
| 1703 | |
| 1704 | if (intv < 0 && intv != -1) |
| 1705 | return -EINVAL; |
| 1706 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1707 | disk_block_events(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1708 | disk->ev->poll_msecs = intv; |
| 1709 | __disk_unblock_events(disk, true); |
| 1710 | |
| 1711 | return count; |
| 1712 | } |
| 1713 | |
| 1714 | static const DEVICE_ATTR(events, S_IRUGO, disk_events_show, NULL); |
| 1715 | static const DEVICE_ATTR(events_async, S_IRUGO, disk_events_async_show, NULL); |
| 1716 | static const DEVICE_ATTR(events_poll_msecs, S_IRUGO|S_IWUSR, |
| 1717 | disk_events_poll_msecs_show, |
| 1718 | disk_events_poll_msecs_store); |
| 1719 | |
| 1720 | static const struct attribute *disk_events_attrs[] = { |
| 1721 | &dev_attr_events.attr, |
| 1722 | &dev_attr_events_async.attr, |
| 1723 | &dev_attr_events_poll_msecs.attr, |
| 1724 | NULL, |
| 1725 | }; |
| 1726 | |
| 1727 | /* |
| 1728 | * The default polling interval can be specified by the kernel |
| 1729 | * parameter block.events_dfl_poll_msecs which defaults to 0 |
| 1730 | * (disable). This can also be modified runtime by writing to |
| 1731 | * /sys/module/block/events_dfl_poll_msecs. |
| 1732 | */ |
| 1733 | static int disk_events_set_dfl_poll_msecs(const char *val, |
| 1734 | const struct kernel_param *kp) |
| 1735 | { |
| 1736 | struct disk_events *ev; |
| 1737 | int ret; |
| 1738 | |
| 1739 | ret = param_set_ulong(val, kp); |
| 1740 | if (ret < 0) |
| 1741 | return ret; |
| 1742 | |
| 1743 | mutex_lock(&disk_events_mutex); |
| 1744 | |
| 1745 | list_for_each_entry(ev, &disk_events, node) |
Tejun Heo | 85ef06d | 2011-07-01 16:17:47 +0200 | [diff] [blame] | 1746 | disk_flush_events(ev->disk, 0); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1747 | |
| 1748 | mutex_unlock(&disk_events_mutex); |
| 1749 | |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = { |
| 1754 | .set = disk_events_set_dfl_poll_msecs, |
| 1755 | .get = param_get_ulong, |
| 1756 | }; |
| 1757 | |
| 1758 | #undef MODULE_PARAM_PREFIX |
| 1759 | #define MODULE_PARAM_PREFIX "block." |
| 1760 | |
| 1761 | module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops, |
| 1762 | &disk_events_dfl_poll_msecs, 0644); |
| 1763 | |
| 1764 | /* |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1765 | * disk_{alloc|add|del|release}_events - initialize and destroy disk_events. |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1766 | */ |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1767 | static void disk_alloc_events(struct gendisk *disk) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1768 | { |
| 1769 | struct disk_events *ev; |
| 1770 | |
Tejun Heo | 75e3f3e | 2011-05-26 21:06:50 +0200 | [diff] [blame] | 1771 | if (!disk->fops->check_events) |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1772 | return; |
| 1773 | |
| 1774 | ev = kzalloc(sizeof(*ev), GFP_KERNEL); |
| 1775 | if (!ev) { |
| 1776 | pr_warn("%s: failed to initialize events\n", disk->disk_name); |
| 1777 | return; |
| 1778 | } |
| 1779 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1780 | INIT_LIST_HEAD(&ev->node); |
| 1781 | ev->disk = disk; |
| 1782 | spin_lock_init(&ev->lock); |
Tejun Heo | fdd514e | 2011-06-09 20:43:59 +0200 | [diff] [blame] | 1783 | mutex_init(&ev->block_mutex); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1784 | ev->block = 1; |
| 1785 | ev->poll_msecs = -1; |
| 1786 | INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); |
| 1787 | |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1788 | disk->ev = ev; |
| 1789 | } |
| 1790 | |
| 1791 | static void disk_add_events(struct gendisk *disk) |
| 1792 | { |
| 1793 | if (!disk->ev) |
| 1794 | return; |
| 1795 | |
| 1796 | /* FIXME: error handling */ |
| 1797 | if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0) |
| 1798 | pr_warn("%s: failed to create sysfs files for events\n", |
| 1799 | disk->disk_name); |
| 1800 | |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1801 | mutex_lock(&disk_events_mutex); |
Stanislaw Gruszka | 9f53d2fe | 2012-03-02 10:43:28 +0100 | [diff] [blame] | 1802 | list_add_tail(&disk->ev->node, &disk_events); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1803 | mutex_unlock(&disk_events_mutex); |
| 1804 | |
| 1805 | /* |
| 1806 | * Block count is initialized to 1 and the following initial |
| 1807 | * unblock kicks it into action. |
| 1808 | */ |
| 1809 | __disk_unblock_events(disk, true); |
| 1810 | } |
| 1811 | |
| 1812 | static void disk_del_events(struct gendisk *disk) |
| 1813 | { |
| 1814 | if (!disk->ev) |
| 1815 | return; |
| 1816 | |
Tejun Heo | c3af54a | 2011-06-09 20:43:55 +0200 | [diff] [blame] | 1817 | disk_block_events(disk); |
Tejun Heo | 77ea887 | 2010-12-08 20:57:37 +0100 | [diff] [blame] | 1818 | |
| 1819 | mutex_lock(&disk_events_mutex); |
| 1820 | list_del_init(&disk->ev->node); |
| 1821 | mutex_unlock(&disk_events_mutex); |
| 1822 | |
| 1823 | sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs); |
| 1824 | } |
| 1825 | |
| 1826 | static void disk_release_events(struct gendisk *disk) |
| 1827 | { |
| 1828 | /* the block count should be 1 from disk_del_events() */ |
| 1829 | WARN_ON_ONCE(disk->ev && disk->ev->block != 1); |
| 1830 | kfree(disk->ev); |
| 1831 | } |